Merge pull request #2122 from lambdageek/dev/bug-26384
[mono.git] / mcs / class / System / System.Net / WebConnection.cs
1 //
2 // System.Net.WebConnection
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.IO;
32 using System.Collections;
33 using System.Net.Sockets;
34 using System.Reflection;
35 using System.Security.Cryptography.X509Certificates;
36 using System.Text;
37 using System.Threading;
38 using System.Diagnostics;
39 using Mono.Net.Security;
40
41 namespace System.Net
42 {
43         enum ReadState
44         {
45                 None,
46                 Status,
47                 Headers,
48                 Content,
49                 Aborted
50         }
51
52         class WebConnection
53         {
54                 ServicePoint sPoint;
55                 Stream nstream;
56                 internal Socket socket;
57                 object socketLock = new object ();
58                 IWebConnectionState state;
59                 WebExceptionStatus status;
60                 WaitCallback initConn;
61                 bool keepAlive;
62                 byte [] buffer;
63                 static AsyncCallback readDoneDelegate = new AsyncCallback (ReadDone);
64                 EventHandler abortHandler;
65                 AbortHelper abortHelper;
66                 internal WebConnectionData Data;
67                 bool chunkedRead;
68                 ChunkStream chunkStream;
69                 Queue queue;
70                 bool reused;
71                 int position;
72                 HttpWebRequest priority_request;                
73                 NetworkCredential ntlm_credentials;
74                 bool ntlm_authenticated;
75                 bool unsafe_sharing;
76
77                 enum NtlmAuthState
78                 {
79                         None,
80                         Challenge,
81                         Response
82                 }
83                 NtlmAuthState connect_ntlm_auth_state;
84                 HttpWebRequest connect_request;
85
86                 bool ssl;
87                 Exception connect_exception;
88                 static object classLock = new object ();
89                 IMonoTlsProvider tlsProvider;
90                 MonoTlsStream tlsStream;
91
92 #if MONOTOUCH
93                 [System.Runtime.InteropServices.DllImport ("__Internal")]
94                 static extern void xamarin_start_wwan (string uri);
95 #endif
96
97                 internal ChunkStream ChunkStream {
98                         get { return chunkStream; }
99                 }
100
101                 public WebConnection (IWebConnectionState wcs, ServicePoint sPoint)
102                 {
103                         this.state = wcs;
104                         this.sPoint = sPoint;
105                         buffer = new byte [4096];
106                         Data = new WebConnectionData ();
107                         initConn = new WaitCallback (state => {
108                                 try {
109                                         InitConnection (state);
110                                 } catch {}
111                                 });
112                         queue = wcs.Group.Queue;
113                         abortHelper = new AbortHelper ();
114                         abortHelper.Connection = this;
115                         abortHandler = new EventHandler (abortHelper.Abort);
116                 }
117
118                 class AbortHelper {
119                         public WebConnection Connection;
120
121                         public void Abort (object sender, EventArgs args)
122                         {
123                                 WebConnection other = ((HttpWebRequest) sender).WebConnection;
124                                 if (other == null)
125                                         other = Connection;
126                                 other.Abort (sender, args);
127                         }
128                 }
129
130                 bool CanReuse ()
131                 {
132                         // The real condition is !(socket.Poll (0, SelectMode.SelectRead) || socket.Available != 0)
133                         // but if there's data pending to read (!) we won't reuse the socket.
134                         return (socket.Poll (0, SelectMode.SelectRead) == false);
135                 }
136                 
137                 void Connect (HttpWebRequest request)
138                 {
139                         lock (socketLock) {
140                                 if (socket != null && socket.Connected && status == WebExceptionStatus.Success) {
141                                         // Take the chunked stream to the expected state (State.None)
142                                         if (CanReuse () && CompleteChunkedRead ()) {
143                                                 reused = true;
144                                                 return;
145                                         }
146                                 }
147
148                                 reused = false;
149                                 if (socket != null) {
150                                         socket.Close();
151                                         socket = null;
152                                 }
153
154                                 chunkStream = null;
155                                 IPHostEntry hostEntry = sPoint.HostEntry;
156
157                                 if (hostEntry == null) {
158 #if MONOTOUCH
159                                         xamarin_start_wwan (sPoint.Address.ToString ());
160                                         hostEntry = sPoint.HostEntry;
161                                         if (hostEntry == null) {
162 #endif
163                                                 status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
164                                                                             WebExceptionStatus.NameResolutionFailure;
165                                                 return;
166 #if MONOTOUCH
167                                         }
168 #endif
169                                 }
170
171                                 //WebConnectionData data = Data;
172                                 foreach (IPAddress address in hostEntry.AddressList) {
173                                         try {
174                                                 socket = new Socket (address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
175                                         } catch (Exception se) {
176                                                 // The Socket ctor can throw if we run out of FD's
177                                                 if (!request.Aborted)
178                                                                 status = WebExceptionStatus.ConnectFailure;
179                                                 connect_exception = se;
180                                                 return;
181                                         }
182                                         IPEndPoint remote = new IPEndPoint (address, sPoint.Address.Port);
183                                         socket.NoDelay = !sPoint.UseNagleAlgorithm;
184                                         try {
185                                                 sPoint.KeepAliveSetup (socket);
186                                         } catch {
187                                                 // Ignore. Not supported in all platforms.
188                                         }
189
190                                         if (!sPoint.CallEndPointDelegate (socket, remote)) {
191                                                 socket.Close ();
192                                                 socket = null;
193                                                 status = WebExceptionStatus.ConnectFailure;
194                                         } else {
195                                                 try {
196                                                         if (request.Aborted)
197                                                                 return;
198                                                         socket.Connect (remote);
199                                                         status = WebExceptionStatus.Success;
200                                                         break;
201                                                 } catch (ThreadAbortException) {
202                                                         // program exiting...
203                                                         Socket s = socket;
204                                                         socket = null;
205                                                         if (s != null)
206                                                                 s.Close ();
207                                                         return;
208                                                 } catch (ObjectDisposedException) {
209                                                         // socket closed from another thread
210                                                         return;
211                                                 } catch (Exception exc) {
212                                                         Socket s = socket;
213                                                         socket = null;
214                                                         if (s != null)
215                                                                 s.Close ();
216                                                         if (!request.Aborted)
217                                                                 status = WebExceptionStatus.ConnectFailure;
218                                                         connect_exception = exc;
219                                                 }
220                                         }
221                                 }
222                         }
223                 }
224
225                 void EnsureSSLStreamAvailable (HttpWebRequest request)
226                 {
227                         tlsProvider = request.TlsProvider;
228                         if (tlsProvider != null)
229                                 return;
230
231                         tlsProvider = MonoTlsProviderFactory.GetProviderInternal ();
232                 }
233
234                 bool CreateTunnel (HttpWebRequest request, Uri connectUri,
235                                    Stream stream, out byte[] buffer)
236                 {
237                         StringBuilder sb = new StringBuilder ();
238                         sb.Append ("CONNECT ");
239                         sb.Append (request.Address.Host);
240                         sb.Append (':');
241                         sb.Append (request.Address.Port);
242                         sb.Append (" HTTP/");
243                         if (request.ServicePoint.ProtocolVersion == HttpVersion.Version11)
244                                 sb.Append ("1.1");
245                         else
246                                 sb.Append ("1.0");
247
248                         sb.Append ("\r\nHost: ");
249                         sb.Append (request.Address.Authority);
250
251                         bool ntlm = false;
252                         var challenge = Data.Challenge;
253                         Data.Challenge = null;
254                         var auth_header = request.Headers ["Proxy-Authorization"];
255                         bool have_auth = auth_header != null;
256                         if (have_auth) {
257                                 sb.Append ("\r\nProxy-Authorization: ");
258                                 sb.Append (auth_header);
259                                 ntlm = auth_header.ToUpper ().Contains ("NTLM");
260                         } else if (challenge != null && Data.StatusCode == 407) {
261                                 ICredentials creds = request.Proxy.Credentials;
262                                 have_auth = true;
263
264                                 if (connect_request == null) {
265                                         // create a CONNECT request to use with Authenticate
266                                         connect_request = (HttpWebRequest)WebRequest.Create (
267                                                 connectUri.Scheme + "://" + connectUri.Host + ":" + connectUri.Port + "/");
268                                         connect_request.Method = "CONNECT";
269                                         connect_request.Credentials = creds;
270                                 }
271
272                                 for (int i = 0; i < challenge.Length; i++) {
273                                         var auth = AuthenticationManager.Authenticate (challenge [i], connect_request, creds);
274                                         if (auth == null)
275                                                 continue;
276                                         ntlm = (auth.Module.AuthenticationType == "NTLM");
277                                         sb.Append ("\r\nProxy-Authorization: ");
278                                         sb.Append (auth.Message);
279                                         break;
280                                 }
281                         }
282
283                         if (ntlm) {
284                                 sb.Append ("\r\nProxy-Connection: keep-alive");
285                                 connect_ntlm_auth_state++;
286                         }
287
288                         sb.Append ("\r\n\r\n");
289
290                         Data.StatusCode = 0;
291                         byte [] connectBytes = Encoding.Default.GetBytes (sb.ToString ());
292                         stream.Write (connectBytes, 0, connectBytes.Length);
293
294                         int status;
295                         WebHeaderCollection result = ReadHeaders (stream, out buffer, out status);
296                         if ((!have_auth || connect_ntlm_auth_state == NtlmAuthState.Challenge) &&
297                             result != null && status == 407) { // Needs proxy auth
298                                 var connectionHeader = result ["Connection"];
299                                 if (socket != null && !string.IsNullOrEmpty (connectionHeader) &&
300                                     connectionHeader.ToLower() == "close") {
301                                         // The server is requesting that this connection be closed
302                                         socket.Close();
303                                         socket = null;
304                                 }
305
306                                 Data.StatusCode = status;
307                                 Data.Challenge = result.GetValues_internal ("Proxy-Authenticate", false);
308                                 return false;
309                         } else if (status != 200) {
310                                 string msg = String.Format ("The remote server returned a {0} status code.", status);
311                                 HandleError (WebExceptionStatus.SecureChannelFailure, null, msg);
312                                 return false;
313                         }
314
315                         return (result != null);
316                 }
317
318                 WebHeaderCollection ReadHeaders (Stream stream, out byte [] retBuffer, out int status)
319                 {
320                         retBuffer = null;
321                         status = 200;
322
323                         byte [] buffer = new byte [1024];
324                         MemoryStream ms = new MemoryStream ();
325
326                         while (true) {
327                                 int n = stream.Read (buffer, 0, 1024);
328                                 if (n == 0) {
329                                         HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders");
330                                         return null;
331                                 }
332                                 
333                                 ms.Write (buffer, 0, n);
334                                 int start = 0;
335                                 string str = null;
336                                 bool gotStatus = false;
337                                 WebHeaderCollection headers = new WebHeaderCollection ();
338                                 while (ReadLine (ms.GetBuffer (), ref start, (int) ms.Length, ref str)) {
339                                         if (str == null) {
340                                                 int contentLen = 0;
341                                                 try     {
342                                                         contentLen = int.Parse(headers["Content-Length"]);
343                                                 }
344                                                 catch {
345                                                         contentLen = 0;
346                                                 }
347
348                                                 if (ms.Length - start - contentLen > 0) {
349                                                         // we've read more data than the response header and conents,
350                                                         // give back extra data to the caller
351                                                         retBuffer = new byte[ms.Length - start - contentLen];
352                                                         Buffer.BlockCopy(ms.GetBuffer(), start + contentLen, retBuffer, 0, retBuffer.Length);
353                                                 }
354                                                 else {
355                                                         // haven't read in some or all of the contents for the response, do so now
356                                                         FlushContents(stream, contentLen - (int)(ms.Length - start));
357                                                 }
358
359                                                 return headers;
360                                         }
361
362                                         if (gotStatus) {
363                                                 headers.Add (str);
364                                                 continue;
365                                         }
366
367                                         string[] parts = str.Split (' ');
368                                         if (parts.Length < 2) {
369                                                 HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders2");
370                                                 return null;
371                                         }
372
373                                         if (String.Compare (parts [0], "HTTP/1.1", true) == 0)
374                                                 Data.ProxyVersion = HttpVersion.Version11;
375                                         else if (String.Compare (parts [0], "HTTP/1.0", true) == 0)
376                                                 Data.ProxyVersion = HttpVersion.Version10;
377                                         else {
378                                                 HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders2");
379                                                 return null;
380                                         }
381
382                                         status = (int)UInt32.Parse (parts [1]);
383                                         gotStatus = true;
384                                 }
385                         }
386                 }
387
388                 void FlushContents(Stream stream, int contentLength)
389                 {
390                         while (contentLength > 0) {
391                                 byte[] contentBuffer = new byte[contentLength];
392                                 int bytesRead = stream.Read(contentBuffer, 0, contentLength);
393                                 if (bytesRead > 0) {
394                                         contentLength -= bytesRead;
395                                 }
396                                 else {
397                                         break;
398                                 }
399                         }
400                 }
401
402                 bool CreateStream (HttpWebRequest request)
403                 {
404                         try {
405                                 NetworkStream serverStream = new NetworkStream (socket, false);
406
407                                 if (request.Address.Scheme == Uri.UriSchemeHttps) {
408 #if SECURITY_DEP
409                                         ssl = true;
410                                         EnsureSSLStreamAvailable (request);
411                                         if (!reused || nstream == null || tlsStream == null) {
412                                                 byte [] buffer = null;
413                                                 if (sPoint.UseConnect) {
414                                                         bool ok = CreateTunnel (request, sPoint.Address, serverStream, out buffer);
415                                                         if (!ok)
416                                                                 return false;
417                                                 }
418                                                 tlsStream = new MonoTlsStream (request, serverStream);
419                                                 nstream = tlsStream.CreateStream (buffer);
420                                         }
421                                         // we also need to set ServicePoint.Certificate 
422                                         // and ServicePoint.ClientCertificate but this can
423                                         // only be done later (after handshake - which is
424                                         // done only after a read operation).
425 #else
426                                         throw new NotSupportedException ();
427 #endif
428                                 } else {
429                                         ssl = false;
430                                         nstream = serverStream;
431                                 }
432                         } catch (Exception ex) {
433                                 if (tlsStream != null)
434                                         status = tlsStream.ExceptionStatus;
435                                 else if (!request.Aborted)
436                                         status = WebExceptionStatus.ConnectFailure;
437                                 return false;
438                         }
439
440                         return true;
441                 }
442                 
443                 void HandleError (WebExceptionStatus st, Exception e, string where)
444                 {
445                         status = st;
446                         lock (this) {
447                                 if (st == WebExceptionStatus.RequestCanceled)
448                                         Data = new WebConnectionData ();
449                         }
450
451                         if (e == null) { // At least we now where it comes from
452                                 try {
453                                         throw new Exception (new System.Diagnostics.StackTrace ().ToString ());
454                                 } catch (Exception e2) {
455                                         e = e2;
456                                 }
457                         }
458
459                         HttpWebRequest req = null;
460                         if (Data != null && Data.request != null)
461                                 req = Data.request;
462
463                         Close (true);
464                         if (req != null) {
465                                 req.FinishedReading = true;
466                                 req.SetResponseError (st, e, where);
467                         }
468                 }
469                 
470                 static void ReadDone (IAsyncResult result)
471                 {
472                         WebConnection cnc = (WebConnection)result.AsyncState;
473                         WebConnectionData data = cnc.Data;
474                         Stream ns = cnc.nstream;
475                         if (ns == null) {
476                                 cnc.Close (true);
477                                 return;
478                         }
479
480                         int nread = -1;
481                         try {
482                                 nread = ns.EndRead (result);
483                         } catch (ObjectDisposedException) {
484                                 return;
485                         } catch (Exception e) {
486                                 if (e.InnerException is ObjectDisposedException)
487                                         return;
488
489                                 cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "ReadDone1");
490                                 return;
491                         }
492
493                         if (nread == 0) {
494                                 cnc.HandleError (WebExceptionStatus.ReceiveFailure, null, "ReadDone2");
495                                 return;
496                         }
497
498                         if (nread < 0) {
499                                 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3");
500                                 return;
501                         }
502
503                         int pos = -1;
504                         nread += cnc.position;
505                         if (data.ReadState == ReadState.None) { 
506                                 Exception exc = null;
507                                 try {
508                                         pos = GetResponse (data, cnc.sPoint, cnc.buffer, nread);
509                                 } catch (Exception e) {
510                                         exc = e;
511                                 }
512
513                                 if (exc != null || pos == -1) {
514                                         cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, exc, "ReadDone4");
515                                         return;
516                                 }
517                         }
518
519                         if (data.ReadState == ReadState.Aborted) {
520                                 cnc.HandleError (WebExceptionStatus.RequestCanceled, null, "ReadDone");
521                                 return;
522                         }
523
524                         if (data.ReadState != ReadState.Content) {
525                                 int est = nread * 2;
526                                 int max = (est < cnc.buffer.Length) ? cnc.buffer.Length : est;
527                                 byte [] newBuffer = new byte [max];
528                                 Buffer.BlockCopy (cnc.buffer, 0, newBuffer, 0, nread);
529                                 cnc.buffer = newBuffer;
530                                 cnc.position = nread;
531                                 data.ReadState = ReadState.None;
532                                 InitRead (cnc);
533                                 return;
534                         }
535
536                         cnc.position = 0;
537
538                         WebConnectionStream stream = new WebConnectionStream (cnc, data);
539                         bool expect_content = ExpectContent (data.StatusCode, data.request.Method);
540                         string tencoding = null;
541                         if (expect_content)
542                                 tencoding = data.Headers ["Transfer-Encoding"];
543
544                         cnc.chunkedRead = (tencoding != null && tencoding.IndexOf ("chunked", StringComparison.OrdinalIgnoreCase) != -1);
545                         if (!cnc.chunkedRead) {
546                                 stream.ReadBuffer = cnc.buffer;
547                                 stream.ReadBufferOffset = pos;
548                                 stream.ReadBufferSize = nread;
549                                 try {
550                                         stream.CheckResponseInBuffer ();
551                                 } catch (Exception e) {
552                                         cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "ReadDone7");
553                                 }
554                         } else if (cnc.chunkStream == null) {
555                                 try {
556                                         cnc.chunkStream = new ChunkStream (cnc.buffer, pos, nread, data.Headers);
557                                 } catch (Exception e) {
558                                         cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone5");
559                                         return;
560                                 }
561                         } else {
562                                 cnc.chunkStream.ResetBuffer ();
563                                 try {
564                                         cnc.chunkStream.Write (cnc.buffer, pos, nread);
565                                 } catch (Exception e) {
566                                         cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone6");
567                                         return;
568                                 }
569                         }
570
571                         data.stream = stream;
572                         
573                         if (!expect_content)
574                                 stream.ForceCompletion ();
575
576                         data.request.SetResponseData (data);
577                 }
578
579                 static bool ExpectContent (int statusCode, string method)
580                 {
581                         if (method == "HEAD")
582                                 return false;
583                         return (statusCode >= 200 && statusCode != 204 && statusCode != 304);
584                 }
585
586                 internal static void InitRead (object state)
587                 {
588                         WebConnection cnc = (WebConnection) state;
589                         Stream ns = cnc.nstream;
590
591                         try {
592                                 int size = cnc.buffer.Length - cnc.position;
593                                 ns.BeginRead (cnc.buffer, cnc.position, size, readDoneDelegate, cnc);
594                         } catch (Exception e) {
595                                 cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "InitRead");
596                         }
597                 }
598                 
599                 static int GetResponse (WebConnectionData data, ServicePoint sPoint,
600                                         byte [] buffer, int max)
601                 {
602                         int pos = 0;
603                         string line = null;
604                         bool lineok = false;
605                         bool isContinue = false;
606                         bool emptyFirstLine = false;
607                         do {
608                                 if (data.ReadState == ReadState.Aborted)
609                                         return -1;
610
611                                 if (data.ReadState == ReadState.None) {
612                                         lineok = ReadLine (buffer, ref pos, max, ref line);
613                                         if (!lineok)
614                                                 return 0;
615
616                                         if (line == null) {
617                                                 emptyFirstLine = true;
618                                                 continue;
619                                         }
620                                         emptyFirstLine = false;
621                                         data.ReadState = ReadState.Status;
622
623                                         string [] parts = line.Split (' ');
624                                         if (parts.Length < 2)
625                                                 return -1;
626
627                                         if (String.Compare (parts [0], "HTTP/1.1", true) == 0) {
628                                                 data.Version = HttpVersion.Version11;
629                                                 sPoint.SetVersion (HttpVersion.Version11);
630                                         } else {
631                                                 data.Version = HttpVersion.Version10;
632                                                 sPoint.SetVersion (HttpVersion.Version10);
633                                         }
634
635                                         data.StatusCode = (int) UInt32.Parse (parts [1]);
636                                         if (parts.Length >= 3)
637                                                 data.StatusDescription = String.Join (" ", parts, 2, parts.Length - 2);
638                                         else
639                                                 data.StatusDescription = "";
640
641                                         if (pos >= max)
642                                                 return pos;
643                                 }
644
645                                 emptyFirstLine = false;
646                                 if (data.ReadState == ReadState.Status) {
647                                         data.ReadState = ReadState.Headers;
648                                         data.Headers = new WebHeaderCollection ();
649                                         ArrayList headers = new ArrayList ();
650                                         bool finished = false;
651                                         while (!finished) {
652                                                 if (ReadLine (buffer, ref pos, max, ref line) == false)
653                                                         break;
654                                         
655                                                 if (line == null) {
656                                                         // Empty line: end of headers
657                                                         finished = true;
658                                                         continue;
659                                                 }
660                                         
661                                                 if (line.Length > 0 && (line [0] == ' ' || line [0] == '\t')) {
662                                                         int count = headers.Count - 1;
663                                                         if (count < 0)
664                                                                 break;
665
666                                                         string prev = (string) headers [count] + line;
667                                                         headers [count] = prev;
668                                                 } else {
669                                                         headers.Add (line);
670                                                 }
671                                         }
672
673                                         if (!finished)
674                                                 return 0;
675
676                                         foreach (string s in headers)
677                                                 data.Headers.SetInternal (s);
678
679                                         if (data.StatusCode == (int) HttpStatusCode.Continue) {
680                                                 sPoint.SendContinue = true;
681                                                 if (pos >= max)
682                                                         return pos;
683
684                                                 if (data.request.ExpectContinue) {
685                                                         data.request.DoContinueDelegate (data.StatusCode, data.Headers);
686                                                         // Prevent double calls when getting the
687                                                         // headers in several packets.
688                                                         data.request.ExpectContinue = false;
689                                                 }
690
691                                                 data.ReadState = ReadState.None;
692                                                 isContinue = true;
693                                         }
694                                         else {
695                                                 data.ReadState = ReadState.Content;
696                                                 return pos;
697                                         }
698                                 }
699                         } while (emptyFirstLine || isContinue);
700
701                         return -1;
702                 }
703                 
704                 void InitConnection (object state)
705                 {
706                         HttpWebRequest request = (HttpWebRequest) state;
707                         request.WebConnection = this;
708                         if (request.ReuseConnection)
709                                 request.StoredConnection = this;
710
711                         if (request.Aborted)
712                                 return;
713
714                         keepAlive = request.KeepAlive;
715                         Data = new WebConnectionData (request);
716                 retry:
717                         Connect (request);
718                         if (request.Aborted)
719                                 return;
720
721                         if (status != WebExceptionStatus.Success) {
722                                 if (!request.Aborted) {
723                                         request.SetWriteStreamError (status, connect_exception);
724                                         Close (true);
725                                 }
726                                 return;
727                         }
728                         
729                         if (!CreateStream (request)) {
730                                 if (request.Aborted)
731                                         return;
732
733                                 WebExceptionStatus st = status;
734                                 if (Data.Challenge != null)
735                                         goto retry;
736
737                                 Exception cnc_exc = connect_exception;
738                                 connect_exception = null;
739                                 request.SetWriteStreamError (st, cnc_exc);
740                                 Close (true);
741                                 return;
742                         }
743
744                         request.SetWriteStream (new WebConnectionStream (this, request));
745                 }
746
747 #if MONOTOUCH
748                 static bool warned_about_queue = false;
749 #endif
750
751                 internal EventHandler SendRequest (HttpWebRequest request)
752                 {
753                         if (request.Aborted)
754                                 return null;
755
756                         lock (this) {
757                                 if (state.TrySetBusy ()) {
758                                         status = WebExceptionStatus.Success;
759                                         ThreadPool.QueueUserWorkItem (initConn, request);
760                                 } else {
761                                         lock (queue) {
762 #if MONOTOUCH
763                                                 if (!warned_about_queue) {
764                                                         warned_about_queue = true;
765                                                         Console.WriteLine ("WARNING: An HttpWebRequest was added to the ConnectionGroup queue because the connection limit was reached.");
766                                                 }
767 #endif
768                                                 queue.Enqueue (request);
769                                         }
770                                 }
771                         }
772
773                         return abortHandler;
774                 }
775                 
776                 void SendNext ()
777                 {
778                         lock (queue) {
779                                 if (queue.Count > 0) {
780                                         SendRequest ((HttpWebRequest) queue.Dequeue ());
781                                 }
782                         }
783                 }
784
785                 internal void NextRead ()
786                 {
787                         lock (this) {
788                                 if (Data.request != null)
789                                         Data.request.FinishedReading = true;
790                                 string header = (sPoint.UsesProxy) ? "Proxy-Connection" : "Connection";
791                                 string cncHeader = (Data.Headers != null) ? Data.Headers [header] : null;
792                                 bool keepAlive = (Data.Version == HttpVersion.Version11 && this.keepAlive);
793                                 if (Data.ProxyVersion != null && Data.ProxyVersion != HttpVersion.Version11)
794                                         keepAlive = false;
795                                 if (cncHeader != null) {
796                                         cncHeader = cncHeader.ToLower ();
797                                         keepAlive = (this.keepAlive && cncHeader.IndexOf ("keep-alive", StringComparison.Ordinal) != -1);
798                                 }
799
800                                 if ((socket != null && !socket.Connected) ||
801                                    (!keepAlive || (cncHeader != null && cncHeader.IndexOf ("close", StringComparison.Ordinal) != -1))) {
802                                         Close (false);
803                                 }
804
805                                 state.SetIdle ();
806                                 if (priority_request != null) {
807                                         SendRequest (priority_request);
808                                         priority_request = null;
809                                 } else {
810                                         SendNext ();
811                                 }
812                         }
813                 }
814                 
815                 static bool ReadLine (byte [] buffer, ref int start, int max, ref string output)
816                 {
817                         bool foundCR = false;
818                         StringBuilder text = new StringBuilder ();
819
820                         int c = 0;
821                         while (start < max) {
822                                 c = (int) buffer [start++];
823
824                                 if (c == '\n') {                        // newline
825                                         if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
826                                                 text.Length--;
827
828                                         foundCR = false;
829                                         break;
830                                 } else if (foundCR) {
831                                         text.Length--;
832                                         break;
833                                 }
834
835                                 if (c == '\r')
836                                         foundCR = true;
837                                         
838
839                                 text.Append ((char) c);
840                         }
841
842                         if (c != '\n' && c != '\r')
843                                 return false;
844
845                         if (text.Length == 0) {
846                                 output = null;
847                                 return (c == '\n' || c == '\r');
848                         }
849
850                         if (foundCR)
851                                 text.Length--;
852
853                         output = text.ToString ();
854                         return true;
855                 }
856
857
858                 internal IAsyncResult BeginRead (HttpWebRequest request, byte [] buffer, int offset, int size, AsyncCallback cb, object state)
859                 {
860                         Stream s = null;
861                         lock (this) {
862                                 if (Data.request != request)
863                                         throw new ObjectDisposedException (typeof (NetworkStream).FullName);
864                                 if (nstream == null)
865                                         return null;
866                                 s = nstream;
867                         }
868
869                         IAsyncResult result = null;
870                         if (!chunkedRead || (!chunkStream.DataAvailable && chunkStream.WantMore)) {
871                                 try {
872                                         result = s.BeginRead (buffer, offset, size, cb, state);
873                                         cb = null;
874                                 } catch (Exception) {
875                                         HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked BeginRead");
876                                         throw;
877                                 }
878                         }
879
880                         if (chunkedRead) {
881                                 WebAsyncResult wr = new WebAsyncResult (cb, state, buffer, offset, size);
882                                 wr.InnerAsyncResult = result;
883                                 if (result == null) {
884                                         // Will be completed from the data in ChunkStream
885                                         wr.SetCompleted (true, (Exception) null);
886                                         wr.DoCallback ();
887                                 }
888                                 return wr;
889                         }
890
891                         return result;
892                 }
893                 
894                 internal int EndRead (HttpWebRequest request, IAsyncResult result)
895                 {
896                         Stream s = null;
897                         lock (this) {
898                                 if (Data.request != request)
899                                         throw new ObjectDisposedException (typeof (NetworkStream).FullName);
900                                 if (nstream == null)
901                                         throw new ObjectDisposedException (typeof (NetworkStream).FullName);
902                                 s = nstream;
903                         }
904
905                         int nbytes = 0;
906                         bool done = false;
907                         WebAsyncResult wr = null;
908                         IAsyncResult nsAsync = ((WebAsyncResult) result).InnerAsyncResult;
909                         if (chunkedRead && (nsAsync is WebAsyncResult)) {
910                                 wr = (WebAsyncResult) nsAsync;
911                                 IAsyncResult inner = wr.InnerAsyncResult;
912                                 if (inner != null && !(inner is WebAsyncResult)) {
913                                         nbytes = s.EndRead (inner);
914                                         done = nbytes == 0;
915                                 }
916                         } else if (!(nsAsync is WebAsyncResult)) {
917                                 nbytes = s.EndRead (nsAsync);
918                                 wr = (WebAsyncResult) result;
919                                 done = nbytes == 0;
920                         }
921
922                         if (chunkedRead) {
923                                 try {
924                                         chunkStream.WriteAndReadBack (wr.Buffer, wr.Offset, wr.Size, ref nbytes);
925                                         if (!done && nbytes == 0 && chunkStream.WantMore)
926                                                 nbytes = EnsureRead (wr.Buffer, wr.Offset, wr.Size);
927                                 } catch (Exception e) {
928                                         if (e is WebException)
929                                                 throw e;
930
931                                         throw new WebException ("Invalid chunked data.", e,
932                                                                 WebExceptionStatus.ServerProtocolViolation, null);
933                                 }
934
935                                 if ((done || nbytes == 0) && chunkStream.ChunkLeft != 0) {
936                                         HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked EndRead");
937                                         throw new WebException ("Read error", null, WebExceptionStatus.ReceiveFailure, null);
938                                 }
939                         }
940
941                         return (nbytes != 0) ? nbytes : -1;
942                 }
943
944                 // To be called on chunkedRead when we can read no data from the ChunkStream yet
945                 int EnsureRead (byte [] buffer, int offset, int size)
946                 {
947                         byte [] morebytes = null;
948                         int nbytes = 0;
949                         while (nbytes == 0 && chunkStream.WantMore) {
950                                 int localsize = chunkStream.ChunkLeft;
951                                 if (localsize <= 0) // not read chunk size yet
952                                         localsize = 1024;
953                                 else if (localsize > 16384)
954                                         localsize = 16384;
955
956                                 if (morebytes == null || morebytes.Length < localsize)
957                                         morebytes = new byte [localsize];
958
959                                 int nread = nstream.Read (morebytes, 0, localsize);
960                                 if (nread <= 0)
961                                         return 0; // Error
962
963                                 chunkStream.Write (morebytes, 0, nread);
964                                 nbytes += chunkStream.Read (buffer, offset + nbytes, size - nbytes);
965                         }
966
967                         return nbytes;
968                 }
969
970                 bool CompleteChunkedRead()
971                 {
972                         if (!chunkedRead || chunkStream == null)
973                                 return true;
974
975                         while (chunkStream.WantMore) {
976                                 int nbytes = nstream.Read (buffer, 0, buffer.Length);
977                                 if (nbytes <= 0)
978                                         return false; // Socket was disconnected
979
980                                 chunkStream.Write (buffer, 0, nbytes);
981                         }
982
983                         return true;
984                 }
985
986                 internal IAsyncResult BeginWrite (HttpWebRequest request, byte [] buffer, int offset, int size, AsyncCallback cb, object state)
987                 {
988                         Stream s = null;
989                         lock (this) {
990                                 if (Data.request != request)
991                                         throw new ObjectDisposedException (typeof (NetworkStream).FullName);
992                                 if (nstream == null)
993                                         return null;
994                                 s = nstream;
995                         }
996
997                         IAsyncResult result = null;
998                         try {
999                                 result = s.BeginWrite (buffer, offset, size, cb, state);
1000                         } catch (Exception) {
1001                                 status = WebExceptionStatus.SendFailure;
1002                                 throw;
1003                         }
1004
1005                         return result;
1006                 }
1007
1008                 internal bool EndWrite (HttpWebRequest request, bool throwOnError, IAsyncResult result)
1009                 {
1010                         if (request.FinishedReading)
1011                                 return true;
1012
1013                         Stream s = null;
1014                         lock (this) {
1015                                 if (status == WebExceptionStatus.RequestCanceled)
1016                                         return true;
1017                                 if (Data.request != request)
1018                                         throw new ObjectDisposedException (typeof (NetworkStream).FullName);
1019                                 if (nstream == null)
1020                                         throw new ObjectDisposedException (typeof (NetworkStream).FullName);
1021                                 s = nstream;
1022                         }
1023
1024                         try {
1025                                 s.EndWrite (result);
1026                                 return true;
1027                         } catch (Exception exc) {
1028                                 status = WebExceptionStatus.SendFailure;
1029                                 if (throwOnError && exc.InnerException != null)
1030                                         throw exc.InnerException;
1031                                 return false;
1032                         }
1033                 }
1034
1035                 internal int Read (HttpWebRequest request, byte [] buffer, int offset, int size)
1036                 {
1037                         Stream s = null;
1038                         lock (this) {
1039                                 if (Data.request != request)
1040                                         throw new ObjectDisposedException (typeof (NetworkStream).FullName);
1041                                 if (nstream == null)
1042                                         return 0;
1043                                 s = nstream;
1044                         }
1045
1046                         int result = 0;
1047                         try {
1048                                 bool done = false;
1049                                 if (!chunkedRead) {
1050                                         result = s.Read (buffer, offset, size);
1051                                         done = (result == 0);
1052                                 }
1053
1054                                 if (chunkedRead) {
1055                                         try {
1056                                                 chunkStream.WriteAndReadBack (buffer, offset, size, ref result);
1057                                                 if (!done && result == 0 && chunkStream.WantMore)
1058                                                         result = EnsureRead (buffer, offset, size);
1059                                         } catch (Exception e) {
1060                                                 HandleError (WebExceptionStatus.ReceiveFailure, e, "chunked Read1");
1061                                                 throw;
1062                                         }
1063
1064                                         if ((done || result == 0) && chunkStream.WantMore) {
1065                                                 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked Read2");
1066                                                 throw new WebException ("Read error", null, WebExceptionStatus.ReceiveFailure, null);
1067                                         }
1068                                 }
1069                         } catch (Exception e) {
1070                                 HandleError (WebExceptionStatus.ReceiveFailure, e, "Read");
1071                         }
1072
1073                         return result;
1074                 }
1075
1076                 internal bool Write (HttpWebRequest request, byte [] buffer, int offset, int size, ref string err_msg)
1077                 {
1078                         err_msg = null;
1079                         Stream s = null;
1080                         lock (this) {
1081                                 if (Data.request != request)
1082                                         throw new ObjectDisposedException (typeof (NetworkStream).FullName);
1083                                 s = nstream;
1084                                 if (s == null)
1085                                         return false;
1086                         }
1087
1088                         try {
1089                                 s.Write (buffer, offset, size);
1090                         } catch (Exception e) {
1091                                 err_msg = e.Message;
1092                                 WebExceptionStatus wes = WebExceptionStatus.SendFailure;
1093                                 string msg = "Write: " + err_msg;
1094                                 if (e is WebException) {
1095                                         HandleError (wes, e, msg);
1096                                         return false;
1097                                 }
1098
1099                                 HandleError (wes, e, msg);
1100                                 return false;
1101                         }
1102                         return true;
1103                 }
1104
1105                 internal void Close (bool sendNext)
1106                 {
1107                         lock (this) {
1108                                 if (Data != null && Data.request != null && Data.request.ReuseConnection) {
1109                                         Data.request.ReuseConnection = false;
1110                                         return;
1111                                 }
1112
1113                                 if (nstream != null) {
1114                                         try {
1115                                                 nstream.Close ();
1116                                         } catch {}
1117                                         nstream = null;
1118                                 }
1119
1120                                 if (socket != null) {
1121                                         try {
1122                                                 socket.Close ();
1123                                         } catch {}
1124                                         socket = null;
1125                                 }
1126
1127                                 if (ntlm_authenticated)
1128                                         ResetNtlm ();
1129                                 if (Data != null) {
1130                                         lock (Data) {
1131                                                 Data.ReadState = ReadState.Aborted;
1132                                         }
1133                                 }
1134                                 state.SetIdle ();
1135                                 Data = new WebConnectionData ();
1136                                 if (sendNext)
1137                                         SendNext ();
1138                                 
1139                                 connect_request = null;
1140                                 connect_ntlm_auth_state = NtlmAuthState.None;
1141                         }
1142                 }
1143
1144                 void Abort (object sender, EventArgs args)
1145                 {
1146                         lock (this) {
1147                                 lock (queue) {
1148                                         HttpWebRequest req = (HttpWebRequest) sender;
1149                                         if (Data.request == req || Data.request == null) {
1150                                                 if (!req.FinishedReading) {
1151                                                         status = WebExceptionStatus.RequestCanceled;
1152                                                         Close (false);
1153                                                         if (queue.Count > 0) {
1154                                                                 Data.request = (HttpWebRequest) queue.Dequeue ();
1155                                                                 SendRequest (Data.request);
1156                                                         }
1157                                                 }
1158                                                 return;
1159                                         }
1160
1161                                         req.FinishedReading = true;
1162                                         req.SetResponseError (WebExceptionStatus.RequestCanceled, null, "User aborted");
1163                                         if (queue.Count > 0 && queue.Peek () == sender) {
1164                                                 queue.Dequeue ();
1165                                         } else if (queue.Count > 0) {
1166                                                 object [] old = queue.ToArray ();
1167                                                 queue.Clear ();
1168                                                 for (int i = old.Length - 1; i >= 0; i--) {
1169                                                         if (old [i] != sender)
1170                                                                 queue.Enqueue (old [i]);
1171                                                 }
1172                                         }
1173                                 }
1174                         }
1175                 }
1176
1177                 internal void ResetNtlm ()
1178                 {
1179                         ntlm_authenticated = false;
1180                         ntlm_credentials = null;
1181                         unsafe_sharing = false;
1182                 }
1183
1184                 internal bool Connected {
1185                         get {
1186                                 lock (this) {
1187                                         return (socket != null && socket.Connected);
1188                                 }
1189                         }
1190                 }
1191
1192                 // -Used for NTLM authentication
1193                 internal HttpWebRequest PriorityRequest {
1194                         set { priority_request = value; }
1195                 }
1196
1197                 internal bool NtlmAuthenticated {
1198                         get { return ntlm_authenticated; }
1199                         set { ntlm_authenticated = value; }
1200                 }
1201
1202                 internal NetworkCredential NtlmCredential {
1203                         get { return ntlm_credentials; }
1204                         set { ntlm_credentials = value; }
1205                 }
1206
1207                 internal bool UnsafeAuthenticatedConnectionSharing {
1208                         get { return unsafe_sharing; }
1209                         set { unsafe_sharing = value; }
1210                 }
1211                 // -
1212         }
1213 }
1214