forked from newlysoft/HttpClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpConnectionResponseContent.cs
More file actions
60 lines (53 loc) · 1.67 KB
/
Copy pathHttpConnectionResponseContent.cs
File metadata and controls
60 lines (53 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace Microsoft.Net.Http.Client
{
public class HttpConnectionResponseContent : HttpContent
{
private readonly HttpConnection _connection;
private Stream _responseStream;
public HttpConnectionResponseContent(HttpConnection connection)
{
_connection = connection;
}
public void ResolveResponseStream(bool chunked)
{
if (_responseStream != null)
{
throw new InvalidOperationException("Called multiple times");
}
if (chunked)
{
_responseStream = new ChunkedReadStream(_connection.Transport);
}
else if (Headers.ContentLength.HasValue)
{
_responseStream = new ContentLengthReadStream(_connection.Transport, Headers.ContentLength.Value);
}
else
{
// Raw, read until end and close
_responseStream = _connection.Transport;
}
}
protected override Task SerializeToStreamAsync(Stream stream, System.Net.TransportContext context)
{
return _responseStream.CopyToAsync(stream);
}
protected override Task<Stream> CreateContentReadStreamAsync()
{
return Task.FromResult(_responseStream);
}
protected override bool TryComputeLength(out long length)
{
length = 0;
return false;
}
protected override void Dispose(bool disposing)
{
_responseStream.Dispose();
}
}
}