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