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); } }
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)); }
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); }
public T Map(HttpRequestLine requestLine) { for (int i = 0; i < count; i++) { if (requestLine.RequestUri.Equals(Uris[i])) { return(Apis[i]); } } return(default(T)); }
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); }
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; }
public static bool TryParseRequestLine(ByteSpan buffer, out HttpRequestLine requestLine) { int parsedBytes; return TryParseRequestLine(buffer, out requestLine, out parsedBytes); }
// 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); }
protected abstract HttpServerBuffer CreateResponse(HttpRequestLine requestLine, ByteSpan headersAndBody);
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); }
public static bool TryParseRequestLine(ReadOnlySpan <byte> buffer, out HttpRequestLine requestLine) { int parsedBytes; return(TryParseRequestLine(buffer, out requestLine, out parsedBytes)); }
public HttpRequest(HttpRequestLine requestLine, HttpHeaders headers, ByteSpan bytes) { _requestLine = requestLine; _headers = headers; _body = bytes; }
// TODO: this needs to be smarter internal static bool IsKeepAlive(this HttpRequestLine request) { return((request.Version != HttpVersion.V1_0) && (request.Version != HttpVersion.Unknown)); }
public static bool TryParseRequestLine(ByteSpan buffer, out HttpRequestLine requestLine) { int parsedBytes; return(TryParseRequestLine(buffer, out requestLine, out parsedBytes)); }
protected abstract HttpServerBuffer CreateResponse(HttpRequestLine requestLine, Span <byte> headersAndBody);