public async Task WriteResponseAsync(SwitchboardResponse response, CancellationToken ct) { var ms = new MemoryStream(); var sw = new StreamWriter(ms, headerEncoding); sw.NewLine = "\r\n"; sw.WriteLine("HTTP/{0} {1} {2}", response.ProtocolVersion, response.StatusCode, response.StatusDescription); for (int i = 0; i < response.Headers.Count; i++) { sw.WriteLine("{0}: {1}", response.Headers.GetKey(i), response.Headers.Get(i)); } sw.WriteLine(); sw.Flush(); var writeStream = this.GetWriteStream(); await writeStream.WriteAsync(ms.GetBuffer(), 0, (int)ms.Length).ConfigureAwait(false); Debug.WriteLine("{0}: Wrote headers ({1}b)", this.RemoteEndPoint, ms.Length); if (response.ResponseBody != null && response.ResponseBody.CanRead) { byte[] buffer = new byte[8192]; int read; long written = 0; while ((read = await response.ResponseBody.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0) { written += read; Debug.WriteLine("{0}: Read {1:N0} bytes from response body", this.RemoteEndPoint, read); await writeStream.WriteAsync(buffer, 0, read).ConfigureAwait(false); Debug.WriteLine("{0}: Wrote {1:N0} bytes to client", this.RemoteEndPoint, read); } Debug.WriteLine("{0}: Wrote response body ({1:N0} bytes) to client", this.RemoteEndPoint, written); } await writeStream.FlushAsync().ConfigureAwait(false); }
public async Task WriteResponseAsync(SwitchboardResponse response, CancellationToken ct) { var ms = new MemoryStream(); var sw = new StreamWriter(ms, headerEncoding); sw.NewLine = "\r\n"; sw.WriteLine("HTTP/{0} {1} {2}", response.ProtocolVersion, response.StatusCode, response.StatusDescription); for (int i = 0; i < response.Headers.Count; i++) sw.WriteLine("{0}: {1}", response.Headers.GetKey(i), response.Headers.Get(i)); sw.WriteLine(); sw.Flush(); var writeStream = this.GetWriteStream(); await writeStream.WriteAsync(ms.GetBuffer(), 0, (int)ms.Length).ConfigureAwait(false); Debug.WriteLine("{0}: Wrote headers ({1}b)", this.RemoteEndPoint, ms.Length); if (response.ResponseBody != null && response.ResponseBody.CanRead) { byte[] buffer = new byte[8192]; int read; long written = 0; while ((read = await response.ResponseBody.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0) { written += read; Debug.WriteLine("{0}: Read {1:N0} bytes from response body", this.RemoteEndPoint, read); await writeStream.WriteAsync(buffer, 0, read).ConfigureAwait(false); Debug.WriteLine("{0}: Wrote {1:N0} bytes to client", this.RemoteEndPoint, read); } Debug.WriteLine("{0}: Wrote response body ({1:N0} bytes) to client", this.RemoteEndPoint, written); } await writeStream.FlushAsync().ConfigureAwait(false); }
public async Task WriteResponseAsync(SwitchboardResponse response) { await WriteResponseAsync(response, CancellationToken.None) .ConfigureAwait(false); }