HttpWebRequestTest.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. //
  2. // HttpWebRequestTest.cs - NUnit Test Cases for System.Net.HttpWebRequest
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Martin Willemoes Hansen ([email protected])
  7. // Gonzalo Paniagua Javier ([email protected])
  8. //
  9. // (C) 2003 Martin Willemoes Hansen
  10. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com
  11. //
  12. using NUnit.Framework;
  13. using System;
  14. using System.Collections;
  15. using System.Collections.Specialized;
  16. using System.Globalization;
  17. using System.IO;
  18. using System.Net;
  19. using System.Net.Sockets;
  20. using System.Security.Cryptography;
  21. using System.Security.Cryptography.X509Certificates;
  22. using System.Text;
  23. using System.Threading;
  24. #if !TARGET_JVM
  25. using Mono.Security.Authenticode;
  26. using Mono.Security.Protocol.Tls;
  27. #endif
  28. namespace MonoTests.System.Net
  29. {
  30. [TestFixture]
  31. public class HttpWebRequestTest
  32. {
  33. [Test]
  34. #if TARGET_JVM
  35. [Ignore ("Ignore failures in Sys.Net")]
  36. #endif
  37. public void Proxy_Null ()
  38. {
  39. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  40. Assert.IsNotNull (req.Proxy, "#1");
  41. #if NET_2_0
  42. req.Proxy = null;
  43. Assert.IsNull (req.Proxy, "#2");
  44. #else
  45. try {
  46. req.Proxy = null;
  47. Assert.Fail ("#2");
  48. } catch (ArgumentNullException ex) {
  49. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#3");
  50. Assert.IsNull (ex.InnerException, "#4");
  51. Assert.IsNotNull (ex.Message, "#5");
  52. Assert.IsNotNull (ex.ParamName, "#6");
  53. Assert.AreEqual ("value", ex.ParamName, "#7");
  54. }
  55. #endif
  56. }
  57. [Test]
  58. [Category("InetAccess")]
  59. #if TARGET_JVM
  60. [Ignore ("NMA - wrong cookies number returned")]
  61. #endif
  62. public void Sync ()
  63. {
  64. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  65. Assertion.AssertNotNull ("req:If Modified Since: ", req.IfModifiedSince);
  66. req.UserAgent = "MonoClient v1.0";
  67. Assert.AreEqual ("User-Agent", req.Headers.GetKey (0), "#A1");
  68. Assert.AreEqual ("MonoClient v1.0", req.Headers.Get (0), "#A2");
  69. HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
  70. Assert.AreEqual ("OK", res.StatusCode.ToString (), "#B1");
  71. Assert.AreEqual ("OK", res.StatusDescription, "#B2");
  72. Assert.AreEqual ("text/html; charset=ISO-8859-1", res.Headers.Get ("Content-Type"), "#C1");
  73. Assert.IsNotNull (res.LastModified, "#C2");
  74. Assert.AreEqual (0, res.Cookies.Count, "#C3");
  75. res.Close ();
  76. }
  77. [Test]
  78. public void AddRange ()
  79. {
  80. HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");
  81. req.AddRange (10);
  82. req.AddRange (50, 90);
  83. req.AddRange ("bytes", 100);
  84. req.AddRange ("bytes", 100, 120);
  85. Assertion.AssertEquals ("#1", "bytes=10-,50-90,100-,100-120", req.Headers ["Range"]);
  86. try {
  87. req.AddRange ("bits", 2000);
  88. Assertion.Fail ("#2");
  89. } catch (InvalidOperationException) {}
  90. }
  91. [Test]
  92. [Category("InetAccess")]
  93. public void Cookies1 ()
  94. {
  95. // The purpose of this test is to ensure that the cookies we get from a request
  96. // are stored in both, the CookieCollection in HttpWebResponse and the CookieContainer
  97. // in HttpWebRequest.
  98. // If this URL stops sending *one* and only one cookie, replace it.
  99. string url = "http://www.elmundo.es";
  100. CookieContainer cookies = new CookieContainer ();
  101. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  102. req.KeepAlive = false;
  103. req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv; 1.7.6) Gecko/20050317 Firefox/1.0.2";
  104. req.CookieContainer = cookies;
  105. Assertion.AssertEquals ("#01", 0, cookies.Count);
  106. using (HttpWebResponse res = (HttpWebResponse) req.GetResponse()) {
  107. CookieCollection coll = req.CookieContainer.GetCookies (new Uri (url));
  108. Assertion.AssertEquals ("#02", 1, coll.Count);
  109. Assertion.AssertEquals ("#03", 1, res.Cookies.Count);
  110. Cookie one = coll [0];
  111. Cookie two = res.Cookies [0];
  112. Assertion.AssertEquals ("#04", true, object.ReferenceEquals (one, two));
  113. }
  114. }
  115. #if !TARGET_JVM //NotWorking
  116. [Test]
  117. public void SslClientBlock ()
  118. {
  119. // This tests that the write request/initread/write body sequence does not hang
  120. // when using SSL.
  121. // If there's a regression for this, the test will hang.
  122. ServicePointManager.CertificatePolicy = new AcceptAllPolicy ();
  123. try {
  124. SslHttpServer server = new SslHttpServer ();
  125. server.Start ();
  126. string url = String.Format ("https://{0}:{1}/nothing.html", server.IPAddress, server.Port);
  127. HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
  128. request.Method = "POST";
  129. Stream stream = request.GetRequestStream ();
  130. byte [] bytes = new byte [100];
  131. stream.Write (bytes, 0, bytes.Length);
  132. stream.Close ();
  133. HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
  134. Assertion.AssertEquals ("StatusCode", 200, (int) resp.StatusCode);
  135. StreamReader sr = new StreamReader (resp.GetResponseStream (), Encoding.UTF8);
  136. string x = sr.ReadToEnd ();
  137. sr.Close ();
  138. resp.Close ();
  139. server.Stop ();
  140. if (server.Error != null)
  141. throw server.Error;
  142. } finally {
  143. ServicePointManager.CertificatePolicy = null;
  144. }
  145. }
  146. #endif
  147. [Test]
  148. #if TARGET_JVM
  149. [Category("NotWorking")]
  150. #endif
  151. public void Missing_ContentEncoding ()
  152. {
  153. ServicePointManager.CertificatePolicy = new AcceptAllPolicy ();
  154. try {
  155. BadChunkedServer server = new BadChunkedServer ();
  156. server.Start ();
  157. string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
  158. HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
  159. request.Method = "GET";
  160. HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
  161. Assert.AreEqual ("", resp.ContentEncoding);
  162. resp.Close ();
  163. server.Stop ();
  164. if (server.Error != null)
  165. throw server.Error;
  166. } finally {
  167. ServicePointManager.CertificatePolicy = null;
  168. }
  169. }
  170. [Test]
  171. #if TARGET_JVM
  172. [Category ("NotWorking")]
  173. #endif
  174. public void BadServer_ChunkedClose ()
  175. {
  176. // The server will send a chunked response without a 'last-chunked' mark
  177. // and then shutdown the socket for sending.
  178. BadChunkedServer server = new BadChunkedServer ();
  179. server.Start ();
  180. string url = String.Format ("http://{0}:{1}/nothing.html", server.IPAddress, server.Port);
  181. HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);
  182. HttpWebResponse resp = (HttpWebResponse) request.GetResponse ();
  183. string x = null;
  184. try {
  185. byte [] bytes = new byte [32];
  186. // Using StreamReader+UTF8Encoding here fails on MS runtime
  187. Stream stream = resp.GetResponseStream ();
  188. int nread = stream.Read (bytes, 0, 32);
  189. Assertion.AssertEquals ("#01", 16, nread);
  190. x = Encoding.ASCII.GetString (bytes, 0, 16);
  191. } finally {
  192. resp.Close ();
  193. server.Stop ();
  194. }
  195. if (server.Error != null)
  196. throw server.Error;
  197. Assertion.AssertEquals ("1234567890123456", x);
  198. }
  199. [Test]
  200. [Ignore ("This test asserts that our code violates RFC 2616")]
  201. public void MethodCase ()
  202. {
  203. ListDictionary methods = new ListDictionary ();
  204. #if NET_2_0
  205. methods.Add ("post", "POST");
  206. methods.Add ("puT", "PUT");
  207. #else
  208. methods.Add ("post", "post");
  209. methods.Add ("puT", "puT");
  210. #endif
  211. methods.Add ("POST", "POST");
  212. methods.Add ("whatever", "whatever");
  213. methods.Add ("PUT", "PUT");
  214. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8000);
  215. string url = "http://" + IPAddress.Loopback.ToString () + ":8000/test/";
  216. foreach (DictionaryEntry de in methods) {
  217. SocketResponder responder = new SocketResponder (new IPEndPoint (IPAddress.Loopback, 8000),
  218. new SocketRequestHandler (EchoRequestHandler));
  219. responder.Start ();
  220. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  221. req.Method = (string) de.Key;
  222. req.Timeout = 2000;
  223. req.ReadWriteTimeout = 2000;
  224. req.KeepAlive = false;
  225. Stream rs = req.GetRequestStream ();
  226. rs.Close ();
  227. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  228. StreamReader sr = new StreamReader (resp.GetResponseStream (),
  229. Encoding.UTF8);
  230. string line = sr.ReadLine ();
  231. sr.Close ();
  232. Assert.AreEqual (((string) de.Value) + " /test/ HTTP/1.1",
  233. line, req.Method);
  234. resp.Close ();
  235. }
  236. responder.Stop ();
  237. }
  238. }
  239. [Test]
  240. [Ignore ("This test asserts that our code violates RFC 2616")]
  241. public void GetRequestStream_Body_NotAllowed ()
  242. {
  243. string [] methods = new string [] { "GET", "HEAD", "CONNECT",
  244. "get", "HeAd", "ConNect" };
  245. foreach (string method in methods) {
  246. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (
  247. "http://localhost:8000");
  248. req.Method = method;
  249. try {
  250. req.GetRequestStream ();
  251. Assert.Fail ("#1:" + method);
  252. } catch (ProtocolViolationException ex) {
  253. Assert.AreEqual (typeof (ProtocolViolationException), ex.GetType (), "#2:" + method);
  254. Assert.IsNull (ex.InnerException, "#3:" + method);
  255. Assert.IsNotNull (ex.Message, "#4:" + method);
  256. }
  257. }
  258. }
  259. [Test]
  260. public void ReadTimeout ()
  261. {
  262. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  263. string url = "http://" + localEP.ToString () + "/original/";
  264. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  265. responder.Start ();
  266. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  267. req.Method = "POST";
  268. req.AllowAutoRedirect = false;
  269. req.Timeout = 200;
  270. req.ReadWriteTimeout = 100;
  271. req.KeepAlive = false;
  272. Stream rs = req.GetRequestStream ();
  273. rs.Close ();
  274. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  275. try {
  276. Stream s = resp.GetResponseStream ();
  277. s.ReadByte ();
  278. Assert.Fail ("#1");
  279. } catch (WebException ex) {
  280. Assert.AreEqual (typeof (WebException), ex.GetType (), "#2");
  281. Assert.IsNull (ex.InnerException, "#3");
  282. Assert.IsNull (ex.Response, "#4");
  283. Assert.AreEqual (WebExceptionStatus.Timeout, ex.Status, "#5");
  284. }
  285. }
  286. responder.Stop ();
  287. }
  288. }
  289. [Test] // bug #81624
  290. public void AllowAutoRedirect ()
  291. {
  292. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  293. string url = "http://" + localEP.ToString () + "/original/";
  294. // allow autoredirect
  295. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  296. responder.Start ();
  297. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  298. req.Method = "POST";
  299. req.Timeout = 2000;
  300. req.ReadWriteTimeout = 2000;
  301. req.KeepAlive = false;
  302. Stream rs = req.GetRequestStream ();
  303. rs.Close ();
  304. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  305. StreamReader sr = new StreamReader (resp.GetResponseStream (),
  306. Encoding.UTF8);
  307. string body = sr.ReadToEnd ();
  308. Assert.AreEqual (resp.StatusCode, HttpStatusCode.OK, "#A1");
  309. Assert.AreEqual (resp.ResponseUri.ToString (), "http://" +
  310. localEP.ToString () + "/moved/", "#A2");
  311. Assert.AreEqual ("GET", resp.Method, "#A3");
  312. Assert.AreEqual ("LOOKS OK", body, "#A4");
  313. }
  314. responder.Stop ();
  315. }
  316. // do not allow autoredirect
  317. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  318. responder.Start ();
  319. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  320. req.Method = "POST";
  321. req.AllowAutoRedirect = false;
  322. req.Timeout = 1000;
  323. req.ReadWriteTimeout = 1000;
  324. req.KeepAlive = false;
  325. Stream rs = req.GetRequestStream ();
  326. rs.Close ();
  327. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  328. Assert.AreEqual (resp.StatusCode, HttpStatusCode.Found, "#B1");
  329. Assert.AreEqual (url, resp.ResponseUri.ToString (), "#B2");
  330. Assert.AreEqual ("POST", resp.Method, "#B3");
  331. }
  332. responder.Stop ();
  333. }
  334. }
  335. [Test] // bug #81671
  336. [Category ("NotWorking")]
  337. public void InternalServerError ()
  338. {
  339. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  340. string url = "http://" + localEP.ToString () + "/original/";
  341. // POST
  342. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (InternalErrorHandler))) {
  343. responder.Start ();
  344. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  345. req.Method = "POST";
  346. req.Timeout = 2000;
  347. req.ReadWriteTimeout = 2000;
  348. req.KeepAlive = false;
  349. Stream rs = req.GetRequestStream ();
  350. rs.Close ();
  351. try {
  352. req.GetResponse ();
  353. Assert.Fail ("#A1");
  354. } catch (WebException ex) {
  355. Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
  356. Assert.IsNull (ex.InnerException, "#A3");
  357. Assert.IsNotNull (ex.Message, "#A4");
  358. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#A5");
  359. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  360. Assert.IsNotNull (webResponse, "#A6");
  361. Assert.AreEqual ("POST", webResponse.Method, "#A7");
  362. webResponse.Close ();
  363. }
  364. responder.Stop ();
  365. }
  366. // GET
  367. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (InternalErrorHandler))) {
  368. responder.Start ();
  369. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  370. req.Method = "GET";
  371. req.Timeout = 2000;
  372. req.ReadWriteTimeout = 2000;
  373. req.KeepAlive = false;
  374. try {
  375. req.GetResponse ();
  376. Assert.Fail ("#B1");
  377. } catch (WebException ex) {
  378. Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
  379. Assert.IsNull (ex.InnerException, "#B3");
  380. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
  381. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  382. Assert.IsNotNull (webResponse, "#B5");
  383. Assert.AreEqual ("GET", webResponse.Method, "#B6");
  384. webResponse.Close ();
  385. }
  386. responder.Stop ();
  387. }
  388. }
  389. [Test]
  390. [Category ("NotWorking")] // we report a timeout
  391. public void NoContentLength ()
  392. {
  393. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  394. string url = "http://" + localEP.ToString () + "/original/";
  395. // POST
  396. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (NoContentLengthHandler))) {
  397. responder.Start ();
  398. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  399. req.Method = "POST";
  400. req.Timeout = 2000;
  401. req.ReadWriteTimeout = 2000;
  402. req.KeepAlive = false;
  403. Stream rs = req.GetRequestStream ();
  404. rs.Close ();
  405. try {
  406. req.GetResponse ();
  407. Assert.Fail ("#A1");
  408. } catch (WebException ex) {
  409. Assert.AreEqual (typeof (WebException), ex.GetType (), "#A2");
  410. #if NET_2_0
  411. //Assert.IsNotNull (ex.InnerException, "#A3");
  412. Assert.AreEqual (WebExceptionStatus.ReceiveFailure, ex.Status, "#A4");
  413. Assert.AreEqual (typeof (IOException), ex.InnerException.GetType (), "#A5");
  414. // Unable to read data from the transport connection:
  415. // A connection attempt failed because the connected party
  416. // did not properly respond after a period of time, or
  417. // established connection failed because connected host has
  418. // failed to respond
  419. IOException ioe = (IOException) ex.InnerException;
  420. Assert.IsNotNull (ioe.InnerException, "#A6");
  421. Assert.IsNotNull (ioe.Message, "#A7");
  422. Assert.AreEqual (typeof (SocketException), ioe.InnerException.GetType (), "#A8");
  423. // A connection attempt failed because the connected party
  424. // did not properly respond after a period of time, or
  425. // established connection failed because connected host has
  426. // failed to respond
  427. SocketException soe = (SocketException) ioe.InnerException;
  428. Assert.IsNull (soe.InnerException, "#A9");
  429. Assert.IsNotNull (soe.Message, "#A10");
  430. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  431. Assert.IsNull (webResponse, "#A11");
  432. #else
  433. Assert.IsNull (ex.InnerException, "#A3");
  434. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#A4");
  435. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  436. Assert.IsNotNull (webResponse, "#A5");
  437. Assert.AreEqual ("POST", webResponse.Method, "#A6");
  438. webResponse.Close ();
  439. #endif
  440. }
  441. responder.Stop ();
  442. }
  443. // GET
  444. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (NoContentLengthHandler))) {
  445. responder.Start ();
  446. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  447. req.Method = "GET";
  448. req.Timeout = 2000;
  449. req.ReadWriteTimeout = 2000;
  450. req.KeepAlive = false;
  451. try {
  452. req.GetResponse ();
  453. Assert.Fail ("#B1");
  454. } catch (WebException ex) {
  455. Assert.AreEqual (typeof (WebException), ex.GetType (), "#B2");
  456. Assert.IsNull (ex.InnerException, "#B3");
  457. Assert.AreEqual (WebExceptionStatus.ProtocolError, ex.Status, "#B4");
  458. HttpWebResponse webResponse = ex.Response as HttpWebResponse;
  459. Assert.IsNotNull (webResponse, "#B5");
  460. Assert.AreEqual ("GET", webResponse.Method, "#B6");
  461. webResponse.Close ();
  462. }
  463. responder.Stop ();
  464. }
  465. }
  466. #if NET_2_0
  467. [Test] // bug #81504
  468. public void Stream_CanTimeout ()
  469. {
  470. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, 8764);
  471. string url = "http://" + localEP.ToString () + "/original/";
  472. // allow autoredirect
  473. using (SocketResponder responder = new SocketResponder (localEP, new SocketRequestHandler (RedirectRequestHandler))) {
  474. responder.Start ();
  475. HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
  476. req.Method = "POST";
  477. req.Timeout = 2000;
  478. req.ReadWriteTimeout = 2000;
  479. req.KeepAlive = false;
  480. Stream rs = req.GetRequestStream ();
  481. Assert.IsTrue (rs.CanTimeout, "#1");
  482. rs.Close ();
  483. using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse ()) {
  484. Stream os = resp.GetResponseStream ();
  485. Assert.IsTrue (os.CanTimeout, "#2");
  486. os.Close ();
  487. }
  488. responder.Stop ();
  489. }
  490. }
  491. #endif
  492. static byte [] EchoRequestHandler (Socket socket)
  493. {
  494. MemoryStream ms = new MemoryStream ();
  495. byte [] buffer = new byte [4096];
  496. int bytesReceived = socket.Receive (buffer);
  497. while (bytesReceived > 0) {
  498. ms.Write (buffer, 0, bytesReceived);
  499. if (socket.Available > 0) {
  500. bytesReceived = socket.Receive (buffer);
  501. } else {
  502. bytesReceived = 0;
  503. }
  504. }
  505. ms.Flush ();
  506. ms.Position = 0;
  507. StreamReader sr = new StreamReader (ms, Encoding.UTF8);
  508. string request = sr.ReadToEnd ();
  509. StringWriter sw = new StringWriter ();
  510. sw.WriteLine ("HTTP/1.1 200 OK");
  511. sw.WriteLine ("Content-Type: text/xml");
  512. sw.WriteLine ("Content-Length: " + request.Length.ToString (CultureInfo.InvariantCulture));
  513. sw.WriteLine ();
  514. sw.Write (request);
  515. sw.Flush ();
  516. return Encoding.UTF8.GetBytes (sw.ToString ());
  517. }
  518. static byte [] RedirectRequestHandler (Socket socket)
  519. {
  520. MemoryStream ms = new MemoryStream ();
  521. byte [] buffer = new byte [4096];
  522. int bytesReceived = socket.Receive (buffer);
  523. while (bytesReceived > 0) {
  524. ms.Write (buffer, 0, bytesReceived);
  525. if (socket.Available > 0) {
  526. bytesReceived = socket.Receive (buffer);
  527. } else {
  528. bytesReceived = 0;
  529. }
  530. }
  531. ms.Flush ();
  532. ms.Position = 0;
  533. string statusLine = null;
  534. using (StreamReader sr = new StreamReader (ms, Encoding.UTF8)) {
  535. statusLine = sr.ReadLine ();
  536. }
  537. StringWriter sw = new StringWriter ();
  538. if (statusLine.StartsWith ("POST /original/")) {
  539. sw.WriteLine ("HTTP/1.1 302 Found");
  540. sw.WriteLine ("Location: " + "http://" + IPAddress.Loopback.ToString () + ":8764/moved/");
  541. sw.WriteLine ();
  542. sw.Flush ();
  543. } else if (statusLine.StartsWith ("GET /moved/")) {
  544. sw.WriteLine ("HTTP/1.1 200 OK");
  545. sw.WriteLine ("Content-Type: text/plain");
  546. sw.WriteLine ("Content-Length: 8");
  547. sw.WriteLine ();
  548. sw.Write ("LOOKS OK");
  549. sw.Flush ();
  550. } else {
  551. sw.WriteLine ("HTTP/1.1 500 Too Lazy");
  552. sw.WriteLine ();
  553. sw.Flush ();
  554. }
  555. return Encoding.UTF8.GetBytes (sw.ToString ());
  556. }
  557. static byte [] InternalErrorHandler (Socket socket)
  558. {
  559. StringWriter sw = new StringWriter ();
  560. sw.WriteLine ("HTTP/1.1 500 Too Lazy");
  561. sw.WriteLine ("Content-Length: 0");
  562. sw.WriteLine ();
  563. sw.Flush ();
  564. return Encoding.UTF8.GetBytes (sw.ToString ());
  565. }
  566. static byte [] NoContentLengthHandler (Socket socket)
  567. {
  568. StringWriter sw = new StringWriter ();
  569. sw.WriteLine ("HTTP/1.1 500 Too Lazy");
  570. sw.WriteLine ();
  571. sw.Flush ();
  572. return Encoding.UTF8.GetBytes (sw.ToString ());
  573. }
  574. class BadChunkedServer : HttpServer {
  575. protected override void Run ()
  576. {
  577. Socket client = sock.Accept ();
  578. NetworkStream ns = new NetworkStream (client, true);
  579. StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
  580. writer.Write ( "HTTP/1.1 200 OK\r\n" +
  581. "Transfer-Encoding: chunked\r\n" +
  582. "Connection: close\r\n" +
  583. "Content-Type: text/plain; charset=UTF-8\r\n\r\n");
  584. // This body lacks a 'last-chunk' (see RFC 2616)
  585. writer.Write ("10\r\n1234567890123456\r\n");
  586. writer.Flush ();
  587. client.Shutdown (SocketShutdown.Send);
  588. Thread.Sleep (1000);
  589. writer.Close ();
  590. }
  591. }
  592. class AcceptAllPolicy : ICertificatePolicy {
  593. public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
  594. {
  595. return true;
  596. }
  597. }
  598. abstract class HttpServer
  599. {
  600. protected Socket sock;
  601. protected Exception error;
  602. protected ManualResetEvent evt;
  603. public HttpServer ()
  604. {
  605. sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  606. sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  607. sock.Listen (1);
  608. }
  609. public void Start ()
  610. {
  611. evt = new ManualResetEvent (false);
  612. Thread th = new Thread (new ThreadStart (Run));
  613. th.Start ();
  614. }
  615. public void Stop ()
  616. {
  617. evt.Set ();
  618. sock.Close ();
  619. }
  620. public IPAddress IPAddress {
  621. get { return ((IPEndPoint) sock.LocalEndPoint).Address; }
  622. }
  623. public int Port {
  624. get { return ((IPEndPoint) sock.LocalEndPoint).Port; }
  625. }
  626. public Exception Error {
  627. get { return error; }
  628. }
  629. protected abstract void Run ();
  630. }
  631. #if !TARGET_JVM
  632. class SslHttpServer : HttpServer {
  633. X509Certificate _certificate;
  634. protected override void Run ()
  635. {
  636. try {
  637. Socket client = sock.Accept ();
  638. NetworkStream ns = new NetworkStream (client, true);
  639. SslServerStream s = new SslServerStream (ns, Certificate, false, false);
  640. s.PrivateKeyCertSelectionDelegate += new PrivateKeySelectionCallback (GetPrivateKey);
  641. StreamReader reader = new StreamReader (s);
  642. StreamWriter writer = new StreamWriter (s, Encoding.ASCII);
  643. string line;
  644. string hello = "<html><body><h1>Hello World!</h1></body></html>";
  645. string answer = "HTTP/1.0 200\r\n" +
  646. "Connection: close\r\n" +
  647. "Content-Type: text/html\r\n" +
  648. "Content-Encoding: " + Encoding.ASCII.WebName + "\r\n" +
  649. "Content-Length: " + hello.Length + "\r\n" +
  650. "\r\n" + hello;
  651. // Read the headers
  652. do {
  653. line = reader.ReadLine ();
  654. } while (line != "" && line != null && line.Length > 0);
  655. // Now the content. We know it's 100 bytes.
  656. // This makes BeginRead in sslclientstream block.
  657. char [] cs = new char [100];
  658. reader.Read (cs, 0, 100);
  659. writer.Write (answer);
  660. writer.Flush ();
  661. evt.WaitOne (50000, false);
  662. } catch (Exception e) {
  663. error = e;
  664. }
  665. }
  666. X509Certificate Certificate {
  667. get {
  668. if (_certificate == null)
  669. _certificate = new X509Certificate (CertData.Certificate);
  670. return _certificate;
  671. }
  672. }
  673. AsymmetricAlgorithm GetPrivateKey (X509Certificate certificate, string targetHost)
  674. {
  675. PrivateKey key = new PrivateKey (CertData.PrivateKey, null);
  676. return key.RSA;
  677. }
  678. }
  679. class CertData {
  680. public readonly static byte [] Certificate = {
  681. 48, 130, 1, 191, 48, 130, 1, 40, 160, 3, 2, 1, 2, 2, 16, 36,
  682. 14, 97, 190, 146, 132, 208, 71, 175, 6, 87, 168, 185, 175, 55, 43, 48,
  683. 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 48, 18,
  684. 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103, 111, 110, 122, 97,
  685. 108, 111, 48, 30, 23, 13, 48, 53, 48, 54, 50, 50, 49, 57, 51, 48,
  686. 52, 54, 90, 23, 13, 51, 57, 49, 50, 51, 49, 50, 51, 53, 57, 53,
  687. 57, 90, 48, 18, 49, 16, 48, 14, 6, 3, 85, 4, 3, 19, 7, 103,
  688. 111, 110, 122, 97, 108, 111, 48, 129, 158, 48, 13, 6, 9, 42, 134, 72,
  689. 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 140, 0, 48, 129, 136, 2,
  690. 129, 129, 0, 138, 9, 38, 25, 166, 252, 59, 26, 39, 184, 128, 216, 38,
  691. 73, 41, 86, 30, 228, 160, 205, 41, 135, 115, 223, 44, 62, 42, 198, 178,
  692. 190, 81, 11, 25, 21, 216, 49, 179, 130, 246, 52, 97, 175, 212, 94, 157,
  693. 231, 162, 66, 161, 103, 63, 204, 83, 141, 172, 119, 97, 225, 206, 98, 101,
  694. 210, 106, 2, 206, 81, 90, 173, 47, 41, 199, 209, 241, 177, 177, 96, 207,
  695. 254, 220, 190, 66, 180, 153, 0, 209, 14, 178, 69, 194, 3, 37, 116, 239,
  696. 49, 23, 185, 245, 255, 126, 35, 85, 246, 56, 244, 107, 117, 24, 14, 57,
  697. 9, 111, 147, 189, 220, 142, 57, 104, 153, 193, 205, 19, 14, 22, 157, 16,
  698. 24, 80, 201, 2, 2, 0, 17, 163, 23, 48, 21, 48, 19, 6, 3, 85,
  699. 29, 37, 4, 12, 48, 10, 6, 8, 43, 6, 1, 5, 5, 7, 3, 1,
  700. 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 4, 5, 0, 3,
  701. 129, 129, 0, 64, 49, 57, 253, 218, 198, 229, 51, 189, 12, 154, 225, 183,
  702. 160, 147, 90, 113, 172, 69, 122, 28, 77, 97, 215, 231, 194, 150, 29, 196,
  703. 65, 95, 218, 99, 142, 111, 79, 205, 109, 76, 32, 92, 220, 76, 88, 53,
  704. 237, 80, 11, 85, 44, 91, 21, 210, 12, 34, 223, 234, 18, 187, 136, 62,
  705. 26, 240, 103, 180, 12, 226, 221, 250, 247, 129, 51, 23, 129, 165, 56, 67,
  706. 43, 83, 244, 110, 207, 24, 253, 195, 16, 46, 80, 113, 80, 18, 2, 254,
  707. 120, 147, 151, 164, 23, 210, 230, 100, 19, 197, 179, 28, 194, 48, 106, 159,
  708. 155, 144, 37, 82, 44, 160, 40, 52, 146, 174, 77, 188, 160, 230, 75, 172,
  709. 123, 3, 254,
  710. };
  711. public readonly static byte [] PrivateKey = {
  712. 30, 241, 181, 176, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
  713. 0, 0, 0, 0, 84, 2, 0, 0, 7, 2, 0, 0, 0, 36, 0, 0,
  714. 82, 83, 65, 50, 0, 4, 0, 0, 17, 0, 0, 0, 201, 80, 24, 16,
  715. 157, 22, 14, 19, 205, 193, 153, 104, 57, 142, 220, 189, 147, 111, 9, 57,
  716. 14, 24, 117, 107, 244, 56, 246, 85, 35, 126, 255, 245, 185, 23, 49, 239,
  717. 116, 37, 3, 194, 69, 178, 14, 209, 0, 153, 180, 66, 190, 220, 254, 207,
  718. 96, 177, 177, 241, 209, 199, 41, 47, 173, 90, 81, 206, 2, 106, 210, 101,
  719. 98, 206, 225, 97, 119, 172, 141, 83, 204, 63, 103, 161, 66, 162, 231, 157,
  720. 94, 212, 175, 97, 52, 246, 130, 179, 49, 216, 21, 25, 11, 81, 190, 178,
  721. 198, 42, 62, 44, 223, 115, 135, 41, 205, 160, 228, 30, 86, 41, 73, 38,
  722. 216, 128, 184, 39, 26, 59, 252, 166, 25, 38, 9, 138, 175, 88, 190, 223,
  723. 27, 24, 224, 123, 190, 69, 164, 234, 129, 59, 108, 229, 248, 62, 187, 15,
  724. 235, 147, 162, 83, 47, 123, 170, 190, 224, 31, 215, 110, 143, 31, 227, 216,
  725. 85, 88, 154, 83, 207, 229, 41, 28, 237, 116, 181, 17, 37, 141, 224, 185,
  726. 164, 144, 141, 233, 164, 138, 177, 241, 115, 181, 230, 150, 7, 92, 139, 141,
  727. 113, 95, 57, 191, 211, 165, 217, 250, 197, 68, 164, 184, 168, 43, 48, 65,
  728. 177, 237, 173, 144, 148, 221, 62, 189, 147, 63, 216, 188, 206, 103, 226, 171,
  729. 32, 20, 230, 116, 144, 192, 1, 39, 202, 87, 74, 250, 6, 142, 188, 23,
  730. 45, 4, 112, 191, 253, 67, 69, 70, 128, 143, 44, 234, 41, 96, 195, 82,
  731. 202, 35, 158, 149, 240, 151, 23, 25, 166, 179, 85, 144, 58, 120, 149, 229,
  732. 205, 34, 8, 110, 86, 119, 130, 210, 37, 173, 65, 71, 169, 67, 8, 51,
  733. 20, 96, 51, 155, 3, 39, 85, 187, 40, 193, 57, 19, 99, 78, 173, 28,
  734. 129, 154, 108, 175, 8, 138, 237, 71, 27, 148, 129, 35, 47, 57, 101, 237,
  735. 168, 178, 227, 221, 212, 63, 124, 254, 253, 215, 183, 159, 49, 103, 74, 49,
  736. 67, 160, 171, 72, 194, 215, 108, 251, 178, 18, 184, 100, 211, 105, 21, 186,
  737. 39, 66, 218, 154, 72, 222, 90, 237, 179, 251, 51, 224, 212, 56, 251, 6,
  738. 209, 151, 198, 176, 89, 110, 35, 141, 248, 237, 223, 68, 135, 206, 207, 169,
  739. 254, 219, 243, 130, 71, 11, 94, 113, 233, 92, 63, 156, 169, 72, 215, 110,
  740. 95, 94, 191, 50, 59, 89, 187, 59, 183, 99, 161, 146, 233, 245, 219, 80,
  741. 87, 113, 251, 50, 144, 195, 158, 46, 189, 232, 119, 91, 75, 22, 6, 176,
  742. 39, 206, 25, 196, 213, 195, 219, 24, 28, 103, 104, 36, 137, 128, 4, 119,
  743. 163, 40, 126, 87, 18, 86, 128, 243, 213, 101, 2, 237, 78, 64, 160, 55,
  744. 199, 93, 90, 126, 175, 199, 55, 89, 234, 190, 5, 16, 196, 88, 28, 208,
  745. 28, 92, 32, 115, 204, 9, 202, 101, 15, 123, 43, 75, 90, 144, 95, 179,
  746. 102, 249, 57, 150, 204, 99, 147, 203, 16, 63, 81, 244, 226, 237, 82, 204,
  747. 20, 200, 140, 65, 83, 217, 161, 23, 123, 37, 115, 12, 100, 73, 70, 190,
  748. 32, 235, 174, 140, 148, 157, 47, 238, 40, 208, 228, 80, 54, 187, 156, 252,
  749. 253, 230, 231, 156, 138, 125, 96, 79, 3, 27, 143, 55, 146, 169, 165, 61,
  750. 238, 60, 227, 77, 217, 93, 117, 122, 111, 46, 173, 113,
  751. };
  752. }
  753. #endif
  754. }
  755. }