Esempio n. 1
0
        public void Parse_BufferIsInvalid_ThrowsBadHttpDataException(string input)
        {
            // Arrange
            var buffer = input.ToAsciiBytes();

            // Act & Assert
            Assert.Throws <BadHttpDataException>(() => HttpRequestLine.Parse(buffer, 3));
        }
Esempio n. 2
0
        public void Parse_BufferIsNull_ThrowsArgumentNullException(int start)
        {
            // Arrange

            // Act & Assert
            var ex = Assert.Throws <ArgumentNullException>(() => HttpRequestLine.Parse(null, 5));

            Assert.That(ex.ParamName, Is.EqualTo("buffer"));
        }
Esempio n. 3
0
        public void Parse_StartIsOutOfRange_ThrowsArgumentOutOfRangeException(int start)
        {
            // Arrange

            // Act & Assert
            var ex = Assert.Throws <ArgumentOutOfRangeException>(() => HttpRequestLine.Parse(new byte[10], start));

            Assert.That(ex.ParamName, Is.EqualTo("start"));
        }
Esempio n. 4
0
        public void Parse_ValidArguments_CreatesLine(string input, string expectedMethod, string expectedUri, string expectedVersion)
        {
            // Arrange
            var buffer = input.ToAsciiBytes();

            // Act
            var line = HttpRequestLine.Parse(buffer, 3);

            // Assert
            Assert.That(line, Is.Not.Null);
            Assert.That(line.Method.ToString(), Is.EqualTo(expectedMethod));
            Assert.That(line.RequestUri, Is.EqualTo(expectedUri));
            Assert.That(line.Version, Is.EqualTo(expectedVersion));
        }
Esempio n. 5
0
        public Task ProcessRequest()
        {
            try {
                //build out request environment
                var requestEnvironment = OwinFactory.CreateScopedEnvironment(_sessionEnvironment);
                _context = OwinContext.Get(requestEnvironment);
                Trace("Session - Process Request");

                //todo: configure OnSendingHeaders aggregator

                // parse request line
                HttpRequestLine requestLine = HttpRequestLine.Parse(_networkStream.ReadLine());
                if (!requestLine.IsValid)
                {
                    ProcessError(Status.Is.BadRequest);
                    return(_sessionCompleted.Task);
                }
                if (!ValidVerbs.Contains(requestLine.Method))
                {
                    ProcessError(Status.Is.NotImplemented);
                    return(_sessionCompleted.Task);
                }
                _context.Request.PathBase = string.Empty;
                _context.Request.Method   = requestLine.Method;
                if (requestLine.Uri.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                {
                    Uri requestUri;
                    if (!Uri.TryCreate(requestLine.Uri, UriKind.Absolute, out requestUri))
                    {
                        ProcessError(Status.Is.BadRequest);
                        return(_sessionCompleted.Task);
                    }
                    _context.Request.FullUri = requestUri;
                }
                else
                {
                    _context.Request.Scheme = "http";
                    var splitUri = requestLine.Uri.Split('?');
                    _context.Request.Path        = splitUri[0];
                    _context.Request.QueryString = splitUri.Length == 2 ? splitUri[1] : string.Empty;
                }
                _context.Request.Protocol = requestLine.HttpVersion;
                _httpVer = requestLine.HttpVersion.Substring(requestLine.HttpVersion.IndexOf('/') + 1);

                // parse http headers
                var headers = new List <string>();
                while (true)
                {
                    string headerLine = _networkStream.ReadLine();
                    if (headerLine == string.Empty)
                    {
                        break;
                    }
                    headers.Add(headerLine);
                }
                _context.Request.Headers.AddRaw(headers);

                _keepAlive = (_httpVer == "1.0" && _context.Request.Headers.ValueIs(HttpHeaderKeys.Connection, "Keep-Alive", false)) ||
                             !_context.Request.Headers.ValueIs(HttpHeaderKeys.Connection, "Close", false);
                _context.Request.Body  = _networkStream;
                _context.Response.Body = _output;

                // handle 100-continue
                _context.Request.Headers.GetValue(HttpHeaderKeys.Expect);
                if (_context.Request.Headers.ValueIs(HttpHeaderKeys.Expect, "100-Continue", false))
                {
                    _networkStream.WriteAsync("HTTP/1.1 100 Continue\r\n", Encoding.UTF8)
                    .ContinueWith(task => {
                        if (task.IsFaulted)
                        {
                            SessionFaulted(task.Exception);
                            return;
                        }
                        _appFunc(_context.Environment)
                        .ContinueWith(ProcessResult);
                    });
                }
                else
                {
                    _appFunc(_context.Environment)
                    .ContinueWith(ProcessResult);
                }
            }
            catch (Exception exception) {
                SessionFaulted(exception);
            }
            return(_sessionCompleted.Task);
        }