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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//
// HttpListenerResponse.cs
//    Copied from System.Net.HttpListenerResponse.cs
//
// Author:
//    Gonzalo Paniagua Javier (gonzalo@novell.com)
//
// Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
// Copyright (c) 2012 sta.blockhead (sta.blockhead@gmail.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
 
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
 
namespace WebSocketSharp.Net
{
    public sealed class HttpListenerResponse : IDisposable
    {
        #region Private Fields
 
        bool chunked;
        bool cl_set;
        Encoding content_encoding;
        long content_length;
        string content_type;
        HttpListenerContext context;
        CookieCollection cookies;
        bool disposed;
        bool force_close_chunked;
        WebHeaderCollection headers;
        bool keep_alive;
        string location;
        ResponseStream output_stream;
        int status_code;
        string status_description;
        Version version;
 
        #endregion
 
        #region Internal Fields
 
        internal bool HeadersSent;
 
        #endregion
 
        #region Constructor
 
        internal HttpListenerResponse(HttpListenerContext context)
        {
            this.context = context;
            Init();
        }
 
        #endregion
 
        #region Internal Property
 
        internal bool ForceCloseChunked
        {
            get { return force_close_chunked; }
        }
 
        #endregion
 
        #region Public Properties
 
        public Encoding ContentEncoding
        {
            get
            {
                if (content_encoding == null)
                    content_encoding = Encoding.Default;
                return content_encoding;
            }
            set
            {
                if (disposed)
                    throw new ObjectDisposedException(GetType().ToString());
 
                // TODO: is null ok?
                if (HeadersSent)
                    throw new InvalidOperationException("Cannot be changed after headers are sent.");
 
                content_encoding = value;
            }
        }
 
        public long ContentLength64
        {
            get { return content_length; }
            set
            {
                if (disposed)
                    throw new ObjectDisposedException(GetType().ToString());
 
                if (HeadersSent)
                    throw new InvalidOperationException("Cannot be changed after headers are sent.");
 
                if (value < 0)
                    throw new ArgumentOutOfRangeException("Must be >= 0", "value");
 
                cl_set = true;
                content_length = value;
            }
        }
 
        public string ContentType
        {
            get { return content_type; }
            set
            {
                // TODO: is null ok?
                if (disposed)
                    throw new ObjectDisposedException(GetType().ToString());
 
                if (HeadersSent)
                    throw new InvalidOperationException("Cannot be changed after headers are sent.");
 
                content_type = value;
            }
        }
 
        // RFC 2109, 2965 + the netscape specification at http://wp.netscape.com/newsref/std/cookie_spec.html
        public CookieCollection Cookies
        {
            get
            {
                if (cookies == null)
                    cookies = new CookieCollection();
                return cookies;
            }
            set { cookies = value; } // null allowed?
        }
 
        public WebHeaderCollection Headers
        {
            get { return headers; }
            set
            {
                /**
                 *    "If you attempt to set a Content-Length, Keep-Alive, Transfer-Encoding, or
                 *    WWW-Authenticate header using the Headers property, an exception will be
                 *    thrown. Use the KeepAlive or ContentLength64 properties to set these headers.
                 *    You cannot set the Transfer-Encoding or WWW-Authenticate headers manually."
                */
                // TODO: check if this is marked readonly after headers are sent.
                headers = value;
            }
        }
 
        public bool KeepAlive
        {
            get { return keep_alive; }
            set
            {
                if (disposed)
                    throw new ObjectDisposedException(GetType().ToString());
 
                if (HeadersSent)
                    throw new InvalidOperationException("Cannot be changed after headers are sent.");
 
                keep_alive = value;
            }
        }
 
        public Stream OutputStream
        {
            get
            {
                if (output_stream == null)
                    output_stream = context.Connection.GetResponseStream();
                return output_stream;
            }
        }
 
        public Version ProtocolVersion
        {
            get { return version; }
            set
            {
                if (disposed)
                    throw new ObjectDisposedException(GetType().ToString());
 
                if (HeadersSent)
                    throw new InvalidOperationException("Cannot be changed after headers are sent.");
 
                if (value == null)
                    throw new ArgumentNullException("value");
 
                if (value.Major != 1 || (value.Minor != 0 && value.Minor != 1))
                    throw new ArgumentException("Must be 1.0 or 1.1", "value");
 
                if (disposed)
                    throw new ObjectDisposedException(GetType().ToString());
 
                version = value;
            }
        }
 
