// // this method runs in the context of the user Begin call . // private static void WrappedGetRequestStreamCallback(object state) { GlobalLog.Enter("FileWebRequest::GetRequestStreamCallback"); ContextAwareResult asyncResult = (ContextAwareResult)state; FileWebRequest request = (FileWebRequest)asyncResult.AsyncObject; try { if (request.m_stream == null) { request.m_stream = new FileWebStream(request, request.m_uri.LocalPath, FileMode.Create, FileAccess.Write, FileShare.Read); request.m_fileAccess = FileAccess.Write; request.m_writing = true; asyncResult.InvokeCallback(request.m_stream); GlobalLog.Leave("FileWebRequest::GetRequestStreamCallback"); } } catch (Exception e) { if (asyncResult.IsCompleted || NclUtilities.IsFatal(e)) { throw; } Exception ex = new WebException(e.Message, e); GlobalLog.LeaveException("FileWebRequest::GetRequestStreamCallback", ex); asyncResult.InvokeCallback(ex); } }
public override WebResponse GetResponse() { GlobalLog.Enter("FileWebRequest::GetResponse"); m_syncHint = true; IAsyncResult result; try { result = BeginGetResponse(null, null); if ((Timeout != System.Threading.Timeout.Infinite) && !result.IsCompleted) { if (!result.AsyncWaitHandle.WaitOne(Timeout, false) || !result.IsCompleted) { if (m_response != null) { m_response.Close(); } Exception e = new WebException(NetRes.GetWebStatusString(WebExceptionStatus.Timeout), WebExceptionStatus.Timeout); GlobalLog.LeaveException("FileWebRequest::GetResponse", e); throw e; } } } catch (Exception exception) { if (Logging.On) { Logging.Exception(Logging.Web, this, "GetResponse", exception); } throw; } finally { GlobalLog.Leave("FileWebRequest::GetResponse"); } return(EndGetResponse(result)); }
public override WebResponse EndGetResponse(IAsyncResult asyncResult) { GlobalLog.Enter("FileWebRequest::EndGetResponse"); WebResponse response; try { ContextAwareResult ar = asyncResult as ContextAwareResult; if (asyncResult == null || ar == null) { Exception e = asyncResult == null? new ArgumentNullException("asyncResult"): new ArgumentException(SR.GetString(SR.InvalidAsyncResult), "asyncResult"); GlobalLog.LeaveException("FileWebRequest::EndGetRequestStream", e); throw e; } object result = ar.InternalWaitForCompletion(); if (result is Exception) { throw (Exception)result; } response = (WebResponse)result; m_readPending = false; } catch (Exception exception) { if (Logging.On) { Logging.Exception(Logging.Web, this, "EndGetResponse", exception); } throw; } finally { GlobalLog.Leave("FileWebRequest::EndGetResponse"); } return(response); }
private static void GetRequestStreamCallback(object state) { GlobalLog.Enter("FileWebRequest::GetRequestStreamCallback"); LazyAsyncResult asyncResult = (LazyAsyncResult)state; FileWebRequest request = (FileWebRequest)asyncResult.AsyncObject; try { if (request.m_stream == null) { request.m_stream = new FileWebStream(request, request.m_uri.LocalPath, FileMode.Create, FileAccess.Write, FileShare.Read); request.m_fileAccess = FileAccess.Write; request.m_writing = true; } } catch (Exception e) { // any exceptions previously thrown must be passed to the callback Exception ex = new WebException(e.Message, e); GlobalLog.LeaveException("FileWebRequest::GetRequestStreamCallback", ex); // if the callback throws, correct behavior is to crash the process asyncResult.InvokeCallback(ex); return; } // if the callback throws, correct behavior is to crash the process asyncResult.InvokeCallback(request.m_stream); GlobalLog.Leave("FileWebRequest::GetRequestStreamCallback"); }
// methods /// <include file='doc\filewebrequest.uex' path='docs/doc[@for="FileWebRequest.BeginGetRequestStream"]/*' /> public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state) { GlobalLog.Enter("FileWebRequest::BeginGetRequestStream"); if (!CanGetRequestStream()) { Exception e = new ProtocolViolationException(SR.GetString(SR.net_nouploadonget)); GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e); throw e; } if (m_response != null) { Exception e = new InvalidOperationException(SR.GetString(SR.net_reqsubmitted)); GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e); throw e; } lock (this) { if (m_writePending) { Exception e = new InvalidOperationException(SR.GetString(SR.net_repcall)); GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e); throw e; } m_writePending = true; } m_GetRequestStreamDelegate = new AsyncGetRequestStream(InternalGetRequestStream); GlobalLog.Leave("FileWebRequest::BeginGetRequestStream"); return(m_GetRequestStreamDelegate.BeginInvoke(callback, state)); }
public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state) { GlobalLog.Enter("FileWebRequest::BeginGetRequestStream"); try { if (Aborted) { throw ExceptionHelper.RequestAbortedException; } if (!CanGetRequestStream()) { Exception e = new ProtocolViolationException(SR.GetString(SR.net_nouploadonget)); GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e); throw e; } if (m_response != null) { Exception e = new InvalidOperationException(SR.GetString(SR.net_reqsubmitted)); GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e); throw e; } lock (this) { if (m_writePending) { Exception e = new InvalidOperationException(SR.GetString(SR.net_repcall)); GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e); throw e; } m_writePending = true; } //we need to force the capture of the identity and context to make sure the //posted callback doesn't inavertently gain access to something it shouldn't. m_ReadAResult = new LazyAsyncResult(this, state, callback); ThreadPool.QueueUserWorkItem(s_GetRequestStreamCallback, m_ReadAResult); } catch (Exception exception) { if (Logging.On) { Logging.Exception(Logging.Web, this, "BeginGetRequestStream", exception); } throw; } finally { GlobalLog.Leave("FileWebRequest::BeginGetRequestSteam"); } string suri; if (FrameworkEventSource.Log.IsEnabled(EventLevel.Verbose, FrameworkEventSource.Keywords.NetClient)) { suri = this.RequestUri.ToString(); } else { suri = this.RequestUri.OriginalString; } FrameworkEventSource.Log.BeginGetRequestStream(this, suri); return(m_ReadAResult); }
public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state) { GlobalLog.Enter("FileWebRequest::BeginGetRequestStream"); #if !MONO bool success = true; #endif try { if (Aborted) { throw ExceptionHelper.RequestAbortedException; } if (!CanGetRequestStream()) { Exception e = new ProtocolViolationException(SR.GetString(SR.net_nouploadonget)); GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e); throw e; } if (m_response != null) { Exception e = new InvalidOperationException(SR.GetString(SR.net_reqsubmitted)); GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e); throw e; } lock (this) { if (m_writePending) { Exception e = new InvalidOperationException(SR.GetString(SR.net_repcall)); GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e); throw e; } m_writePending = true; } //we need to force the capture of the identity and context to make sure the //posted callback doesn't inavertently gain access to something it shouldn't. m_ReadAResult = new LazyAsyncResult(this, state, callback); ThreadPool.QueueUserWorkItem(s_GetRequestStreamCallback, m_ReadAResult); } catch (Exception exception) { #if !MONO success = false; #endif if (Logging.On) { Logging.Exception(Logging.Web, this, "BeginGetRequestStream", exception); } throw; } finally { #if !MONO if (FrameworkEventSource.Log.IsEnabled()) { LogBeginGetRequestStream(success, synchronous: false); } #endif GlobalLog.Leave("FileWebRequest::BeginGetRequestSteam"); } return(m_ReadAResult); }
public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state) { GlobalLog.Enter("FileWebRequest::BeginGetRequestStream"); try { if (Aborted) { throw ExceptionHelper.RequestAbortedException; } if (!CanGetRequestStream()) { Exception e = new ProtocolViolationException(SR.GetString(SR.net_nouploadonget)); GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e); throw e; } if (m_response != null) { Exception e = new InvalidOperationException(SR.GetString(SR.net_reqsubmitted)); GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e); throw e; } lock (this) { if (m_writePending) { Exception e = new InvalidOperationException(SR.GetString(SR.net_repcall)); GlobalLog.LeaveException("FileWebRequest::BeginGetRequestStream", e); throw e; } m_writePending = true; } //we need to force the capture of the identity and context to make sure the //posted callback doesn't inavertently gain access to something it shouldn't. m_ReadAResult = new ContextAwareResult(true, true, true, this, state, callback); lock (m_ReadAResult.StartPostingAsyncOp()) { ThreadPool.UnsafeQueueUserWorkItem(s_GetRequestStreamCallback, m_ReadAResult); m_ReadAResult.FinishPostingAsyncOp(); } } catch (Exception exception) { if (Logging.On) { Logging.Exception(Logging.Web, this, "BeginGetRequestStream", exception); } throw; } finally { GlobalLog.Leave("FileWebRequest::BeginGetRequestSteam"); } return(m_ReadAResult); }
public override WebResponse EndGetResponse(IAsyncResult asyncResult) { GlobalLog.Enter("FileWebRequest::EndGetResponse"); WebResponse response; #if !MONO bool success = false; #endif try { LazyAsyncResult ar = asyncResult as LazyAsyncResult; if (asyncResult == null || ar == null) { Exception e = asyncResult == null? new ArgumentNullException("asyncResult"): new ArgumentException(SR.GetString(SR.InvalidAsyncResult), "asyncResult"); GlobalLog.LeaveException("FileWebRequest::EndGetRequestStream", e); throw e; } object result = ar.InternalWaitForCompletion(); if (result is Exception) { throw (Exception)result; } response = (WebResponse)result; m_readPending = false; #if !MONO success = true; #endif } catch (Exception exception) { if (Logging.On) { Logging.Exception(Logging.Web, this, "EndGetResponse", exception); } throw; } finally { GlobalLog.Leave("FileWebRequest::EndGetResponse"); #if !MONO // there is no statusCode in FileWebRequest object, defaulting it to zero. if (FrameworkEventSource.Log.IsEnabled()) { LogEndGetResponse(success, synchronous: false, statusCode: 0); } #endif } return(response); }
public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state) { GlobalLog.Enter("FileWebRequest::BeginGetResponse"); try { if (Aborted) { throw ExceptionHelper.RequestAbortedException; } lock (this) { if (m_readPending) { Exception e = new InvalidOperationException(SR.GetString(SR.net_repcall)); GlobalLog.LeaveException("FileWebRequest::BeginGetResponse", e); throw e; } m_readPending = true; } m_WriteAResult = new LazyAsyncResult(this, state, callback); ThreadPool.QueueUserWorkItem(s_GetResponseCallback, m_WriteAResult); } catch (Exception exception) { if (Logging.On) { Logging.Exception(Logging.Web, this, "BeginGetResponse", exception); } throw; } finally { GlobalLog.Leave("FileWebRequest::BeginGetResponse"); } string suri; if (FrameworkEventSource.Log.IsEnabled(EventLevel.Verbose, FrameworkEventSource.Keywords.NetClient)) { suri = this.RequestUri.ToString(); } else { suri = this.RequestUri.OriginalString; } if (FrameworkEventSource.Log.IsEnabled()) { LogBeginGetResponse(suri); } return(m_WriteAResult); }
/// <include file='doc\filewebrequest.uex' path='docs/doc[@for="FileWebRequest.BeginGetResponse"]/*' /> public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state) { GlobalLog.Enter("FileWebRequest::BeginGetResponse"); lock (this) { if (m_readPending) { Exception e = new InvalidOperationException(SR.GetString(SR.net_repcall)); GlobalLog.LeaveException("FileWebRequest::BeginGetResponse", e); throw e; } m_readPending = true; } m_GetResponseDelegate = new AsyncGetResponse(InternalGetResponse); GlobalLog.Leave("FileWebRequest::BeginGetResponse"); return(m_GetResponseDelegate.BeginInvoke(callback, state)); }
public override Stream EndGetRequestStream(IAsyncResult asyncResult) { GlobalLog.Enter("FileWebRequest::EndGetRequestStream"); Stream stream; #if !MONO bool success = false; #endif try { LazyAsyncResult ar = asyncResult as LazyAsyncResult; if (asyncResult == null || ar == null) { Exception e = asyncResult == null? new ArgumentNullException("asyncResult"): new ArgumentException(SR.GetString(SR.InvalidAsyncResult), "asyncResult"); GlobalLog.LeaveException("FileWebRequest::EndGetRequestStream", e); throw e; } object result = ar.InternalWaitForCompletion(); if (result is Exception) { throw (Exception)result; } stream = (Stream)result; m_writePending = false; #if !MONO success = true; #endif } catch (Exception exception) { if (Logging.On) { Logging.Exception(Logging.Web, this, "EndGetRequestStream", exception); } throw; } finally { GlobalLog.Leave("FileWebRequest::EndGetRequestStream"); #if !MONO if (FrameworkEventSource.Log.IsEnabled()) { LogEndGetRequestStream(success, synchronous: false); } #endif } return(stream); }
public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state) { GlobalLog.Enter("FileWebRequest::BeginGetResponse"); #if !MONO bool success = true; #endif try { if (Aborted) { throw ExceptionHelper.RequestAbortedException; } lock (this) { if (m_readPending) { Exception e = new InvalidOperationException(SR.GetString(SR.net_repcall)); GlobalLog.LeaveException("FileWebRequest::BeginGetResponse", e); throw e; } m_readPending = true; } m_WriteAResult = new LazyAsyncResult(this, state, callback); ThreadPool.QueueUserWorkItem(s_GetResponseCallback, m_WriteAResult); } catch (Exception exception) { #if !MONO success = false; #endif if (Logging.On) { Logging.Exception(Logging.Web, this, "BeginGetResponse", exception); } throw; } finally { #if !MONO if (FrameworkEventSource.Log.IsEnabled()) { LogBeginGetResponse(success, synchronous: false); } #endif GlobalLog.Leave("FileWebRequest::BeginGetResponse"); } return(m_WriteAResult); }
// constructors internal FileWebResponse(FileWebRequest request, Uri uri, FileAccess access, bool asyncHint) { GlobalLog.Enter("FileWebResponse::FileWebResponse", "uri=" + uri + ", access=" + access + ", asyncHint=" + asyncHint); try { m_fileAccess = access; if (access == FileAccess.Write) { m_stream = Stream.Null; } else { // // apparently, specifying async when the stream will be read // synchronously, or vice versa, can lead to a 10x perf hit. // While we don't know how the app will read the stream, we // use the hint from whether the app called BeginGetResponse // or GetResponse to supply the async flag to the stream ctor // m_stream = new FileWebStream(request, uri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultFileStreamBufferSize, asyncHint ); m_contentLength = m_stream.Length; } m_headers = new WebHeaderCollection(WebHeaderCollectionType.FileWebResponse); m_headers.AddInternal(HttpKnownHeaderNames.ContentLength, m_contentLength.ToString(NumberFormatInfo.InvariantInfo)); m_headers.AddInternal(HttpKnownHeaderNames.ContentType, DefaultFileContentType); m_uri = uri; } catch (Exception e) { Exception ex = new WebException(e.Message, e, WebExceptionStatus.ConnectFailure, null); GlobalLog.LeaveException("FileWebResponse::FileWebResponse", ex); throw ex; } catch { Exception ex = new WebException(string.Empty, new Exception(SR.GetString(SR.net_nonClsCompliantException)), WebExceptionStatus.ConnectFailure, null); GlobalLog.LeaveException("FileWebResponse::FileWebResponse", ex); throw ex; } GlobalLog.Leave("FileWebResponse::FileWebResponse"); }
internal static byte[] EncodingRightGetBytes(string rawString) { GlobalLog.Enter("BasicClient::EncodingRightGetBytes", "[" + rawString.Length.ToString() + ":" + rawString + "]"); // // in order to know if there will not be any '?' translations (which means // we should use the Default Encoding) we need to attempt encoding and then decoding. // <STRIP>this is a limitation only on win9x, if we ever drop support for this platform there might be // a more efficient way of doing this.</STRIP> // GlobalLog.Print("BasicClient::EncodingRightGetBytes(): Default Encoding is:" + Encoding.Default.EncodingName); byte[] bytes = Encoding.Default.GetBytes(rawString); string rawCopy = Encoding.Default.GetString(bytes); bool canMapToCurrentCodePage = string.Compare(rawString, rawCopy, StringComparison.Ordinal) == 0; GlobalLog.Print("BasicClient::EncodingRightGetBytes(): canMapToCurrentCodePage:" + canMapToCurrentCodePage.ToString()); //<STRIP> // if mapping to the current code page leaves characters out of the // [0x00, 0xFF] range, then we need to use the new encoding that IIS6.0 // will support. do it when they decide it's good enough. // </STRIP> if (!canMapToCurrentCodePage) { //<STRIP> // for now throw. when IIS 6.0 adds support test it. //</STRIP> GlobalLog.LeaveException("BasicClient::EncodingRightGetBytes", ExceptionHelper.MethodNotSupportedException); throw ExceptionHelper.MethodNotSupportedException; /* * GlobalLog.Print("BasicClient::EncodingRightGetBytes(): using:" + Encoding.UTF8.EncodingName); * bytes = Encoding.UTF8.GetBytes(rawString); * * string blob = "=?utf-8?B?" + Convert.ToBase64String(bytes) + "?="; * bytes = Encoding.ASCII.GetBytes(blob); */ } GlobalLog.Dump(bytes); GlobalLog.Leave("BasicClient::EncodingRightGetBytes", bytes.Length.ToString()); return(bytes); }
internal static byte[] EncodingRightGetBytes(string rawString) { GlobalLog.Enter("BasicClient::EncodingRightGetBytes", "[" + rawString.Length.ToString() + ":" + rawString + "]"); // // in order to know if there will not be any '?' translations (which means // we should use the Default Encoding) we need to attempt encoding and then decoding. // < GlobalLog.Print("BasicClient::EncodingRightGetBytes(): Default Encoding is:" + Encoding.Default.EncodingName); byte[] bytes = Encoding.Default.GetBytes(rawString); string rawCopy = Encoding.Default.GetString(bytes); bool canMapToCurrentCodePage = string.Compare(rawString, rawCopy, StringComparison.Ordinal) == 0; GlobalLog.Print("BasicClient::EncodingRightGetBytes(): canMapToCurrentCodePage:" + canMapToCurrentCodePage.ToString()); //< if (!canMapToCurrentCodePage) { //< GlobalLog.LeaveException("BasicClient::EncodingRightGetBytes", ExceptionHelper.MethodNotSupportedException); throw ExceptionHelper.MethodNotSupportedException; /* * GlobalLog.Print("BasicClient::EncodingRightGetBytes(): using:" + Encoding.UTF8.EncodingName); * bytes = Encoding.UTF8.GetBytes(rawString); * * string blob = "=?utf-8?B?" + Convert.ToBase64String(bytes) + "?="; * bytes = Encoding.ASCII.GetBytes(blob); */ } GlobalLog.Dump(bytes); GlobalLog.Leave("BasicClient::EncodingRightGetBytes", bytes.Length.ToString()); return(bytes); }
private static void GetResponseCallback(object state) { GlobalLog.Enter("FileWebRequest::GetResponseCallback"); LazyAsyncResult asyncResult = (LazyAsyncResult)state; FileWebRequest request = (FileWebRequest)asyncResult.AsyncObject; if (request.m_writePending || request.m_writing) { lock (request) { if (request.m_writePending || request.m_writing) { request.m_readerEvent = new ManualResetEvent(false); } } } if (request.m_readerEvent != null) { request.m_readerEvent.WaitOne(); } try { if (request.m_response == null) { request.m_response = new FileWebResponse(request, request.m_uri, request.m_fileAccess, !request.m_syncHint); } } catch (Exception e) { // any exceptions previously thrown must be passed to the callback Exception ex = new WebException(e.Message, e); GlobalLog.LeaveException("FileWebRequest::GetResponseCallback", ex); // if the callback throws, correct behavior is to crash the process asyncResult.InvokeCallback(ex); return; } // if the callback throws, the correct behavior is to crash the process asyncResult.InvokeCallback(request.m_response); GlobalLog.Leave("FileWebRequest::GetResponseCallback"); }
/// <include file='doc\filewebrequest.uex' path='docs/doc[@for="FileWebRequest.EndGetResponse"]/*' /> public override WebResponse EndGetResponse(IAsyncResult asyncResult) { GlobalLog.Enter("FileWebRequest::EndGetResponse"); if (asyncResult == null) { Exception e = new ArgumentNullException("asyncResult"); GlobalLog.LeaveException("FileWebRequest::EndGetResponse", e); throw e; } if (!asyncResult.IsCompleted) { asyncResult.AsyncWaitHandle.WaitOne(); } WebResponse response = m_GetResponseDelegate.EndInvoke(asyncResult); m_readPending = false; GlobalLog.Leave("FileWebRequest::EndGetResponse"); return(response); }
/// <include file='doc\filewebrequest.uex' path='docs/doc[@for="FileWebRequest.EndGetRequestStream"]/*' /> public override Stream EndGetRequestStream(IAsyncResult asyncResult) { GlobalLog.Enter("FileWebRequest::EndGetRequestStream"); if (asyncResult == null) { Exception e = new ArgumentNullException("asyncResult"); GlobalLog.LeaveException("FileWebRequest::EndGetRequestStream", e); throw e; } if (!asyncResult.IsCompleted) { asyncResult.AsyncWaitHandle.WaitOne(); } Stream stream = m_GetRequestStreamDelegate.EndInvoke(asyncResult); m_writePending = false; GlobalLog.Leave("FileWebRequest::EndGetRequestStream"); return(stream); }
// // this method runs in the context of the user Begin call . // private static void WrappedGetResponseCallback(object state) { GlobalLog.Enter("FileWebRequest::GetResponseCallback"); ContextAwareResult asyncResult = (ContextAwareResult)state; FileWebRequest request = (FileWebRequest)asyncResult.AsyncObject; if (request.m_writePending || request.m_writing) { lock (request) { if (request.m_writePending || request.m_writing) { request.m_readerEvent = new ManualResetEvent(false); } } } if (request.m_readerEvent != null) { request.m_readerEvent.WaitOne(); } try { if (request.m_response == null) { request.m_response = new FileWebResponse(request, request.m_uri, request.m_fileAccess, !request.m_syncHint); } GlobalLog.Leave("FileWebRequest::GetResponseCallback"); asyncResult.InvokeCallback(request.m_response); } catch (Exception e) { if (asyncResult.IsCompleted || NclUtilities.IsFatal(e)) { throw; } Exception ex = new WebException(e.Message, e); GlobalLog.LeaveException("FileWebRequest::GetResponseCallback", ex); asyncResult.InvokeCallback(ex); } }
/// <include file='doc\filewebrequest.uex' path='docs/doc[@for="FileWebRequest.GetRequestStream"]/*' /> public override Stream GetRequestStream() { GlobalLog.Enter("FileWebRequest::GetRequestStream"); IAsyncResult result = BeginGetRequestStream(null, null); if ((Timeout != System.Threading.Timeout.Infinite) && !result.IsCompleted) { if (!result.AsyncWaitHandle.WaitOne(Timeout, false) || !result.IsCompleted) { if (m_stream != null) { m_stream.Close(); } Exception e = new WebException(SR.GetString(SR.net_timeout), WebExceptionStatus.Timeout); GlobalLog.LeaveException("FileWebRequest::GetRequestStream", e); throw e; } } GlobalLog.Leave("FileWebRequest::GetRequestStream"); return(EndGetRequestStream(result)); }
private Stream InternalGetRequestStream() { GlobalLog.Enter("FileWebRequest::InternalGetRequestStream"); try { if (m_stream == null) { m_stream = new FileWebStream(this, m_uri.LocalPath, FileMode.Create, FileAccess.Write, FileShare.Read ); m_fileAccess = FileAccess.Write; m_writing = true; } } catch (Exception e) { Exception ex = new WebException(e.Message, e); GlobalLog.LeaveException("FileWebRequest::InternalGetRequestStream", ex); throw ex; } GlobalLog.Leave("FileWebRequest::InternalGetRequestStream"); return(m_stream); }
private static ServicePoint FindServicePointHelper(Uri address, bool isProxyServicePoint) { GlobalLog.Enter("ServicePointManager::FindServicePointHelper() address:" + address.ToString()); if (isProxyServicePoint) { if (address.Scheme != Uri.UriSchemeHttp) { // < Exception exception = new NotSupportedException(SR.GetString(SR.net_proxyschemenotsupported, address.Scheme)); GlobalLog.LeaveException("ServicePointManager::FindServicePointHelper() proxy has unsupported scheme:" + address.Scheme.ToString(), exception); throw exception; } } // // Search for the correct proxy host, // then match its acutal host by using ConnectionGroups // which are located on the actual ServicePoint. // string tempEntry = MakeQueryString(address, isProxyServicePoint); // lookup service point in the table ServicePoint servicePoint = null; GlobalLog.Print("ServicePointManager::FindServicePointHelper() locking and looking up tempEntry:[" + tempEntry.ToString() + "]"); lock (s_ServicePointTable) { // once we grab the lock, check if it wasn't already added WeakReference servicePointReference = s_ServicePointTable[tempEntry] as WeakReference; GlobalLog.Print("ServicePointManager::FindServicePointHelper() lookup returned WeakReference#" + ValidationHelper.HashString(servicePointReference)); if (servicePointReference != null) { servicePoint = (ServicePoint)servicePointReference.Target; GlobalLog.Print("ServicePointManager::FindServicePointHelper() successful lookup returned ServicePoint#" + ValidationHelper.HashString(servicePoint)); } if (servicePoint == null) { // lookup failure or timeout, we need to create a new ServicePoint if (s_MaxServicePoints <= 0 || s_ServicePointTable.Count < s_MaxServicePoints) { // Determine Connection Limit int connectionLimit = InternalConnectionLimit; string schemeHostPort = MakeQueryString(address); bool userDefined = s_UserChangedLimit; if (ConfigTable.ContainsKey(schemeHostPort)) { connectionLimit = (int)ConfigTable[schemeHostPort]; userDefined = true; } servicePoint = new ServicePoint(address, s_ServicePointIdlingQueue, connectionLimit, tempEntry, userDefined, isProxyServicePoint); GlobalLog.Print("ServicePointManager::FindServicePointHelper() created ServicePoint#" + ValidationHelper.HashString(servicePoint)); servicePointReference = new WeakReference(servicePoint); s_ServicePointTable[tempEntry] = servicePointReference; GlobalLog.Print("ServicePointManager::FindServicePointHelper() adding entry WeakReference#" + ValidationHelper.HashString(servicePointReference) + " key:[" + tempEntry + "]"); } else { Exception exception = new InvalidOperationException(SR.GetString(SR.net_maxsrvpoints)); GlobalLog.LeaveException("ServicePointManager::FindServicePointHelper() reached the limit count:" + s_ServicePointTable.Count.ToString() + " limit:" + s_MaxServicePoints.ToString(), exception); throw exception; } } } GlobalLog.Leave("ServicePointManager::FindServicePointHelper() servicePoint#" + ValidationHelper.HashString(servicePoint)); return(servicePoint); }
// If abortState becomes non-zero, the attempt to find a service point has been aborted. internal static ServicePoint FindServicePoint(Uri address, IWebProxy proxy, out ProxyChain chain, ref HttpAbortDelegate abortDelegate, ref int abortState) { if (address == null) { throw new ArgumentNullException("address"); } GlobalLog.Enter("ServicePointManager::FindServicePoint() address:" + address.ToString()); bool isProxyServicePoint = false; chain = null; // // find proxy info, and then switch on proxy // Uri proxyAddress = null; if (proxy != null && !address.IsLoopback) { IAutoWebProxy autoProxy = proxy as IAutoWebProxy; if (autoProxy != null) { chain = autoProxy.GetProxies(address); // Set up our ability to abort this MoveNext call. Note that the current implementations of ProxyChain will only // take time on the first call, so this is the only place we do this. If a new ProxyChain takes time in later // calls, this logic should be copied to other places MoveNext is called. GlobalLog.Assert(abortDelegate == null, "ServicePointManager::FindServicePoint()|AbortDelegate already set."); abortDelegate = chain.HttpAbortDelegate; try { Thread.MemoryBarrier(); if (abortState != 0) { Exception exception = new WebException(NetRes.GetWebStatusString(WebExceptionStatus.RequestCanceled), WebExceptionStatus.RequestCanceled); GlobalLog.LeaveException("ServicePointManager::FindServicePoint() Request aborted before proxy lookup.", exception); throw exception; } if (!chain.Enumerator.MoveNext()) { GlobalLog.Assert("ServicePointManager::FindServicePoint()|GetProxies() returned zero proxies."); /* * Exception exception = new WebException(NetRes.GetWebStatusString(WebExceptionStatus.RequestProhibitedByProxy), WebExceptionStatus.RequestProhibitedByProxy); * GlobalLog.LeaveException("ServicePointManager::FindServicePoint() Proxy prevented request.", exception); * throw exception; */ } proxyAddress = chain.Enumerator.Current; } finally { abortDelegate = null; } } else if (!proxy.IsBypassed(address)) { // use proxy support // rework address proxyAddress = proxy.GetProxy(address); } // null means DIRECT if (proxyAddress != null) { address = proxyAddress; isProxyServicePoint = true; } } ServicePoint servicePoint = FindServicePointHelper(address, isProxyServicePoint); GlobalLog.Leave("ServicePointManager::FindServicePoint() servicePoint#" + ValidationHelper.HashString(servicePoint)); return(servicePoint); }
// // FindServicePoint - Query using an Uri for a given server point // /// <devdoc> /// <para>Findes an existing <see cref='System.Net.ServicePoint'/> or creates a new <see cref='System.Net.ServicePoint'/> to manage communications to the specified <see cref='System.Uri'/> /// instance.</para> /// </devdoc> internal static ServicePoint FindServicePoint(string host, int port) { if (host == null) { throw new ArgumentNullException("address"); } GlobalLog.Enter("ServicePointManager::FindServicePoint() host:" + host.ToString()); string tempEntry = null; bool isProxyServicePoint = false; // // Search for the correct proxy host, // then match its acutal host by using ConnectionGroups // which are located on the actual ServicePoint. // tempEntry = "ByHost:" + host + ":" + port.ToString(CultureInfo.InvariantCulture); // lookup service point in the table ServicePoint servicePoint = null; GlobalLog.Print("ServicePointManager::FindServicePoint() locking and looking up tempEntry:[" + tempEntry.ToString() + "]"); lock (s_ServicePointTable) { // once we grab the lock, check if it wasn't already added WeakReference servicePointReference = s_ServicePointTable[tempEntry] as WeakReference; GlobalLog.Print("ServicePointManager::FindServicePoint() lookup returned WeakReference#" + ValidationHelper.HashString(servicePointReference)); if (servicePointReference != null) { servicePoint = (ServicePoint)servicePointReference.Target; GlobalLog.Print("ServicePointManager::FindServicePoint() successfull lookup returned ServicePoint#" + ValidationHelper.HashString(servicePoint)); } if (servicePoint == null) { // lookup failure or timeout, we need to create a new ServicePoint if (s_MaxServicePoints <= 0 || s_ServicePointTable.Count < s_MaxServicePoints) { // Determine Connection Limit int connectionLimit = InternalConnectionLimit; bool userDefined = s_UserChangedLimit; string schemeHostPort = host + ":" + port.ToString(CultureInfo.InvariantCulture); if (ConfigTable.ContainsKey(schemeHostPort)) { connectionLimit = (int)ConfigTable[schemeHostPort]; userDefined = true; } servicePoint = new ServicePoint(host, port, s_ServicePointIdlingQueue, connectionLimit, tempEntry, userDefined, isProxyServicePoint); GlobalLog.Print("ServicePointManager::FindServicePoint() created ServicePoint#" + ValidationHelper.HashString(servicePoint)); servicePointReference = new WeakReference(servicePoint); s_ServicePointTable[tempEntry] = servicePointReference; GlobalLog.Print("ServicePointManager::FindServicePoint() adding entry WeakReference#" + ValidationHelper.HashString(servicePointReference) + " key:[" + tempEntry + "]"); } else { Exception exception = new InvalidOperationException(SR.GetString(SR.net_maxsrvpoints)); GlobalLog.LeaveException("ServicePointManager::FindServicePoint() reached the limit count:" + s_ServicePointTable.Count.ToString() + " limit:" + s_MaxServicePoints.ToString(), exception); throw exception; } } } GlobalLog.Leave("ServicePointManager::FindServicePoint() servicePoint#" + ValidationHelper.HashString(servicePoint)); return(servicePoint); }
// // FindServicePoint - Query using an Uri for a given server point // /// <include file='doc\ServicePointManager.uex' path='docs/doc[@for="ServicePointManager.FindServicePoint2"]/*' /> /// <devdoc> /// <para>Findes an existing <see cref='System.Net.ServicePoint'/> or creates a new <see cref='System.Net.ServicePoint'/> to manage communications to the specified <see cref='System.Uri'/> /// instance.</para> /// </devdoc> public static ServicePoint FindServicePoint(Uri address, IWebProxy proxy) { if (address == null) { throw new ArgumentNullException("address"); } GlobalLog.Enter("ServicePointManager::FindServicePoint() address:" + address.ToString()); string tempEntry; bool isProxyServicePoint = false; ScavengeIdleServicePoints(); // // find proxy info, and then switch on proxy // if (proxy != null && !proxy.IsBypassed(address)) { // use proxy support // rework address Uri proxyAddress = proxy.GetProxy(address); if (proxyAddress.Scheme != Uri.UriSchemeHttps && proxyAddress.Scheme != Uri.UriSchemeHttp) { Exception exception = new NotSupportedException(SR.GetString(SR.net_proxyschemenotsupported, proxyAddress.Scheme)); GlobalLog.LeaveException("ServicePointManager::FindServicePoint() proxy has unsupported scheme:" + proxyAddress.Scheme.ToString(), exception); throw exception; } address = proxyAddress; isProxyServicePoint = true; // // Search for the correct proxy host, // then match its acutal host by using ConnectionGroups // which are located on the actual ServicePoint. // tempEntry = MakeQueryString(proxyAddress); } else { // // Typical Host lookup // tempEntry = MakeQueryString(address); } // // lookup service point in the table // ServicePoint servicePoint = null; lock (s_ServicePointTable) { // // once we grab the lock, check if it wasn't already added // WeakReference servicePointReference = (WeakReference)s_ServicePointTable[tempEntry]; if (servicePointReference != null) { servicePoint = (ServicePoint)servicePointReference.Target; } if (servicePoint == null) { // // lookup failure or timeout, we need to create a new ServicePoint // if (s_MaxServicePoints <= 0 || s_ServicePointTable.Count < s_MaxServicePoints) { // // Determine Connection Limit // int connectionLimit = InternalConnectionLimit; string schemeHostPort = MakeQueryString(address); if (ConfigTable.ContainsKey(schemeHostPort)) { connectionLimit = (int)ConfigTable[schemeHostPort]; } // Note: we don't check permissions to access proxy. // Rather, we should protect proxy property from being changed servicePoint = new ServicePoint(address, s_MaxServicePointIdleTime, connectionLimit); servicePointReference = new WeakReference(servicePoint); // only set this when created, donates a proxy, statt Server servicePoint.InternalProxyServicePoint = isProxyServicePoint; s_ServicePointTable[tempEntry] = servicePointReference; } else { Exception exception = new InvalidOperationException(SR.GetString(SR.net_maxsrvpoints)); GlobalLog.LeaveException("ServicePointManager::FindServicePoint() reached the limit count:" + s_ServicePointTable.Count.ToString() + " limit:" + s_MaxServicePoints.ToString(), exception); throw exception; } } } // lock GlobalLog.Leave("ServicePointManager::FindServicePoint() servicePoint#" + ValidationHelper.HashString(servicePoint)); return(servicePoint); }