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