        public string RedirectLocation
        {
            get { return location; }
            set
            {
                if (disposed)
                    throw new ObjectDisposedException(GetType().ToString());
 
                if (HeadersSent)
                    throw new InvalidOperationException("Cannot be changed after headers are sent.");
 
                location = value;
            }
        }
 
        public bool SendChunked
        {
            get { return chunked; }
            set
            {
                if (disposed)
                    throw new ObjectDisposedException(GetType().ToString());
 
                if (HeadersSent)
                    throw new InvalidOperationException("Cannot be changed after headers are sent.");
 
                chunked = value;
            }
        }
 
        public int StatusCode
        {
            get { return status_code; }
            set
            {
                if (disposed)
                    throw new ObjectDisposedException(GetType().ToString());
 
                if (HeadersSent)
                    throw new InvalidOperationException("Cannot be changed after headers are sent.");
 
                if (value < 100 || value > 999)
                    throw new ProtocolViolationException("StatusCode must be between 100 and 999.");
 
                status_code = value;
                status_description = Ext.GetStatusDescription(value);
            }
        }
 
        public string StatusDescription
        {
            get { return status_description; }
            set
            {
                status_description = value;
            }
        }
 
        #endregion
 
        #region Private Methods
 
        void Close(bool force)
        {
            disposed = true;
            context.Connection.Close(force);
        }
 
        void IDisposable.Dispose()
        {
            Close(true); // TODO: Abort or Close?
        }
 
        bool FindCookie(Cookie cookie)
        {
            string name = cookie.Name;
            string domain = cookie.Domain;
            string path = cookie.Path;
            foreach (Cookie c in cookies)
            {
                if (name != c.Name)
                    continue;
                if (domain != c.Domain)
                    continue;
                if (path == c.Path)
                    return true;
            }
 
            return false;
        }
 
        void Init()
        {
            headers = new WebHeaderCollection();
            keep_alive = true;
            status_code = 200;
            status_description = "OK";
            version = HttpVersion.Version11;
        }
 
        #endregion
 
        #region Internal Method
 
        internal void SendHeaders(bool closing, MemoryStream ms)
        {
            Encoding encoding = content_encoding;
            if (encoding == null)
                encoding = Encoding.Default;
 
            if (content_type != null)
            {
                if (content_encoding != null && content_type.IndexOf("charset=", StringComparison.Ordinal) == -1)
                {
                    string enc_name = content_encoding.WebName;
                    headers.SetInternal("Content-Type", content_type + "; charset=" + enc_name);
                }
                else
                {
                    headers.SetInternal("Content-Type", content_type);
                }
            }
 
            if (headers["Server"] == null)
                headers.SetInternal("Server", "Mono-HTTPAPI/1.0");
 
            CultureInfo inv = CultureInfo.InvariantCulture;
            if (headers["Date"] == null)
                headers.SetInternal("Date", DateTime.UtcNow.ToString("r", inv));
 
            if (!chunked)
            {
                if (!cl_set && closing)
                {
                    cl_set = true;
                    content_length = 0;
                }
 
                if (cl_set)
                    headers.SetInternal("Content-Length", content_length.ToString(inv));
            }
 
            Version v = context.Request.ProtocolVersion;
            if (!cl_set && !chunked && v >= HttpVersion.Version11)
                chunked = true;
 
            /* Apache forces closing the connection for these status codes:
             *    HttpStatusCode.BadRequest                 400
             *    HttpStatusCode.RequestTimeout             408
             *    HttpStatusCode.LengthRequired             411
             *    HttpStatusCode.RequestEntityTooLarge     413
             *    HttpStatusCode.RequestUriTooLong         414
             *    HttpStatusCode.InternalServerError         500
             *    HttpStatusCode.ServiceUnavailable         503
             */
            bool conn_close = (status_code == 400 || status_code == 408 || status_code == 411 ||
                    status_code == 413 || status_code == 414 || status_code == 500 ||
                    status_code == 503);
 
            if (conn_close == false)
                conn_close = !context.Request.KeepAlive;
 
            // They sent both KeepAlive: true and Connection: close!?
            if (!keep_alive || conn_close)
            {
                headers.SetInternal("Connection", "close");
                conn_close = true;
            }
 
            if (chunked)
                headers.SetInternal("Transfer-Encoding", "chunked");
 
            int reuses = context.Connection.Reuses;
            if (reuses >= 100)
            {
                force_close_chunked = true;
                if (!conn_close)
                {
                    headers.SetInternal("Connection", "close");
                    conn_close = true;
                }
            }
 
            if (!conn_close)
            {
                headers.SetInternal("Keep-Alive", String.Format("timeout=15,max={0}", 100 - reuses));
                if (context.Request.ProtocolVersion <= HttpVersion.Version10)
                    headers.SetInternal("Connection", "keep-alive");
            }
 
            if (location != null)
                headers.SetInternal("Location", location);
 
            if (cookies != null)
            {
                foreach (Cookie cookie in cookies)
                    headers.SetInternal("Set-Cookie", cookie.ToClientString());
            }
 
            StreamWriter writer = new StreamWriter(ms, encoding, 256);
            writer.Write("HTTP/{0} {1} {2}\r\n", version, status_code, status_description);
            string headers_str = headers.ToStringMultiValue();
            writer.Write(headers_str);
            writer.Flush();
            int preamble = (encoding.CodePage == 65001) ? 3 : encoding.GetPreamble().Length;
            if (output_stream == null)
                output_stream = context.Connection.GetResponseStream();
 
            /* Assumes that the ms was at position 0 */
            ms.Position = preamble;
            HeadersSent = true;
        }
 
