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;
39 using Mono.Security.Protocol.Tls;
57 object socketLock = new object ();
58 WebExceptionStatus status;
59 WaitCallback initConn;
62 static AsyncCallback readDoneDelegate = new AsyncCallback (ReadDone);
63 EventHandler abortHandler;
64 AbortHelper abortHelper;
66 internal WebConnectionData Data;
68 ChunkStream chunkStream;
73 HttpWebRequest priority_request;
74 NetworkCredential ntlm_credentials;
75 bool ntlm_authenticated;
80 Exception connect_exception;
81 static object classLock = new object ();
82 static Type sslStream;
83 static PropertyInfo piClient;
84 static PropertyInfo piServer;
85 static PropertyInfo piTrustFailure;
88 static MethodInfo start_wwan;
90 static WebConnection ()
92 Type type = Type.GetType ("MonoTouch.ObjCRuntime.Runtime, monotouch");
94 start_wwan = type.GetMethod ("StartWWAN");
98 public WebConnection (WebConnectionGroup group, ServicePoint sPoint)
100 this.sPoint = sPoint;
101 buffer = new byte [4096];
102 readState = ReadState.None;
103 Data = new WebConnectionData ();
104 initConn = new WaitCallback (state => {
106 InitConnection (state);
110 abortHelper = new AbortHelper ();
111 abortHelper.Connection = this;
112 abortHandler = new EventHandler (abortHelper.Abort);
116 public WebConnection Connection;
118 public void Abort (object sender, EventArgs args)
120 WebConnection other = ((HttpWebRequest) sender).WebConnection;
123 other.Abort (sender, args);
129 // The real condition is !(socket.Poll (0, SelectMode.SelectRead) || socket.Available != 0)
130 // but if there's data pending to read (!) we won't reuse the socket.
131 return (socket.Poll (0, SelectMode.SelectRead) == false);
134 void Connect (HttpWebRequest request)
137 if (socket != null && socket.Connected && status == WebExceptionStatus.Success) {
138 // Take the chunked stream to the expected state (State.None)
139 if (CanReuse () && CompleteChunkedRead ()) {
146 if (socket != null) {
152 IPHostEntry hostEntry = sPoint.HostEntry;
154 if (hostEntry == null) {
156 if (start_wwan != null) {
157 start_wwan.Invoke (null, new object [1] { sPoint.Address });
158 hostEntry = sPoint.HostEntry;
160 if (hostEntry == null) {
162 status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
163 WebExceptionStatus.NameResolutionFailure;
170 //WebConnectionData data = Data;
171 foreach (IPAddress address in hostEntry.AddressList) {
172 socket = new Socket (address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
173 IPEndPoint remote = new IPEndPoint (address, sPoint.Address.Port);
174 socket.NoDelay = !sPoint.UseNagleAlgorithm;
176 sPoint.KeepAliveSetup (socket);
178 // Ignore. Not supported in all platforms.
181 if (!sPoint.CallEndPointDelegate (socket, remote)) {
184 status = WebExceptionStatus.ConnectFailure;
189 socket.Connect (remote);
190 status = WebExceptionStatus.Success;
192 } catch (ThreadAbortException) {
193 // program exiting...
199 } catch (ObjectDisposedException) {
200 // socket closed from another thread
202 } catch (Exception exc) {
207 if (!request.Aborted)
208 status = WebExceptionStatus.ConnectFailure;
209 connect_exception = exc;
216 static void EnsureSSLStreamAvailable ()
219 if (sslStream != null)
222 #if NET_2_1 && SECURITY_DEP
223 sslStream = typeof (Mono.Security.Protocol.Tls.HttpsClientStream);
225 // HttpsClientStream is an internal glue class in Mono.Security.dll
226 sslStream = Type.GetType ("Mono.Security.Protocol.Tls.HttpsClientStream, " +
227 Consts.AssemblyMono_Security, false);
229 if (sslStream == null) {
230 string msg = "Missing Mono.Security.dll assembly. " +
231 "Support for SSL/TLS is unavailable.";
233 throw new NotSupportedException (msg);
236 piClient = sslStream.GetProperty ("SelectedClientCertificate");
237 piServer = sslStream.GetProperty ("ServerCertificate");
238 piTrustFailure = sslStream.GetProperty ("TrustFailure");
242 bool CreateTunnel (HttpWebRequest request, Stream stream, out byte [] buffer)
244 StringBuilder sb = new StringBuilder ();
245 sb.Append ("CONNECT ");
246 sb.Append (request.Address.Host);
248 sb.Append (request.Address.Port);
249 sb.Append (" HTTP/");
250 if (request.ServicePoint.ProtocolVersion == HttpVersion.Version11)
255 sb.Append ("\r\nHost: ");
256 sb.Append (request.Address.Authority);
257 string challenge = Data.Challenge;
258 Data.Challenge = null;
259 bool have_auth = (request.Headers ["Proxy-Authorization"] != null);
261 sb.Append ("\r\nProxy-Authorization: ");
262 sb.Append (request.Headers ["Proxy-Authorization"]);
263 } else if (challenge != null && Data.StatusCode == 407) {
265 ICredentials creds = request.Proxy.Credentials;
266 Authorization auth = AuthenticationManager.Authenticate (challenge, request, creds);
268 sb.Append ("\r\nProxy-Authorization: ");
269 sb.Append (auth.Message);
272 sb.Append ("\r\n\r\n");
275 byte [] connectBytes = Encoding.Default.GetBytes (sb.ToString ());
276 stream.Write (connectBytes, 0, connectBytes.Length);
279 WebHeaderCollection result = ReadHeaders (request, stream, out buffer, out status);
280 if (!have_auth && result != null && status == 407) { // Needs proxy auth
281 Data.StatusCode = status;
282 Data.Challenge = result ["Proxy-Authenticate"];
284 } else if (status != 200) {
285 string msg = String.Format ("The remote server returned a {0} status code.", status);
286 HandleError (WebExceptionStatus.SecureChannelFailure, null, msg);
290 return (result != null);
293 WebHeaderCollection ReadHeaders (HttpWebRequest request, Stream stream, out byte [] retBuffer, out int status)
298 byte [] buffer = new byte [1024];
299 MemoryStream ms = new MemoryStream ();
300 bool gotStatus = false;
301 WebHeaderCollection headers = null;
304 int n = stream.Read (buffer, 0, 1024);
306 HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders");
310 ms.Write (buffer, 0, n);
313 headers = new WebHeaderCollection ();
314 while (ReadLine (ms.GetBuffer (), ref start, (int) ms.Length, ref str)) {
316 if (ms.Length - start > 0) {
317 retBuffer = new byte [ms.Length - start];
318 Buffer.BlockCopy (ms.GetBuffer (), start, retBuffer, 0, retBuffer.Length);
328 int spaceidx = str.IndexOf (' ');
329 if (spaceidx == -1) {
330 HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders2");
334 status = (int) UInt32.Parse (str.Substring (spaceidx + 1, 3));
340 bool CreateStream (HttpWebRequest request)
343 NetworkStream serverStream = new NetworkStream (socket, false);
345 if (request.Address.Scheme == Uri.UriSchemeHttps) {
347 EnsureSSLStreamAvailable ();
348 if (!reused || nstream == null || nstream.GetType () != sslStream) {
349 byte [] buffer = null;
350 if (sPoint.UseConnect) {
351 bool ok = CreateTunnel (request, serverStream, out buffer);
356 object[] args = new object [4] { serverStream,
357 request.ClientCertificates,
359 nstream = (Stream) Activator.CreateInstance (sslStream, args);
361 SslClientStream scs = (SslClientStream) nstream;
362 var helper = new ServicePointManager.ChainValidationHelper (request);
363 scs.ServerCertValidation2 += new CertificateValidationCallback2 (helper.ValidateChain);
365 certsAvailable = false;
367 // we also need to set ServicePoint.Certificate
368 // and ServicePoint.ClientCertificate but this can
369 // only be done later (after handshake - which is
370 // done only after a read operation).
373 nstream = serverStream;
375 } catch (Exception) {
376 if (!request.Aborted)
377 status = WebExceptionStatus.ConnectFailure;
384 void HandleError (WebExceptionStatus st, Exception e, string where)
388 if (st == WebExceptionStatus.RequestCanceled)
389 Data = new WebConnectionData ();
392 if (e == null) { // At least we now where it comes from
395 throw new Exception ();
397 throw new Exception (new System.Diagnostics.StackTrace ().ToString ());
399 } catch (Exception e2) {
404 HttpWebRequest req = null;
405 if (Data != null && Data.request != null)
410 req.FinishedReading = true;
411 req.SetResponseError (st, e, where);
415 static void ReadDone (IAsyncResult result)
417 WebConnection cnc = (WebConnection) result.AsyncState;
418 WebConnectionData data = cnc.Data;
419 Stream ns = cnc.nstream;
427 nread = ns.EndRead (result);
428 } catch (Exception e) {
429 cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "ReadDone1");
434 cnc.HandleError (WebExceptionStatus.ReceiveFailure, null, "ReadDone2");
439 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3");
444 nread += cnc.position;
445 if (cnc.readState == ReadState.None) {
446 Exception exc = null;
448 pos = cnc.GetResponse (cnc.buffer, nread);
449 } catch (Exception e) {
454 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, exc, "ReadDone4");
459 if (cnc.readState != ReadState.Content) {
461 int max = (est < cnc.buffer.Length) ? cnc.buffer.Length : est;
462 byte [] newBuffer = new byte [max];
463 Buffer.BlockCopy (cnc.buffer, 0, newBuffer, 0, nread);
464 cnc.buffer = newBuffer;
465 cnc.position = nread;
466 cnc.readState = ReadState.None;
473 WebConnectionStream stream = new WebConnectionStream (cnc);
474 bool expect_content = ExpectContent (data.StatusCode, data.request.Method);
475 string tencoding = null;
477 tencoding = data.Headers ["Transfer-Encoding"];
479 cnc.chunkedRead = (tencoding != null && tencoding.IndexOf ("chunked", StringComparison.OrdinalIgnoreCase) != -1);
480 if (!cnc.chunkedRead) {
481 stream.ReadBuffer = cnc.buffer;
482 stream.ReadBufferOffset = pos;
483 stream.ReadBufferSize = nread;
484 stream.CheckResponseInBuffer ();
485 } else if (cnc.chunkStream == null) {
487 cnc.chunkStream = new ChunkStream (cnc.buffer, pos, nread, data.Headers);
488 } catch (Exception e) {
489 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone5");
493 cnc.chunkStream.ResetBuffer ();
495 cnc.chunkStream.Write (cnc.buffer, pos, nread);
496 } catch (Exception e) {
497 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone6");
502 data.stream = stream;
505 stream.ForceCompletion ();
507 data.request.SetResponseData (data);
510 static bool ExpectContent (int statusCode, string method)
512 if (method == "HEAD")
514 return (statusCode >= 200 && statusCode != 204 && statusCode != 304);
517 internal void GetCertificates ()
519 // here the SSL negotiation have been done
520 X509Certificate client = (X509Certificate) piClient.GetValue (nstream, null);
521 X509Certificate server = (X509Certificate) piServer.GetValue (nstream, null);
522 sPoint.SetCertificates (client, server);
523 certsAvailable = (server != null);
526 internal static void InitRead (object state)
528 WebConnection cnc = (WebConnection) state;
529 Stream ns = cnc.nstream;
532 int size = cnc.buffer.Length - cnc.position;
533 ns.BeginRead (cnc.buffer, cnc.position, size, readDoneDelegate, cnc);
534 } catch (Exception e) {
535 cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "InitRead");
539 int GetResponse (byte [] buffer, int max)
544 bool isContinue = false;
545 bool emptyFirstLine = false;
547 if (readState == ReadState.None) {
548 lineok = ReadLine (buffer, ref pos, max, ref line);
553 emptyFirstLine = true;
556 emptyFirstLine = false;
558 readState = ReadState.Status;
560 string [] parts = line.Split (' ');
561 if (parts.Length < 2)
564 if (String.Compare (parts [0], "HTTP/1.1", true) == 0) {
565 Data.Version = HttpVersion.Version11;
566 sPoint.SetVersion (HttpVersion.Version11);
568 Data.Version = HttpVersion.Version10;
569 sPoint.SetVersion (HttpVersion.Version10);
572 Data.StatusCode = (int) UInt32.Parse (parts [1]);
573 if (parts.Length >= 3)
574 Data.StatusDescription = String.Join (" ", parts, 2, parts.Length - 2);
576 Data.StatusDescription = "";
582 emptyFirstLine = false;
583 if (readState == ReadState.Status) {
584 readState = ReadState.Headers;
585 Data.Headers = new WebHeaderCollection ();
586 ArrayList headers = new ArrayList ();
587 bool finished = false;
589 if (ReadLine (buffer, ref pos, max, ref line) == false)
593 // Empty line: end of headers
598 if (line.Length > 0 && (line [0] == ' ' || line [0] == '\t')) {
599 int count = headers.Count - 1;
603 string prev = (string) headers [count] + line;
604 headers [count] = prev;
613 foreach (string s in headers)
614 Data.Headers.SetInternal (s);
616 if (Data.StatusCode == (int) HttpStatusCode.Continue) {
617 sPoint.SendContinue = true;
621 if (Data.request.ExpectContinue) {
622 Data.request.DoContinueDelegate (Data.StatusCode, Data.Headers);
623 // Prevent double calls when getting the
624 // headers in several packets.
625 Data.request.ExpectContinue = false;
628 readState = ReadState.None;
632 readState = ReadState.Content;
636 } while (emptyFirstLine || isContinue);
641 void InitConnection (object state)
643 HttpWebRequest request = (HttpWebRequest) state;
644 request.WebConnection = this;
649 keepAlive = request.KeepAlive;
650 Data = new WebConnectionData ();
651 Data.request = request;
657 if (status != WebExceptionStatus.Success) {
658 if (!request.Aborted) {
659 request.SetWriteStreamError (status, connect_exception);
665 if (!CreateStream (request)) {
669 WebExceptionStatus st = status;
670 if (Data.Challenge != null)
673 Exception cnc_exc = connect_exception;
674 connect_exception = null;
675 request.SetWriteStreamError (st, cnc_exc);
680 readState = ReadState.None;
681 request.SetWriteStream (new WebConnectionStream (this, request));
684 internal EventHandler SendRequest (HttpWebRequest request)
692 status = WebExceptionStatus.Success;
693 ThreadPool.QueueUserWorkItem (initConn, request);
696 queue.Enqueue (request);
707 if (queue.Count > 0) {
708 SendRequest ((HttpWebRequest) queue.Dequeue ());
713 internal void NextRead ()
716 Data.request.FinishedReading = true;
717 string header = (sPoint.UsesProxy) ? "Proxy-Connection" : "Connection";
718 string cncHeader = (Data.Headers != null) ? Data.Headers [header] : null;
719 bool keepAlive = (Data.Version == HttpVersion.Version11 && this.keepAlive);
720 if (cncHeader != null) {
721 cncHeader = cncHeader.ToLower ();
722 keepAlive = (this.keepAlive && cncHeader.IndexOf ("keep-alive", StringComparison.Ordinal) != -1);
725 if ((socket != null && !socket.Connected) ||
726 (!keepAlive || (cncHeader != null && cncHeader.IndexOf ("close", StringComparison.Ordinal) != -1))) {
731 if (priority_request != null) {
732 SendRequest (priority_request);
733 priority_request = null;
740 static bool ReadLine (byte [] buffer, ref int start, int max, ref string output)
742 bool foundCR = false;
743 StringBuilder text = new StringBuilder ();
746 while (start < max) {
747 c = (int) buffer [start++];
749 if (c == '\n') { // newline
750 if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
755 } else if (foundCR) {
764 text.Append ((char) c);
767 if (c != '\n' && c != '\r')
770 if (text.Length == 0) {
772 return (c == '\n' || c == '\r');
778 output = text.ToString ();
783 internal IAsyncResult BeginRead (HttpWebRequest request, byte [] buffer, int offset, int size, AsyncCallback cb, object state)
786 if (Data.request != request)
787 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
792 IAsyncResult result = null;
793 if (!chunkedRead || chunkStream.WantMore) {
795 result = nstream.BeginRead (buffer, offset, size, cb, state);
797 } catch (Exception) {
798 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked BeginRead");
804 WebAsyncResult wr = new WebAsyncResult (cb, state, buffer, offset, size);
805 wr.InnerAsyncResult = result;
806 if (result == null) {
807 // Will be completed from the data in ChunkStream
808 wr.SetCompleted (true, (Exception) null);
817 internal int EndRead (HttpWebRequest request, IAsyncResult result)
820 if (Data.request != request)
821 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
823 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
827 WebAsyncResult wr = null;
828 IAsyncResult nsAsync = ((WebAsyncResult) result).InnerAsyncResult;
829 if (chunkedRead && (nsAsync is WebAsyncResult)) {
830 wr = (WebAsyncResult) nsAsync;
831 IAsyncResult inner = wr.InnerAsyncResult;
832 if (inner != null && !(inner is WebAsyncResult))
833 nbytes = nstream.EndRead (inner);
834 } else if (!(nsAsync is WebAsyncResult)) {
835 nbytes = nstream.EndRead (nsAsync);
836 wr = (WebAsyncResult) result;
840 bool done = (nbytes == 0);
842 chunkStream.WriteAndReadBack (wr.Buffer, wr.Offset, wr.Size, ref nbytes);
843 if (!done && nbytes == 0 && chunkStream.WantMore)
844 nbytes = EnsureRead (wr.Buffer, wr.Offset, wr.Size);
845 } catch (Exception e) {
846 if (e is WebException)
849 throw new WebException ("Invalid chunked data.", e,
850 WebExceptionStatus.ServerProtocolViolation, null);
853 if ((done || nbytes == 0) && chunkStream.ChunkLeft != 0) {
854 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked EndRead");
855 throw new WebException ("Read error", null, WebExceptionStatus.ReceiveFailure, null);
859 return (nbytes != 0) ? nbytes : -1;
862 // To be called on chunkedRead when we can read no data from the ChunkStream yet
863 int EnsureRead (byte [] buffer, int offset, int size)
865 byte [] morebytes = null;
867 while (nbytes == 0 && chunkStream.WantMore) {
868 int localsize = chunkStream.ChunkLeft;
869 if (localsize <= 0) // not read chunk size yet
871 else if (localsize > 16384)
874 if (morebytes == null || morebytes.Length < localsize)
875 morebytes = new byte [localsize];
877 int nread = nstream.Read (morebytes, 0, localsize);
881 chunkStream.Write (morebytes, 0, nread);
882 nbytes += chunkStream.Read (buffer, offset + nbytes, size - nbytes);
888 bool CompleteChunkedRead()
890 if (!chunkedRead || chunkStream == null)
893 while (chunkStream.WantMore) {
894 int nbytes = nstream.Read (buffer, 0, buffer.Length);
896 return false; // Socket was disconnected
898 chunkStream.Write (buffer, 0, nbytes);
904 internal IAsyncResult BeginWrite (HttpWebRequest request, byte [] buffer, int offset, int size, AsyncCallback cb, object state)
907 if (Data.request != request)
908 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
913 IAsyncResult result = null;
915 result = nstream.BeginWrite (buffer, offset, size, cb, state);
916 } catch (Exception) {
917 status = WebExceptionStatus.SendFailure;
924 internal void EndWrite2 (HttpWebRequest request, IAsyncResult result)
926 if (request.FinishedReading)
930 if (Data.request != request)
931 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
933 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
937 nstream.EndWrite (result);
938 } catch (Exception exc) {
939 status = WebExceptionStatus.SendFailure;
940 if (exc.InnerException != null)
941 throw exc.InnerException;
946 internal bool EndWrite (HttpWebRequest request, IAsyncResult result)
948 if (request.FinishedReading)
952 if (Data.request != request)
953 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
955 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
959 nstream.EndWrite (result);
962 status = WebExceptionStatus.SendFailure;
967 internal int Read (HttpWebRequest request, byte [] buffer, int offset, int size)
970 if (Data.request != request)
971 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
980 result = nstream.Read (buffer, offset, size);
981 done = (result == 0);
986 chunkStream.WriteAndReadBack (buffer, offset, size, ref result);
987 if (!done && result == 0 && chunkStream.WantMore)
988 result = EnsureRead (buffer, offset, size);
989 } catch (Exception e) {
990 HandleError (WebExceptionStatus.ReceiveFailure, e, "chunked Read1");
994 if ((done || result == 0) && chunkStream.WantMore) {
995 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked Read2");
996 throw new WebException ("Read error", null, WebExceptionStatus.ReceiveFailure, null);
999 } catch (Exception e) {
1000 HandleError (WebExceptionStatus.ReceiveFailure, e, "Read");
1006 internal bool Write (HttpWebRequest request, byte [] buffer, int offset, int size, ref string err_msg)
1010 if (Data.request != request)
1011 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
1012 if (nstream == null)
1017 nstream.Write (buffer, offset, size);
1018 // here SSL handshake should have been done
1019 if (ssl && !certsAvailable)
1021 } catch (Exception e) {
1022 err_msg = e.Message;
1023 WebExceptionStatus wes = WebExceptionStatus.SendFailure;
1024 string msg = "Write: " + err_msg;
1025 if (e is WebException) {
1026 HandleError (wes, e, msg);
1030 // if SSL is in use then check for TrustFailure
1031 if (ssl && (bool) piTrustFailure.GetValue (nstream, null)) {
1032 wes = WebExceptionStatus.TrustFailure;
1033 msg = "Trust failure";
1036 HandleError (wes, e, msg);
1042 internal void Close (bool sendNext)
1045 if (nstream != null) {
1052 if (socket != null) {
1059 if (ntlm_authenticated)
1062 Data = new WebConnectionData ();
1068 void Abort (object sender, EventArgs args)
1072 HttpWebRequest req = (HttpWebRequest) sender;
1073 if (Data.request == req) {
1074 if (!req.FinishedReading) {
1075 status = WebExceptionStatus.RequestCanceled;
1077 if (queue.Count > 0) {
1078 Data.request = (HttpWebRequest) queue.Dequeue ();
1079 SendRequest (Data.request);
1085 req.FinishedReading = true;
1086 req.SetResponseError (WebExceptionStatus.RequestCanceled, null, "User aborted");
1087 if (queue.Count > 0 && queue.Peek () == sender) {
1089 } else if (queue.Count > 0) {
1090 object [] old = queue.ToArray ();
1092 for (int i = old.Length - 1; i >= 0; i--) {
1093 if (old [i] != sender)
1094 queue.Enqueue (old [i]);
1101 internal void ResetNtlm ()
1103 ntlm_authenticated = false;
1104 ntlm_credentials = null;
1105 unsafe_sharing = false;
1108 internal bool Busy {
1109 get { lock (this) return busy; }
1112 internal bool Connected {
1115 return (socket != null && socket.Connected);
1120 // -Used for NTLM authentication
1121 internal HttpWebRequest PriorityRequest {
1122 set { priority_request = value; }
1125 internal bool NtlmAuthenticated {
1126 get { return ntlm_authenticated; }
1127 set { ntlm_authenticated = value; }
1130 internal NetworkCredential NtlmCredential {
1131 get { return ntlm_credentials; }
1132 set { ntlm_credentials = value; }
1135 internal bool UnsafeAuthenticatedConnectionSharing {
1136 get { return unsafe_sharing; }
1137 set { unsafe_sharing = value; }