File: HttpListener2Test.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (841 lines) | stat: -rw-r--r-- 27,289 bytes parent folder | download
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
//
// HttpListener2Test.cs
//	- Unit tests for System.Net.HttpListener - connection testing
//
// Author:
//	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.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.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

using NUnit.Framework;

using MonoTests.Helpers;

// ***************************************************************************************
// NOTE: when adding prefixes, make then unique per test, as MS might take 'some time' to
// unregister it even after explicitly closing the listener.
// ***************************************************************************************
namespace MonoTests.System.Net {
	
	[TestFixture]
	public class HttpListener2Test {
		
		private HttpListener _listener = null;
		
		public class MyNetworkStream : NetworkStream {
			public MyNetworkStream (Socket sock) : base (sock, true)
			{
			}

			public Socket GetSocket ()
			{
				return Socket;
			}
		}

		public static HttpListener CreateAndStartListener (string prefix)
		{
			HttpListener listener = new HttpListener ();
			listener.Prefixes.Add (prefix);
			listener.Start ();
			return listener;
		}

		public static HttpListener CreateAndStartListener (string prefix, AuthenticationSchemes authSchemes)
		{
			HttpListener listener = new HttpListener ();
			listener.AuthenticationSchemes = authSchemes;
			listener.Prefixes.Add (prefix);
			listener.Start ();
			return listener;
		}

		public static MyNetworkStream CreateNS (int port)
		{
			return CreateNS (IPAddress.Loopback, port, 5000);
		}

		public static MyNetworkStream CreateNS (int port, int timeout_ms)
		{
			return CreateNS (IPAddress.Loopback, port, timeout_ms);
		}

		public static MyNetworkStream CreateNS (IPAddress ip, int port)
		{
			return CreateNS (ip, port, 5000);
		}

		public static MyNetworkStream CreateNS (IPAddress ip, int port, int timeout_ms)
		{
			Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			sock.Connect (new IPEndPoint (ip, port));
			sock.SendTimeout = timeout_ms;
			sock.ReceiveTimeout = timeout_ms;
			return new MyNetworkStream (sock);
		}

		public static void Send (Stream stream, string str)
		{
			byte [] bytes = Encoding.ASCII.GetBytes (str);
			stream.Write (bytes, 0, bytes.Length);
		}

		public static string Receive (Stream stream, int size)
		{
			byte [] bytes = new byte [size];
			int nread = stream.Read (bytes, 0, size);
			return Encoding.ASCII.GetString (bytes, 0, nread);
		}

		public static string ReceiveWithTimeout (Stream stream, int size, int timeout, out bool timed_out)
		{
			byte [] bytes = new byte [size];
			IAsyncResult ares = stream.BeginRead (bytes, 0, size, null, null);
			timed_out = !ares.AsyncWaitHandle.WaitOne (timeout, false);
			if (timed_out)
				return null;
			int nread = stream.EndRead (ares);
			return Encoding.ASCII.GetString (bytes, 0, nread);
		}

		public static HttpListenerContext GetContextWithTimeout (HttpListener listener, int timeout, out bool timed_out)
		{
			IAsyncResult ares = listener.BeginGetContext (null, null);
			timed_out = !ares.AsyncWaitHandle.WaitOne (timeout, false);
			if (timed_out) 
				return null;
			return listener.EndGetContext (ares);
		}
		
		[TearDown] 
		public void Dispose()
		{
			if (_listener != null) {
				_listener.Close();
				_listener = null;
			}
		}

		[Test]
		public void Test1 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test1/");
			NetworkStream ns = CreateNS (port);
			Send (ns, "GET / HTTP/1.1\r\n\r\n"); // No host
			string response = Receive (ns, 512);
			ns.Close ();
			StringAssert.StartsWith ("HTTP/1.1 400", response);
		}

