WebConnection.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. //
  2. // System.Net.WebConnection
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.IO;
  30. using System.Collections;
  31. using System.Net.Sockets;
  32. using System.Reflection;
  33. using System.Security.Cryptography.X509Certificates;
  34. using System.Text;
  35. using System.Threading;
  36. namespace System.Net
  37. {
  38. enum ReadState
  39. {
  40. None,
  41. Status,
  42. Headers,
  43. Content
  44. }
  45. class WebConnection
  46. {
  47. ServicePoint sPoint;
  48. Stream nstream;
  49. Socket socket;
  50. WebExceptionStatus status;
  51. WebConnectionGroup group;
  52. bool busy;
  53. WaitOrTimerCallback initConn;
  54. bool keepAlive;
  55. byte [] buffer;
  56. static AsyncCallback readDoneDelegate = new AsyncCallback (ReadDone);
  57. EventHandler abortHandler;
  58. ReadState readState;
  59. internal WebConnectionData Data;
  60. WebConnectionStream prevStream;
  61. bool chunkedRead;
  62. ChunkStream chunkStream;
  63. AutoResetEvent goAhead;
  64. Queue queue;
  65. bool reused;
  66. int position;
  67. bool ssl;
  68. bool certsAvailable;
  69. static object classLock = new object ();
  70. static Type sslStream;
  71. static PropertyInfo piClient;
  72. static PropertyInfo piServer;
  73. public WebConnection (WebConnectionGroup group, ServicePoint sPoint)
  74. {
  75. this.group = group;
  76. this.sPoint = sPoint;
  77. buffer = new byte [4096];
  78. readState = ReadState.None;
  79. Data = new WebConnectionData ();
  80. initConn = new WaitOrTimerCallback (InitConnection);
  81. abortHandler = new EventHandler (Abort);
  82. goAhead = new AutoResetEvent (true);
  83. queue = group.Queue;
  84. }
  85. public void Connect ()
  86. {
  87. lock (this) {
  88. if (socket != null && socket.Connected && status == WebExceptionStatus.Success) {
  89. reused = true;
  90. return;
  91. }
  92. reused = false;
  93. if (socket != null) {
  94. socket.Close();
  95. socket = null;
  96. }
  97. chunkStream = null;
  98. IPHostEntry hostEntry = sPoint.HostEntry;
  99. if (hostEntry == null) {
  100. status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
  101. WebExceptionStatus.NameResolutionFailure;
  102. return;
  103. }
  104. foreach (IPAddress address in hostEntry.AddressList) {
  105. socket = new Socket (address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  106. try {
  107. socket.Connect (new IPEndPoint(address, sPoint.Address.Port));
  108. status = WebExceptionStatus.Success;
  109. break;
  110. } catch (SocketException) {
  111. socket.Close();
  112. socket = null;
  113. status = WebExceptionStatus.ConnectFailure;
  114. }
  115. }
  116. }
  117. }
  118. static void EnsureSSLStreamAvailable ()
  119. {
  120. lock (classLock) {
  121. if (sslStream != null)
  122. return;
  123. // HttpsClientStream is an internal glue class in Mono.Security.dll
  124. sslStream = Type.GetType ("Mono.Security.Protocol.Tls.HttpsClientStream, " +
  125. Consts.AssemblyMono_Security, false);
  126. if (sslStream == null) {
  127. string msg = "Missing Mono.Security.dll assembly. " +
  128. "Support for SSL/TLS is unavailable.";
  129. throw new NotSupportedException (msg);
  130. }
  131. piClient = sslStream.GetProperty ("SelectedClientCertificate");
  132. piServer = sslStream.GetProperty ("ServerCertificate");
  133. }
  134. }
  135. bool CreateStream (HttpWebRequest request)
  136. {
  137. try {
  138. NetworkStream serverStream = new NetworkStream (socket, false);
  139. if (request.Address.Scheme == Uri.UriSchemeHttps) {
  140. ssl = true;
  141. EnsureSSLStreamAvailable ();
  142. if (!reused || nstream == null || nstream.GetType () != sslStream) {
  143. object[] args = new object [3] { serverStream,
  144. request.ClientCertificates,
  145. request };
  146. nstream = (Stream) Activator.CreateInstance (sslStream, args);
  147. }
  148. // we also need to set ServicePoint.Certificate
  149. // and ServicePoint.ClientCertificate but this can
  150. // only be done later (after handshake - which is
  151. // done only after a read operation).
  152. } else {
  153. ssl = false;
  154. nstream = serverStream;
  155. }
  156. } catch (Exception) {
  157. status = WebExceptionStatus.ConnectFailure;
  158. return false;
  159. }
  160. return true;
  161. }
  162. void HandleError (WebExceptionStatus st, Exception e)
  163. {
  164. status = st;
  165. lock (this) {
  166. busy = false;
  167. if (st == WebExceptionStatus.RequestCanceled)
  168. Data = new WebConnectionData ();
  169. status = st;
  170. }
  171. if (e == null) { // At least we now where it comes from
  172. try {
  173. throw new Exception (new System.Diagnostics.StackTrace ().ToString ());
  174. } catch (Exception e2) {
  175. e = e2;
  176. }
  177. }
  178. if (Data != null && Data.request != null)
  179. Data.request.SetResponseError (st, e);
  180. Close (true);
  181. }
  182. static void ReadDone (IAsyncResult result)
  183. {
  184. WebConnection cnc = (WebConnection) result.AsyncState;
  185. WebConnectionData data = cnc.Data;
  186. Stream ns = cnc.nstream;
  187. if (ns == null) {
  188. cnc.Close (true);
  189. return;
  190. }
  191. int nread = -1;
  192. try {
  193. nread = ns.EndRead (result);
  194. } catch (Exception e) {
  195. cnc.status = WebExceptionStatus.ReceiveFailure;
  196. cnc.HandleError (cnc.status, e);
  197. return;
  198. }
  199. if (nread == 0) {
  200. cnc.status = WebExceptionStatus.ReceiveFailure;
  201. cnc.HandleError (cnc.status, null);
  202. return;
  203. }
  204. if (nread < 0) {
  205. cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, null);
  206. return;
  207. }
  208. //Console.WriteLine (System.Text.Encoding.Default.GetString (cnc.buffer, 0, nread + cnc.position));
  209. int pos = -1;
  210. nread += cnc.position;
  211. if (cnc.readState == ReadState.None) {
  212. Exception exc = null;
  213. try {
  214. pos = cnc.GetResponse (cnc.buffer, nread);
  215. } catch (Exception e) {
  216. exc = e;
  217. }
  218. if (exc != null) {
  219. cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, exc);
  220. return;
  221. }
  222. }
  223. if (cnc.readState != ReadState.Content) {
  224. int est = nread * 2;
  225. int max = (est < cnc.buffer.Length) ? cnc.buffer.Length : est;
  226. byte [] newBuffer = new byte [max];
  227. Buffer.BlockCopy (cnc.buffer, 0, newBuffer, 0, nread);
  228. cnc.buffer = newBuffer;
  229. cnc.position = nread;
  230. cnc.readState = ReadState.None;
  231. InitRead (cnc);
  232. return;
  233. }
  234. cnc.position = 0;
  235. WebConnectionStream stream = new WebConnectionStream (cnc);
  236. string contentType = data.Headers ["Transfer-Encoding"];
  237. cnc.chunkedRead = (contentType != null && contentType.ToLower ().IndexOf ("chunked") != -1);
  238. if (!cnc.chunkedRead) {
  239. stream.ReadBuffer = cnc.buffer;
  240. stream.ReadBufferOffset = pos;
  241. stream.ReadBufferSize = nread;
  242. } else if (cnc.chunkStream == null) {
  243. cnc.chunkStream = new ChunkStream (cnc.buffer, pos, nread, data.Headers);
  244. } else {
  245. cnc.chunkStream.ResetBuffer ();
  246. cnc.chunkStream.Write (cnc.buffer, pos, nread);
  247. }
  248. data.stream = stream;
  249. if (!ExpectContent (data.StatusCode))
  250. stream.ForceCompletion ();
  251. lock (cnc) {
  252. lock (cnc.queue) {
  253. if (cnc.queue.Count > 0) {
  254. stream.ReadAll ();
  255. } else {
  256. cnc.prevStream = stream;
  257. stream.CheckComplete ();
  258. }
  259. }
  260. }
  261. data.request.SetResponseData (data);
  262. }
  263. static bool ExpectContent (int statusCode)
  264. {
  265. return (statusCode >= 200 && statusCode != 204 && statusCode != 304);
  266. }
  267. internal void GetCertificates ()
  268. {
  269. // here the SSL negotiation have been done
  270. X509Certificate client = (X509Certificate) piClient.GetValue (nstream, null);
  271. X509Certificate server = (X509Certificate) piServer.GetValue (nstream, null);
  272. sPoint.SetCertificates (client, server);
  273. certsAvailable = (server != null);
  274. }
  275. static void InitRead (object state)
  276. {
  277. WebConnection cnc = (WebConnection) state;
  278. Stream ns = cnc.nstream;
  279. try {
  280. int size = cnc.buffer.Length - cnc.position;
  281. ns.BeginRead (cnc.buffer, cnc.position, size, readDoneDelegate, cnc);
  282. } catch (Exception e) {
  283. cnc.HandleError (WebExceptionStatus.ReceiveFailure, e);
  284. }
  285. }
  286. int GetResponse (byte [] buffer, int max)
  287. {
  288. int pos = 0;
  289. string line = null;
  290. bool lineok = false;
  291. bool isContinue = false;
  292. do {
  293. if (readState == ReadState.None) {
  294. lineok = ReadLine (buffer, ref pos, max, ref line);
  295. if (!lineok)
  296. return -1;
  297. readState = ReadState.Status;
  298. string [] parts = line.Split (' ');
  299. if (parts.Length < 2)
  300. return -1;
  301. if (String.Compare (parts [0], "HTTP/1.1", true) == 0) {
  302. Data.Version = HttpVersion.Version11;
  303. sPoint.SetVersion (HttpVersion.Version11);
  304. } else {
  305. Data.Version = HttpVersion.Version10;
  306. sPoint.SetVersion (HttpVersion.Version10);
  307. }
  308. Data.StatusCode = (int) UInt32.Parse (parts [1]);
  309. if (parts.Length >= 3)
  310. Data.StatusDescription = String.Join (" ", parts, 2, parts.Length - 2);
  311. else
  312. Data.StatusDescription = "";
  313. if (pos >= max)
  314. return pos;
  315. }
  316. if (readState == ReadState.Status) {
  317. readState = ReadState.Headers;
  318. Data.Headers = new WebHeaderCollection ();
  319. ArrayList headers = new ArrayList ();
  320. bool finished = false;
  321. while (!finished) {
  322. if (ReadLine (buffer, ref pos, max, ref line) == false)
  323. break;
  324. if (line == null) {
  325. // Empty line: end of headers
  326. finished = true;
  327. continue;
  328. }
  329. if (line.Length > 0 && (line [0] == ' ' || line [0] == '\t')) {
  330. int count = headers.Count - 1;
  331. if (count < 0)
  332. break;
  333. string prev = (string) headers [count] + line;
  334. headers [count] = prev;
  335. } else {
  336. headers.Add (line);
  337. }
  338. }
  339. if (!finished)
  340. return -1;
  341. foreach (string s in headers)
  342. Data.Headers.SetInternal (s);
  343. if (Data.StatusCode == (int) HttpStatusCode.Continue) {
  344. sPoint.SendContinue = true;
  345. if (pos >= max)
  346. return pos;
  347. if (Data.request.ExpectContinue) {
  348. Data.request.DoContinueDelegate (Data.StatusCode, Data.Headers);
  349. // Prevent double calls when getting the
  350. // headers in several packets.
  351. Data.request.ExpectContinue = false;
  352. }
  353. readState = ReadState.None;
  354. isContinue = true;
  355. }
  356. else {
  357. readState = ReadState.Content;
  358. return pos;
  359. }
  360. }
  361. } while (isContinue == true);
  362. return -1;
  363. }
  364. void InitConnection (object state, bool notUsed)
  365. {
  366. HttpWebRequest request = (HttpWebRequest) state;
  367. if (status == WebExceptionStatus.RequestCanceled) {
  368. busy = false;
  369. Data = new WebConnectionData ();
  370. goAhead.Set ();
  371. SendNext ();
  372. return;
  373. }
  374. keepAlive = request.KeepAlive;
  375. Data = new WebConnectionData ();
  376. Data.request = request;
  377. Connect ();
  378. if (status != WebExceptionStatus.Success) {
  379. request.SetWriteStreamError (status);
  380. Close (true);
  381. return;
  382. }
  383. if (!CreateStream (request)) {
  384. request.SetWriteStreamError (status);
  385. Close (true);
  386. return;
  387. }
  388. readState = ReadState.None;
  389. request.SetWriteStream (new WebConnectionStream (this, request));
  390. InitRead (this);
  391. }
  392. internal EventHandler SendRequest (HttpWebRequest request)
  393. {
  394. lock (this) {
  395. if (prevStream != null && socket != null && socket.Connected) {
  396. prevStream.ReadAll ();
  397. prevStream = null;
  398. }
  399. if (!busy) {
  400. busy = true;
  401. ThreadPool.RegisterWaitForSingleObject (goAhead, initConn,
  402. request, -1, true);
  403. } else {
  404. lock (queue) {
  405. queue.Enqueue (request);
  406. }
  407. }
  408. }
  409. return abortHandler;
  410. }
  411. void SendNext ()
  412. {
  413. lock (queue) {
  414. if (queue.Count > 0) {
  415. prevStream = null;
  416. SendRequest ((HttpWebRequest) queue.Dequeue ());
  417. }
  418. }
  419. }
  420. internal void NextRead ()
  421. {
  422. lock (this) {
  423. busy = false;
  424. string header = (sPoint.UsesProxy) ? "Proxy-Connection" : "Connection";
  425. string cncHeader = (Data.Headers != null) ? Data.Headers [header] : null;
  426. bool keepAlive = (Data.Version == HttpVersion.Version11);
  427. if (cncHeader != null) {
  428. cncHeader = cncHeader.ToLower ();
  429. keepAlive = (this.keepAlive && cncHeader.IndexOf ("keep-alive") != -1);
  430. }
  431. if ((socket != null && !socket.Connected) ||
  432. (!keepAlive || (cncHeader != null && cncHeader.IndexOf ("close") != -1))) {
  433. Close (false);
  434. }
  435. goAhead.Set ();
  436. lock (queue) {
  437. if (queue.Count > 0) {
  438. prevStream = null;
  439. SendRequest ((HttpWebRequest) queue.Dequeue ());
  440. }
  441. }
  442. }
  443. }
  444. static bool ReadLine (byte [] buffer, ref int start, int max, ref string output)
  445. {
  446. bool foundCR = false;
  447. StringBuilder text = new StringBuilder ();
  448. int c = 0;
  449. while (start < max) {
  450. c = (int) buffer [start++];
  451. if (c == '\n') { // newline
  452. if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
  453. text.Length--;
  454. foundCR = false;
  455. break;
  456. } else if (foundCR) {
  457. text.Length--;
  458. break;
  459. }
  460. if (c == '\r')
  461. foundCR = true;
  462. text.Append ((char) c);
  463. }
  464. if (c != '\n' && c != '\r')
  465. return false;
  466. if (text.Length == 0) {
  467. output = null;
  468. return (c == '\n' || c == '\r');
  469. }
  470. if (foundCR)
  471. text.Length--;
  472. output = text.ToString ();
  473. return true;
  474. }
  475. internal IAsyncResult BeginRead (byte [] buffer, int offset, int size, AsyncCallback cb, object state)
  476. {
  477. if (nstream == null)
  478. return null;
  479. IAsyncResult result = null;
  480. if (!chunkedRead || chunkStream.WantMore) {
  481. try {
  482. result = nstream.BeginRead (buffer, offset, size, cb, state);
  483. } catch (Exception) {
  484. status = WebExceptionStatus.ReceiveFailure;
  485. throw;
  486. }
  487. }
  488. if (chunkedRead) {
  489. WebAsyncResult wr = new WebAsyncResult (null, null, buffer, offset, size);
  490. wr.InnerAsyncResult = result;
  491. return wr;
  492. }
  493. return result;
  494. }
  495. internal int EndRead (IAsyncResult result)
  496. {
  497. if (nstream == null)
  498. return 0;
  499. if (chunkedRead) {
  500. WebAsyncResult wr = (WebAsyncResult) result;
  501. int nbytes = 0;
  502. if (wr.InnerAsyncResult != null)
  503. nbytes = nstream.EndRead (wr.InnerAsyncResult);
  504. chunkStream.WriteAndReadBack (wr.Buffer, wr.Offset, wr.Size, ref nbytes);
  505. if (nbytes < wr.Size && chunkStream.WantMore) {
  506. int size = chunkStream.ChunkLeft;
  507. if (size < 0) // not read chunk size yet
  508. size = 1024;
  509. byte [] morebytes = new byte [size];
  510. int nread;
  511. nread = nstream.Read (morebytes, 0, size);
  512. chunkStream.Write (morebytes, 0, nread);
  513. morebytes = null;
  514. nbytes += chunkStream.Read (wr.Buffer, wr.Offset + nbytes,
  515. wr.Size - nbytes);
  516. }
  517. return nbytes;
  518. }
  519. return nstream.EndRead (result);
  520. }
  521. internal IAsyncResult BeginWrite (byte [] buffer, int offset, int size, AsyncCallback cb, object state)
  522. {
  523. IAsyncResult result = null;
  524. if (nstream == null)
  525. return null;
  526. try {
  527. result = nstream.BeginWrite (buffer, offset, size, cb, state);
  528. } catch (Exception) {
  529. status = WebExceptionStatus.SendFailure;
  530. throw;
  531. }
  532. return result;
  533. }
  534. internal void EndWrite (IAsyncResult result)
  535. {
  536. if (nstream != null)
  537. nstream.EndWrite (result);
  538. }
  539. internal int Read (byte [] buffer, int offset, int size)
  540. {
  541. if (nstream == null)
  542. return 0;
  543. int result = 0;
  544. try {
  545. if (!chunkedRead || chunkStream.WantMore)
  546. result = nstream.Read (buffer, offset, size);
  547. if (chunkedRead)
  548. chunkStream.WriteAndReadBack (buffer, offset, size, ref result);
  549. } catch (Exception e) {
  550. status = WebExceptionStatus.ReceiveFailure;
  551. HandleError (status, e);
  552. }
  553. return result;
  554. }
  555. internal void Write (byte [] buffer, int offset, int size)
  556. {
  557. if (nstream == null)
  558. return;
  559. try {
  560. nstream.Write (buffer, offset, size);
  561. // here SSL handshake should have been done
  562. if (ssl && !certsAvailable) {
  563. GetCertificates ();
  564. }
  565. } catch (Exception) {
  566. }
  567. }
  568. internal bool TryReconnect ()
  569. {
  570. lock (this) {
  571. if (!reused) {
  572. HandleError (WebExceptionStatus.SendFailure, null);
  573. return false;
  574. }
  575. Close (false);
  576. reused = false;
  577. Connect ();
  578. if (status != WebExceptionStatus.Success) {
  579. HandleError (WebExceptionStatus.SendFailure, null);
  580. return false;
  581. }
  582. if (!CreateStream (Data.request)) {
  583. HandleError (WebExceptionStatus.SendFailure, null);
  584. return false;
  585. }
  586. }
  587. return true;
  588. }
  589. void Close (bool sendNext)
  590. {
  591. lock (this) {
  592. busy = false;
  593. if (nstream != null) {
  594. try {
  595. nstream.Close ();
  596. } catch {}
  597. nstream = null;
  598. }
  599. if (socket != null) {
  600. try {
  601. socket.Close ();
  602. } catch {}
  603. socket = null;
  604. }
  605. if (sendNext) {
  606. goAhead.Set ();
  607. SendNext ();
  608. }
  609. }
  610. }
  611. void Abort (object sender, EventArgs args)
  612. {
  613. HandleError (WebExceptionStatus.RequestCanceled, null);
  614. }
  615. internal bool Busy {
  616. get { lock (this) return busy; }
  617. }
  618. internal bool Connected {
  619. get {
  620. lock (this) {
  621. return (socket != null && socket.Connected);
  622. }
  623. }
  624. }
  625. ~WebConnection ()
  626. {
  627. Close (false);
  628. }
  629. }
  630. }