示例#1
0
        public static bool TryParseRequestLine(ReadOnlySpan <byte> buffer, out HttpRequestLine requestLine, out int totalParsedBytes)
        {
            requestLine      = new HttpRequestLine();
            totalParsedBytes = 0;

            var reader = new HttpRequestReader();

            reader.Buffer = buffer;

            requestLine.Method = reader.ReadMethod();
            if (requestLine.Method == HttpMethod.Unknown)
            {
                return(false);
            }

            requestLine.RequestUri = reader.ReadRequestUri();
            if (requestLine.RequestUri.Length == 0)
            {
                return(false);
            }

            requestLine.Version = reader.ReadHttpVersion();
            if (requestLine.Version == HttpVersion.Unknown)
            {
                return(false);
            }

            totalParsedBytes = buffer.Length - reader.Buffer.Length;
            return(true);
        }
 protected override HttpServerBuffer CreateResponse(HttpRequestLine requestLine, ByteSpan headerAndBody)
 {
     var api = Apis.Map(requestLine);
     switch (api) {
         case Api.GetTime:
             return CreateResponseForGetTime();
         default:
             return CreateResponseFor404(requestLine, headerAndBody);
     }
 }
示例#3
0
        protected virtual HttpServerBuffer CreateResponseFor404(HttpRequestLine requestLine) // Not Found
        {
            Log.LogMessage(Log.Level.Warning, "Request {0}, Response: 404 Not Found", requestLine);

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

            WriteCommonHeaders(formatter, "1.1", "404", "Not Found", false);
            formatter.Append(HttpNewline);
            return(new HttpServerBuffer(formatter.Buffer, formatter.CommitedByteCount, BufferPool.Shared));
        }
示例#4
0
    static HttpServerBuffer CreateResponseForGetTime(HttpRequestLine request)
    {
        var formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8);
        WriteCommonHeaders(formatter, @"HTTP/1.1 200 OK", request.IsKeepAlive());
        formatter.Append(HttpNewline);

        formatter.Append(@"<html><head><title>Time</title></head><body>");
        formatter.Append(DateTime.UtcNow, 'O');
        formatter.Append(@"</body></html>");
        return new HttpServerBuffer(formatter.Buffer, formatter.CommitedByteCount, BufferPool.Shared);
    }
示例#5
0
 public T Map(HttpRequestLine requestLine)
 {
     for (int i = 0; i < count; i++)
     {
         if (requestLine.RequestUri.Equals(Uris[i]))
         {
             return(Apis[i]);
         }
     }
     return(default(T));
 }
示例#6
0
    static HttpServerBuffer CreateResponseForGetTime(HttpRequestLine request)
    {
        var body = string.Format(@"<html><head><title>Time</title></head><body>{0}</body></html>", DateTime.UtcNow.ToString("O"));
        var formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8);

        WriteCommonHeaders(formatter, "1.1", "200", "Ok", request.IsKeepAlive());
        formatter.WriteHttpHeader(new Utf8String("Content-Length"), new Utf8String(body.Length.ToString()));
        formatter.EndHttpHeaderSection();

        formatter.WriteHttpBody(new Utf8String(body));

        return new HttpServerBuffer(formatter.Buffer, formatter.CommitedByteCount, BufferPool.Shared);
    }
示例#7
0
        public static bool TryParseRequestLine(ByteSpan buffer, out HttpRequestLine requestLine, out int totalParsedBytes)
        {
            requestLine = new HttpRequestLine();
            totalParsedBytes = 0;

            var reader = new HttpRequestReader();
            reader.Buffer = buffer;

            requestLine.Method = reader.ReadMethod();
            if(requestLine.Method == HttpMethod.Unknown) { return false; }

            requestLine.RequestUri = reader.ReadRequestUri();
            if(requestLine.RequestUri.Length == 0) { return false; }

            requestLine.Version = reader.ReadHttpVersion();
            if (requestLine.Version == HttpVersion.Unknown) { return false; }

            totalParsedBytes = buffer.Length - reader.Buffer.Length;
            return true;
        }
示例#8
0
 public static bool TryParseRequestLine(ByteSpan buffer, out HttpRequestLine requestLine)
 {
     int parsedBytes;
     return TryParseRequestLine(buffer, out requestLine, out parsedBytes);
 }
示例#9
0
        // Not Found
        protected virtual HttpServerBuffer CreateResponseFor404(HttpRequestLine requestLine, ByteSpan headersAndBody)
        {
            Log.LogMessage(Log.Level.Warning, "Request {0}, Response: 404 Not Found", requestLine);

            BufferFormatter formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8);
            WriteCommonHeaders(formatter, @"HTTP/1.1 404 Not Found");
            formatter.Append(HttpNewline);
            return new HttpServerBuffer(formatter.Buffer, formatter.CommitedByteCount, BufferPool.Shared);
        }
示例#10
0
 protected abstract HttpServerBuffer CreateResponse(HttpRequestLine requestLine, ByteSpan headersAndBody);
示例#11
0
 protected virtual void WriteResponseFor404(BufferFormatter formatter, HttpRequestLine requestLine) // Not Found
 {
     Log.LogMessage(Log.Level.Warning, "Request {0}, Response: 404 Not Found", requestLine);
     WriteCommonHeaders(formatter, "1.1", "404", "Not Found", false);
     formatter.Append(HttpNewline);
 }
示例#12
0
        public static bool TryParseRequestLine(ReadOnlySpan <byte> buffer, out HttpRequestLine requestLine)
        {
            int parsedBytes;

            return(TryParseRequestLine(buffer, out requestLine, out parsedBytes));
        }
示例#13
0
 public HttpRequest(HttpRequestLine requestLine, HttpHeaders headers, ByteSpan bytes)
 {
     _requestLine = requestLine;
     _headers     = headers;
     _body        = bytes;
 }
示例#14
0
 // TODO: this needs to be smarter
 internal static bool IsKeepAlive(this HttpRequestLine request)
 {
     return((request.Version != HttpVersion.V1_0) && (request.Version != HttpVersion.Unknown));
 }
示例#15
0
        public static bool TryParseRequestLine(ByteSpan buffer, out HttpRequestLine requestLine)
        {
            int parsedBytes;

            return(TryParseRequestLine(buffer, out requestLine, out parsedBytes));
        }
示例#16
0
 protected abstract HttpServerBuffer CreateResponse(HttpRequestLine requestLine, Span <byte> headersAndBody);