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