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) {
173 socket = new Socket (address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
174 } catch (Exception se) {
175 // The Socket ctor can throw if we run out of FD's
176 if (!request.Aborted)
177 status = WebExceptionStatus.ConnectFailure;
178 connect_exception = se;
181 IPEndPoint remote = new IPEndPoint (address, sPoint.Address.Port);
182 socket.NoDelay = !sPoint.UseNagleAlgorithm;
184 sPoint.KeepAliveSetup (socket);
186 // Ignore. Not supported in all platforms.
189 if (!sPoint.CallEndPointDelegate (socket, remote)) {
192 status = WebExceptionStatus.ConnectFailure;
197 socket.Connect (remote);
198 status = WebExceptionStatus.Success;
200 } catch (ThreadAbortException) {
201 // program exiting...
207 } catch (ObjectDisposedException) {
208 // socket closed from another thread
210 } catch (Exception exc) {
215 if (!request.Aborted)
216 status = WebExceptionStatus.ConnectFailure;
217 connect_exception = exc;
224 static void EnsureSSLStreamAvailable ()
227 if (sslStream != null)
230 #if NET_2_1 && SECURITY_DEP
231 sslStream = typeof (Mono.Security.Protocol.Tls.HttpsClientStream);
233 // HttpsClientStream is an internal glue class in Mono.Security.dll
234 sslStream = Type.GetType ("Mono.Security.Protocol.Tls.HttpsClientStream, " +
235 Consts.AssemblyMono_Security, false);
237 if (sslStream == null) {
238 string msg = "Missing Mono.Security.dll assembly. " +
239 "Support for SSL/TLS is unavailable.";
241 throw new NotSupportedException (msg);
244 piClient = sslStream.GetProperty ("SelectedClientCertificate");
245 piServer = sslStream.GetProperty ("ServerCertificate");
246 piTrustFailure = sslStream.GetProperty ("TrustFailure");
250 bool CreateTunnel (HttpWebRequest request, Stream stream, out byte [] buffer)
252 StringBuilder sb = new StringBuilder ();
253 sb.Append ("CONNECT ");
254 sb.Append (request.Address.Host);
256 sb.Append (request.Address.Port);
257 sb.Append (" HTTP/");
258 if (request.ServicePoint.ProtocolVersion == HttpVersion.Version11)
263 sb.Append ("\r\nHost: ");
264 sb.Append (request.Address.Authority);
265 string challenge = Data.Challenge;
266 Data.Challenge = null;
267 bool have_auth = (request.Headers ["Proxy-Authorization"] != null);
269 sb.Append ("\r\nProxy-Authorization: ");
270 sb.Append (request.Headers ["Proxy-Authorization"]);
271 } else if (challenge != null && Data.StatusCode == 407) {
273 ICredentials creds = request.Proxy.Credentials;
274 Authorization auth = AuthenticationManager.Authenticate (challenge, request, creds);
276 sb.Append ("\r\nProxy-Authorization: ");
277 sb.Append (auth.Message);
280 sb.Append ("\r\n\r\n");
283 byte [] connectBytes = Encoding.Default.GetBytes (sb.ToString ());
284 stream.Write (connectBytes, 0, connectBytes.Length);
287 WebHeaderCollection result = ReadHeaders (request, stream, out buffer, out status);
288 if (!have_auth && result != null && status == 407) { // Needs proxy auth
289 Data.StatusCode = status;
290 Data.Challenge = result ["Proxy-Authenticate"];
292 } else if (status != 200) {
293 string msg = String.Format ("The remote server returned a {0} status code.", status);
294 HandleError (WebExceptionStatus.SecureChannelFailure, null, msg);
298 return (result != null);
301 WebHeaderCollection ReadHeaders (HttpWebRequest request, Stream stream, out byte [] retBuffer, out int status)
306 byte [] buffer = new byte [1024];
307 MemoryStream ms = new MemoryStream ();
308 bool gotStatus = false;
309 WebHeaderCollection headers = null;
312 int n = stream.Read (buffer, 0, 1024);
314 HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders");
318 ms.Write (buffer, 0, n);
321 headers = new WebHeaderCollection ();
322 while (ReadLine (ms.GetBuffer (), ref start, (int) ms.Length, ref str)) {
324 if (ms.Length - start > 0) {
325 retBuffer = new byte [ms.Length - start];
326 Buffer.BlockCopy (ms.GetBuffer (), start, retBuffer, 0, retBuffer.Length);
336 int spaceidx = str.IndexOf (' ');
337 if (spaceidx == -1) {
338 HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders2");
342 status = (int) UInt32.Parse (str.Substring (spaceidx + 1, 3));
348 bool CreateStream (HttpWebRequest request)
351 NetworkStream serverStream = new NetworkStream (socket, false);
353 if (request.Address.Scheme == Uri.UriSchemeHttps) {
355 EnsureSSLStreamAvailable ();
356 if (!reused || nstream == null || nstream.GetType () != sslStream) {
357 byte [] buffer = null;
358 if (sPoint.UseConnect) {
359 bool ok = CreateTunnel (request, serverStream, out buffer);
364 object[] args = new object [4] { serverStream,
365 request.ClientCertificates,
367 nstream = (Stream) Activator.CreateInstance (sslStream, args);
369 SslClientStream scs = (SslClientStream) nstream;
370 var helper = new ServicePointManager.ChainValidationHelper (request);
371 scs.ServerCertValidation2 += new CertificateValidationCallback2 (helper.ValidateChain);
373 certsAvailable = false;
375 // we also need to set ServicePoint.Certificate
376 // and ServicePoint.ClientCertificate but this can
377 // only be done later (after handshake - which is
378 // done only after a read operation).
381 nstream = serverStream;
383 } catch (Exception) {
384 if (!request.Aborted)
385 status = WebExceptionStatus.ConnectFailure;
392 void HandleError (WebExceptionStatus st, Exception e, string where)
396 if (st == WebExceptionStatus.RequestCanceled)
397 Data = new WebConnectionData ();
400 if (e == null) { // At least we now where it comes from
403 throw new Exception ();
405 throw new Exception (new System.Diagnostics.StackTrace ().ToString ());
407 } catch (Exception e2) {
412 HttpWebRequest req = null;
413 if (Data != null && Data.request != null)
418 req.FinishedReading = true;
419 req.SetResponseError (st, e, where);
423 static void ReadDone (IAsyncResult result)
425 WebConnection cnc = (WebConnection) result.AsyncState;
426 WebConnectionData data = cnc.Data;
427 Stream ns = cnc.nstream;
435 nread = ns.EndRead (result);
436 } catch (Exception e) {
437 cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "ReadDone1");
442 cnc.HandleError (WebExceptionStatus.ReceiveFailure, null, "ReadDone2");
447 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3");
452 nread += cnc.position;
453 if (cnc.readState == ReadState.None) {
454 Exception exc = null;
456 pos = cnc.GetResponse (cnc.buffer, nread);
457 } catch (Exception e) {
462 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, exc, "ReadDone4");
467 if (cnc.readState != ReadState.Content) {
469 int max = (est < cnc.buffer.Length) ? cnc.buffer.Length : est;
470 byte [] newBuffer = new byte [max];
471 Buffer.BlockCopy (cnc.buffer, 0, newBuffer, 0, nread);
472 cnc.buffer = newBuffer;
473 cnc.position = nread;
474 cnc.readState = ReadState.None;
481 WebConnectionStream stream = new WebConnectionStream (cnc);
482 bool expect_content = ExpectContent (data.StatusCode, data.request.Method);
483 string tencoding = null;
485 tencoding = data.Headers ["Transfer-Encoding"];
487 cnc.chunkedRead = (tencoding != null && tencoding.IndexOf ("chunked", StringComparison.OrdinalIgnoreCase) != -1);
488 if (!cnc.chunkedRead) {
489 stream.ReadBuffer = cnc.buffer;
490 stream.ReadBufferOffset = pos;
491 stream.ReadBufferSize = nread;
492 stream.CheckResponseInBuffer ();
493 } else if (cnc.chunkStream == null) {
495 cnc.chunkStream = new ChunkStream (cnc.buffer, pos, nread, data.Headers);
496 } catch (Exception e) {
497 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone5");
501 cnc.chunkStream.ResetBuffer ();
503 cnc.chunkStream.Write (cnc.buffer, pos, nread);
504 } catch (Exception e) {
505 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone6");
510 data.stream = stream;
513 stream.ForceCompletion ();
515 data.request.SetResponseData (data);
518 static bool ExpectContent (int statusCode, string method)
520 if (method == "HEAD")
522 return (statusCode >= 200 && statusCode != 204 && statusCode != 304);
525 internal void GetCertificates ()
527 // here the SSL negotiation have been done
528 X509Certificate client = (X509Certificate) piClient.GetValue (nstream, null);
529 X509Certificate server = (X509Certificate) piServer.GetValue (nstream, null);
530 sPoint.SetCertificates (client, server);
531 certsAvailable = (server != null);
534 internal static void InitRead (object state)
536 WebConnection cnc = (WebConnection) state;
537 Stream ns = cnc.nstream;
540 int size = cnc.buffer.Length - cnc.position;
541 ns.BeginRead (cnc.buffer, cnc.position, size, readDoneDelegate, cnc);
542 } catch (Exception e) {
543 cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "InitRead");
547 int GetResponse (byte [] buffer, int max)
552 bool isContinue = false;
553 bool emptyFirstLine = false;
555 if (readState == ReadState.None) {
556 lineok = ReadLine (buffer, ref pos, max, ref line);
561 emptyFirstLine = true;
564 emptyFirstLine = false;
566 readState = ReadState.Status;
568 string [] parts = line.Split (' ');
569 if (parts.Length < 2)
572 if (String.Compare (parts [0], "HTTP/1.1", true) == 0) {
573 Data.Version = HttpVersion.Version11;
574 sPoint.SetVersion (HttpVersion.Version11);
576 Data.Version = HttpVersion.Version10;
577 sPoint.SetVersion (HttpVersion.Version10);
580 Data.StatusCode = (int) UInt32.Parse (parts [1]);
581 if (parts.Length >= 3)
582 Data.StatusDescription = String.Join (" ", parts, 2, parts.Length - 2);
584 Data.StatusDescription = "";
590 emptyFirstLine = false;
591 if (readState == ReadState.Status) {
592 readState = ReadState.Headers;
593 Data.Headers = new WebHeaderCollection ();
594 ArrayList headers = new ArrayList ();
595 bool finished = false;
597 if (ReadLine (buffer, ref pos, max, ref line) == false)
601 // Empty line: end of headers
606 if (line.Length > 0 && (line [0] == ' ' || line [0] == '\t')) {
607 int count = headers.Count - 1;
611 string prev = (string) headers [count] + line;
612 headers [count] = prev;
621 foreach (string s in headers)
622 Data.Headers.SetInternal (s);
624 if (Data.StatusCode == (int) HttpStatusCode.Continue) {
625 sPoint.SendContinue = true;
629 if (Data.request.ExpectContinue) {
630 Data.request.DoContinueDelegate (Data.StatusCode, Data.Headers);
631 // Prevent double calls when getting the
632 // headers in several packets.
633 Data.request.ExpectContinue = false;
636 readState = ReadState.None;
640 readState = ReadState.Content;
644 } while (emptyFirstLine || isContinue);
649 void InitConnection (object state)
651 HttpWebRequest request = (HttpWebRequest) state;
652 request.WebConnection = this;
657 keepAlive = request.KeepAlive;
658 Data = new WebConnectionData ();
659 Data.request = request;
665 if (status != WebExceptionStatus.Success) {
666 if (!request.Aborted) {
667 request.SetWriteStreamError (status, connect_exception);
673 if (!CreateStream (request)) {
677 WebExceptionStatus st = status;
678 if (Data.Challenge != null)
681 Exception cnc_exc = connect_exception;
682 connect_exception = null;
683 request.SetWriteStreamError (st, cnc_exc);
688 readState = ReadState.None;
689 request.SetWriteStream (new WebConnectionStream (this, request));
692 internal EventHandler SendRequest (HttpWebRequest request)
700 status = WebExceptionStatus.Success;
701 ThreadPool.QueueUserWorkItem (initConn, request);
704 queue.Enqueue (request);
715 if (queue.Count > 0) {
716 SendRequest ((HttpWebRequest) queue.Dequeue ());
721 internal void NextRead ()
724 Data.request.FinishedReading = true;
725 string header = (sPoint.UsesProxy) ? "Proxy-Connection" : "Connection";
726 string cncHeader = (Data.Headers != null) ? Data.Headers [header] : null;
727 bool keepAlive = (Data.Version == HttpVersion.Version11 && this.keepAlive);
728 if (cncHeader != null) {
729 cncHeader = cncHeader.ToLower ();
730 keepAlive = (this.keepAlive && cncHeader.IndexOf ("keep-alive", StringComparison.Ordinal) != -1);
733 if ((socket != null && !socket.Connected) ||
734 (!keepAlive || (cncHeader != null && cncHeader.IndexOf ("close", StringComparison.Ordinal) != -1))) {
739 if (priority_request != null) {
740 SendRequest (priority_request);
741 priority_request = null;
748 static bool ReadLine (byte [] buffer, ref int start, int max, ref string output)
750 bool foundCR = false;
751 StringBuilder text = new StringBuilder ();
754 while (start < max) {
755 c = (int) buffer [start++];
757 if (c == '\n') { // newline
758 if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
763 } else if (foundCR) {
772 text.Append ((char) c);
775 if (c != '\n' && c != '\r')
778 if (text.Length == 0) {
780 return (c == '\n' || c == '\r');
786 output = text.ToString ();
791 internal IAsyncResult BeginRead (HttpWebRequest request, byte [] buffer, int offset, int size, AsyncCallback cb, object state)
794 if (Data.request != request)
795 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
800 IAsyncResult result = null;
801 if (!chunkedRead || chunkStream.WantMore) {
803 result = nstream.BeginRead (buffer, offset, size, cb, state);
805 } catch (Exception) {
806 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked BeginRead");
812 WebAsyncResult wr = new WebAsyncResult (cb, state, buffer, offset, size);
813 wr.InnerAsyncResult = result;
814 if (result == null) {
815 // Will be completed from the data in ChunkStream
816 wr.SetCompleted (true, (Exception) null);
825 internal int EndRead (HttpWebRequest request, IAsyncResult result)
828 if (Data.request != request)
829 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
831 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
835 WebAsyncResult wr = null;
836 IAsyncResult nsAsync = ((WebAsyncResult) result).InnerAsyncResult;
837 if (chunkedRead && (nsAsync is WebAsyncResult)) {
838 wr = (WebAsyncResult) nsAsync;
839 IAsyncResult inner = wr.InnerAsyncResult;
840 if (inner != null && !(inner is WebAsyncResult))
841 nbytes = nstream.EndRead (inner);
842 } else if (!(nsAsync is WebAsyncResult)) {
843 nbytes = nstream.EndRead (nsAsync);
844 wr = (WebAsyncResult) result;
848 bool done = (nbytes == 0);
850 chunkStream.WriteAndReadBack (wr.Buffer, wr.Offset, wr.Size, ref nbytes);
851 if (!done && nbytes == 0 && chunkStream.WantMore)
852 nbytes = EnsureRead (wr.Buffer, wr.Offset, wr.Size);
853 } catch (Exception e) {
854 if (e is WebException)
857 throw new WebException ("Invalid chunked data.", e,
858 WebExceptionStatus.ServerProtocolViolation, null);
861 if ((done || nbytes == 0) && chunkStream.ChunkLeft != 0) {
862 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked EndRead");
863 throw new WebException ("Read error", null, WebExceptionStatus.ReceiveFailure, null);
867 return (nbytes != 0) ? nbytes : -1;
870 // To be called on chunkedRead when we can read no data from the ChunkStream yet
871 int EnsureRead (byte [] buffer, int offset, int size)
873 byte [] morebytes = null;
875 while (nbytes == 0 && chunkStream.WantMore) {
876 int localsize = chunkStream.ChunkLeft;
877 if (localsize <= 0) // not read chunk size yet
879 else if (localsize > 16384)
882 if (morebytes == null || morebytes.Length < localsize)
883 morebytes = new byte [localsize];
885 int nread = nstream.Read (morebytes, 0, localsize);
889 chunkStream.Write (morebytes, 0, nread);
890 nbytes += chunkStream.Read (buffer, offset + nbytes, size - nbytes);
896 bool CompleteChunkedRead()
898 if (!chunkedRead || chunkStream == null)
901 while (chunkStream.WantMore) {
902 int nbytes = nstream.Read (buffer, 0, buffer.Length);
904 return false; // Socket was disconnected
906 chunkStream.Write (buffer, 0, nbytes);
912 internal IAsyncResult BeginWrite (HttpWebRequest request, byte [] buffer, int offset, int size, AsyncCallback cb, object state)
915 if (Data.request != request)
916 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
921 IAsyncResult result = null;
923 result = nstream.BeginWrite (buffer, offset, size, cb, state);
924 } catch (Exception) {
925 status = WebExceptionStatus.SendFailure;
932 internal void EndWrite2 (HttpWebRequest request, IAsyncResult result)
934 if (request.FinishedReading)
938 if (Data.request != request)
939 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
941 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
945 nstream.EndWrite (result);
946 } catch (Exception exc) {
947 status = WebExceptionStatus.SendFailure;
948 if (exc.InnerException != null)
949 throw exc.InnerException;
954 internal bool EndWrite (HttpWebRequest request, IAsyncResult result)
956 if (request.FinishedReading)
960 if (Data.request != request)
961 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
963 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
967 nstream.EndWrite (result);
970 status = WebExceptionStatus.SendFailure;
975 internal int Read (HttpWebRequest request, byte [] buffer, int offset, int size)
978 if (Data.request != request)
979 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
988 result = nstream.Read (buffer, offset, size);
989 done = (result == 0);
994 chunkStream.WriteAndReadBack (buffer, offset, size, ref result);
995 if (!done && result == 0 && chunkStream.WantMore)
996 result = EnsureRead (buffer, offset, size);
997 } catch (Exception e) {
998 HandleError (WebExceptionStatus.ReceiveFailure, e, "chunked Read1");
1002 if ((done || result == 0) && chunkStream.WantMore) {
1003 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked Read2");
1004 throw new WebException ("Read error", null, WebExceptionStatus.ReceiveFailure, null);
1007 } catch (Exception e) {
1008 HandleError (WebExceptionStatus.ReceiveFailure, e, "Read");
1014 internal bool Write (HttpWebRequest request, byte [] buffer, int offset, int size, ref string err_msg)
1018 if (Data.request != request)
1019 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
1020 if (nstream == null)
1025 nstream.Write (buffer, offset, size);
1026 // here SSL handshake should have been done
1027 if (ssl && !certsAvailable)
1029 } catch (Exception e) {
1030 err_msg = e.Message;
1031 WebExceptionStatus wes = WebExceptionStatus.SendFailure;
1032 string msg = "Write: " + err_msg;
1033 if (e is WebException) {
1034 HandleError (wes, e, msg);
1038 // if SSL is in use then check for TrustFailure
1039 if (ssl && (bool) piTrustFailure.GetValue (nstream, null)) {
1040 wes = WebExceptionStatus.TrustFailure;
1041 msg = "Trust failure";
1044 HandleError (wes, e, msg);
1050 internal void Close (bool sendNext)
1053 if (nstream != null) {
1060 if (socket != null) {
1067 if (ntlm_authenticated)
1070 Data = new WebConnectionData ();
1076 void Abort (object sender, EventArgs args)
1080 HttpWebRequest req = (HttpWebRequest) sender;
1081 if (Data.request == req) {
1082 if (!req.FinishedReading) {
1083 status = WebExceptionStatus.RequestCanceled;
1085 if (queue.Count > 0) {
1086 Data.request = (HttpWebRequest) queue.Dequeue ();
1087 SendRequest (Data.request);
1093 req.FinishedReading = true;
1094 req.SetResponseError (WebExceptionStatus.RequestCanceled, null, "User aborted");
1095 if (queue.Count > 0 && queue.Peek () == sender) {
1097 } else if (queue.Count > 0) {
1098 object [] old = queue.ToArray ();
1100 for (int i = old.Length - 1; i >= 0; i--) {
1101 if (old [i] != sender)
1102 queue.Enqueue (old [i]);
1109 internal void ResetNtlm ()
1111 ntlm_authenticated = false;
1112 ntlm_credentials = null;
1113 unsafe_sharing = false;
1116 internal bool Busy {
1117 get { lock (this) return busy; }
1120 internal bool Connected {
1123 return (socket != null && socket.Connected);
1128 // -Used for NTLM authentication
1129 internal HttpWebRequest PriorityRequest {
1130 set { priority_request = value; }
1133 internal bool NtlmAuthenticated {
1134 get { return ntlm_authenticated; }
1135 set { ntlm_authenticated = value; }
1138 internal NetworkCredential NtlmCredential {
1139 get { return ntlm_credentials; }
1140 set { ntlm_credentials = value; }
1143 internal bool UnsafeAuthenticatedConnectionSharing {
1144 get { return unsafe_sharing; }
1145 set { unsafe_sharing = value; }