Esempio n. 1
0
        internal 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);
        }
Esempio n. 2
0
    // This method is a bit of a mess. We need to fix many Http and Json APIs
    void WriteResponseForPostJson(BufferFormatter formatter, HttpRequestLine requestLine, ReadOnlySpan<byte> body)
    {
        Console.WriteLine(new Utf8String(body));

        uint requestedCount = ReadCountUsingReader(body).GetValueOrDefault(1);
        //uint requestedCount = ReadCountUsingNonAllocatingDom(body).GetValueOrDefault(1);

        // TODO: this needs to be written directly to the buffer after content length reservation is implemented.
        var buffer = ArrayPool<byte>.Shared.Rent(2048);
        var spanFormatter = new SpanFormatter(buffer.Slice(), FormattingData.InvariantUtf8);
        var json = new JsonWriter<SpanFormatter>(spanFormatter,  prettyPrint: true);
        json.WriteObjectStart();
        json.WriteArrayStart();
        for (int i = 0; i < requestedCount; i++) {
            json.WriteString(DateTime.UtcNow.ToString()); // TODO: this needs to not allocate.
        }
        json.WriteArrayEnd(); ;
        json.WriteObjectEnd();
        var responseBodyText = new Utf8String(buffer, 0, spanFormatter.CommitedByteCount);

        formatter.AppendHttpStatusLine(HttpVersion.V1_1, 200, new Utf8String("OK"));
        formatter.Append(new Utf8String("Content-Length : "));
        formatter.Append(responseBodyText.Length);
        formatter.AppendHttpNewLine();
        formatter.Append("Content-Type : text/plain; charset=UTF-8");
        formatter.AppendHttpNewLine();
        formatter.Append("Server : .NET Core Sample Serve");
        formatter.AppendHttpNewLine();
        formatter.Append(new Utf8String("Date : "));
        formatter.Append(DateTime.UtcNow.ToString("R"));
        formatter.AppendHttpNewLine();
        formatter.AppendHttpNewLine();
        formatter.Append(responseBodyText);

        ArrayPool<byte>.Shared.Return(buffer);
    }
Esempio n. 3
0
 public HttpRequest(HttpRequestLine requestLine, HttpHeaders headers, ReadOnlySpan<byte> bytes)
 {
     _requestLine = requestLine;
     _headers = headers;
     _body = bytes;
 }
Esempio n. 4
0
        internal 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;
        }
Esempio n. 5
0
 public static bool TryParseRequestLine(ReadOnlySpan<byte> buffer, out HttpRequestLine requestLine)
 {
     int parsedBytes;
     return TryParseRequestLine(buffer, out requestLine, out parsedBytes);
 }
Esempio n. 6
0
 public HttpRequest(HttpRequestLine requestLine, HttpHeaders headers, ReadOnlySpan <byte> bytes)
 {
     _requestLine = requestLine;
     _headers     = headers;
     _body        = bytes;
 }
Esempio n. 7
0
        public static bool TryParseRequestLine(ReadOnlySpan <byte> buffer, out HttpRequestLine requestLine)
        {
            int parsedBytes;

            return(TryParseRequestLine(buffer, out requestLine, out parsedBytes));
        }
Esempio n. 8
0
 static void WriteResponseForGetTime(BufferFormatter formatter, HttpRequestLine request)
 {
     // TODO: this needs to not allocate.
     var body = string.Format(@"<html><head><title>Time</title></head><body>{0}</body></html>", DateTime.UtcNow.ToString("O"));
     WriteCommonHeaders(formatter, HttpVersion.V1_1, 200, "OK", keepAlive: false);
     formatter.Append(new Utf8String("Content-Length : "));
     formatter.Append(body.Length);
     formatter.AppendHttpNewLine();
     formatter.AppendHttpNewLine();
     formatter.Append(body);
 }
Esempio n. 9
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, HttpVersion.V1_1, 404, "Not Found", false);
     formatter.Append(HttpNewline);
 }