예제 #1
0
        protected virtual void ProcessRequest(TcpConnection socket)
        {
            Log.LogVerbose("Processing Request");

            var buffer   = BufferPool.Shared.RentBuffer(4096);
            var received = socket.Receive(buffer);

            var receivedBytes = buffer.Slice(0, received);
            HttpServerBuffer responseBytes;

            // parse request
            HttpRequestLine requestLine;
            int             requestLineBytes;

            if (!HttpRequestParser.TryParseRequestLine(receivedBytes, out requestLine, out requestLineBytes))
            {
                Log.LogError("invalid request line");
                responseBytes = CreateResponseFor400(receivedBytes);
            }
            else
            {
                var restOfRequestBytes = receivedBytes.Slice(requestLineBytes);

                if (Log.IsVerbose)
                {
                    Log.LogMessage(Log.Level.Verbose, "\tMethod:       {0}", requestLine.Method);
                    Log.LogMessage(Log.Level.Verbose, "\tRequest-URI:  {0}", requestLine.RequestUri.ToString());
                    Log.LogMessage(Log.Level.Verbose, "\tHTTP-Version: {0}", requestLine.Version);
                    LogRestOfRequest(restOfRequestBytes);
                }

                responseBytes = CreateResponse(requestLine, restOfRequestBytes);
            }
            BufferPool.Shared.ReturnBuffer(ref buffer);

            // send response
            var segment = responseBytes;

            socket.Send(segment._buffer, segment._count);
            socket.Close();
            responseBytes.Return();
            if (Log.IsVerbose)
            {
                Log.LogMessage(Log.Level.Verbose, "Request Processed", DateTime.UtcNow.Ticks);
            }
        }
예제 #2
0
        public static HttpRequest Parse(ByteSpan bytes)
        {
            int             parsed;
            HttpRequestLine requestLine;

            if (!HttpRequestParser.TryParseRequestLine(bytes, out requestLine, out parsed))
            {
                throw new NotImplementedException();
            }
            bytes = bytes.Slice(parsed);

            HttpHeaders headers;

            if (!HttpRequestParser.TryParseHeaders(bytes, out headers, out parsed))
            {
                throw new NotImplementedException();
            }
            var body = bytes.Slice(parsed + 4); // TODO: this needs to verify two newlines

            var request = new HttpRequest(requestLine, headers, body);

            return(request);
        }