Exemplo n.º 1
0
        protected virtual void ProcessRequest(TcpConnection socket)
        {
            Log.LogVerbose("Processing Request");
            
            var requestBuffer = ManagedBufferPool<byte>.SharedByteBufferPool.RentBuffer(RequestBufferSize);
            var requestByteCount = socket.Receive(requestBuffer);

            if(requestByteCount == 0) {
                socket.Close();
                return;
            }

            var requestBytes = requestBuffer.Slice(0, requestByteCount);
            var request = HttpRequest.Parse(requestBytes);
            Log.LogRequest(request);

            var formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8);
            WriteResponse(formatter, request);

            ManagedBufferPool<byte>.SharedByteBufferPool.ReturnBuffer(ref requestBuffer);

            socket.Send(formatter.Buffer.Slice(formatter.CommitedByteCount));
            socket.Close();

            if (Log.IsVerbose)
            {
                Log.LogMessage(Log.Level.Verbose, "Request Processed", DateTime.UtcNow.Ticks);
            }
        }
Exemplo n.º 2
0
        protected virtual void ProcessRequest(TcpConnection socket)
        {
            Log.LogVerbose("Processing Request");

            var buffer = _buffers.Rent();
            var received = socket.ReceiveWithAsyncAPI(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);
            }
            _buffers.Return(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);
            }
        }