        #endregion
 
        #region Public Methods
 
        public void Abort()
        {
            if (disposed)
                return;
 
            Close(true);
        }
 
        public void AddHeader(string name, string value)
        {
            if (name == null)
                throw new ArgumentNullException("name");
 
            if (name == "")
                throw new ArgumentException("'name' cannot be empty", "name");
 
            // TODO: check for forbidden headers and invalid characters
            if (value.Length > 65535)
                throw new ArgumentOutOfRangeException("value");
 
            headers.Set(name, value);
        }
 
        public void AppendCookie(Cookie cookie)
        {
            if (cookie == null)
                throw new ArgumentNullException("cookie");
 
            Cookies.Add(cookie);
        }
 
        public void AppendHeader(string name, string value)
        {
            if (name == null)
                throw new ArgumentNullException("name");
 
            if (name == "")
                throw new ArgumentException("'name' cannot be empty", "name");
 
            if (value.Length > 65535)
                throw new ArgumentOutOfRangeException("value");
 
            headers.Add(name, value);
        }
 
        public void Close()
        {
            if (disposed)
                return;
 
            Close(false);
        }
 
        public void Close(byte[] responseEntity, bool willBlock)
        {
            if (disposed)
                return;
 
            if (responseEntity == null)
                throw new ArgumentNullException("responseEntity");
 
            // TODO: if willBlock -> BeginWrite + Close ?
            ContentLength64 = responseEntity.Length;
            OutputStream.Write(responseEntity, 0, (int)content_length);
            Close(false);
        }
 
        public void CopyFrom(HttpListenerResponse templateResponse)
        {
            headers.Clear();
            headers.Add(templateResponse.headers);
            content_length = templateResponse.content_length;
            status_code = templateResponse.status_code;
            status_description = templateResponse.status_description;
            keep_alive = templateResponse.keep_alive;
            version = templateResponse.version;
        }
 
        public void Redirect(string url)
        {
            StatusCode = 302; // Found
            location = url;
        }
 
        public void SetCookie(Cookie cookie)
        {
            if (cookie == null)
                throw new ArgumentNullException("cookie");
 
            if (cookies != null)
            {
                if (FindCookie(cookie))
                    throw new ArgumentException("The cookie already exists.");
            }
            else
            {
                cookies = new CookieCollection();
            }
 
            cookies.Add(cookie);
        }
 
        #endregion
    }
}