forked from newlysoft/HttpClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpConnection.cs
More file actions
162 lines (147 loc) · 5.82 KB
/
HttpConnection.cs
File metadata and controls
162 lines (147 loc) · 5.82 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Net.Http.Client
{
public class HttpConnection : IDisposable
{
private const string CRLF = "\r\n";
public HttpConnection(BufferedReadStream transport)
{
Transport = transport;
}
public BufferedReadStream Transport { get; private set; }
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
try
{
// Serialize headers & send
string rawRequest = SerializeRequest(request);
byte[] requestBytes = Encoding.ASCII.GetBytes(rawRequest);
await Transport.WriteAsync(requestBytes, 0, requestBytes.Length, cancellationToken);
// TODO: Determin if there's a request body?
// Wait for 100-continue?
// Send body
// Receive headers
List<string> responseLines = await ReadResponseLinesAsync(cancellationToken);
// Determine response type (Chunked, Content-Length, opaque, none...)
// Receive body
return CreateResponseMessage(responseLines);
}
catch (Exception ex)
{
Dispose(); // Any errors at this layer abort the connection.
throw new HttpRequestException("The requested failed, see inner exception for details.", ex);
}
}
private string SerializeRequest(HttpRequestMessage request)
{
StringBuilder builder = new StringBuilder();
builder.Append(request.Method);
builder.Append(' ');
builder.Append(request.GetAddressLineProperty());
builder.Append(" HTTP/");
builder.Append(request.Version.ToString(2));
builder.Append(CRLF);
foreach (var header in request.Headers)
{
foreach (var value in header.Value)
{
builder.Append(header.Key);
builder.Append(": ");
builder.Append(value);
builder.Append(CRLF);
}
}
if (request.Content != null)
{
foreach (var header in request.Content.Headers)
{
foreach (var value in header.Value)
{
builder.Append(header.Key);
builder.Append(": ");
builder.Append(value);
builder.Append(CRLF);
}
}
}
// Headers end with an empty line
builder.Append(CRLF);
return builder.ToString();
}
private async Task<List<string>> ReadResponseLinesAsync(CancellationToken cancellationToken)
{
List<string> lines = new List<string>();
string line = await Transport.ReadLineAsync(cancellationToken);
while (line.Length > 0)
{
lines.Add(line);
line = await Transport.ReadLineAsync(cancellationToken);
}
return lines;
}
private HttpResponseMessage CreateResponseMessage(List<string> responseLines)
{
string responseLine = responseLines.First();
// HTTP/1.1 200 OK
string[] responseLineParts = responseLine.Split(new[] { ' ' }, 3);
// TODO: Verify HTTP/1.0 or 1.1.
if (responseLineParts.Length < 2)
{
throw new HttpRequestException("Invalid response line: " + responseLine);
}
int statusCode = 0;
if (int.TryParse(responseLineParts[1], NumberStyles.None, CultureInfo.InvariantCulture, out statusCode))
{
// TODO: Validate range
}
else
{
throw new HttpRequestException("Invalid status code: " + responseLineParts[1]);
}
HttpResponseMessage response = new HttpResponseMessage((HttpStatusCode)statusCode);
if (responseLineParts.Length >= 3)
{
response.ReasonPhrase = responseLineParts[2];
}
var content = new HttpConnectionResponseContent(this);
response.Content = content;
foreach (var rawHeader in responseLines.Skip(1))
{
int colonOffset = rawHeader.IndexOf(':');
if (colonOffset <= 0)
{
throw new HttpRequestException("The given header line format is invalid: " + rawHeader);
}
string headerName = rawHeader.Substring(0, colonOffset);
string headerValue = rawHeader.Substring(colonOffset + 2);
if (!response.Headers.TryAddWithoutValidation(headerName, headerValue))
{
bool success = response.Content.Headers.TryAddWithoutValidation(headerName, headerValue);
System.Diagnostics.Debug.Assert(success, "Failed to add response header: " + rawHeader);
}
}
// After headers have been set
content.ResolveResponseStream(chunked: response.Headers.TransferEncodingChunked.HasValue && response.Headers.TransferEncodingChunked.Value);
return response;
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Transport.Dispose();
}
}
}
}