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