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