Example #1
0
        protected HTTPResponseHeader MakeHeader(string httpVersion, string mimeType, string statusCode, string location)
        {
            HTTPResponseHeader header = MakeHeader(httpVersion, mimeType, statusCode);

            header.SetField(HTTPResponseHeader.LOCATION_FIELD, location);
            return(header);
        }
Example #2
0
        private HTTPResponseHeader MakeHeader(string httpVersion, string mimeType, string statusCode)
        {
            HTTPResponseHeader header = new HTTPResponseHeader(httpVersion, statusCode);

            header.SetField(HTTPResponseHeader.SERVER_NAME_FIELD, ServerName);
            header.SetField(HTTPResponseHeader.CONTENT_TYPE_FIELD, mimeType);
            header.SetField(HTTPResponseHeader.ACCEPT_RANGES_FIELD, ACCEPT_RANGES_SUFFIX);
            if (AttachLastModifiedField)
            {
                header.SetField(HTTPResponseHeader.LAST_MODIFIED_FIELD, LastModified);
            }
            return(header);
        }
Example #3
0
        protected override HTTPResponse Process(HTTPRequest request)
        {
            if (!SupportedMethods.Contains(request.HTTPMethod))
            {
                Trace.WriteLine(NOT_IMPLEMENTED_STATUS);
                string             err    = NOT_IMPLEMENTED_MESSAGE_PREFIX + NOT_IMPLEMENTED_STATUS + NOT_IMPLEMENTED_MESSAGE_SUFFIX;
                HTTPResponseHeader header = MakeHeader(request.HTTPVersion, MESSAGE_MIME_TYPE, NOT_IMPLEMENTED_STATUS);
                return(new HTTPResponse(header, new MemoryStream(Encoding.ASCII.GetBytes(err))));
            }

            string path = ResolveRootPath(request.Path);

            Trace.WriteLine(path);
            return(GetHTTPResponseForPath(path, request.HTTPVersion));
        }
 /// <summary>
 /// Return stream which at first gives bytes from header encoded given encoding
 /// and when it ends read bytes from given string
 /// </summary>
 /// <param name="header"></param>
 /// <param name="stream"></param>
 public HTTPResponse(HTTPResponseHeader header, Stream stream, Encoding encoding)
 {
     try
     {
         _streamLength = stream.Length;
     }
     catch (NotSupportedException)
     {
         throw new StreamTypeException(LENGTH_ERROR_MESSAGE);
     }
     header.SetField(HTTPResponseHeader.CONTENT_LENGTH_FIELD, _streamLength.ToString());
     this._header = encoding.GetBytes(header.ToString());
     if (!stream.CanRead)
     {
         throw new StreamTypeException(READ_ERROR_MESSAGE);
     }
     internalStream = stream;
     _position = 0;
     _headerLength = this._header.LongLength;
 }
 /// <summary>
 /// Return stream which at first gives bytes from header encoded given encoding
 /// and when it ends read bytes from given string
 /// </summary>
 /// <param name="header"></param>
 /// <param name="stream"></param>
 public HTTPResponse(HTTPResponseHeader header, Stream stream, Encoding encoding)
 {
     try
     {
         _streamLength = stream.Length;
     }
     catch (NotSupportedException)
     {
         throw new StreamTypeException(LENGTH_ERROR_MESSAGE);
     }
     header.SetField(HTTPResponseHeader.CONTENT_LENGTH_FIELD, _streamLength.ToString());
     this._header = encoding.GetBytes(header.ToString());
     if (!stream.CanRead)
     {
         throw new StreamTypeException(READ_ERROR_MESSAGE);
     }
     internalStream = stream;
     _position      = 0;
     _headerLength  = this._header.LongLength;
 }
Example #6
0
        private HTTPResponse GetHTTPResponseForPath(string path, string httpVersion)
        {
            string extension = path.Substring(path.LastIndexOf(FILE_NAME_EXTENSION_SEPERATOR) + 1);

            if (_pathMapping.ContainsKey(path))
            {
                Stream             stream = _pathMapping[path].Invoke();
                HTTPResponseHeader header = MakeHeader(httpVersion, GetMimeTypeForFileExtension(extension), OkStatus);
                Trace.WriteLine(header);
                try
                {
                    return(new HTTPResponse(header, stream));
                }
                catch (HTTPResponse.StreamTypeException exception)
                {
                    Trace.WriteLine(exception.Message);
                    string             err       = INTERNAL_ERROR_MESSAGE_PREFIX + INTERNAL_ERROR_STATUS + INTERNAL_ERROR_MESSAGE_SUFFIX;
                    HTTPResponseHeader newHeader = MakeHeader(httpVersion, MESSAGE_MIME_TYPE, INTERNAL_ERROR_STATUS);
                    Trace.WriteLine(newHeader);
                    return(new HTTPResponse(newHeader, new MemoryStream(Encoding.ASCII.GetBytes(err))));
                }
            }
            else
            {
                if (_movedPath.ContainsKey(path))
                {
                    string             err    = MOVED_MESSAGE_PREFIX + _movedPath[path] + MOVED_MESSAGE_SUFFIX;
                    HTTPResponseHeader header = MakeHeader(httpVersion, MESSAGE_MIME_TYPE, MovedStatus, _movedPath[path]);
                    Trace.WriteLine(header);
                    return(new HTTPResponse(header, new MemoryStream(Encoding.ASCII.GetBytes(err))));
                }
                else
                {
                    string             err    = NO_FOUND_MESSAGE_PREFIX + NoFoundStatus + NO_FOUND_MESSAGE_SUFFIX;
                    HTTPResponseHeader header = MakeHeader(httpVersion, MESSAGE_MIME_TYPE, NoFoundStatus);
                    Trace.WriteLine(header);
                    return(new HTTPResponse(header, new MemoryStream(Encoding.ASCII.GetBytes(err))));
                }
            }
        }
 /// <summary>
 /// Return stream which at first gives bytes from header encoded in ascii
 /// and when it ends read bytes from given string
 /// </summary>
 /// <param name="header"></param>
 /// <param name="stream"></param>
 public HTTPResponse(HTTPResponseHeader header, Stream stream)
     :this (header, stream, Encoding.ASCII)
 {
 }
 /// <summary>
 /// Return stream which at first gives bytes from header encoded in ascii
 /// and when it ends read bytes from given string
 /// </summary>
 /// <param name="header"></param>
 /// <param name="stream"></param>
 public HTTPResponse(HTTPResponseHeader header, Stream stream)
     : this(header, stream, Encoding.ASCII)
 {
 }