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