Beispiel #1
0
        private void OnMessageReceived(SocketChannel channel, HttpMessage message)
        {
            //Logger.WriteDebug(this, "Pipeline => OnMessageReceived");

            _service = this;

            var context = new HttpContext
            {
                Application = ApplicationInfo,
                Channel = channel,
                Items = new MemoryItemStorage(),
                Request = (IHttpRequest)message,
                Response = ((IHttpRequest)message).CreateResponse()

            };

            context.Request.RemoteEndPoint = channel.RemoteEndpoint;
            context.Response.AddHeader("X-Powered-By", "MicroServer");
            _moduleManager.InvokeAsync(context, SendResponse);
        }
 /// <summary>
 ///     Reset decoder state so that we can decode a new message
 /// </summary>
 public void Clear()
 {
     _message = null;
     _isHeaderParsed = false;
     _frameContentBytesLeft = 0;
 }
        private void TriggerMessageReceived(HttpMessage message)
        {
            var request = message as HttpRequest;
            if (_messageSerializer != null && request != null)
            {
                if (message.Body != null && message.Body.Length > 0)
                {
                    var result = _messageSerializer.Deserialize(message.Headers["Content-Type"], message.Body);

                    if (result == null)
                        throw new BadRequestException("Unsupported content-type: " + message.ContentType);

                    var formAndFiles = result as FormAndFilesResult;
                    if (formAndFiles != null)
                    {
                        request.Form = formAndFiles.Form;
                        request.Files = formAndFiles.Files;
                    }
                    else
                        throw new HttpException(500, "Unknown decoder result: " + result);
                }
                var cookies = request.Headers["Cookie"];
                if (cookies != null)
                {
                    request.Cookies = _cookieParser.Parse(cookies);
                }
            }

            _messageReceived(_channel, message);
        }
        private void OnRequestLine(string part1, string part2, string part3)
        {
            if (part1.StartsWith("http/"))
            {
                int code;
                if (!ParseUtility.TryParseInt(part2, out code))
                    throw new BadRequestException(
                        string.Concat("Second word in the status line should be a HTTP code, you specified ", part2, "."));

                if (_messageSerializer != null)
                    _message = new HttpResponse(code, part3, part1);
                else
                    _message = new HttpResponseBase(code, part3, part1);
            }
            else
            {
                if (!part3.StartsWith("http/"))
                    throw new BadRequestException(
                        string.Concat("Status line for requests should end with the HTTP version. Your line ended with ", part3, "."));

                _message = _messageSerializer != null
                    ? new HttpRequest(part1, part2, part3)
                    : new HttpRequestBase(part1, part2, part3);
            }
        }
        /// <summary>
        ///     We've received bytes from the socket. Build a message out of them.
        /// </summary>
        /// <param name="buffer">Buffer</param>
        public void ProcessReadBytes(SocketChannel channel, SocketBuffer buffer)
        {
            _channel = channel;

            var receiveBufferOffset = buffer.Offset;
            var bytesLeftInReceiveBuffer = buffer.BytesTransferred;
            while (true)
            {
                if (bytesLeftInReceiveBuffer <= 0)
                    break;

                if (!_isHeaderParsed)
                {
                    var offsetBefore = receiveBufferOffset;
                    receiveBufferOffset = _headerParser.Parse(buffer, receiveBufferOffset);
                    if (!_isHeaderParsed)
                        return;

                    bytesLeftInReceiveBuffer -= receiveBufferOffset - offsetBefore;
                    _frameContentBytesLeft = _message.ContentLength;
                    if (_frameContentBytesLeft == 0)
                    {
                        TriggerMessageReceived(_message);
                        _message = null;
                        _isHeaderParsed = false;
                        continue;
                    }

                    _message.Body = new MemoryStream();
                }

                var bytesRead = BytesProcessed(buffer.Offset, receiveBufferOffset);
                var bytesToWrite = System.Math.Min(_frameContentBytesLeft, buffer.BytesTransferred - bytesRead);
                _message.Body.Write(buffer.Buffer, receiveBufferOffset, bytesToWrite);
                _frameContentBytesLeft -= bytesToWrite;
                receiveBufferOffset += bytesToWrite;
                bytesLeftInReceiveBuffer -= bytesToWrite;
                if (_frameContentBytesLeft == 0)
                {
                    _message.Body.Position = 0;
                    TriggerMessageReceived(_message);
                    Clear();
                }
            }
        }
 /// <summary>
 ///     Reset decoder state so that we can decode a new message
 /// </summary>
 public void Clear()
 {
     _message               = null;
     _isHeaderParsed        = false;
     _frameContentBytesLeft = 0;
 }