2 // System.Net.WebConnection
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Net.Sockets;
34 using System.Reflection;
35 using System.Security.Cryptography.X509Certificates;
37 using System.Threading;
54 object socketLock = new object ();
55 WebExceptionStatus status;
56 WaitCallback initConn;
59 static AsyncCallback readDoneDelegate = new AsyncCallback (ReadDone);
60 EventHandler abortHandler;
62 internal WebConnectionData Data;
64 ChunkStream chunkStream;
72 static object classLock = new object ();
73 static Type sslStream;
74 static PropertyInfo piClient;
75 static PropertyInfo piServer;
76 static PropertyInfo piTrustFailure;
78 public WebConnection (WebConnectionGroup group, ServicePoint sPoint)
81 buffer = new byte [4096];
82 readState = ReadState.None;
83 Data = new WebConnectionData ();
84 initConn = new WaitCallback (InitConnection);
85 abortHandler = new EventHandler (Abort);
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);
99 if (socket != null && socket.Connected && status == WebExceptionStatus.Success) {
100 // Take the chunked stream to the expected state (State.None)
101 if (CanReuse () && CompleteChunkedRead ()) {
108 if (socket != null) {
114 IPHostEntry hostEntry = sPoint.HostEntry;
116 if (hostEntry == null) {
117 status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
118 WebExceptionStatus.NameResolutionFailure;
122 foreach (IPAddress address in hostEntry.AddressList) {
123 socket = new Socket (address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
125 socket.Connect (new IPEndPoint(address, sPoint.Address.Port));
126 status = WebExceptionStatus.Success;
128 } catch (SocketException) {
129 // This might be null if the request is aborted
130 if (socket != null) {
133 status = WebExceptionStatus.ConnectFailure;
140 static void EnsureSSLStreamAvailable ()
143 if (sslStream != null)
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);
150 if (sslStream == null) {
151 string msg = "Missing Mono.Security.dll assembly. " +
152 "Support for SSL/TLS is unavailable.";
154 throw new NotSupportedException (msg);
156 piClient = sslStream.GetProperty ("SelectedClientCertificate");
157 piServer = sslStream.GetProperty ("ServerCertificate");
158 piTrustFailure = sslStream.GetProperty ("TrustFailure");
162 bool CreateTunnel (HttpWebRequest request, Stream stream, out byte [] buffer)
164 StringBuilder sb = new StringBuilder ();
165 sb.Append ("CONNECT ");
166 sb.Append (request.Address.Host);
168 sb.Append (request.Address.Port);
169 sb.Append (" HTTP/");
170 if (request.ServicePoint.ProtocolVersion == HttpVersion.Version11)
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);
181 sb.Append ("\r\nProxy-Authorization: ");
182 sb.Append (request.Headers ["Proxy-Authorization"]);
183 } else if (challenge != null && Data.StatusCode == 407) {
185 ICredentials creds = request.Proxy.Credentials;
186 Authorization auth = AuthenticationManager.Authenticate (challenge, request, creds);
188 sb.Append ("\r\nProxy-Authorization: ");
189 sb.Append (auth.Message);
192 sb.Append ("\r\n\r\n");
195 byte [] connectBytes = Encoding.Default.GetBytes (sb.ToString ());
196 stream.Write (connectBytes, 0, connectBytes.Length);
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"];
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);
210 return (result != null);
213 WebHeaderCollection ReadHeaders (HttpWebRequest request, Stream stream, out byte [] retBuffer, out int status)
218 byte [] buffer = new byte [1024];
219 MemoryStream ms = new MemoryStream ();
220 bool gotStatus = false;
221 WebHeaderCollection headers = null;
224 int n = stream.Read (buffer, 0, 1024);
226 HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders");
230 ms.Write (buffer, 0, n);
233 headers = new WebHeaderCollection ();
234 while (ReadLine (ms.GetBuffer (), ref start, (int) ms.Length, ref str)) {
236 if (ms.Length - start > 0) {
237 retBuffer = new byte [ms.Length - start];
238 Buffer.BlockCopy (ms.GetBuffer (), start, retBuffer, 0, retBuffer.Length);
248 int spaceidx = str.IndexOf (' ');
249 if (spaceidx == -1) {
250 HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders2");
254 status = (int) UInt32.Parse (str.Substring (spaceidx + 1, 3));
260 bool CreateStream (HttpWebRequest request)
263 NetworkStream serverStream = new NetworkStream (socket, false);
265 if (request.Address.Scheme == Uri.UriSchemeHttps) {
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);
276 object[] args = new object [4] { serverStream,
277 request.ClientCertificates,
279 nstream = (Stream) Activator.CreateInstance (sslStream, args);
280 certsAvailable = false;
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).
288 nstream = serverStream;
290 } catch (Exception) {
291 status = WebExceptionStatus.ConnectFailure;
298 void HandleError (WebExceptionStatus st, Exception e, string where)
302 if (st == WebExceptionStatus.RequestCanceled)
303 Data = new WebConnectionData ();
306 if (e == null) { // At least we now where it comes from
309 throw new Exception ();
311 throw new Exception (new System.Diagnostics.StackTrace ().ToString ());
313 } catch (Exception e2) {
318 HttpWebRequest req = null;
319 if (Data != null && Data.request != null)
324 req.SetResponseError (st, e, where);
327 static void ReadDone (IAsyncResult result)
329 WebConnection cnc = (WebConnection) result.AsyncState;
330 WebConnectionData data = cnc.Data;
331 Stream ns = cnc.nstream;
339 nread = ns.EndRead (result);
340 } catch (Exception e) {
341 cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "ReadDone1");
346 cnc.HandleError (WebExceptionStatus.ReceiveFailure, null, "ReadDone2");
351 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3");
356 nread += cnc.position;
357 if (cnc.readState == ReadState.None) {
358 Exception exc = null;
360 pos = cnc.GetResponse (cnc.buffer, nread);
361 } catch (Exception e) {
366 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, exc, "ReadDone4");
371 if (cnc.readState != ReadState.Content) {
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;
385 WebConnectionStream stream = new WebConnectionStream (cnc);
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) {
395 cnc.chunkStream = new ChunkStream (cnc.buffer, pos, nread, data.Headers);
396 } catch (Exception e) {
397 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone5");
401 cnc.chunkStream.ResetBuffer ();
403 cnc.chunkStream.Write (cnc.buffer, pos, nread);
404 } catch (Exception e) {
405 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone6");
410 data.stream = stream;
412 if (!ExpectContent (data.StatusCode))
413 stream.ForceCompletion ();
415 data.request.SetResponseData (data);
418 static bool ExpectContent (int statusCode)
420 return (statusCode >= 200 && statusCode != 204 && statusCode != 304);
423 internal void GetCertificates ()
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);
432 internal static void InitRead (object state)
434 WebConnection cnc = (WebConnection) state;
435 Stream ns = cnc.nstream;
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");
445 int GetResponse (byte [] buffer, int max)
450 bool isContinue = false;
451 bool emptyFirstLine = false;
453 if (readState == ReadState.None) {
454 lineok = ReadLine (buffer, ref pos, max, ref line);
459 emptyFirstLine = true;
462 emptyFirstLine = false;
464 readState = ReadState.Status;
466 string [] parts = line.Split (' ');
467 if (parts.Length < 2)
470 if (String.Compare (parts [0], "HTTP/1.1", true) == 0) {
471 Data.Version = HttpVersion.Version11;
472 sPoint.SetVersion (HttpVersion.Version11);
474 Data.Version = HttpVersion.Version10;
475 sPoint.SetVersion (HttpVersion.Version10);
478 Data.StatusCode = (int) UInt32.Parse (parts [1]);
479 if (parts.Length >= 3)
480 Data.StatusDescription = String.Join (" ", parts, 2, parts.Length - 2);
482 Data.StatusDescription = "";
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;
495 if (ReadLine (buffer, ref pos, max, ref line) == false)
499 // Empty line: end of headers
504 if (line.Length > 0 && (line [0] == ' ' || line [0] == '\t')) {
505 int count = headers.Count - 1;
509 string prev = (string) headers [count] + line;
510 headers [count] = prev;
519 foreach (string s in headers)
520 Data.Headers.SetInternal (s);
522 if (Data.StatusCode == (int) HttpStatusCode.Continue) {
523 sPoint.SendContinue = true;
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;
534 readState = ReadState.None;
538 readState = ReadState.Content;
542 } while (emptyFirstLine || isContinue);
547 void InitConnection (object state)
549 HttpWebRequest request = (HttpWebRequest) state;
551 if (status == WebExceptionStatus.RequestCanceled) {
554 Data = new WebConnectionData ();
560 keepAlive = request.KeepAlive;
561 Data = new WebConnectionData ();
562 Data.request = request;
565 if (status != WebExceptionStatus.Success) {
566 if (status != WebExceptionStatus.RequestCanceled) {
567 request.SetWriteStreamError (status);
573 if (!CreateStream (request)) {
574 if (Data.Challenge != null)
577 request.SetWriteStreamError (status);
582 readState = ReadState.None;
583 request.SetWriteStream (new WebConnectionStream (this, request));
586 internal EventHandler SendRequest (HttpWebRequest request)
591 status = WebExceptionStatus.Success;
592 ThreadPool.QueueUserWorkItem (initConn, request);
595 queue.Enqueue (request);
606 if (queue.Count > 0) {
607 SendRequest ((HttpWebRequest) queue.Dequeue ());
612 internal void NextRead ()
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);
623 if ((socket != null && !socket.Connected) ||
624 (!keepAlive || (cncHeader != null && cncHeader.IndexOf ("close") != -1))) {
633 static bool ReadLine (byte [] buffer, ref int start, int max, ref string output)
635 bool foundCR = false;
636 StringBuilder text = new StringBuilder ();
639 while (start < max) {
640 c = (int) buffer [start++];
642 if (c == '\n') { // newline
643 if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
648 } else if (foundCR) {
657 text.Append ((char) c);
660 if (c != '\n' && c != '\r')
663 if (text.Length == 0) {
665 return (c == '\n' || c == '\r');
671 output = text.ToString ();
676 internal IAsyncResult BeginRead (byte [] buffer, int offset, int size, AsyncCallback cb, object state)
681 IAsyncResult result = null;
682 if (!chunkedRead || chunkStream.WantMore) {
684 result = nstream.BeginRead (buffer, offset, size, cb, state);
686 } catch (Exception) {
687 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked BeginRead");
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);
706 internal int EndRead (IAsyncResult result)
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;
725 bool done = (nbytes == 0);
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)
734 throw new WebException ("Invalid chunked data.", e,
735 WebExceptionStatus.ServerProtocolViolation, null);
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);
744 return (nbytes != 0) ? nbytes : -1;
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)
750 byte [] morebytes = null;
752 while (nbytes == 0 && chunkStream.WantMore) {
753 int localsize = chunkStream.ChunkLeft;
754 if (localsize <= 0) // not read chunk size yet
756 else if (localsize > 16384)
759 if (morebytes == null || morebytes.Length < localsize)
760 morebytes = new byte [localsize];
762 int nread = nstream.Read (morebytes, 0, localsize);
766 chunkStream.Write (morebytes, 0, nread);
767 nbytes += chunkStream.Read (buffer, offset + nbytes, size - nbytes);
773 bool CompleteChunkedRead()
775 if (!chunkedRead || chunkStream == null)
778 while (chunkStream.WantMore) {
779 int nbytes = nstream.Read (buffer, 0, buffer.Length);
781 return false; // Socket was disconnected
783 chunkStream.Write (buffer, 0, nbytes);
788 internal IAsyncResult BeginWrite (byte [] buffer, int offset, int size, AsyncCallback cb, object state)
790 IAsyncResult result = null;
795 result = nstream.BeginWrite (buffer, offset, size, cb, state);
796 } catch (Exception) {
797 status = WebExceptionStatus.SendFailure;
804 internal bool EndWrite (IAsyncResult result)
810 nstream.EndWrite (result);
813 status = WebExceptionStatus.SendFailure;
818 internal int Read (byte [] buffer, int offset, int size)
827 result = nstream.Read (buffer, offset, size);
828 done = (result == 0);
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");
841 if ((done || result == 0) && chunkStream.WantMore) {
842 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked Read2");
843 throw new WebException ("Read error", null, WebExceptionStatus.ReceiveFailure, null);
846 } catch (Exception e) {
847 HandleError (WebExceptionStatus.ReceiveFailure, e, "Read");
853 internal void Write (byte [] buffer, int offset, int size)
859 nstream.Write (buffer, offset, size);
860 // here SSL handshake should have been done
861 if (ssl && !certsAvailable)
863 } catch (Exception e) {
864 WebExceptionStatus wes = WebExceptionStatus.SendFailure;
865 string msg = "Write";
866 if (e is WebException) {
867 HandleError (wes, e, msg);
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";
877 HandleError (wes, e, msg);
881 internal void Close (bool sendNext)
884 if (nstream != null) {
891 if (socket != null) {
904 void Abort (object sender, EventArgs args)
907 if (Data.request == sender) {
908 HandleError (WebExceptionStatus.RequestCanceled, null, "Abort");
913 if (queue.Count > 0 && queue.Peek () == sender) {
918 object [] old = queue.ToArray ();
920 for (int i = old.Length - 1; i >= 0; i--) {
921 if (old [i] != sender)
922 queue.Enqueue (old [i]);
929 get { lock (this) return busy; }
932 internal bool Connected {
935 return (socket != null && socket.Connected);