/
HttpListenerResponse.cs
281 lines (244 loc) · 9.5 KB
/
HttpListenerResponse.cs
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
namespace System.Net
{
public sealed unsafe partial class HttpListenerResponse : IDisposable
{
private BoundaryType _boundaryType = BoundaryType.None;
private CookieCollection? _cookies;
private readonly HttpListenerContext? _httpContext;
private bool _keepAlive = true;
private HttpResponseStream? _responseStream;
private string? _statusDescription;
private WebHeaderCollection _webHeaders = new WebHeaderCollection();
public WebHeaderCollection Headers
{
get => _webHeaders;
set
{
_webHeaders = new WebHeaderCollection();
foreach (string headerName in value.AllKeys)
{
_webHeaders.Add(headerName, value[headerName]);
}
}
}
public Encoding? ContentEncoding { get; set; }
public string? ContentType
{
get => Headers[HttpKnownHeaderNames.ContentType];
set
{
CheckDisposed();
if (string.IsNullOrEmpty(value))
{
Headers.Remove(HttpKnownHeaderNames.ContentType);
}
else
{
Headers.Set(HttpKnownHeaderNames.ContentType, value);
}
}
}
private HttpListenerContext HttpListenerContext => _httpContext!;
private HttpListenerRequest HttpListenerRequest => HttpListenerContext!.Request;
internal EntitySendFormat EntitySendFormat
{
get => (EntitySendFormat)_boundaryType;
set
{
CheckDisposed();
CheckSentHeaders();
if (value == EntitySendFormat.Chunked && HttpListenerRequest.ProtocolVersion.Minor == 0)
{
throw new ProtocolViolationException(SR.net_nochunkuploadonhttp10);
}
_boundaryType = (BoundaryType)value;
if (value != EntitySendFormat.ContentLength)
{
_contentLength = -1;
}
}
}
public bool SendChunked
{
get => EntitySendFormat == EntitySendFormat.Chunked;
set => EntitySendFormat = value ? EntitySendFormat.Chunked : EntitySendFormat.ContentLength;
}
private static bool CanSendResponseBody(int responseCode) =>
// We MUST NOT send message-body when we send responses with these Status codes
responseCode is not (100 or 101 or 204 or 205 or 304);
public long ContentLength64
{
get => _contentLength;
set
{
CheckDisposed();
CheckSentHeaders();
ArgumentOutOfRangeException.ThrowIfNegative(value);
_contentLength = value;
_boundaryType = BoundaryType.ContentLength;
}
}
public CookieCollection Cookies
{
get => _cookies ??= new CookieCollection();
set => _cookies = value;
}
public bool KeepAlive
{
get => _keepAlive;
set
{
CheckDisposed();
_keepAlive = value;
}
}
public Stream OutputStream
{
get
{
CheckDisposed();
EnsureResponseStream();
return _responseStream!;
}
}
public string? RedirectLocation
{
get => Headers[HttpResponseHeader.Location];
set
{
// note that this doesn't set the status code to a redirect one
CheckDisposed();
if (string.IsNullOrEmpty(value))
{
Headers.Remove(HttpKnownHeaderNames.Location);
}
else
{
Headers.Set(HttpKnownHeaderNames.Location, value);
}
}
}
public string StatusDescription
{
get
{
// if the user hasn't set this, generate on the fly, if possible.
// We know this one is safe, no need to verify it as in the setter.
return _statusDescription ??= HttpStatusDescription.Get(StatusCode) ?? string.Empty;
}
set
{
CheckDisposed();
ArgumentNullException.ThrowIfNull(value);
// Need to verify the status description doesn't contain any control characters except HT. We mask off the high
// byte since that's how it's encoded.
for (int i = 0; i < value.Length; i++)
{
char c = (char)(0x000000ff & (uint)value[i]);
if ((c <= 31 && c != (byte)'\t') || c == 127)
{
throw new ArgumentException(SR.net_WebHeaderInvalidControlChars, nameof(value));
}
}
_statusDescription = value;
}
}
public void AddHeader(string name, string value)
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"name={name}, value={value}");
Headers.Set(name, value);
}
public void AppendHeader(string name, string value)
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"name={name}, value={value}");
Headers.Add(name, value);
}
public void AppendCookie(Cookie cookie)
{
ArgumentNullException.ThrowIfNull(cookie);
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"cookie: {cookie}");
Cookies.Add(cookie);
}
private void ComputeCookies()
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"Entering Set-Cookie: {Headers[HttpResponseHeader.SetCookie]}, Set-Cookie2: {Headers[HttpKnownHeaderNames.SetCookie2]}");
if (_cookies != null)
{
// now go through the collection, and concatenate all the cookies in per-variant strings
string? setCookie2 = null, setCookie = null;
for (int index = 0; index < _cookies.Count; index++)
{
Cookie cookie = _cookies[index];
string cookieString = cookie.ToServerString();
if (string.IsNullOrEmpty(cookieString))
{
continue;
}
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"Now looking at index:{index} cookie: {cookie}");
if (cookie.IsRfc2965Variant())
{
setCookie2 = setCookie2 == null ? cookieString : setCookie2 + ", " + cookieString;
}
else
{
setCookie = setCookie == null ? cookieString : setCookie + ", " + cookieString;
}
}
if (!string.IsNullOrEmpty(setCookie))
{
Headers.Set(HttpKnownHeaderNames.SetCookie, setCookie);
if (string.IsNullOrEmpty(setCookie2))
{
Headers.Remove(HttpKnownHeaderNames.SetCookie2);
}
}
if (!string.IsNullOrEmpty(setCookie2))
{
Headers.Set(HttpKnownHeaderNames.SetCookie2, setCookie2);
if (string.IsNullOrEmpty(setCookie))
{
Headers.Remove(HttpKnownHeaderNames.SetCookie);
}
}
}
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"Exiting Set-Cookie: {Headers[HttpResponseHeader.SetCookie]} Set-Cookie2: {Headers[HttpKnownHeaderNames.SetCookie2]}");
}
public void Redirect([StringSyntax(StringSyntaxAttribute.Uri)] string url)
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"url={url}");
Headers[HttpResponseHeader.Location] = url;
StatusCode = (int)HttpStatusCode.Redirect;
StatusDescription = HttpStatusDescription.Get(StatusCode)!;
}
public void SetCookie(Cookie cookie)
{
ArgumentNullException.ThrowIfNull(cookie);
Cookie newCookie = cookie.Clone();
int added = Cookies.InternalAdd(newCookie, true);
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"cookie: {cookie}");
if (added != 1)
{
// The Cookie already existed and couldn't be replaced.
throw new ArgumentException(SR.net_cookie_exists, nameof(cookie));
}
}
void IDisposable.Dispose() => Dispose();
private void CheckDisposed()
{
ObjectDisposedException.ThrowIf(Disposed, this);
}
private void CheckSentHeaders()
{
if (SentHeaders)
{
throw new InvalidOperationException(SR.net_rspsubmitted);
}
}
}
}