/// <summary>Echoes all request data back into the response</summary> static async void EchoHandler(IStream stream) { try { // Consume headers var headers = await stream.ReadHeadersAsync(); // Send response headers var responseHeaders = new HeaderField[] { new HeaderField { Name = ":status", Value = "200" }, new HeaderField { Name = "nextone", Value = "i am a header value" }, }; await stream.WriteHeadersAsync(responseHeaders, false); // Write request payload back into response await stream.CopyToAsync(stream); // Write end of stream await stream.WriteAsync(new ArraySegment <byte>(emptyBody), true); } catch (Exception e) { Console.WriteLine("Error during handling request: {0}", e.Message); stream.Cancel(); } }
/// <summary>Drops the request data and writes a sucess response</summary> static async void DrainHandler(IStream stream) { try { // Consume headers var headers = await stream.ReadHeadersAsync(); // Read the request body to the end await stream.DrainAsync(); // Send a response which consists of headers and a payload var responseHeaders = new HeaderField[] { new HeaderField { Name = ":status", Value = "200" }, new HeaderField { Name = "nextone", Value = "i am a header value" }, }; await stream.WriteHeadersAsync(responseHeaders, false); await stream.WriteAsync(new ArraySegment <byte>(responseBody), true); } catch (Exception e) { Console.WriteLine("Error during handling request: {0}", e.Message); stream.Cancel(); } }
private async Task HandleIncomingStream(IStream stream) { try { Stream = stream; await _responseHandler(); } catch (Exception) { stream.Cancel(); } }
static async void HandleIncomingStream(IStream stream) { try { // Read the headers var headers = await stream.ReadHeadersAsync(); var method = headers.First(h => h.Name == ":method").Value; var path = headers.First(h => h.Name == ":path").Value; // Print the request method and path Console.WriteLine("Method: {0}, Path: {1}", method, path); // Read the request body and write it to console var buf = new byte[2048]; while (true) { var readResult = await stream.ReadAsync(new ArraySegment <byte>(buf)); if (readResult.EndOfStream) { break; } // Print the received bytes Console.WriteLine(Encoding.ASCII.GetString(buf, 0, readResult.BytesRead)); } // Send a response which consists of headers and a payload var responseHeaders = new HeaderField[] { new HeaderField { Name = ":status", Value = "200" }, new HeaderField { Name = "content-type", Value = "text/html" }, }; await stream.WriteHeadersAsync(responseHeaders, false); await stream.WriteAsync(new ArraySegment <byte>( responseBody), true); // Request is fully handled here } catch (Exception e) { Console.WriteLine("Error during handling request: {0}", e.Message); stream.Cancel(); } }
bool Http2Handler(IStream stream) { try { using (Http2Context http2Context = new Http2Context(stream, _invoker)) { http2Context.Invoke(); } } catch (Exception e) { Console.WriteLine("处理请求时出错: {0}", e.Message); stream.Cancel(); return(false); } return(true); }