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