public static void LogRequest(this Log log, HttpRequest request) { if (log.IsVerbose) { log.LogMessage(Log.Level.Verbose, "\tMethod: {0}", request.RequestLine.Method); log.LogMessage(Log.Level.Verbose, "\tRequest-URI: {0}", request.RequestLine.RequestUri.ToString()); log.LogMessage(Log.Level.Verbose, "\tHTTP-Version: {0}", request.RequestLine.Version); log.LogMessage(Log.Level.Verbose, "\tHttp Headers:"); foreach (var httpHeader in request.Headers) { log.LogMessage(Log.Level.Verbose, "\t\tName: {0}, Value: {1}", httpHeader.Key, httpHeader.Value); } HttpRequestReader reader = new HttpRequestReader(); reader.Buffer = request.Body; while (true) { var header = reader.ReadHeader(); if (header.Length == 0) break; log.LogMessage(Log.Level.Verbose, "\tHeader: {0}", header.ToString()); } var messageBody = reader.Buffer; log.LogMessage(Log.Level.Verbose, "\tBody bytecount: {0}", messageBody.Length); } }
void WriteResponseForPostJson(HttpRequest request, HttpResponse response) { // read request json int requestedCount; using (var dom = JsonObject.Parse(request.Body)) { requestedCount = (int)dom["Count"]; } // write response JSON var jsonWriter = new JsonWriter<ResponseFormatter>(response.Body, prettyPrint: false); jsonWriter.WriteObjectStart(); jsonWriter.WriteArrayStart(); for (int i = 0; i < requestedCount; i++) { jsonWriter.WriteString("hello!"); } jsonWriter.WriteArrayEnd(); jsonWriter.WriteObjectEnd(); // write headers var headers = response.Headers; headers.AppendHttpStatusLine(HttpVersion.V1_1, 200, new Utf8String("OK")); headers.Append("Content-Length : "); headers.Append(response.Body.WrittenBytes); headers.AppendHttpNewLine(); headers.Append("Content-Type : text/plain; charset=UTF-8"); headers.AppendHttpNewLine(); headers.Append("Server : .NET Core Sample Server"); headers.AppendHttpNewLine(); headers.Append("Date : "); headers.Append(DateTime.UtcNow, 'R'); headers.AppendHttpNewLine(); headers.AppendHttpNewLine(); }
protected override void WriteResponse(HttpRequest request, HttpResponse response) { if (!Apis.TryHandle(request, response)) { WriteResponseFor404(request, response); } }
static void WriteResponseForHelloWorld(HttpRequest request, HttpResponse response) { response.Body.Append("Hello, World"); response.Headers.AppendHttpStatusLine(HttpVersion.V1_1, 200, new Utf8String("OK")); response.Headers.Append("Content-Length : "); response.Headers.Append(response.Body.WrittenBytes); response.Headers.AppendHttpNewLine(); response.Headers.Append("Content-Type : text/plain; charset=UTF-8"); response.Headers.AppendHttpNewLine(); response.Headers.Append("Server : .NET Core Sample Server"); response.Headers.AppendHttpNewLine(); response.Headers.Append("Date : "); response.Headers.Append(DateTime.UtcNow, 'R'); response.Headers.AppendHttpNewLine(); response.Headers.AppendHttpNewLine(); }
public static HttpRequest Parse(ReadOnlySpan<byte> bytes) { int parsed; HttpRequestLine requestLine; if (!HttpRequestParser.TryParseRequestLine(bytes, out requestLine, out parsed)){ throw new NotImplementedException("request line parser"); } bytes = bytes.Slice(parsed); HttpHeaders headers; if (!HttpRequestParser.TryParseHeaders(bytes, out headers, out parsed)) { throw new NotImplementedException("headers parser"); } var body = bytes.Slice(parsed); var request = new HttpRequest(requestLine, headers, body); return request; }
protected abstract void WriteResponse(HttpRequest request, HttpResponse response);
protected virtual void WriteResponseFor404(HttpRequest request, HttpResponse response) // Not Found { Log.LogMessage(Log.Level.Warning, "Request {0}, Response: 404 Not Found", request.RequestLine); WriteCommonHeaders(response, HttpVersion.V1_1, 404, "Not Found", false); response.Headers.Append(HttpNewline); }
protected abstract void WriteResponse(BufferFormatter formatter, HttpRequest request);
static void WriteResponseForGetTime(HttpRequest request, HttpResponse response) { response.Body.Format(@"<html><head><title>Time</title></head><body>{0:O}</body></html>", DateTime.UtcNow); WriteCommonHeaders(response, HttpVersion.V1_1, 200, "OK", keepAlive: false); response.Headers.Append("Content-Length : "); response.Headers.Append(response.Body.WrittenBytes); response.Headers.AppendHttpNewLine(); response.Headers.AppendHttpNewLine(); }
public static HttpRequest Parse(Span<byte> 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; }