public bool TryParse(byte[] buffer, int bufferLength, out HttpRequest request)
        {
            if (buffer == null) throw new ArgumentNullException(nameof(buffer));

            try
            {
                var content = Encoding.UTF8.GetString(buffer, 0, bufferLength);
                var groups = _regex.Match(content).Groups;

                _method = (HttpMethod)Enum.Parse(typeof(HttpMethod), groups["method"].Value, true);
                _uri = groups["uri"].Value;
                _httpVersion = groups["version"].Value;
                ParseHeaders(groups["headers"].Value);
                _body = groups["body"].Value;

                ParseQuery();

                var binaryBodyLength = 0;
                if (!string.IsNullOrEmpty(_body))
                {
                    binaryBodyLength = Encoding.UTF8.GetByteCount(_body);
                }

                request = new HttpRequest(_method, _uri, Version.Parse(_httpVersion), _query, _headers, _body, binaryBodyLength);
                return true;
            }
            catch (Exception)
            {
                request = null;
                return false;
            }
        }
        public WebSocketConnectedEventArgs(HttpRequest httpRequest, IWebSocketClientSession webSocketClientSession)
        {
            if (httpRequest == null) throw new ArgumentNullException(nameof(httpRequest));
            if (webSocketClientSession == null) throw new ArgumentNullException(nameof(webSocketClientSession));

            HttpRequest = httpRequest;
            WebSocketClientSession = webSocketClientSession;
        }
Пример #3
0
        public HttpContext(HttpRequest request, HttpResponse response)
        {
            if (request == null) throw new ArgumentNullException(nameof(request));
            if (response == null) throw new ArgumentNullException(nameof(response));

            Request = request;
            Response = response;
        }
        public void Serialize_HttpRequest()
        {
            var request = new HttpRequest(HttpMethod.Get, "", new Version(1, 1), "", new HttpHeaderCollection(), "", 0);

            var response = new HttpResponse();
            response.StatusCode = HttpStatusCode.BadRequest;
            response.Body = new PlainTextBody().WithContent("{\"text\":1234}");
            response.Headers["A"] = 1.ToString();
            response.Headers["B"] = "x";

            var serializer = new HttpResponseSerializer();
            byte[] buffer = serializer.SerializeResponse(new HttpContext(request, response));
            byte[] requiredBuffer = { 72,84,84,80,47,49,46,49,32,52,48,48,32,66,97,100,32,82,101,113,117,101,115,116,13,10,65,58,49,13,10,66,58,120,13,10,67,111,110,116,101,110,116,45,84,121,112,101,58,116,101,120,116,47,112,108,97,105,110,59,32,99,104,97,114,115,101,116,61,117,116,102,45,56,13,10,67,111,110,116,101,110,116,45,76,101,110,103,116,104,58,49,51,13,10,13,10,123,34,116,101,120,116,34,58,49,50,51,52,125};

            bool matching = buffer.SequenceEqual(requiredBuffer);
            matching.ShouldBeEquivalentTo(true);
        }
        private bool TryReceiveHttpRequest(out HttpRequest httpRequest)
        {
            httpRequest = null;

            try
            {
                var receivedBytes = _inputStream.Read(_buffer, 0, _buffer.Length);
                if (receivedBytes == 0)
                {
                    return false;
                }

                if (!_requestParser.TryParse(_buffer, receivedBytes, out httpRequest))
                {
                    return false;
                }

                while (httpRequest.GetRequiresContinue())
                {
                    var additionalReceivedBytes = _inputStream.Read(_buffer, 0, _buffer.Length);
                    if (additionalReceivedBytes == 0)
                    {
                        return false;
                    }

                    receivedBytes += additionalReceivedBytes;

                    if (!_requestParser.TryParse(_buffer, receivedBytes, out httpRequest))
                    {
                        return false;
                    }
                }

                return true;
            }
            catch (IOException)
            {
                return false;
            }
        }
        public UpgradedToWebSocketSessionEventArgs(HttpRequest httpRequest)
        {
            if (httpRequest == null) throw new ArgumentNullException(nameof(httpRequest));

            HttpRequest = httpRequest;
        }