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.SetSocketOption (SocketOptionLevel.Tcp, SocketOptionName.NoDelay, sPoint.UseNagleAlgorithm ? 0 : 1);
175 socket.NoDelay = !sPoint.UseNagleAlgorithm;
176 if (!sPoint.CallEndPointDelegate (socket, remote)) {
179 status = WebExceptionStatus.ConnectFailure;
184 socket.Connect (remote);
185 status = WebExceptionStatus.Success;
187 } catch (ThreadAbortException) {
188 // program exiting...
194 } catch (ObjectDisposedException) {
195 // socket closed from another thread
197 } catch (Exception exc) {
202 if (!request.Aborted)
203 status = WebExceptionStatus.ConnectFailure;
204 connect_exception = exc;
211 static void EnsureSSLStreamAvailable ()
214 if (sslStream != null)
217 #if NET_2_1 && SECURITY_DEP
218 sslStream = typeof (Mono.Security.Protocol.Tls.HttpsClientStream);
220 // HttpsClientStream is an internal glue class in Mono.Security.dll
221 sslStream = Type.GetType ("Mono.Security.Protocol.Tls.HttpsClientStream, " +
222 Consts.AssemblyMono_Security, false);
224 if (sslStream == null) {
225 string msg = "Missing Mono.Security.dll assembly. " +
226 "Support for SSL/TLS is unavailable.";
228 throw new NotSupportedException (msg);
231 piClient = sslStream.GetProperty ("SelectedClientCertificate");
232 piServer = sslStream.GetProperty ("ServerCertificate");
233 piTrustFailure = sslStream.GetProperty ("TrustFailure");
237 bool CreateTunnel (HttpWebRequest request, Stream stream, out byte [] buffer)
239 StringBuilder sb = new StringBuilder ();
240 sb.Append ("CONNECT ");
241 sb.Append (request.Address.Host);
243 sb.Append (request.Address.Port);
244 sb.Append (" HTTP/");
245 if (request.ServicePoint.ProtocolVersion == HttpVersion.Version11)
250 sb.Append ("\r\nHost: ");
251 sb.Append (request.Address.Authority);
252 string challenge = Data.Challenge;
253 Data.Challenge = null;
254 bool have_auth = (request.Headers ["Proxy-Authorization"] != null);
256 sb.Append ("\r\nProxy-Authorization: ");
257 sb.Append (request.Headers ["Proxy-Authorization"]);
258 } else if (challenge != null && Data.StatusCode == 407) {
260 ICredentials creds = request.Proxy.Credentials;
261 Authorization auth = AuthenticationManager.Authenticate (challenge, request, creds);
263 sb.Append ("\r\nProxy-Authorization: ");
264 sb.Append (auth.Message);
267 sb.Append ("\r\n\r\n");
270 byte [] connectBytes = Encoding.Default.GetBytes (sb.ToString ());
271 stream.Write (connectBytes, 0, connectBytes.Length);
274 WebHeaderCollection result = ReadHeaders (request, stream, out buffer, out status);
275 if (!have_auth && result != null && status == 407) { // Needs proxy auth
276 Data.StatusCode = status;
277 Data.Challenge = result ["Proxy-Authenticate"];
279 } else if (status != 200) {
280 string msg = String.Format ("The remote server returned a {0} status code.", status);
281 HandleError (WebExceptionStatus.SecureChannelFailure, null, msg);
285 return (result != null);
288 WebHeaderCollection ReadHeaders (HttpWebRequest request, Stream stream, out byte [] retBuffer, out int status)
293 byte [] buffer = new byte [1024];
294 MemoryStream ms = new MemoryStream ();
295 bool gotStatus = false;
296 WebHeaderCollection headers = null;
299 int n = stream.Read (buffer, 0, 1024);
301 HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders");
305 ms.Write (buffer, 0, n);
308 headers = new WebHeaderCollection ();
309 while (ReadLine (ms.GetBuffer (), ref start, (int) ms.Length, ref str)) {
311 if (ms.Length - start > 0) {
312 retBuffer = new byte [ms.Length - start];
313 Buffer.BlockCopy (ms.GetBuffer (), start, retBuffer, 0, retBuffer.Length);
323 int spaceidx = str.IndexOf (' ');
324 if (spaceidx == -1) {
325 HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders2");
329 status = (int) UInt32.Parse (str.Substring (spaceidx + 1, 3));
335 bool CreateStream (HttpWebRequest request)
338 NetworkStream serverStream = new NetworkStream (socket, false);
340 if (request.Address.Scheme == Uri.UriSchemeHttps) {
342 EnsureSSLStreamAvailable ();
343 if (!reused || nstream == null || nstream.GetType () != sslStream) {
344 byte [] buffer = null;
345 if (sPoint.UseConnect) {
346 bool ok = CreateTunnel (request, serverStream, out buffer);
351 object[] args = new object [4] { serverStream,
352 request.ClientCertificates,
354 nstream = (Stream) Activator.CreateInstance (sslStream, args);
356 SslClientStream scs = (SslClientStream) nstream;
357 var helper = new ServicePointManager.ChainValidationHelper (request);
358 scs.ServerCertValidation2 += new CertificateValidationCallback2 (helper.ValidateChain);
360 certsAvailable = false;
362 // we also need to set ServicePoint.Certificate
363 // and ServicePoint.ClientCertificate but this can
364 // only be done later (after handshake - which is
365 // done only after a read operation).
368 nstream = serverStream;
370 } catch (Exception) {
371 if (!request.Aborted)
372 status = WebExceptionStatus.ConnectFailure;
379 void HandleError (WebExceptionStatus st, Exception e, string where)
383 if (st == WebExceptionStatus.RequestCanceled)
384 Data = new WebConnectionData ();
387 if (e == null) { // At least we now where it comes from
390 throw new Exception ();
392 throw new Exception (new System.Diagnostics.StackTrace ().ToString ());
394 } catch (Exception e2) {
399 HttpWebRequest req = null;
400 if (Data != null && Data.request != null)
405 req.FinishedReading = true;
406 req.SetResponseError (st, e, where);
410 static void ReadDone (IAsyncResult result)
412 WebConnection cnc = (WebConnection) result.AsyncState;
413 WebConnectionData data = cnc.Data;
414 Stream ns = cnc.nstream;
422 nread = ns.EndRead (result);
423 } catch (Exception e) {
424 cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "ReadDone1");
429 cnc.HandleError (WebExceptionStatus.ReceiveFailure, null, "ReadDone2");
434 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3");
439 nread += cnc.position;
440 if (cnc.readState == ReadState.None) {
441 Exception exc = null;
443 pos = cnc.GetResponse (cnc.buffer, nread);
444 } catch (Exception e) {
449 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, exc, "ReadDone4");
454 if (cnc.readState != ReadState.Content) {
456 int max = (est < cnc.buffer.Length) ? cnc.buffer.Length : est;
457 byte [] newBuffer = new byte [max];
458 Buffer.BlockCopy (cnc.buffer, 0, newBuffer, 0, nread);
459 cnc.buffer = newBuffer;
460 cnc.position = nread;
461 cnc.readState = ReadState.None;
468 WebConnectionStream stream = new WebConnectionStream (cnc);
470 string contentType = data.Headers ["Transfer-Encoding"];
471 cnc.chunkedRead = (contentType != null && contentType.IndexOf ("chunked", StringComparison.OrdinalIgnoreCase) != -1);
472 if (!cnc.chunkedRead) {
473 stream.ReadBuffer = cnc.buffer;
474 stream.ReadBufferOffset = pos;
475 stream.ReadBufferSize = nread;
476 stream.CheckResponseInBuffer ();
477 } else if (cnc.chunkStream == null) {
479 cnc.chunkStream = new ChunkStream (cnc.buffer, pos, nread, data.Headers);
480 } catch (Exception e) {
481 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone5");
485 cnc.chunkStream.ResetBuffer ();
487 cnc.chunkStream.Write (cnc.buffer, pos, nread);
488 } catch (Exception e) {
489 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone6");
494 data.stream = stream;
496 if (!ExpectContent (data.StatusCode) || data.request.Method == "HEAD")
497 stream.ForceCompletion ();
499 data.request.SetResponseData (data);
502 static bool ExpectContent (int statusCode)
504 return (statusCode >= 200 && statusCode != 204 && statusCode != 304);
507 internal void GetCertificates ()
509 // here the SSL negotiation have been done
510 X509Certificate client = (X509Certificate) piClient.GetValue (nstream, null);
511 X509Certificate server = (X509Certificate) piServer.GetValue (nstream, null);
512 sPoint.SetCertificates (client, server);
513 certsAvailable = (server != null);
516 internal static void InitRead (object state)
518 WebConnection cnc = (WebConnection) state;
519 Stream ns = cnc.nstream;
522 int size = cnc.buffer.Length - cnc.position;
523 ns.BeginRead (cnc.buffer, cnc.position, size, readDoneDelegate, cnc);
524 } catch (Exception e) {
525 cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "InitRead");
529 int GetResponse (byte [] buffer, int max)
534 bool isContinue = false;
535 bool emptyFirstLine = false;
537 if (readState == ReadState.None) {
538 lineok = ReadLine (buffer, ref pos, max, ref line);
543 emptyFirstLine = true;
546 emptyFirstLine = false;
548 readState = ReadState.Status;
550 string [] parts = line.Split (' ');
551 if (parts.Length < 2)
554 if (String.Compare (parts [0], "HTTP/1.1", true) == 0) {
555 Data.Version = HttpVersion.Version11;
556 sPoint.SetVersion (HttpVersion.Version11);
558 Data.Version = HttpVersion.Version10;
559 sPoint.SetVersion (HttpVersion.Version10);
562 Data.StatusCode = (int) UInt32.Parse (parts [1]);
563 if (parts.Length >= 3)
564 Data.StatusDescription = String.Join (" ", parts, 2, parts.Length - 2);
566 Data.StatusDescription = "";
572 emptyFirstLine = false;
573 if (readState == ReadState.Status) {
574 readState = ReadState.Headers;
575 Data.Headers = new WebHeaderCollection ();
576 ArrayList headers = new ArrayList ();
577 bool finished = false;
579 if (ReadLine (buffer, ref pos, max, ref line) == false)
583 // Empty line: end of headers
588 if (line.Length > 0 && (line [0] == ' ' || line [0] == '\t')) {
589 int count = headers.Count - 1;
593 string prev = (string) headers [count] + line;
594 headers [count] = prev;
603 foreach (string s in headers)
604 Data.Headers.SetInternal (s);
606 if (Data.StatusCode == (int) HttpStatusCode.Continue) {
607 sPoint.SendContinue = true;
611 if (Data.request.ExpectContinue) {
612 Data.request.DoContinueDelegate (Data.StatusCode, Data.Headers);
613 // Prevent double calls when getting the
614 // headers in several packets.
615 Data.request.ExpectContinue = false;
618 readState = ReadState.None;
622 readState = ReadState.Content;
626 } while (emptyFirstLine || isContinue);
631 void InitConnection (object state)
633 HttpWebRequest request = (HttpWebRequest) state;
634 request.WebConnection = this;
639 keepAlive = request.KeepAlive;
640 Data = new WebConnectionData ();
641 Data.request = request;
647 if (status != WebExceptionStatus.Success) {
648 if (!request.Aborted) {
649 request.SetWriteStreamError (status, connect_exception);
655 if (!CreateStream (request)) {
659 WebExceptionStatus st = status;
660 if (Data.Challenge != null)
663 Exception cnc_exc = connect_exception;
664 connect_exception = null;
665 request.SetWriteStreamError (st, cnc_exc);
670 readState = ReadState.None;
671 request.SetWriteStream (new WebConnectionStream (this, request));
674 internal EventHandler SendRequest (HttpWebRequest request)
682 status = WebExceptionStatus.Success;
683 ThreadPool.QueueUserWorkItem (initConn, request);
686 queue.Enqueue (request);
697 if (queue.Count > 0) {
698 SendRequest ((HttpWebRequest) queue.Dequeue ());
703 internal void NextRead ()
706 Data.request.FinishedReading = true;
707 string header = (sPoint.UsesProxy) ? "Proxy-Connection" : "Connection";
708 string cncHeader = (Data.Headers != null) ? Data.Headers [header] : null;
709 bool keepAlive = (Data.Version == HttpVersion.Version11 && this.keepAlive);
710 if (cncHeader != null) {
711 cncHeader = cncHeader.ToLower ();
712 keepAlive = (this.keepAlive && cncHeader.IndexOf ("keep-alive", StringComparison.Ordinal) != -1);
715 if ((socket != null && !socket.Connected) ||
716 (!keepAlive || (cncHeader != null && cncHeader.IndexOf ("close", StringComparison.Ordinal) != -1))) {
721 if (priority_request != null) {
722 SendRequest (priority_request);
723 priority_request = null;
730 static bool ReadLine (byte [] buffer, ref int start, int max, ref string output)
732 bool foundCR = false;
733 StringBuilder text = new StringBuilder ();
736 while (start < max) {
737 c = (int) buffer [start++];
739 if (c == '\n') { // newline
740 if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
745 } else if (foundCR) {
754 text.Append ((char) c);
757 if (c != '\n' && c != '\r')
760 if (text.Length == 0) {
762 return (c == '\n' || c == '\r');
768 output = text.ToString ();
773 internal IAsyncResult BeginRead (HttpWebRequest request, byte [] buffer, int offset, int size, AsyncCallback cb, object state)
776 if (Data.request != request)
777 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
782 IAsyncResult result = null;
783 if (!chunkedRead || chunkStream.WantMore) {
785 result = nstream.BeginRead (buffer, offset, size, cb, state);
787 } catch (Exception) {
788 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked BeginRead");
794 WebAsyncResult wr = new WebAsyncResult (cb, state, buffer, offset, size);
795 wr.InnerAsyncResult = result;
796 if (result == null) {
797 // Will be completed from the data in ChunkStream
798 wr.SetCompleted (true, (Exception) null);
807 internal int EndRead (HttpWebRequest request, IAsyncResult result)
810 if (Data.request != request)
811 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
813 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
817 WebAsyncResult wr = null;
818 IAsyncResult nsAsync = ((WebAsyncResult) result).InnerAsyncResult;
819 if (chunkedRead && (nsAsync is WebAsyncResult)) {
820 wr = (WebAsyncResult) nsAsync;
821 IAsyncResult inner = wr.InnerAsyncResult;
822 if (inner != null && !(inner is WebAsyncResult))
823 nbytes = nstream.EndRead (inner);
824 } else if (!(nsAsync is WebAsyncResult)) {
825 nbytes = nstream.EndRead (nsAsync);
826 wr = (WebAsyncResult) result;
830 bool done = (nbytes == 0);
832 chunkStream.WriteAndReadBack (wr.Buffer, wr.Offset, wr.Size, ref nbytes);
833 if (!done && nbytes == 0 && chunkStream.WantMore)
834 nbytes = EnsureRead (wr.Buffer, wr.Offset, wr.Size);
835 } catch (Exception e) {
836 if (e is WebException)
839 throw new WebException ("Invalid chunked data.", e,
840 WebExceptionStatus.ServerProtocolViolation, null);
843 if ((done || nbytes == 0) && chunkStream.ChunkLeft != 0) {
844 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked EndRead");
845 throw new WebException ("Read error", null, WebExceptionStatus.ReceiveFailure, null);
849 return (nbytes != 0) ? nbytes : -1;
852 // To be called on chunkedRead when we can read no data from the ChunkStream yet
853 int EnsureRead (byte [] buffer, int offset, int size)
855 byte [] morebytes = null;
857 while (nbytes == 0 && chunkStream.WantMore) {
858 int localsize = chunkStream.ChunkLeft;
859 if (localsize <= 0) // not read chunk size yet
861 else if (localsize > 16384)
864 if (morebytes == null || morebytes.Length < localsize)
865 morebytes = new byte [localsize];
867 int nread = nstream.Read (morebytes, 0, localsize);
871 chunkStream.Write (morebytes, 0, nread);
872 nbytes += chunkStream.Read (buffer, offset + nbytes, size - nbytes);
878 bool CompleteChunkedRead()
880 if (!chunkedRead || chunkStream == null)
883 while (chunkStream.WantMore) {
884 int nbytes = nstream.Read (buffer, 0, buffer.Length);
886 return false; // Socket was disconnected
888 chunkStream.Write (buffer, 0, nbytes);
894 internal IAsyncResult BeginWrite (HttpWebRequest request, byte [] buffer, int offset, int size, AsyncCallback cb, object state)
897 if (Data.request != request)
898 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
903 IAsyncResult result = null;
905 result = nstream.BeginWrite (buffer, offset, size, cb, state);
906 } catch (Exception) {
907 status = WebExceptionStatus.SendFailure;
914 internal void EndWrite2 (HttpWebRequest request, IAsyncResult result)
916 if (request.FinishedReading)
920 if (Data.request != request)
921 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
923 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
927 nstream.EndWrite (result);
928 } catch (Exception exc) {
929 status = WebExceptionStatus.SendFailure;
930 if (exc.InnerException != null)
931 throw exc.InnerException;
936 internal bool EndWrite (HttpWebRequest request, IAsyncResult result)
938 if (request.FinishedReading)
942 if (Data.request != request)
943 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
945 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
949 nstream.EndWrite (result);
952 status = WebExceptionStatus.SendFailure;
957 internal int Read (HttpWebRequest request, byte [] buffer, int offset, int size)
960 if (Data.request != request)
961 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
970 result = nstream.Read (buffer, offset, size);
971 done = (result == 0);
976 chunkStream.WriteAndReadBack (buffer, offset, size, ref result);
977 if (!done && result == 0 && chunkStream.WantMore)
978 result = EnsureRead (buffer, offset, size);
979 } catch (Exception e) {
980 HandleError (WebExceptionStatus.ReceiveFailure, e, "chunked Read1");
984 if ((done || result == 0) && chunkStream.WantMore) {
985 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked Read2");
986 throw new WebException ("Read error", null, WebExceptionStatus.ReceiveFailure, null);
989 } catch (Exception e) {
990 HandleError (WebExceptionStatus.ReceiveFailure, e, "Read");
996 internal bool Write (HttpWebRequest request, byte [] buffer, int offset, int size, ref string err_msg)
1000 if (Data.request != request)
1001 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
1002 if (nstream == null)
1007 nstream.Write (buffer, offset, size);
1008 // here SSL handshake should have been done
1009 if (ssl && !certsAvailable)
1011 } catch (Exception e) {
1012 err_msg = e.Message;
1013 WebExceptionStatus wes = WebExceptionStatus.SendFailure;
1014 string msg = "Write: " + err_msg;
1015 if (e is WebException) {
1016 HandleError (wes, e, msg);
1020 // if SSL is in use then check for TrustFailure
1021 if (ssl && (bool) piTrustFailure.GetValue (nstream, null)) {
1022 wes = WebExceptionStatus.TrustFailure;
1023 msg = "Trust failure";
1026 HandleError (wes, e, msg);
1032 internal void Close (bool sendNext)
1035 if (nstream != null) {
1042 if (socket != null) {
1049 if (ntlm_authenticated)
1052 Data = new WebConnectionData ();
1058 void Abort (object sender, EventArgs args)
1062 HttpWebRequest req = (HttpWebRequest) sender;
1063 if (Data.request == req) {
1064 if (!req.FinishedReading) {
1065 status = WebExceptionStatus.RequestCanceled;
1067 if (queue.Count > 0) {
1068 Data.request = (HttpWebRequest) queue.Dequeue ();
1069 SendRequest (Data.request);
1075 req.FinishedReading = true;
1076 req.SetResponseError (WebExceptionStatus.RequestCanceled, null, "User aborted");
1077 if (queue.Count > 0 && queue.Peek () == sender) {
1079 } else if (queue.Count > 0) {
1080 object [] old = queue.ToArray ();
1082 for (int i = old.Length - 1; i >= 0; i--) {
1083 if (old [i] != sender)
1084 queue.Enqueue (old [i]);
1091 internal void ResetNtlm ()
1093 ntlm_authenticated = false;
1094 ntlm_credentials = null;
1095 unsafe_sharing = false;
1098 internal bool Busy {
1099 get { lock (this) return busy; }
1102 internal bool Connected {
1105 return (socket != null && socket.Connected);
1110 // -Used for NTLM authentication
1111 internal HttpWebRequest PriorityRequest {
1112 set { priority_request = value; }
1115 internal bool NtlmAuthenticated {
1116 get { return ntlm_authenticated; }
1117 set { ntlm_authenticated = value; }
1120 internal NetworkCredential NtlmCredential {
1121 get { return ntlm_credentials; }
1122 set { ntlm_credentials = value; }
1125 internal bool UnsafeAuthenticatedConnectionSharing {
1126 get { return unsafe_sharing; }
1127 set { unsafe_sharing = value; }