/// <summary>
        ///     Are about to send a new message
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <remarks>
        ///     Can be used to prepare the next message. for instance serialize it etc.
        /// </remarks>
        /// <exception cref="NotSupportedException">Message is of a type that the encoder cannot handle.</exception>
        public void Prepare(object message)
        {
            if (!(message is HttpMessage))
                throw new InvalidOperationException("This encoder only supports messages deriving from 'HttpMessage'");

            _message = (HttpMessage) message;
            if (_message.Body == null || _message.Body.Length == 0)
                _message.Headers["Content-Length"] = "0";
        }
Esempio n. 2
0
        /// <summary>
        ///     Remove everything used for the last message
        /// </summary>
        public void Clear()
        {
            if (_message != null)
            {
                lock (_resetLock)
                {
                    if (_message != null && _message.Body != null)
                        _message.Body.Dispose();

                    _message = null;
                }
            }

            _headerBytesLeft = 0;
            _bodyBytesToSend = 0;
            _offset = 0;
            _headerStream.SetLength(0);
        }
        public void ProcessReadBytes(ISocketBuffer buffer)
        {
            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)
                    {
                        MessageReceived(_message);
                        _message = null;
                        _isHeaderParsed = false;
                        continue;
                    }

                    _message.Body = new MemoryStream();
                }

                var bytesRead = BytesProcessed(buffer.Offset, receiveBufferOffset);
                var bytesToWrite = 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;
                    MessageReceived(_message);
                    Clear();
                }
            }
        }
        /// <summary>
        ///     Remove everything used for the last message
        /// </summary>
        public void Clear()
        {
            if (_message != null)
            {
                lock (_resetLock)
                {
                    if (_message != null && _message.Body != null)
                        _message.Body.Dispose();

                    _message = null;
                }
            }

            _bytesToSend = 0;
            _isHeaderSent = false;
            _stream.SetLength(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(message);
        }
        private void OnRequestLine(string part1, string part2, string part3)
        {
            if (part1.StartsWith("http/", StringComparison.OrdinalIgnoreCase))
            {
                int code;
                if (!int.TryParse(part2, out code))
                    throw new BadRequestException(
                        string.Format("Second word in the status line should be a HTTP code, you specified '{0}'.",
                            part2));

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

                _message = _messageSerializer != null
                    ? new HttpRequest(part1, part2, part3)
                    : new HttpRequestBase(part1, part2, part3);
            }
        }
 /// <summary>
 ///     Reset decoder state so that we can decode a new message
 /// </summary>
 public void Clear()
 {
     _message = null;
     _isHeaderParsed = false;
     _headerParser.Reset();
     _frameContentBytesLeft = 0;
 }
        /// <summary>
        ///     We've received bytes from the socket. Build a message out of them.
        /// </summary>
        /// <param name="buffer">Buffer</param>
        public void ProcessReadBytes(ISocketBuffer buffer)
        {
            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;

                    if (_message == null)
                        throw new HttpException(HttpStatusCode.InternalServerError, "Failed to decode message properly. Decoder state: " + _headerParser.State);

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

                    if (_message == null)
                        throw new HttpException(HttpStatusCode.InternalServerError, "Failed to decode message properly. Decoder state: " + _headerParser.State);
                    _message.Body = new MemoryStream();
                }

                var bytesRead = BytesProcessed(buffer.Offset, receiveBufferOffset);
                var bytesToWrite = 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();
                }
            }
        }
        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)
                    {
                        //it's a so simple protocol, we can expect that the client can handle it.
                        if (!"text/plain".Equals(message.Headers["Content-Type"], StringComparison.OrdinalIgnoreCase))
                        throw new BadRequestException("Unsupported content-type: " + message.ContentType);
                    }
                    else
                    {
                        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(message);
        }
        private void TriggerMessageReceived(HttpMessage message)
        {
            var request = message as HttpRequest;
            if (BodyDecoder != null && request != null)
            {
                if (message.Body != null && message.Body.Length > 0)
                {
                    var result = BodyDecoder.Decode(request);
                    if (!result)
                        throw new BadRequestException("Unknown body content-type.");
                }
                var cookies = request.Headers["Cookie"];
                if (cookies != null)
                {
                    request.Cookies = _cookieParser.Parse(cookies);
                }
            }

            _messageReceived(message);
        }
 /// <summary>
 ///     Remove everything used for the last message
 /// </summary>
 public void Clear()
 {
     _bytesToSend = 0;
     _message = null;
     _isHeaderSent = false;
 }