		[Test]
		public void Test2 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test2/");
			NetworkStream ns = CreateNS (port);
			Send (ns, "GET / HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"); // no prefix
			string response = Receive (ns, 512);
			ns.Close ();
			StringAssert.StartsWith ("HTTP/1.1 400", response);
		}

		[Test]
		public void Test3 ()
		{
			StringBuilder bad = new StringBuilder ();
			for (int i = 0; i < 33; i++){
				if (i != 13)
					bad.Append ((char) i);
			}
			bad.Append ('(');
			bad.Append (')');
			bad.Append ('[');
			bad.Append (']');
			bad.Append ('<');
			bad.Append ('>');
			bad.Append ('@');
			bad.Append (',');
			bad.Append (';');
			bad.Append (':');
			bad.Append ('\\');
			bad.Append ('"');
			bad.Append ('/');
			bad.Append ('?');
			bad.Append ('=');
			bad.Append ('{');
			bad.Append ('}');

			foreach (char b in bad.ToString ()){
				var port = NetworkHelpers.FindFreePort ();
				HttpListener listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test3/");
				NetworkStream ns = CreateNS (port);
				Send (ns, String.Format ("MA{0} / HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n", b)); // bad method
				
				string response = Receive (ns, 512);
				ns.Close ();
				listener.Close ();
				StringAssert.StartsWith ("HTTP/1.1 400", response, String.Format ("Failed on {0}", (int) b));
			}
		}

		[Test]
		public void Test4 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test4/");
			NetworkStream ns = CreateNS (port);
			Send (ns, "POST /test4/ HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"); // length required
			string response = Receive (ns, 512);
			ns.Close ();
			StringAssert.StartsWith ("HTTP/1.1 411", response);
		}

		[Test]
		public void Test5 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test5/");
			NetworkStream ns = CreateNS (port);
			Send (ns, "POST / HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: pepe\r\n\r\n"); // not implemented
			string response = Receive (ns, 512);
			ns.Close ();
			StringAssert.StartsWith ("HTTP/1.1 501", response);
		}

		[Test]
		public void Test6 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test6/");
			NetworkStream ns = CreateNS (port);
			 // not implemented! This is against the RFC. Should be a bad request/length required
			Send (ns, "POST /test6/ HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: identity\r\n\r\n");
			string response = Receive (ns, 512);
			ns.Close ();
			StringAssert.StartsWith ("HTTP/1.1 501", response);
		}

		[Test]
		public void Test7 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test7/");
			NetworkStream ns = CreateNS (port);
			Send (ns, "POST /test7/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
			HttpListenerContext ctx = _listener.GetContext ();
			Send (ctx.Response.OutputStream, "%%%OK%%%");
			ctx.Response.Close ();
			string response = Receive (ns, 1024);
			ns.Close ();
			StringAssert.StartsWith ("HTTP/1.1 200", response);
			StringAssert.Contains ("Transfer-Encoding: chunked", response);
		}

		[Test]
		public void Test8 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test8/");
			NetworkStream ns = CreateNS (port);
			// Just like Test7, but 1.0
			Send (ns, "POST /test8/ HTTP/1.0\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
			HttpListenerContext ctx = _listener.GetContext ();
			Send (ctx.Response.OutputStream, "%%%OK%%%");
			ctx.Response.Close ();
			string response = Receive (ns, 512);
			ns.Close ();
			StringAssert.StartsWith ("HTTP/1.1 200", response);
			Assert.IsTrue (-1 == response.IndexOf ("Transfer-Encoding: chunked"));
		}

		[Test]
		public void Test9 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			// 1.0 + "Transfer-Encoding: chunked"
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test9/");
			NetworkStream ns = CreateNS (port);
			Send (ns, "POST /test9/ HTTP/1.0\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n3\r\n123\r\n0\r\n\r\n");
			bool timeout;
			string response = ReceiveWithTimeout (ns, 512, 1000, out timeout);
			ns.Close ();
			Assert.IsFalse (timeout);
			StringAssert.StartsWith ("HTTP/1.1 411", response);
		}

		[Test]
		public void Test10 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			// Same as Test9, but now we shutdown the socket for sending.
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test10/");
			MyNetworkStream ns = CreateNS (port);
			Send (ns, "POST /test10/ HTTP/1.0\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n3\r\n123\r\n0\r\n\r\n");
			ns.GetSocket ().Shutdown (SocketShutdown.Send);
			bool timeout;
			string response = ReceiveWithTimeout (ns, 512, 1000, out timeout);
			ns.Close ();
			Assert.IsFalse (timeout);
			StringAssert.StartsWith ("HTTP/1.1 411", response);
		}

		[Test]
		public void Test11 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			// 0.9
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test11/");
			MyNetworkStream ns = CreateNS (port);
			Send (ns, "POST /test11/ HTTP/0.9\r\nHost: 127.0.0.1\r\n\r\n123");
			ns.GetSocket ().Shutdown (SocketShutdown.Send);
			string input = Receive (ns, 512);
			ns.Close ();
			StringAssert.StartsWith ("HTTP/1.1 400", input);
		}

		[Test]
		public void Test12 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			// 0.9
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test12/");
			MyNetworkStream ns = CreateNS (port);
			Send (ns, "POST /test12/ HTTP/0.9\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
			ns.GetSocket ().Shutdown (SocketShutdown.Send);
			string input = Receive (ns, 512);
			ns.Close ();
			StringAssert.StartsWith ("HTTP/1.1 400", input);
		}

		[Test]
		public void Test13 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			// 0.9
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test13/");
			MyNetworkStream ns = CreateNS (port);
			Send (ns, "GEt /test13/ HTTP/0.9\r\nHost: 127.0.0.1\r\n\r\n");
			ns.GetSocket ().Shutdown (SocketShutdown.Send);
			string input = Receive (ns, 512);
			ns.Close ();
			StringAssert.StartsWith ("HTTP/1.1 400", input);
		}

		HttpListenerRequest test14_request;
		ManualResetEvent test_evt;
		bool test14_error;
		[Test]
		public void Test14 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test14/");
			MyNetworkStream ns = CreateNS (port);
			Send (ns, "POST /test14/ HTTP/1.0\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
			HttpListenerContext c = _listener.GetContext ();
			test14_request = c.Request;
			test_evt = new ManualResetEvent (false);
			Thread thread = new Thread (ReadToEnd);
			thread.Start ();
			if (test_evt.WaitOne (3000, false) == false) {
#if MONO_FEATURE_THREAD_ABORT
				thread.Abort ();
#else
				thread.Interrupt ();
#endif
				test_evt.Close ();
				Assert.IsTrue (false, "Timed out");
			}
			test_evt.Close ();
			c.Response.Close ();
			ns.Close ();
			Assert.AreEqual ("123", read_to_end, "Did not get the expected input.");
		}

		string read_to_end;
		void ReadToEnd ()
		{
			using (StreamReader r = new StreamReader (test14_request.InputStream)) {
				read_to_end = r.ReadToEnd ();
				test_evt.Set ();
			}
		}

		[Test]
		public void Test15 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			// 2 separate writes -> 2 packets. Body size > 8kB
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test15/");
			MyNetworkStream ns = CreateNS (port);
			Send (ns, "POST /test15/ HTTP/1.0\r\nHost: 127.0.0.1\r\nContent-Length: 8888\r\n\r\n");
			Thread.Sleep (800);
			string data = new string ('a', 8888);
			Send (ns, data);
			HttpListenerContext c = _listener.GetContext ();
			HttpListenerRequest req = c.Request;
			using (StreamReader r = new StreamReader (req.InputStream)) {
				read_to_end = r.ReadToEnd ();
			}
			Assert.AreEqual (read_to_end.Length, data.Length, "Wrong length");
			Assert.IsTrue (data == read_to_end, "Wrong data");
			c.Response.Close ();
			ns.Close ();
		}

		[Test]
		public void Test16 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			// 1 single write with headers + body (size > 8kB)
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test16/");
			MyNetworkStream ns = CreateNS (port);
			StringBuilder sb = new StringBuilder ();
			sb.Append ("POST /test16/ HTTP/1.0\r\nHost: 127.0.0.1\r\nContent-Length: 8888\r\n\r\n");
			string eights = new string ('b', 8888);
			sb.Append (eights);
			string data = sb.ToString ();
			Send (ns, data);
			HttpListenerContext c = _listener.GetContext ();
			HttpListenerRequest req = c.Request;
			using (StreamReader r = new StreamReader (req.InputStream)) {
				read_to_end = r.ReadToEnd ();
			}
			Assert.AreEqual (read_to_end.Length, read_to_end.Length, "Wrong length");
			Assert.IsTrue (eights == read_to_end, "Wrong data");
			c.Response.Close ();
			ns.Close ();
		}

		[Test]
		public void Test17 ()
		{
			var port = NetworkHelpers.FindFreePort ();
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/test17/");
			NetworkStream ns = CreateNS (port);
			Send (ns, "RANDOM /test17/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
			HttpListenerContext ctx = _listener.GetContext ();
			Send (ctx.Response.OutputStream, "%%%OK%%%");
			ctx.Response.Close ();
			string response = Receive (ns, 1024);
			ns.Close ();
			StringAssert.StartsWith ("HTTP/1.1 200", response);
			StringAssert.Contains ("Transfer-Encoding: chunked", response);
		}

		[Test]
		public void Test_MultipleClosesOnOuputStreamAllowed ()
		{
			var port = NetworkHelpers.FindFreePort ();
			_listener = CreateAndStartListener ("http://127.0.0.1:" + port + "/MultipleCloses/");
			NetworkStream ns = CreateNS (port);
			Send (ns, "GET /MultipleCloses/ HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");

			HttpListenerContext ctx = _listener.GetContext ();
			ctx.Response.OutputStream.Close ();
			ctx.Response.OutputStream.Close ();
			ctx.Response.OutputStream.Close ();
			ctx.Response.Close ();
		}
	
		void SendCookie ()
		{		
			NetworkStream ns = CreateNS (sendCookiePort);
			Send (ns, "GET /SendCookie/ HTTP/1.1\r\nHost: 127.0.0.1\r\n"+
				"Cookie:$Version=\"1\"; "+
				"Cookie1=Value1; $Path=\"/\"; "+
				"CookieM=ValueM; $Path=\"/p2\"; $Domain=\"test\"; $Port=\"99\";"+
				"Cookie2=Value2; $Path=\"/foo\";"+
				"\r\n"+
				"\r\n");
			ns.Flush ();
			Thread.Sleep (200);
			ns.Close();
		}

		[Test]
		public void ReceiveCookiesFromClient ()
		{
			sendCookiePort = NetworkHelpers.FindFreePort ();			
			_listener = CreateAndStartListener ("http://127.0.0.1:" + sendCookiePort + "/SendCookie/");
			Thread clientThread = new Thread (new ThreadStart (SendCookie));
			clientThread.Start ();

			HttpListenerContext context = _listener.GetContext();
			HttpListenerRequest request = context.Request;

			Assert.AreEqual (3, request.Cookies.Count, "#1");
			foreach (Cookie c in request.Cookies) {
				if (c.Name == "Cookie1") {
					Assert.AreEqual ("Value1", c.Value, "#2");
					Assert.AreEqual ("\"/\"", c.Path, "#3");
					Assert.AreEqual (0, c.Port.Length, "#4");
					Assert.AreEqual (0, c.Domain.Length, "#5");
				} else if (c.Name == "CookieM") {
					Assert.AreEqual ("ValueM", c.Value, "#6");
					Assert.AreEqual ("\"/p2\"", c.Path, "#7");
					Assert.AreEqual ("\"99\"", c.Port, "#8");
					Assert.AreEqual ("\"test\"", c.Domain, "#9");
				} else if (c.Name == "Cookie2") {
					Assert.AreEqual ("Value2", c.Value, "#10");
					Assert.AreEqual ("\"/foo\"", c.Path, "#11");
					Assert.AreEqual (0, c.Port.Length, "#12");
					Assert.AreEqual (0, c.Domain.Length, "#13");
				} else
					Assert.Fail ("Invalid cookie name " + c.Name);
			}
		}

		private object _lock = new Object();
		private string cookieResponse;
		private int receiveCookiePort;
		private int sendCookiePort;		

		void ReceiveCookie () {
			lock (_lock) {
				NetworkStream ns = CreateNS (receiveCookiePort);
				Send (ns, "GET /ReceiveCookie/ HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
				cookieResponse = Receive (ns, 512);
			}
		}

		[Test]
		public void SendCookiestoClient ()
		{
			receiveCookiePort = NetworkHelpers.FindFreePort ();
			_listener = CreateAndStartListener ("http://127.0.0.1:" + receiveCookiePort + "/ReceiveCookie/");
			Thread clientThread = new Thread (new ThreadStart (ReceiveCookie));
			clientThread.Start ();

			HttpListenerContext context = _listener.GetContext();
			HttpListenerRequest request = context.Request;
			HttpListenerResponse response = context.Response;

			Cookie cookie = new Cookie ();
			cookie.Name = "Name0";
			cookie.Value = "Value0";
			cookie.Domain = "blue";
			cookie.Path = "/path/";
			cookie.Port = "\"80\"";
			cookie.Version = 1;
			response.Cookies.Add (cookie);

			string responseString = "<HTML><BODY>----</BODY></HTML>";
			byte[] buffer = Encoding.UTF8.GetBytes(responseString);
			response.ContentLength64 = buffer.Length;
			Stream output = response.OutputStream;
			output.Write(buffer, 0, buffer.Length);
			output.Flush ();
			response.Close();
			
			lock (_lock) {
				bool foundCookie = false;
				foreach (String str in cookieResponse.Split ('\n')) {
					if (!str.StartsWith ("Set-Cookie"))
						continue;
					Dictionary<string, String> dic = new Dictionary<string, String>();
					foreach (String p in str.Substring (str.IndexOf (":") + 1).Split (';')) {
						String[] parts = p.Split('=');
						dic.Add (parts [0].Trim (), parts [1].Trim ());
					}
					Assert.AreEqual ("Value0", dic ["Name0"], "#1");
					Assert.AreEqual ("blue", dic ["Domain"], "#2");
					Assert.AreEqual ("\"/path/\"", dic ["Path"], "#3");
					Assert.AreEqual ("\"80\"", dic ["Port"], "#4");
					Assert.AreEqual ("1", dic ["Version"], "#5");
					foundCookie = true;
					break;
				}
				Assert.IsTrue (foundCookie, "#6");
			}
		}

		[Test]
		public void MultiResponses ()
		{
			echoServerPort = NetworkHelpers.FindFreePort ();
			Thread srv = new Thread (new ThreadStart (EchoServer));
			srv.Start ();
			Thread.Sleep (200);

			for (int i = 0; i < 10; i++) {
				string payload = string.Format (CultureInfo.InvariantCulture,
					"Client{0}", i);

				HttpWebRequest req = (HttpWebRequest) WebRequest.Create (
					"http://localhost:" + echoServerPort + "/foobar/");
				req.ServicePoint.Expect100Continue = false;
				req.ServicePoint.UseNagleAlgorithm = false;
				req.Method = "POST";
				StreamWriter w = new StreamWriter (req.GetRequestStream ());
				w.WriteLine (payload);
				w.Close ();

				HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
				StreamReader r = new StreamReader (resp.GetResponseStream ());
				Assert.AreEqual ("Hello, " + payload + "!", r.ReadToEnd ().Trim ());
				r.Close ();
			}

			manualReset.Set ();
			srv.Join ();
		}

		int echoServerPort;
		void EchoServer ()
		{
			_listener = new HttpListener ();
			_listener.Prefixes.Add ("http://*:" + echoServerPort + "/foobar/");
			_listener.Start ();

			manualReset = new ManualResetEvent (false);

			IAsyncResult result = _listener.BeginGetContext (
				new AsyncCallback (EchoCallback), _listener);
			manualReset.WaitOne ();
		}

		void EchoCallback (IAsyncResult result)
		{
			HttpListener listener = (HttpListener) result.AsyncState;
			HttpListenerContext context = listener.EndGetContext (result);
			HttpListenerRequest req = context.Request;
			StreamReader r = new StreamReader (req.InputStream);
			string reqBody = r.ReadToEnd ().Trim ();

			HttpListenerResponse resp = context.Response;
			StreamWriter o = new StreamWriter (resp.OutputStream);
			o.WriteLine ("Hello, " + reqBody + "!");
			o.Close ();

			listener.BeginGetContext (new AsyncCallback (EchoCallback), listener);
		}

		private ManualResetEvent manualReset;

	}

	[TestFixture]
	public class HttpListenerBugs {
		[Test]
		public void TestNonChunkedAsync ()
		{
			var port = NetworkHelpers.FindFreePort ();
			HttpListener listener = HttpListener2Test.CreateAndStartListener ("http://127.0.0.1:" + port + "/");

			listener.BeginGetContext (callback, listener);
			
			HttpListener2Test.MyNetworkStream ns = HttpListener2Test.CreateNS (port);
			string message = "<script>\n"+
				" <!-- register the blueprint for our show-headers service -->\n"+
				" <action verb=\"POST\" path=\"/host/register\">\n" +
				"    <blueprint>\n" +
				"      <assembly>dream.tutorial.show-headers</assembly>\n" +
				"      <class>MindTouch.Dream.Tutorial.ShowHeadersService</class>\n" +
				"    </blueprint>\n" +
				"  </action>\n" +
				"\n" +
				"  <!-- instantiate it -->\n" +
				"  <action verb=\"POST\" path=\"/host/start\">\n" +
				"    <config>\n" +
				"      <path>show-headers</path>\n" +
				"      <class>MindTouch.Dream.Tutorial.ShowHeadersService</class>\n" +
				"    </config>\n" +
				"  </action>\n" +
				"</script>";
			string s = String.Format ("POST / HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: {0}\r\n\r\n{1}",
						message.Length, message);  
			HttpListener2Test.Send (ns, s);
			bool timedout;
			string response = HttpListener2Test.ReceiveWithTimeout (ns, 1024, 3000, out timedout);
			ns.Close ();
			listener.Close ();
			Assert.IsFalse (timedout);
		}

		void callback (IAsyncResult ar)
		{
			HttpListener l = (HttpListener) ar.AsyncState;

			HttpListenerContext c = l.EndGetContext (ar);
			HttpListenerRequest request = c.Request;

			StreamReader r = new StreamReader (request.InputStream);
			string sr =r.ReadToEnd ();
			HttpListener2Test.Send (c.Response.OutputStream, "Miguel is love");
			c.Response.Close ();
		}

		//
		// As it turns out, when we closed the OutputStream,
		// we were not shutting down the connection, which was
		// a documented pattern to close the connection
		// 
		[Test]
		public void Test_MultipleConnections ()
		{
			var port = NetworkHelpers.FindFreePort ();			
			HttpListener listener = HttpListener2Test.CreateAndStartListener ("http://127.0.0.1:" + port + "/multiple/");

			// First one
			NetworkStream ns = HttpListener2Test.CreateNS (port);
			HttpListener2Test.Send (ns, "POST /multiple/ HTTP/1.0\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
			HttpListenerContext ctx = listener.GetContext ();
			HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
			ctx.Response.OutputStream.Close ();
			string response = HttpListener2Test.Receive (ns, 1024);
			ns.Close ();

			// Second one
			ns = HttpListener2Test.CreateNS (port);
			HttpListener2Test.Send (ns, "POST /multiple/ HTTP/1.0\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
			ctx = listener.GetContext ();
			HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
			ctx.Response.OutputStream.Close ();
			response = HttpListener2Test.Receive (ns, 1024);
			ns.Close ();
			
			listener.Close ();
		}

		//
		// Test case for bug 341443, an pretty old bug, filed on November of 2007.
		//
		[Test]
		public void Test_HostInUri ()
		{
			var wait = new ManualResetEvent (false);
			var wait2 = new ManualResetEvent (false);
			var port = NetworkHelpers.FindFreePort ();
			
			Thread t = new Thread (delegate (object a) {
				wait.WaitOne ();

				NetworkStream ns = HttpListener2Test.CreateNS (port);
				HttpListener2Test.Send (ns, "GET http://www.google.com/ HTTP/1.1\r\nHost: www.google.com\r\nContent-Length: 3\r\n\r\n123456");

				wait2.WaitOne ();
				ns.Close ();
			});
			t.Start ();
				
			HttpListener listener = HttpListener2Test.CreateAndStartListener ("http://*:" + port + "/");
			wait.Set ();
			HttpListenerContext ctx = listener.GetContext ();
			
			Assert.AreEqual ("http://www.google.com:" + port + "/", ctx.Request.Url.ToString ());
			Assert.AreEqual ("http://www.google.com/", ctx.Request.RawUrl);
			wait2.Set ();

			listener.Close ();
		}

		[Test] // bug #513849
		public void ClosePort ()
		{
			var port = NetworkHelpers.FindFreePort ();
			var h = new HttpListener ();
			h.Prefixes.Add ("http://127.0.0.1:" + port + "/");
			h.Start ();
			h.BeginGetContext (null, null);
			h.Stop ();
			TcpListener t = new TcpListener (IPAddress.Parse ("127.0.0.1"), port);
			t.Start ();
			t.Stop ();
		}

		//
		// Bugs: #17204, #10818
		//
		// Sadly, on Unix, if there are different calls to bind
		// like *:port and host:port that is not an error,
		// it would only be an error if host:port is done twice, so
		// the best we can hope for is that listening on a specific interface
		// does not also listen to another interface.
		//
		[Test]
		public void BindToSingleInterface ()
		{
			IPAddress [] machineAddress = null;

			try {
				machineAddress = Dns.GetHostAddresses (Dns.GetHostName ());
			} catch (SocketException){
				// The build hosts sometimes can not resolve the hostname
				Assert.Ignore ("Hostname couldn't be resolved.");
			}
			
			int port = NetworkHelpers.FindFreePort ();;
			var h = new HttpListener ();
			h.Prefixes.Add ("http://" + machineAddress [0] + ":" + port + "/");
			h.Start ();

			try {
				var c = new TcpClient ("localhost", port);
				Assert.Fail ("The TcpClient should have failed to connect since HttpListener is not listening on localhost");
			} catch (SocketException){
				// Pass
			}
			h.Stop ();
		}

		[Test]
		public void BindToAllInterfaces ()
		{
			var h = new HttpListener ();
			int port = NetworkHelpers.FindFreePort ();
			h.Prefixes.Add ("http://*:" + port + "/");
			h.Start ();
			var c = new TcpClient ("localhost", port);
			h.Stop ();
		}

		// Test case for bug #31209
		[Test]
		public void Test_EmptyLineAtStart ()
		{
			var port = NetworkHelpers.FindFreePort ();
			var listener = HttpListener2Test.CreateAndStartListener ("http://127.0.0.1:" + port + "/");
			var ns = HttpListener2Test.CreateNS (port);

			HttpListener2Test.Send (ns, "\r\nGET / HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");

			bool timedout;
			HttpListener2Test.GetContextWithTimeout (listener, 1000, out timedout);

			Assert.IsFalse (timedout, "timed out");

			ns.Close ();
			listener.Close ();
		}
	}
}