| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- //
- // System.Net.WebConnection
- //
- // Authors:
- // Gonzalo Paniagua Javier ([email protected])
- // Martin Baulig <[email protected]>
- //
- // (C) 2003 Ximian, Inc (http://www.ximian.com)
- // Copyright (c) 2017 Xamarin Inc. (http://www.xamarin.com)
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System.IO;
- using System.Collections;
- using System.Net.Sockets;
- using System.Security.Cryptography.X509Certificates;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Runtime.ExceptionServices;
- using System.Diagnostics;
- using Mono.Net.Security;
- namespace System.Net
- {
- enum ReadState
- {
- None,
- Status,
- Headers,
- Content,
- Aborted
- }
- class WebConnection : IDisposable
- {
- NetworkCredential ntlm_credentials;
- bool ntlm_authenticated;
- bool unsafe_sharing;
- Stream networkStream;
- Socket socket;
- MonoTlsStream monoTlsStream;
- WebConnectionTunnel tunnel;
- int disposed;
- public ServicePoint ServicePoint {
- get;
- }
- #if MONOTOUCH && !MONOTOUCH_TV && !MONOTOUCH_WATCH
- [System.Runtime.InteropServices.DllImport ("__Internal")]
- static extern void xamarin_start_wwan (string uri);
- #endif
- public WebConnection (ServicePoint sPoint)
- {
- ServicePoint = sPoint;
- }
- #if MONO_WEB_DEBUG
- internal static bool EnableWebDebug {
- get; set;
- }
- static WebConnection ()
- {
- if (Environment.GetEnvironmentVariable ("MONO_WEB_DEBUG") != null)
- EnableWebDebug = true;
- }
- #endif
- [Conditional ("MONO_WEB_DEBUG")]
- internal static void Debug (string message, params object[] args)
- {
- #if MONO_WEB_DEBUG
- if (EnableWebDebug)
- Console.Error.WriteLine (string.Format (message, args));
- #endif
- }
- [Conditional ("MONO_WEB_DEBUG")]
- internal static void Debug (string message)
- {
- #if MONO_WEB_DEBUG
- if (EnableWebDebug)
- Console.Error.WriteLine (message);
- #endif
- }
- bool CanReuse ()
- {
- // The real condition is !(socket.Poll (0, SelectMode.SelectRead) || socket.Available != 0)
- // but if there's data pending to read (!) we won't reuse the socket.
- return (socket.Poll (0, SelectMode.SelectRead) == false);
- }
- bool CheckReusable ()
- {
- if (socket != null && socket.Connected) {
- try {
- if (CanReuse ())
- return true;
- } catch { }
- }
- return false;
- }
- async Task Connect (WebOperation operation, CancellationToken cancellationToken)
- {
- IPHostEntry hostEntry = ServicePoint.HostEntry;
- if (hostEntry == null || hostEntry.AddressList.Length == 0) {
- #if MONOTOUCH && !MONOTOUCH_TV && !MONOTOUCH_WATCH
- xamarin_start_wwan (ServicePoint.Address.ToString ());
- hostEntry = ServicePoint.HostEntry;
- if (hostEntry == null) {
- #endif
- throw GetException (ServicePoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
- WebExceptionStatus.NameResolutionFailure, null);
- #if MONOTOUCH && !MONOTOUCH_TV && !MONOTOUCH_WATCH
- }
- #endif
- }
- foreach (IPAddress address in hostEntry.AddressList) {
- operation.ThrowIfDisposed (cancellationToken);
- try {
- socket = new Socket (address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- } catch (Exception se) {
- // The Socket ctor can throw if we run out of FD's
- throw GetException (WebExceptionStatus.ConnectFailure, se);
- }
- IPEndPoint remote = new IPEndPoint (address, ServicePoint.Address.Port);
- socket.NoDelay = !ServicePoint.UseNagleAlgorithm;
- try {
- ServicePoint.KeepAliveSetup (socket);
- } catch {
- // Ignore. Not supported in all platforms.
- }
- if (!ServicePoint.CallEndPointDelegate (socket, remote)) {
- Interlocked.Exchange (ref socket, null)?.Close ();
- continue;
- } else {
- try {
- operation.ThrowIfDisposed (cancellationToken);
- await socket.ConnectAsync (remote).ConfigureAwait (false);
- } catch (ObjectDisposedException) {
- throw;
- } catch (Exception exc) {
- Interlocked.Exchange (ref socket, null)?.Close ();
- throw GetException (WebExceptionStatus.ConnectFailure, exc);
- }
- }
- if (socket != null)
- return;
- }
- throw GetException (WebExceptionStatus.ConnectFailure, null);
- }
- #if MONO_WEB_DEBUG
- static int nextID, nextRequestID;
- readonly int id = ++nextID;
- public int ID => disposed != 0 ? -id : id;
- #else
- internal readonly int ID;
- #endif
- async Task<bool> CreateStream (WebOperation operation, bool reused, CancellationToken cancellationToken)
- {
- #if MONO_WEB_DEBUG
- var requestID = ++nextRequestID;
- #else
- var requestID = 0;
- #endif
- try {
- var stream = new NetworkStream (socket, false);
- Debug ($"WC CREATE STREAM: Cnc={ID} {requestID} {reused} socket={socket.ID}");
- if (operation.Request.Address.Scheme == Uri.UriSchemeHttps) {
- if (!reused || monoTlsStream == null) {
- if (ServicePoint.UseConnect) {
- if (tunnel == null)
- tunnel = new WebConnectionTunnel (operation.Request, ServicePoint.Address);
- await tunnel.Initialize (stream, cancellationToken).ConfigureAwait (false);
- if (!tunnel.Success)
- return false;
- }
- monoTlsStream = new MonoTlsStream (operation.Request, stream);
- networkStream = await monoTlsStream.CreateStream (tunnel, cancellationToken).ConfigureAwait (false);
- }
- return true;
- }
- networkStream = stream;
- return true;
- } catch (Exception ex) {
- ex = HttpWebRequest.FlattenException (ex);
- Debug ($"WC CREATE STREAM EX: Cnc={ID} {requestID} {operation.Aborted} - {ex.Message}");
- if (operation.Aborted || monoTlsStream == null)
- throw GetException (WebExceptionStatus.ConnectFailure, ex);
- throw GetException (monoTlsStream.ExceptionStatus, ex);
- } finally {
- Debug ($"WC CREATE STREAM DONE: Cnc={ID} {requestID}");
- }
- }
- internal async Task<WebRequestStream> InitConnection (WebOperation operation, CancellationToken cancellationToken)
- {
- Debug ($"WC INIT CONNECTION: Cnc={ID} Req={operation.Request.ID} Op={operation.ID}");
- bool reset = true;
- retry:
- operation.ThrowIfClosedOrDisposed (cancellationToken);
- var reused = CheckReusable ();
- Debug ($"WC INIT CONNECTION #1: Cnc={ID} Op={operation.ID} - {reused} - {operation.WriteBuffer != null} {operation.IsNtlmChallenge}");
- if (!reused) {
- CloseSocket ();
- if (reset)
- Reset ();
- try {
- await Connect (operation, cancellationToken).ConfigureAwait (false);
- Debug ($"WC INIT CONNECTION #2: Cnc={ID} Op={operation.ID} {socket.LocalEndPoint}");
- } catch (Exception ex) {
- Debug ($"WC INIT CONNECTION #2 FAILED: Cnc={ID} Op={operation.ID} - {ex.Message}\n{ex}");
- throw;
- }
- }
- var success = await CreateStream (operation, reused, cancellationToken).ConfigureAwait (false);
- Debug ($"WC INIT CONNECTION #3: Cnc={ID} Op={operation.ID} - {success}");
- if (!success) {
- if (tunnel?.Challenge == null)
- throw GetException (WebExceptionStatus.ProtocolError, null);
- if (tunnel.CloseConnection)
- CloseSocket ();
- reset = false;
- goto retry;
- }
- return new WebRequestStream (this, operation, networkStream, tunnel);
- }
- internal static WebException GetException (WebExceptionStatus status, Exception error)
- {
- if (error == null)
- return new WebException ($"Error: {status}", status);
- if (error is WebException wex)
- return wex;
- return new WebException ($"Error: {status} ({error.Message})", status,
- WebExceptionInternalStatus.RequestFatal, error);
- }
- internal static bool ReadLine (byte[] buffer, ref int start, int max, ref string output)
- {
- bool foundCR = false;
- StringBuilder text = new StringBuilder ();
- int c = 0;
- while (start < max) {
- c = (int)buffer[start++];
- if (c == '\n') { // newline
- if ((text.Length > 0) && (text[text.Length - 1] == '\r'))
- text.Length--;
- foundCR = false;
- break;
- } else if (foundCR) {
- text.Length--;
- break;
- }
- if (c == '\r')
- foundCR = true;
- text.Append ((char)c);
- }
- if (c != '\n' && c != '\r')
- return false;
- if (text.Length == 0) {
- output = null;
- return (c == '\n' || c == '\r');
- }
- if (foundCR)
- text.Length--;
- output = text.ToString ();
- return true;
- }
- internal bool CanReuseConnection (WebOperation operation)
- {
- lock (this) {
- if (Closed || currentOperation != null)
- return false;
- if (!NtlmAuthenticated)
- return true;
- NetworkCredential cnc_cred = NtlmCredential;
- var request = operation.Request;
- bool isProxy = (request.Proxy != null && !request.Proxy.IsBypassed (request.RequestUri));
- ICredentials req_icreds = (!isProxy) ? request.Credentials : request.Proxy.Credentials;
- NetworkCredential req_cred = (req_icreds != null) ? req_icreds.GetCredential (request.RequestUri, "NTLM") : null;
- if (cnc_cred == null || req_cred == null ||
- cnc_cred.Domain != req_cred.Domain || cnc_cred.UserName != req_cred.UserName ||
- cnc_cred.Password != req_cred.Password) {
- return false;
- }
- bool req_sharing = request.UnsafeAuthenticatedConnectionSharing;
- bool cnc_sharing = UnsafeAuthenticatedConnectionSharing;
- return !(req_sharing == false || req_sharing != cnc_sharing);
- }
- }
- bool PrepareSharingNtlm (WebOperation operation)
- {
- if (operation == null || !NtlmAuthenticated)
- return true;
- bool needs_reset = false;
- NetworkCredential cnc_cred = NtlmCredential;
- var request = operation.Request;
- bool isProxy = (request.Proxy != null && !request.Proxy.IsBypassed (request.RequestUri));
- ICredentials req_icreds = (!isProxy) ? request.Credentials : request.Proxy.Credentials;
- NetworkCredential req_cred = (req_icreds != null) ? req_icreds.GetCredential (request.RequestUri, "NTLM") : null;
- if (cnc_cred == null || req_cred == null ||
- cnc_cred.Domain != req_cred.Domain || cnc_cred.UserName != req_cred.UserName ||
- cnc_cred.Password != req_cred.Password) {
- needs_reset = true;
- }
- if (!needs_reset) {
- bool req_sharing = request.UnsafeAuthenticatedConnectionSharing;
- bool cnc_sharing = UnsafeAuthenticatedConnectionSharing;
- needs_reset = (req_sharing == false || req_sharing != cnc_sharing);
- }
- return needs_reset;
- }
- void Reset ()
- {
- lock (this) {
- tunnel = null;
- ResetNtlm ();
- }
- }
- void Close (bool reset)
- {
- lock (this) {
- CloseSocket ();
- if (reset)
- Reset ();
- }
- }
- void CloseSocket ()
- {
- lock (this) {
- if (networkStream != null) {
- try {
- networkStream.Dispose ();
- } catch { }
- networkStream = null;
- }
- if (socket != null) {
- try {
- socket.Dispose ();
- } catch { }
- socket = null;
- }
- monoTlsStream = null;
- }
- }
- DateTime idleSince;
- WebOperation currentOperation;
- public bool Closed => disposed != 0;
- public bool Busy {
- get { return currentOperation != null; }
- }
- public DateTime IdleSince {
- get { return idleSince; }
- }
- public bool StartOperation (WebOperation operation, bool reused)
- {
- lock (this) {
- if (Closed)
- return false;
- if (Interlocked.CompareExchange (ref currentOperation, operation, null) != null)
- return false;
- idleSince = DateTime.UtcNow + TimeSpan.FromDays (3650);
- if (reused && !PrepareSharingNtlm (operation)) {
- Debug ($"WC START - CAN'T REUSE: Cnc={ID} Op={operation.ID}");
- Close (true);
- }
- operation.RegisterRequest (ServicePoint, this);
- Debug ($"WC START: Cnc={ID} Op={operation.ID}");
- }
- operation.Run ();
- return true;
- }
- public bool Continue (WebOperation next)
- {
- lock (this) {
- if (Closed)
- return false;
- Debug ($"WC CONTINUE: Cnc={ID} connected={socket?.Connected} next={next?.ID} current={currentOperation?.ID}");
- if (socket == null || !socket.Connected || !PrepareSharingNtlm (next)) {
- Close (true);
- return false;
- }
- currentOperation = next;
- if (next == null)
- return true;
- // Ok, we got another connection. Let's run it!
- next.RegisterRequest (ServicePoint, this);
- }
- next.Run ();
- return true;
- }
- void Dispose (bool disposing)
- {
- if (Interlocked.CompareExchange (ref disposed, 1, 0) != 0)
- return;
- Debug ($"WC DISPOSE: Cnc={ID}");
- Close (true);
- }
- public void Dispose ()
- {
- Dispose (true);
- }
- void ResetNtlm ()
- {
- ntlm_authenticated = false;
- ntlm_credentials = null;
- unsafe_sharing = false;
- }
- internal bool NtlmAuthenticated {
- get { return ntlm_authenticated; }
- set { ntlm_authenticated = value; }
- }
- internal NetworkCredential NtlmCredential {
- get { return ntlm_credentials; }
- set { ntlm_credentials = value; }
- }
- internal bool UnsafeAuthenticatedConnectionSharing {
- get { return unsafe_sharing; }
- set { unsafe_sharing = value; }
- }
- // -
- }
- }
|