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", new Type [] { typeof (System.Uri) });
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 (ObjectDisposedException) {
438 } catch (Exception e) {
439 if (e.InnerException is ObjectDisposedException)
442 cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "ReadDone1");
447 cnc.HandleError (WebExceptionStatus.ReceiveFailure, null, "ReadDone2");
452 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3");
457 nread += cnc.position;
458 if (cnc.readState == ReadState.None) {
459 Exception exc = null;
461 pos = cnc.GetResponse (cnc.buffer, nread);
462 } catch (Exception e) {
466 if (exc != null || pos == -1) {
467 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, exc, "ReadDone4");
472 if (cnc.readState != ReadState.Content) {
474 int max = (est < cnc.buffer.Length) ? cnc.buffer.Length : est;
475 byte [] newBuffer = new byte [max];
476 Buffer.BlockCopy (cnc.buffer, 0, newBuffer, 0, nread);
477 cnc.buffer = newBuffer;
478 cnc.position = nread;
479 cnc.readState = ReadState.None;
486 WebConnectionStream stream = new WebConnectionStream (cnc);
487 bool expect_content = ExpectContent (data.StatusCode, data.request.Method);
488 string tencoding = null;
490 tencoding = data.Headers ["Transfer-Encoding"];
492 cnc.chunkedRead = (tencoding != null && tencoding.IndexOf ("chunked", StringComparison.OrdinalIgnoreCase) != -1);
493 if (!cnc.chunkedRead) {
494 stream.ReadBuffer = cnc.buffer;
495 stream.ReadBufferOffset = pos;
496 stream.ReadBufferSize = nread;
497 stream.CheckResponseInBuffer ();
498 } else if (cnc.chunkStream == null) {
500 cnc.chunkStream = new ChunkStream (cnc.buffer, pos, nread, data.Headers);
501 } catch (Exception e) {
502 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone5");
506 cnc.chunkStream.ResetBuffer ();
508 cnc.chunkStream.Write (cnc.buffer, pos, nread);
509 } catch (Exception e) {
510 cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone6");
515 data.stream = stream;
518 stream.ForceCompletion ();
520 data.request.SetResponseData (data);
523 static bool ExpectContent (int statusCode, string method)
525 if (method == "HEAD")
527 return (statusCode >= 200 && statusCode != 204 && statusCode != 304);
530 internal void GetCertificates ()
532 // here the SSL negotiation have been done
533 X509Certificate client = (X509Certificate) piClient.GetValue (nstream, null);
534 X509Certificate server = (X509Certificate) piServer.GetValue (nstream, null);
535 sPoint.SetCertificates (client, server);
536 certsAvailable = (server != null);
539 internal static void InitRead (object state)
541 WebConnection cnc = (WebConnection) state;
542 Stream ns = cnc.nstream;
545 int size = cnc.buffer.Length - cnc.position;
546 ns.BeginRead (cnc.buffer, cnc.position, size, readDoneDelegate, cnc);
547 } catch (Exception e) {
548 cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "InitRead");
552 int GetResponse (byte [] buffer, int max)
557 bool isContinue = false;
558 bool emptyFirstLine = false;
560 if (readState == ReadState.None) {
561 lineok = ReadLine (buffer, ref pos, max, ref line);
566 emptyFirstLine = true;
569 emptyFirstLine = false;
571 readState = ReadState.Status;
573 string [] parts = line.Split (' ');
574 if (parts.Length < 2)
577 if (String.Compare (parts [0], "HTTP/1.1", true) == 0) {
578 Data.Version = HttpVersion.Version11;
579 sPoint.SetVersion (HttpVersion.Version11);
581 Data.Version = HttpVersion.Version10;
582 sPoint.SetVersion (HttpVersion.Version10);
585 Data.StatusCode = (int) UInt32.Parse (parts [1]);
586 if (parts.Length >= 3)
587 Data.StatusDescription = String.Join (" ", parts, 2, parts.Length - 2);
589 Data.StatusDescription = "";
595 emptyFirstLine = false;
596 if (readState == ReadState.Status) {
597 readState = ReadState.Headers;
598 Data.Headers = new WebHeaderCollection ();
599 ArrayList headers = new ArrayList ();
600 bool finished = false;
602 if (ReadLine (buffer, ref pos, max, ref line) == false)
606 // Empty line: end of headers
611 if (line.Length > 0 && (line [0] == ' ' || line [0] == '\t')) {
612 int count = headers.Count - 1;
616 string prev = (string) headers [count] + line;
617 headers [count] = prev;
626 foreach (string s in headers)
627 Data.Headers.SetInternal (s);
629 if (Data.StatusCode == (int) HttpStatusCode.Continue) {
630 sPoint.SendContinue = true;
634 if (Data.request.ExpectContinue) {
635 Data.request.DoContinueDelegate (Data.StatusCode, Data.Headers);
636 // Prevent double calls when getting the
637 // headers in several packets.
638 Data.request.ExpectContinue = false;
641 readState = ReadState.None;
645 readState = ReadState.Content;
649 } while (emptyFirstLine || isContinue);
654 void InitConnection (object state)
656 HttpWebRequest request = (HttpWebRequest) state;
657 request.WebConnection = this;
662 keepAlive = request.KeepAlive;
663 Data = new WebConnectionData (request);
669 if (status != WebExceptionStatus.Success) {
670 if (!request.Aborted) {
671 request.SetWriteStreamError (status, connect_exception);
677 if (!CreateStream (request)) {
681 WebExceptionStatus st = status;
682 if (Data.Challenge != null)
685 Exception cnc_exc = connect_exception;
686 connect_exception = null;
687 request.SetWriteStreamError (st, cnc_exc);
692 readState = ReadState.None;
693 request.SetWriteStream (new WebConnectionStream (this, request));
696 internal EventHandler SendRequest (HttpWebRequest request)
704 status = WebExceptionStatus.Success;
705 ThreadPool.QueueUserWorkItem (initConn, request);
708 queue.Enqueue (request);
719 if (queue.Count > 0) {
720 Data.request = (HttpWebRequest) queue.Dequeue ();
721 SendRequest (Data.request);
726 internal void NextRead ()
729 Data.request.FinishedReading = true;
730 string header = (sPoint.UsesProxy) ? "Proxy-Connection" : "Connection";
731 string cncHeader = (Data.Headers != null) ? Data.Headers [header] : null;
732 bool keepAlive = (Data.Version == HttpVersion.Version11 && this.keepAlive);
733 if (cncHeader != null) {
734 cncHeader = cncHeader.ToLower ();
735 keepAlive = (this.keepAlive && cncHeader.IndexOf ("keep-alive", StringComparison.Ordinal) != -1);
738 if ((socket != null && !socket.Connected) ||
739 (!keepAlive || (cncHeader != null && cncHeader.IndexOf ("close", StringComparison.Ordinal) != -1))) {
744 if (priority_request != null) {
745 SendRequest (priority_request);
746 priority_request = null;
753 static bool ReadLine (byte [] buffer, ref int start, int max, ref string output)
755 bool foundCR = false;
756 StringBuilder text = new StringBuilder ();
759 while (start < max) {
760 c = (int) buffer [start++];
762 if (c == '\n') { // newline
763 if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
768 } else if (foundCR) {
777 text.Append ((char) c);
780 if (c != '\n' && c != '\r')
783 if (text.Length == 0) {
785 return (c == '\n' || c == '\r');
791 output = text.ToString ();
796 internal IAsyncResult BeginRead (HttpWebRequest request, byte [] buffer, int offset, int size, AsyncCallback cb, object state)
800 if (Data.request != request)
801 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
807 IAsyncResult result = null;
808 if (!chunkedRead || (!chunkStream.DataAvailable && chunkStream.WantMore)) {
810 result = s.BeginRead (buffer, offset, size, cb, state);
812 } catch (Exception) {
813 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked BeginRead");
819 WebAsyncResult wr = new WebAsyncResult (cb, state, buffer, offset, size);
820 wr.InnerAsyncResult = result;
821 if (result == null) {
822 // Will be completed from the data in ChunkStream
823 wr.SetCompleted (true, (Exception) null);
832 internal int EndRead (HttpWebRequest request, IAsyncResult result)
836 if (Data.request != request)
837 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
839 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
844 WebAsyncResult wr = null;
845 IAsyncResult nsAsync = ((WebAsyncResult) result).InnerAsyncResult;
846 if (chunkedRead && (nsAsync is WebAsyncResult)) {
847 wr = (WebAsyncResult) nsAsync;
848 IAsyncResult inner = wr.InnerAsyncResult;
849 if (inner != null && !(inner is WebAsyncResult))
850 nbytes = s.EndRead (inner);
851 } else if (!(nsAsync is WebAsyncResult)) {
852 nbytes = s.EndRead (nsAsync);
853 wr = (WebAsyncResult) result;
857 bool done = (nbytes == 0);
859 chunkStream.WriteAndReadBack (wr.Buffer, wr.Offset, wr.Size, ref nbytes);
860 if (!done && nbytes == 0 && chunkStream.WantMore)
861 nbytes = EnsureRead (wr.Buffer, wr.Offset, wr.Size);
862 } catch (Exception e) {
863 if (e is WebException)
866 throw new WebException ("Invalid chunked data.", e,
867 WebExceptionStatus.ServerProtocolViolation, null);
870 if ((done || nbytes == 0) && chunkStream.ChunkLeft != 0) {
871 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked EndRead");
872 throw new WebException ("Read error", null, WebExceptionStatus.ReceiveFailure, null);
876 return (nbytes != 0) ? nbytes : -1;
879 // To be called on chunkedRead when we can read no data from the ChunkStream yet
880 int EnsureRead (byte [] buffer, int offset, int size)
882 byte [] morebytes = null;
884 while (nbytes == 0 && chunkStream.WantMore) {
885 int localsize = chunkStream.ChunkLeft;
886 if (localsize <= 0) // not read chunk size yet
888 else if (localsize > 16384)
891 if (morebytes == null || morebytes.Length < localsize)
892 morebytes = new byte [localsize];
894 int nread = nstream.Read (morebytes, 0, localsize);
898 chunkStream.Write (morebytes, 0, nread);
899 nbytes += chunkStream.Read (buffer, offset + nbytes, size - nbytes);
905 bool CompleteChunkedRead()
907 if (!chunkedRead || chunkStream == null)
910 while (chunkStream.WantMore) {
911 int nbytes = nstream.Read (buffer, 0, buffer.Length);
913 return false; // Socket was disconnected
915 chunkStream.Write (buffer, 0, nbytes);
921 internal IAsyncResult BeginWrite (HttpWebRequest request, byte [] buffer, int offset, int size, AsyncCallback cb, object state)
925 if (Data.request != request)
926 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
932 IAsyncResult result = null;
934 result = s.BeginWrite (buffer, offset, size, cb, state);
935 } catch (Exception) {
936 status = WebExceptionStatus.SendFailure;
943 internal void EndWrite2 (HttpWebRequest request, IAsyncResult result)
945 if (request.FinishedReading)
950 if (Data.request != request)
951 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
953 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
959 } catch (Exception exc) {
960 status = WebExceptionStatus.SendFailure;
961 if (exc.InnerException != null)
962 throw exc.InnerException;
967 internal bool EndWrite (HttpWebRequest request, IAsyncResult result)
969 if (request.FinishedReading)
974 if (Data.request != request)
975 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
977 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
985 status = WebExceptionStatus.SendFailure;
990 internal int Read (HttpWebRequest request, byte [] buffer, int offset, int size)
994 if (Data.request != request)
995 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
1005 result = s.Read (buffer, offset, size);
1006 done = (result == 0);
1011 chunkStream.WriteAndReadBack (buffer, offset, size, ref result);
1012 if (!done && result == 0 && chunkStream.WantMore)
1013 result = EnsureRead (buffer, offset, size);
1014 } catch (Exception e) {
1015 HandleError (WebExceptionStatus.ReceiveFailure, e, "chunked Read1");
1019 if ((done || result == 0) && chunkStream.WantMore) {
1020 HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked Read2");
1021 throw new WebException ("Read error", null, WebExceptionStatus.ReceiveFailure, null);
1024 } catch (Exception e) {
1025 HandleError (WebExceptionStatus.ReceiveFailure, e, "Read");
1031 internal bool Write (HttpWebRequest request, byte [] buffer, int offset, int size, ref string err_msg)
1036 if (Data.request != request)
1037 throw new ObjectDisposedException (typeof (NetworkStream).FullName);
1038 if (nstream == null)
1044 s.Write (buffer, offset, size);
1045 // here SSL handshake should have been done
1046 if (ssl && !certsAvailable)
1048 } catch (Exception e) {
1049 err_msg = e.Message;
1050 WebExceptionStatus wes = WebExceptionStatus.SendFailure;
1051 string msg = "Write: " + err_msg;
1052 if (e is WebException) {
1053 HandleError (wes, e, msg);
1057 // if SSL is in use then check for TrustFailure
1058 if (ssl && (bool) piTrustFailure.GetValue (nstream, null)) {
1059 wes = WebExceptionStatus.TrustFailure;
1060 msg = "Trust failure";
1063 HandleError (wes, e, msg);
1069 internal void Close (bool sendNext)
1072 if (nstream != null) {
1079 if (socket != null) {
1086 if (ntlm_authenticated)
1089 Data = new WebConnectionData ();
1095 void Abort (object sender, EventArgs args)
1099 HttpWebRequest req = (HttpWebRequest) sender;
1100 if (Data.request == req || Data.request == null) {
1101 if (!req.FinishedReading) {
1102 status = WebExceptionStatus.RequestCanceled;
1104 if (queue.Count > 0) {
1105 Data.request = (HttpWebRequest) queue.Dequeue ();
1106 SendRequest (Data.request);
1112 req.FinishedReading = true;
1113 req.SetResponseError (WebExceptionStatus.RequestCanceled, null, "User aborted");
1114 if (queue.Count > 0 && queue.Peek () == sender) {
1116 } else if (queue.Count > 0) {
1117 object [] old = queue.ToArray ();
1119 for (int i = old.Length - 1; i >= 0; i--) {
1120 if (old [i] != sender)
1121 queue.Enqueue (old [i]);
1128 internal void ResetNtlm ()
1130 ntlm_authenticated = false;
1131 ntlm_credentials = null;
1132 unsafe_sharing = false;
1135 internal bool Busy {
1136 get { lock (this) return busy; }
1139 internal bool Connected {
1142 return (socket != null && socket.Connected);
1147 // -Used for NTLM authentication
1148 internal HttpWebRequest PriorityRequest {
1149 set { priority_request = value; }
1152 internal bool NtlmAuthenticated {
1153 get { return ntlm_authenticated; }
1154 set { ntlm_authenticated = value; }
1157 internal NetworkCredential NtlmCredential {
1158 get { return ntlm_credentials; }
1159 set { ntlm_credentials = value; }
1162 internal bool UnsafeAuthenticatedConnectionSharing {
1163 get { return unsafe_sharing; }
1164 set { unsafe_sharing = value; }