private async void OnAccept(Stream stream) { var owinContext = new OwinContext(); owinContext.Set("owin.Version", "1.0"); await PopulateRequestAsync(stream, owinContext.Request).ConfigureAwait(false); var body = new MemoryStream(); owinContext.Response.Body = body; await _app(owinContext.Environment).ConfigureAwait(false); await body.FlushAsync().ConfigureAwait(false); var writer = new HttpHeaderWriter(stream, 1024); await writer.WriteStatusAndHeadersAsync(owinContext.Request.Protocol, owinContext.Response.StatusCode.ToString(), owinContext.Response.ReasonPhrase, owinContext.Response.Headers.Select(i => new KeyValuePair <string, IEnumerable <string> >(i.Key, i.Value))).ConfigureAwait(false); await writer.FlushAsync().ConfigureAwait(false); body.Position = 0; await body.CopyToAsync(stream).ConfigureAwait(false); await stream.FlushAsync().ConfigureAwait(false); await((stream as IWithCloseWriteSupport)?.CloseWriteAsync() ?? Task.CompletedTask).ConfigureAwait(false); }
async void HandleClientStream(Stream stream, IHttpApplication <HostingApplication.Context> application) { using (stream) { var httpCtx = new DefaultHttpContext(); var requestFeature = await CreateRequesteAsync(stream).ConfigureAwait(false); httpCtx.Features.Set <IHttpRequestFeature>(requestFeature); var responseFeature = new HttpResponseFeature(); httpCtx.Features.Set <IHttpResponseFeature>(responseFeature); var ctx = application.CreateContext(httpCtx.Features); var body = new MemoryStream(); responseFeature.Body = body; await application.ProcessRequestAsync(ctx).ConfigureAwait(false); var writer = new HttpHeaderWriter(stream, 1024); await writer.WriteStatusAndHeadersAsync(requestFeature.Protocol, responseFeature.StatusCode.ToString(), responseFeature.ReasonPhrase, responseFeature.Headers.Select(i => new KeyValuePair <string, IEnumerable <string> >(i.Key, i.Value))).ConfigureAwait(false); await writer.FlushAsync().ConfigureAwait(false); body.Position = 0; await body.CopyToAsync(stream).ConfigureAwait(false); await stream.FlushAsync().ConfigureAwait(false); await((stream as IWithCloseWriteSupport)?.CloseWriteAsync() ?? Task.CompletedTask).ConfigureAwait(false); } }
public async Task FlushTwice() { var ms = new MemoryStream(); var testee = new HttpHeaderWriter(ms, 3); await testee.WriteBytesAsync(new byte[] { 1, 2 }); await testee.WriteBytesAsync(new byte[] { 3 }); await testee.WriteBytesAsync(new byte[] { 4 }); await testee.WriteBytesAsync(new byte[] { 5, 6, 7 }); await testee.WriteBytesAsync(new byte[] { 8, 9 }); await testee.WriteBytesAsync(new byte[] { 10, 11, 12, 13 }); await testee.FlushAsync(); await testee.FlushAsync(); await ms.FlushAsync(); await ms.FlushAsync(); Assert.Equal(byteSequence(1, 14), ms.ToArray()); }
public async Task WriteStringWithDifferentAlignments() { var ms = new MemoryStream(); var testee = new HttpHeaderWriter(ms, 3); await testee.WriteStringAsync("Hello world"); await testee.FlushAsync(); await ms.FlushAsync(); Assert.Equal("Hello world", Encoding.UTF8.GetString(ms.ToArray())); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { ValidateAndNormalizeRequest(request); var stream = await _dial.DialAsync(request, cancellationToken).ConfigureAwait(false); request.Properties.Add(UnderlyingStreamProperty, stream); var headerWriter = new HttpHeaderWriter(stream, 4096); await headerWriter.WriteMethodAndHeadersAsync(request).ConfigureAwait(false); await headerWriter.FlushAsync().ConfigureAwait(false); if (request.Content != null) { await request.Content.CopyToAsync(stream).ConfigureAwait(false); } await stream.FlushAsync().ConfigureAwait(false); await((stream as IWithCloseWriteSupport)?.CloseWriteAsync() ?? Task.CompletedTask).ConfigureAwait(false); var responseContent = new DialResponseContent(); var response = new HttpResponseMessage { RequestMessage = request, Content = responseContent }; var lineReader = new ByLineReader(stream, 4096); ParseStatusLine(response, await lineReader.NextLineAsync().ConfigureAwait(false)); for (; ;) { var line = await lineReader.NextLineAsync().ConfigureAwait(false); if (line.Count == 0) { break; } try { (var name, var value) = HttpParser.ParseHeaderNameValue(line); if (!response.Headers.TryAddWithoutValidation(name, value)) { response.Content.Headers.TryAddWithoutValidation(name, value); } } catch (FormatException ex) { throw new HttpRequestException("Error parsing header", ex); } } responseContent.SetContent(lineReader.Remaining, stream); return(response); }
public async Task WriteTwoBytesWithDifferentAlignments() { var ms = new MemoryStream(); var testee = new HttpHeaderWriter(ms, 3); await testee.WriteTwoBytesAsync(1, 2); await testee.WriteTwoBytesAsync(3, 4); await testee.WriteTwoBytesAsync(5, 6); await testee.FlushAsync(); await ms.FlushAsync(); Assert.Equal(byteSequence(1, 7), ms.ToArray()); }
public async Task WriteByteAsyncShouldSucceedWhenFullOrNot() { var ms = new MemoryStream(); var testee = new HttpHeaderWriter(ms, 1); await testee.WriteByteAsync(1); // assert buffered Assert.Equal(0, ms.Length); await testee.WriteByteAsync(2); // assert flushed Assert.Equal(1, ms.Length); await testee.FlushAsync(); await ms.FlushAsync(); Assert.Equal(2, ms.Length); Assert.True(Enumerable.SequenceEqual(new byte[] { 1, 2 }, ms.ToArray())); }
public async Task WriteMultiValueHeader() { var ms = new MemoryStream(); var testee = new HttpHeaderWriter(ms, 3); await testee.WriteStatusAndHeadersAsync("HTTP/1.0", "200", "OK", new Dictionary <string, IEnumerable <string> > { { "Test-Header", new List <string> { "value1", "value2" } } }); await testee.FlushAsync(); await ms.FlushAsync(); Assert.Equal("HTTP/1.0 200 OK\r\nTest-Header: value1, value2\r\n\r\n", Encoding.UTF8.GetString(ms.ToArray())); }
public async Task WriteInvalidStringAsync() { var ms = new MemoryStream(); var testee = new HttpHeaderWriter(ms, 3); await Assert.ThrowsAsync <HttpRequestException>(() => testee.WriteStringAsync("ñ")); }