WebConnection.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 bool sslCheck;
  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. bool CreateStream (HttpWebRequest request)
  119. {
  120. try {
  121. NetworkStream serverStream = new NetworkStream (socket, false);
  122. if (request.Address.Scheme == Uri.UriSchemeHttps) {
  123. ssl = true;
  124. if (!sslCheck) {
  125. lock (typeof (WebConnection)) {
  126. sslCheck = true;
  127. // HttpsClientStream is an internal glue class in Mono.Security.dll
  128. sslStream = Type.GetType ("Mono.Security.Protocol.Tls.HttpsClientStream, " + Consts.AssemblyMono_Security, false);
  129. if (sslStream != null) {
  130. piClient = sslStream.GetProperty ("SelectedClientCertificate");
  131. piServer = sslStream.GetProperty ("ServerCertificate");
  132. }
  133. }
  134. }
  135. if (sslStream == null)
  136. throw new NotSupportedException ("Missing Mono.Security.dll assembly. Support for SSL/TLS is unavailable.");
  137. object[] args = new object [4] { serverStream, request.RequestUri.Host, request.ClientCertificates, request };
  138. nstream = (Stream) Activator.CreateInstance (sslStream, args);
  139. // we also need to set ServicePoint.Certificate
  140. // and ServicePoint.ClientCertificate but this can
  141. // only be done later (after handshake - which is
  142. // done only after a read operation).
  143. }
  144. else
  145. nstream = serverStream;
  146. } catch (Exception) {
  147. status = WebExceptionStatus.ConnectFailure;
  148. return false;
  149. }
  150. return true;
  151. }
  152. void HandleError (WebExceptionStatus st, Exception e)
  153. {
  154. status = st;
  155. lock (this) {
  156. busy = false;
  157. if (st == WebExceptionStatus.RequestCanceled)
  158. Data = new WebConnectionData ();
  159. status = st;
  160. }
  161. if (e == null) { // At least we now where it comes from
  162. try {
  163. throw new Exception (new System.Diagnostics.StackTrace ().ToString ());
  164. } catch (Exception e2) {
  165. e = e2;
  166. }
  167. }
  168. if (Data != null && Data.request != null)
  169. Data.request.SetResponseError (st, e);
  170. Close (true);
  171. }
  172. static void ReadDone (IAsyncResult result)
  173. {
  174. WebConnection cnc = (WebConnection) result.AsyncState;
  175. WebConnectionData data = cnc.Data;
  176. Stream ns = cnc.nstream;
  177. if (ns == null) {
  178. cnc.Close (true);
  179. return;
  180. }
  181. int nread = -1;
  182. try {
  183. nread = ns.EndRead (result);
  184. } catch (Exception e) {
  185. cnc.status = WebExceptionStatus.ReceiveFailure;
  186. cnc.HandleError (cnc.status, e);
  187. return;
  188. }
  189. if (nread == 0) {
  190. cnc.status = WebExceptionStatus.ReceiveFailure;
  191. cnc.HandleError (cnc.status, null);
  192. return;
  193. }
  194. if (nread < 0) {
  195. cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, null);
  196. return;
  197. }
  198. //Console.WriteLine (System.Text.Encoding.Default.GetString (cnc.buffer, 0, nread + cnc.position));
  199. int pos = -1;
  200. nread += cnc.position;
  201. if (cnc.readState == ReadState.None) {
  202. Exception exc = null;
  203. try {
  204. pos = cnc.GetResponse (cnc.buffer, nread);
  205. } catch (Exception e) {
  206. exc = e;
  207. }
  208. if (exc != null) {
  209. cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, exc);
  210. return;
  211. }
  212. }
  213. if (cnc.readState != ReadState.Content) {
  214. int est = nread * 2;
  215. int max = (est < cnc.buffer.Length) ? cnc.buffer.Length : est;
  216. byte [] newBuffer = new byte [max];
  217. Buffer.BlockCopy (cnc.buffer, 0, newBuffer, 0, nread);
  218. cnc.buffer = newBuffer;
  219. cnc.position = nread;
  220. cnc.readState = ReadState.None;
  221. InitRead (cnc);
  222. return;
  223. }
  224. cnc.position = 0;
  225. WebConnectionStream stream = new WebConnectionStream (cnc);
  226. string contentType = data.Headers ["Transfer-Encoding"];
  227. cnc.chunkedRead = (contentType != null && contentType.ToLower ().IndexOf ("chunked") != -1);
  228. if (!cnc.chunkedRead) {
  229. stream.ReadBuffer = cnc.buffer;
  230. stream.ReadBufferOffset = pos;
  231. stream.ReadBufferSize = nread;
  232. } else if (cnc.chunkStream == null) {
  233. cnc.chunkStream = new ChunkStream (cnc.buffer, pos, nread, data.Headers);
  234. } else {
  235. cnc.chunkStream.ResetBuffer ();
  236. cnc.chunkStream.Write (cnc.buffer, pos, nread);
  237. }
  238. data.stream = stream;
  239. lock (cnc) {
  240. lock (cnc.queue) {
  241. if (cnc.queue.Count > 0) {
  242. stream.ReadAll ();
  243. } else {
  244. cnc.prevStream = stream;
  245. stream.CheckComplete ();
  246. }
  247. }
  248. }
  249. data.request.SetResponseData (data);
  250. }
  251. internal void GetCertificates ()
  252. {
  253. // here the SSL negotiation have been done
  254. X509Certificate client = (X509Certificate) piClient.GetValue (nstream, null);
  255. X509Certificate server = (X509Certificate) piServer.GetValue (nstream, null);
  256. sPoint.SetCertificates (client, server);
  257. certsAvailable = (server != null);
  258. }
  259. static void InitRead (object state)
  260. {
  261. WebConnection cnc = (WebConnection) state;
  262. Stream ns = cnc.nstream;
  263. try {
  264. int size = cnc.buffer.Length - cnc.position;
  265. ns.BeginRead (cnc.buffer, cnc.position, size, readDoneDelegate, cnc);
  266. } catch (Exception e) {
  267. cnc.HandleError (WebExceptionStatus.ReceiveFailure, e);
  268. }
  269. }
  270. int GetResponse (byte [] buffer, int max)
  271. {
  272. int pos = 0;
  273. string line = null;
  274. bool lineok = false;
  275. bool isContinue = false;
  276. do {
  277. if (readState == ReadState.None) {
  278. lineok = ReadLine (buffer, ref pos, max, ref line);
  279. if (!lineok)
  280. return -1;
  281. readState = ReadState.Status;
  282. string [] parts = line.Split (' ');
  283. if (parts.Length < 2)
  284. return -1;
  285. if (String.Compare (parts [0], "HTTP/1.1", true) == 0) {
  286. Data.Version = HttpVersion.Version11;
  287. sPoint.SetVersion (HttpVersion.Version11);
  288. } else {
  289. Data.Version = HttpVersion.Version10;
  290. sPoint.SetVersion (HttpVersion.Version10);
  291. }
  292. Data.StatusCode = (int) UInt32.Parse (parts [1]);
  293. if (parts.Length >= 3)
  294. Data.StatusDescription = String.Join (" ", parts, 2, parts.Length - 2);
  295. else
  296. Data.StatusDescription = "";
  297. if (pos >= max)
  298. return pos;
  299. }
  300. if (readState == ReadState.Status) {
  301. readState = ReadState.Headers;
  302. Data.Headers = new WebHeaderCollection ();
  303. ArrayList headers = new ArrayList ();
  304. bool finished = false;
  305. while (!finished) {
  306. if (ReadLine (buffer, ref pos, max, ref line) == false)
  307. break;
  308. if (line == null) {
  309. // Empty line: end of headers
  310. finished = true;
  311. continue;
  312. }
  313. if (line.Length > 0 && (line [0] == ' ' || line [0] == '\t')) {
  314. int count = headers.Count - 1;
  315. if (count < 0)
  316. break;
  317. string prev = (string) headers [count] + line;
  318. headers [count] = prev;
  319. } else {
  320. headers.Add (line);
  321. }
  322. }
  323. if (!finished)
  324. return -1;
  325. foreach (string s in headers)
  326. Data.Headers.SetInternal (s);
  327. if (Data.StatusCode == (int) HttpStatusCode.Continue) {
  328. sPoint.SendContinue = true;
  329. if (pos >= max)
  330. return pos;
  331. if (Data.request.ExpectContinue) {
  332. Data.request.DoContinueDelegate (Data.StatusCode, Data.Headers);
  333. // Prevent double calls when getting the
  334. // headers in several packets.
  335. Data.request.ExpectContinue = false;
  336. }
  337. readState = ReadState.None;
  338. isContinue = true;
  339. }
  340. else {
  341. readState = ReadState.Content;
  342. return pos;
  343. }
  344. }
  345. } while (isContinue == true);
  346. return -1;
  347. }
  348. void InitConnection (object state, bool notUsed)
  349. {
  350. HttpWebRequest request = (HttpWebRequest) state;
  351. if (status == WebExceptionStatus.RequestCanceled) {
  352. busy = false;
  353. Data = new WebConnectionData ();
  354. goAhead.Set ();
  355. SendNext ();
  356. return;
  357. }
  358. keepAlive = request.KeepAlive;
  359. Data = new WebConnectionData ();
  360. Data.request = request;
  361. Connect ();
  362. if (status != WebExceptionStatus.Success) {
  363. request.SetWriteStreamError (status);
  364. Close (true);
  365. return;
  366. }
  367. if (!CreateStream (request)) {
  368. request.SetWriteStreamError (status);
  369. Close (true);
  370. return;
  371. }
  372. readState = ReadState.None;
  373. request.SetWriteStream (new WebConnectionStream (this, request));
  374. InitRead (this);
  375. }
  376. internal EventHandler SendRequest (HttpWebRequest request)
  377. {
  378. lock (this) {
  379. if (prevStream != null && socket != null && socket.Connected) {
  380. prevStream.ReadAll ();
  381. prevStream = null;
  382. }
  383. if (!busy) {
  384. busy = true;
  385. ThreadPool.RegisterWaitForSingleObject (goAhead, initConn,
  386. request, -1, true);
  387. } else {
  388. lock (queue) {
  389. queue.Enqueue (request);
  390. }
  391. }
  392. }
  393. return abortHandler;
  394. }
  395. void SendNext ()
  396. {
  397. lock (queue) {
  398. if (queue.Count > 0) {
  399. prevStream = null;
  400. SendRequest ((HttpWebRequest) queue.Dequeue ());
  401. }
  402. }
  403. }
  404. internal void NextRead ()
  405. {
  406. lock (this) {
  407. busy = false;
  408. string header = (sPoint.UsesProxy) ? "Proxy-Connection" : "Connection";
  409. string cncHeader = (Data.Headers != null) ? Data.Headers [header] : null;
  410. bool keepAlive = (Data.Version == HttpVersion.Version11);
  411. if (cncHeader != null) {
  412. cncHeader = cncHeader.ToLower ();
  413. keepAlive = (this.keepAlive && cncHeader.IndexOf ("keep-alive") != -1);
  414. }
  415. if ((socket != null && !socket.Connected) ||
  416. (!keepAlive || (cncHeader != null && cncHeader.IndexOf ("close") != -1))) {
  417. Close (false);
  418. }
  419. goAhead.Set ();
  420. lock (queue) {
  421. if (queue.Count > 0) {
  422. prevStream = null;
  423. SendRequest ((HttpWebRequest) queue.Dequeue ());
  424. }
  425. }
  426. }
  427. }
  428. static bool ReadLine (byte [] buffer, ref int start, int max, ref string output)
  429. {
  430. bool foundCR = false;
  431. StringBuilder text = new StringBuilder ();
  432. int c = 0;
  433. while (start < max) {
  434. c = (int) buffer [start++];
  435. if (c == '\n') { // newline
  436. if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
  437. text.Length--;
  438. foundCR = false;
  439. break;
  440. } else if (foundCR) {
  441. text.Length--;
  442. break;
  443. }
  444. if (c == '\r')
  445. foundCR = true;
  446. text.Append ((char) c);
  447. }
  448. if (c != '\n' && c != '\r')
  449. return false;
  450. if (text.Length == 0) {
  451. output = null;
  452. return (c == '\n' || c == '\r');
  453. }
  454. if (foundCR)
  455. text.Length--;
  456. output = text.ToString ();
  457. return true;
  458. }
  459. internal IAsyncResult BeginRead (byte [] buffer, int offset, int size, AsyncCallback cb, object state)
  460. {
  461. if (nstream == null)
  462. return null;
  463. IAsyncResult result = null;
  464. if (!chunkedRead || chunkStream.WantMore) {
  465. try {
  466. result = nstream.BeginRead (buffer, offset, size, cb, state);
  467. } catch (Exception) {
  468. status = WebExceptionStatus.ReceiveFailure;
  469. throw;
  470. }
  471. }
  472. if (chunkedRead) {
  473. WebAsyncResult wr = new WebAsyncResult (null, null, buffer, offset, size);
  474. wr.InnerAsyncResult = result;
  475. return wr;
  476. }
  477. return result;
  478. }
  479. internal int EndRead (IAsyncResult result)
  480. {
  481. if (nstream == null)
  482. return 0;
  483. if (chunkedRead) {
  484. WebAsyncResult wr = (WebAsyncResult) result;
  485. int nbytes = 0;
  486. if (wr.InnerAsyncResult != null)
  487. nbytes = nstream.EndRead (wr.InnerAsyncResult);
  488. chunkStream.WriteAndReadBack (wr.Buffer, wr.Offset, wr.Size, ref nbytes);
  489. if (nbytes < wr.Size && chunkStream.WantMore) {
  490. int size = chunkStream.ChunkLeft;
  491. if (size < 0) // not read chunk size yet
  492. size = 1024;
  493. byte [] morebytes = new byte [size];
  494. int nread;
  495. nread = nstream.Read (morebytes, 0, size);
  496. chunkStream.Write (morebytes, 0, nread);
  497. morebytes = null;
  498. nbytes += chunkStream.Read (wr.Buffer, wr.Offset + nbytes,
  499. wr.Size - nbytes);
  500. }
  501. return nbytes;
  502. }
  503. return nstream.EndRead (result);
  504. }
  505. internal IAsyncResult BeginWrite (byte [] buffer, int offset, int size, AsyncCallback cb, object state)
  506. {
  507. IAsyncResult result = null;
  508. if (nstream == null)
  509. return null;
  510. try {
  511. result = nstream.BeginWrite (buffer, offset, size, cb, state);
  512. } catch (Exception) {
  513. status = WebExceptionStatus.SendFailure;
  514. throw;
  515. }
  516. return result;
  517. }
  518. internal void EndWrite (IAsyncResult result)
  519. {
  520. if (nstream != null)
  521. nstream.EndWrite (result);
  522. }
  523. internal int Read (byte [] buffer, int offset, int size)
  524. {
  525. if (nstream == null)
  526. return 0;
  527. int result = 0;
  528. try {
  529. if (!chunkedRead || chunkStream.WantMore)
  530. result = nstream.Read (buffer, offset, size);
  531. if (chunkedRead)
  532. chunkStream.WriteAndReadBack (buffer, offset, size, ref result);
  533. } catch (Exception e) {
  534. status = WebExceptionStatus.ReceiveFailure;
  535. HandleError (status, e);
  536. }
  537. return result;
  538. }
  539. internal void Write (byte [] buffer, int offset, int size)
  540. {
  541. if (nstream == null)
  542. return;
  543. try {
  544. nstream.Write (buffer, offset, size);
  545. // here SSL handshake should have been done
  546. if (ssl && !certsAvailable) {
  547. GetCertificates ();
  548. }
  549. } catch (Exception) {
  550. }
  551. }
  552. internal bool TryReconnect ()
  553. {
  554. lock (this) {
  555. if (!reused) {
  556. HandleError (WebExceptionStatus.SendFailure, null);
  557. return false;
  558. }
  559. Close (false);
  560. reused = false;
  561. Connect ();
  562. if (status != WebExceptionStatus.Success) {
  563. HandleError (WebExceptionStatus.SendFailure, null);
  564. return false;
  565. }
  566. if (!CreateStream (Data.request)) {
  567. HandleError (WebExceptionStatus.SendFailure, null);
  568. return false;
  569. }
  570. }
  571. return true;
  572. }
  573. void Close (bool sendNext)
  574. {
  575. lock (this) {
  576. busy = false;
  577. if (nstream != null) {
  578. try {
  579. nstream.Close ();
  580. } catch {}
  581. nstream = null;
  582. }
  583. if (socket != null) {
  584. try {
  585. socket.Close ();
  586. } catch {}
  587. socket = null;
  588. }
  589. if (sendNext) {
  590. goAhead.Set ();
  591. SendNext ();
  592. }
  593. }
  594. }
  595. void Abort (object sender, EventArgs args)
  596. {
  597. HandleError (WebExceptionStatus.RequestCanceled, null);
  598. }
  599. internal bool Busy {
  600. get { lock (this) return busy; }
  601. }
  602. internal bool Connected {
  603. get {
  604. lock (this) {
  605. return (socket != null && socket.Connected);
  606. }
  607. }
  608. }
  609. ~WebConnection ()
  610. {
  611. Close (false);
  612. }
  613. }
  614. }