// // 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); } }
// // 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); } }
public override void Abort() { GlobalLog.Enter("FileWebRequest::Abort"); if (Logging.On) { Logging.PrintWarning(Logging.Web, NetRes.GetWebStatusString("net_requestaborted", WebExceptionStatus.RequestCanceled)); } try { if (Interlocked.Increment(ref m_Aborted) == 1) { ContextAwareResult readAResult = m_ReadAResult; ContextAwareResult writeAResult = m_WriteAResult; WebException webException = new WebException(NetRes.GetWebStatusString("net_requestaborted", WebExceptionStatus.RequestCanceled), WebExceptionStatus.RequestCanceled); Stream requestStream = m_stream; if (readAResult != null && !readAResult.IsCompleted) { readAResult.InvokeCallback(webException); } if (writeAResult != null && !writeAResult.IsCompleted) { writeAResult.InvokeCallback(webException); } if (requestStream != null) { if (requestStream is ICloseEx) { ((ICloseEx)requestStream).CloseEx(CloseExState.Abort); } else { requestStream.Close(); } } if (m_response != null) { ((ICloseEx)m_response).CloseEx(CloseExState.Abort); } } } catch (Exception exception) { if (Logging.On) { Logging.Exception(Logging.Web, this, "Abort", exception); } throw; } finally { GlobalLog.Leave("FileWebRequest::Abort"); } }
public override IAsyncResult BeginGetUnicastAddresses(AsyncCallback callback, object state) { if (!ComNetOS.IsVista) { throw new PlatformNotSupportedException(SR.GetString("VistaRequired")); } ContextAwareResult result = new ContextAwareResult(false, false, this, state, callback); result.StartPostingAsyncOp(false); if (TeredoHelper.UnsafeNotifyStableUnicastIpAddressTable(new Action<object>(SystemIPGlobalProperties.StableUnicastAddressTableCallback), result)) { result.InvokeCallback(); } result.FinishPostingAsyncOp(); return result; }
/// <summary> /// <para>Used to query for the Response of an FTP request [async version]</para> /// </summary> public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); NetEventSource.Info(this, $"Method: {_methodInfo.Method}"); } ContextAwareResult asyncResult; try { if (_ftpWebResponse != null) { asyncResult = new ContextAwareResult(this, state, callback); asyncResult.InvokeCallback(_ftpWebResponse); return asyncResult; } if (_getResponseStarted) { throw new InvalidOperationException(SR.net_repcall); } _getResponseStarted = true; CheckError(); RequestStage prev = FinishRequestStage(RequestStage.RequestStarted); asyncResult = new ContextAwareResult(true, true, this, state, callback); _readAsyncResult = asyncResult; if (prev >= RequestStage.RequestStarted) { // To make sure the context is flowed asyncResult.StartPostingAsyncOp(); asyncResult.FinishPostingAsyncOp(); if (prev >= RequestStage.ReadReady) asyncResult = null; else { lock (_syncObject) { if (_requestStage >= RequestStage.ReadReady) asyncResult = null; ; } } if (asyncResult == null) { // need to complete it now asyncResult = (ContextAwareResult)_readAsyncResult; if (!asyncResult.InternalPeekCompleted) asyncResult.InvokeCallback(); } } else { // Do internal processing in this handler to optimize context flowing. lock (asyncResult.StartPostingAsyncOp()) { SubmitRequest(true); asyncResult.FinishPostingAsyncOp(); } FinishRequestStage(RequestStage.CheckForError); } } catch (Exception exception) { if (NetEventSource.IsEnabled) NetEventSource.Error(this, exception); throw; } finally { if (NetEventSource.IsEnabled) NetEventSource.Exit(this); } return asyncResult; }
public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state) { if(Logging.On)Logging.Enter(Logging.Web, this, "BeginGetResponse", ""); if(Logging.On)Logging.PrintInfo(Logging.Web, this, "BeginGetResponse", SR.GetString(SR.net_log_method_equal, m_MethodInfo.Method)); GlobalLog.Enter("FtpWebRequest#" + ValidationHelper.HashString(this) + "::BeginGetResponse"); ContextAwareResult asyncResult; try { if (m_FtpWebResponse != null) { asyncResult = new ContextAwareResult(this, state, callback); asyncResult.InvokeCallback(m_FtpWebResponse); return asyncResult; } if (m_GetResponseStarted) { throw new InvalidOperationException(SR.GetString(SR.net_repcall)); } m_GetResponseStarted = true; CheckError(); if (ServicePoint.InternalProxyServicePoint) { HttpWebRequest httpWebRequest = GetHttpWebRequest(); if (Logging.On) Logging.Associate(Logging.Web, this, httpWebRequest); asyncResult = (ContextAwareResult)httpWebRequest.BeginGetResponse(callback, state); } else { RequestStage prev = FinishRequestStage(RequestStage.RequestStarted); asyncResult = new ContextAwareResult(true, true, this, state, callback); m_ReadAsyncResult = asyncResult; if (prev >= RequestStage.RequestStarted) { // To make sure the context is flowed asyncResult.StartPostingAsyncOp(); asyncResult.FinishPostingAsyncOp(); if (prev >= RequestStage.ReadReady) asyncResult = null; else { lock (m_SyncObject) { if (m_RequestStage >= RequestStage.ReadReady) asyncResult = null;; } } if(asyncResult == null) { // need to complete it now asyncResult = (ContextAwareResult)m_ReadAsyncResult; if (!asyncResult.InternalPeekCompleted) asyncResult.InvokeCallback(); } } else { // Do internal processing in this handler to optimize context flowing. lock (asyncResult.StartPostingAsyncOp()) { SubmitRequest(true); asyncResult.FinishPostingAsyncOp(); } FinishRequestStage(RequestStage.CheckForError); } } } catch (Exception exception) { if(Logging.On)Logging.Exception(Logging.Web, this, "BeginGetResponse", exception); throw; } finally { GlobalLog.Leave("FtpWebRequest#" + ValidationHelper.HashString(this) + "::BeginGetResponse"); if(Logging.On)Logging.Exit(Logging.Web, this, "BeginGetResponse", ""); } string suri; if (FrameworkEventSource.Log.IsEnabled(EventLevel.Verbose, FrameworkEventSource.Keywords.NetClient)) suri = this.RequestUri.ToString(); else suri = this.RequestUri.OriginalString; FrameworkEventSource.Log.BeginGetResponse(this, suri); return asyncResult; }
public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state) { #if DEBUG using (GlobalLog.SetThreadKind(ThreadKinds.User | ThreadKinds.Async)) { #endif GlobalLog.Enter("HttpWebRequest#" + ValidationHelper.HashString(this) + "::BeginGetResponse"); if(Logging.On)Logging.Enter(Logging.Web, this, "BeginGetResponse", ""); // No need to recheck the request parameters if we already did it in BeginGetRequestStream(). // This prevents problems when redirects change verbs. if (!RequestSubmitted) { CheckProtocol(false); } ConnectStream stream = _OldSubmitWriteStream != null ? _OldSubmitWriteStream : _SubmitWriteStream; // Close the request stream if the user forgot to do so. Throw an exception if user has not written all of // the promised data. if (stream != null && !stream.IsClosed) { if (stream.BytesLeftToWrite > 0) { throw new ProtocolViolationException(SR.GetString(SR.net_entire_body_not_written)); } else { stream.Close(); } } else if (stream == null && HasEntityBody) { throw new ProtocolViolationException(SR.GetString(SR.net_must_provide_request_body)); } #if !FEATURE_PAL ContextAwareResult asyncResult = new ContextAwareResult(IdentityRequired, true, this, state, callback); #else // FEATURE_PAL ContextAwareResult asyncResult = new ContextAwareResult(false, true, this, state, callback); #endif if (!RequestSubmitted && NclUtilities.IsThreadPoolLow()) { // prevent new requests when low on resources Exception exception = new InvalidOperationException(SR.GetString(SR.net_needmorethreads)); Abort(exception, AbortState.Public); throw exception; } // Need to lock the context until it's created (if necessary) in Returning(). lock (asyncResult.StartPostingAsyncOp()) { bool gotResponse = false; bool requestSubmitted; lock (this) { requestSubmitted = SetRequestSubmitted(); if (HaveResponse) { gotResponse = true; } else { if (_ReadAResult != null) { throw new InvalidOperationException(SR.GetString(SR.net_repcall)); } _ReadAResult = asyncResult; Async = true; } } // Must check this after setting _ReadAResult, which holds the context which may be used for permission checks etc. // See if we need to do the call-done processing here. CheckDeferredCallDone(stream); if (gotResponse) { if (Logging.On) Logging.Exit(Logging.Web, this, "BeginGetResponse", _ReadAResult.Result); GlobalLog.Leave("HttpWebRequest#" + ValidationHelper.HashString(this) + "::BeginGetResponse", "Already Completed, response = " + ValidationHelper.HashString(_ReadAResult.Result)); Exception e = _ReadAResult.Result as Exception; if (e != null) { throw e; } try { asyncResult.InvokeCallback(_ReadAResult.Result); } catch (Exception exception) { Abort(exception, AbortState.Public); throw; } } else { // If we're here it's because we don't have the response yet. We may have // already submitted the request, but if not do so now. if (!requestSubmitted) { // Save Off verb, and use it to make the request GlobalLog.Print("HttpWebRequest#" + ValidationHelper.HashString(this) + ": resetting CurrentMethod to " + _OriginVerb); CurrentMethod = _OriginVerb; } // If we're here it's because we don't have the response yet. We may have // already submitted the request, but if not do so now. if (_RerequestCount > 0 || !requestSubmitted) { while (m_Retry) { // Keep looping in case there are redirects, auth re-requests, etc BeginSubmitRequest(); } } } asyncResult.FinishPostingAsyncOp(); } GlobalLog.Leave("HttpWebRequest#" + ValidationHelper.HashString(this) + "::BeginGetResponse", ValidationHelper.HashString(asyncResult)); if(Logging.On)Logging.Exit(Logging.Web, this, "BeginGetResponse", asyncResult); string suri; if (FrameworkEventSource.Log.IsEnabled(EventLevel.Verbose, FrameworkEventSource.Keywords.NetClient)) suri = this.RequestUri.ToString(); else suri = this.RequestUri.OriginalString; FrameworkEventSource.Log.BeginGetResponse(this, suri); return asyncResult; #if DEBUG } #endif }
public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state) { #if DEBUG using (GlobalLog.SetThreadKind(ThreadKinds.User | ThreadKinds.Async)) { #endif GlobalLog.Enter("HttpWebRequest#" + ValidationHelper.HashString(this) + "::BeginGetRequestStream"); if(Logging.On)Logging.Enter(Logging.Web, this, "BeginGetRequestStream", ""); CheckProtocol(true); #if !FEATURE_PAL ContextAwareResult asyncResult = new ContextAwareResult(IdentityRequired, true, this, state, callback); #else // !FEATURE_PAL ContextAwareResult asyncResult = new ContextAwareResult(false, true, this, state, callback); #endif // !FEATURE_PAL lock (asyncResult.StartPostingAsyncOp()) { // and have a result (weird but was supported in V1.X as repeated calls for the submit stream. if (_WriteAResult != null && _WriteAResult.InternalPeekCompleted) { if (_WriteAResult.Result is Exception) { throw (Exception)_WriteAResult.Result; } try { asyncResult.InvokeCallback(_WriteAResult.Result); } catch (Exception e) { Abort(e, AbortState.Public); throw; } } else { // prevent new requests when low on resources if (!RequestSubmitted && NclUtilities.IsThreadPoolLow()) { Exception exception = new InvalidOperationException(SR.GetString(SR.net_needmorethreads)); Abort(exception, AbortState.Public); throw exception; } lock(this) { if (_WriteAResult != null) { throw new InvalidOperationException(SR.GetString(SR.net_repcall)); } // See if we're already submitted a request (e.g. via GetResponse). if (SetRequestSubmitted()) { // Not completed write stream, this is an application error. throw new InvalidOperationException(SR.GetString(SR.net_reqsubmitted)); } // If there's already been a _ReadAResult completed, it better have been with an exception, like an abort. // We need to check within this lock. Before the lock, _WriteAResult didn't exist so won't have been notified. // BeginSubmitRequest() will fail silently if we go ahead and call it after an abort. Since we know this is the // first call to any of the [Begin]GetRe... methods by the above checks, we know ProcessResponse can't have been // called or any other valid _ReadAResult created yet. if (_ReadAResult != null) { GlobalLog.Assert(_ReadAResult.InternalPeekCompleted, "HttpWebRequest#{0}::BeginGetRequestStream()|Incomplete _ReadAResult present on request.", ValidationHelper.HashString(this)); GlobalLog.Assert(_ReadAResult.Result is Exception, "HttpWebRequest#{0}::BeginGetRequestStream()|_ReadAResult with successful completion already present on request.", ValidationHelper.HashString(this)); throw (Exception) _ReadAResult.Result; } // get async going _WriteAResult = asyncResult; Async = true; } // OK, we haven't submitted the request yet, so do so now // save off verb from origin Verb GlobalLog.Print("HttpWebRequest#" + ValidationHelper.HashString(this) + "::BeginGetRequestStream() resetting CurrentMethod to " + _OriginVerb); CurrentMethod = _OriginVerb; BeginSubmitRequest(); } asyncResult.FinishPostingAsyncOp(); } GlobalLog.Leave("HttpWebRequest#" + ValidationHelper.HashString(this) + "::BeginGetRequestStream", ValidationHelper.HashString(asyncResult)); if(Logging.On)Logging.Exit(Logging.Web, this, "BeginGetRequestStream", asyncResult); 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 asyncResult; #if DEBUG } #endif }
public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state) { #if DEBUG using (GlobalLog.SetThreadKind(ThreadKinds.User | ThreadKinds.Async)) { #endif GlobalLog.Enter("HttpWebRequest#" + ValidationHelper.HashString(this) + "::BeginGetResponse"); if(Logging.On)Logging.Enter(Logging.Web, this, "BeginGetResponse", ""); CheckProtocol(false); ConnectStream stream = _OldSubmitWriteStream != null ? _OldSubmitWriteStream : _SubmitWriteStream; // Close the request stream if the user forgot to do that and all data is written. if(stream != null && !stream.IsClosed && stream.BytesLeftToWrite == 0) { stream.Close(); } ContextAwareResult asyncResult = new ContextAwareResult(false, true, this, state, callback); if (!RequestSubmitted && NclUtilities.IsThreadPoolLow()) { // prevent new requests when low on resources Exception exception = new InvalidOperationException(SR.GetString(SR.net_needmorethreads)); Abort(exception, AbortState.Public); throw exception; } // Need to lock the context until it's created (if necessary) in Returning(). lock (asyncResult.StartPostingAsyncOp()) { bool gotResponse = false; bool requestSubmitted; lock (this) { requestSubmitted = SetRequestSubmitted(); if (HaveResponse) { gotResponse = true; } else { if (_ReadAResult != null) { throw new InvalidOperationException(SR.GetString(SR.net_repcall)); } _ReadAResult = asyncResult; Async = true; } } // Must check this after setting _ReadAResult, which holds the context which may be used for permission checks etc. // See if we need to do the call-done processing here. CheckDeferredCallDone(stream); if (gotResponse) { if (Logging.On) Logging.Exit(Logging.Web, this, "BeginGetResponse", _ReadAResult.Result); GlobalLog.Leave("HttpWebRequest#" + ValidationHelper.HashString(this) + "::BeginGetResponse", "Already Completed, response = " + ValidationHelper.HashString(_ReadAResult.Result)); Exception e = _ReadAResult.Result as Exception; if (e != null) { throw e; } try { asyncResult.InvokeCallback(_ReadAResult.Result); } catch (Exception exception) { Abort(exception, AbortState.Public); throw; } } else { // If we're here it's because we don't have the response yet. We may have // already submitted the request, but if not do so now. if (!requestSubmitted) { // Save Off verb, and use it to make the request GlobalLog.Print("HttpWebRequest#" + ValidationHelper.HashString(this) + ": resetting CurrentMethod to " + _OriginVerb); CurrentMethod = _OriginVerb; } // If we're here it's because we don't have the response yet. We may have // already submitted the request, but if not do so now. if (_RerequestCount > 0 || !requestSubmitted) { while (m_Retry) { // Keep looping in case there are redirects, auth re-requests, etc BeginSubmitRequest(); } } } asyncResult.FinishPostingAsyncOp(); } GlobalLog.Leave("HttpWebRequest#" + ValidationHelper.HashString(this) + "::BeginGetResponse", ValidationHelper.HashString(asyncResult)); if(Logging.On)Logging.Exit(Logging.Web, this, "BeginGetResponse", asyncResult); return asyncResult; #if DEBUG } #endif }
public override IAsyncResult BeginGetUnicastAddresses(AsyncCallback callback, object state){ ContextAwareResult asyncResult = new ContextAwareResult(false, false, this, state, callback); asyncResult.StartPostingAsyncOp(false); if (TeredoHelper.UnsafeNotifyStableUnicastIpAddressTable(StableUnicastAddressTableCallback, asyncResult)) { asyncResult.InvokeCallback(); } asyncResult.FinishPostingAsyncOp(); return asyncResult; }
public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state) { ContextAwareResult readAsyncResult; if (Logging.On) { Logging.Enter(Logging.Web, this, "BeginGetResponse", ""); } if (Logging.On) { Logging.PrintInfo(Logging.Web, this, "BeginGetResponse", SR.GetString("net_log_method_equal", new object[] { this.m_MethodInfo.Method })); } try { if (this.m_FtpWebResponse != null) { readAsyncResult = new ContextAwareResult(this, state, callback); readAsyncResult.InvokeCallback(this.m_FtpWebResponse); return readAsyncResult; } if (this.m_GetResponseStarted) { throw new InvalidOperationException(SR.GetString("net_repcall")); } this.m_GetResponseStarted = true; this.CheckError(); if (this.ServicePoint.InternalProxyServicePoint) { HttpWebRequest httpWebRequest = this.GetHttpWebRequest(); if (Logging.On) { Logging.Associate(Logging.Web, this, httpWebRequest); } return (ContextAwareResult) httpWebRequest.BeginGetResponse(callback, state); } RequestStage stage = this.FinishRequestStage(RequestStage.RequestStarted); readAsyncResult = new ContextAwareResult(true, true, this, state, callback); this.m_ReadAsyncResult = readAsyncResult; if (stage >= RequestStage.RequestStarted) { readAsyncResult.StartPostingAsyncOp(); readAsyncResult.FinishPostingAsyncOp(); if (stage >= RequestStage.ReadReady) { readAsyncResult = null; } else { lock (this.m_SyncObject) { if (this.m_RequestStage >= RequestStage.ReadReady) { readAsyncResult = null; } } } if (readAsyncResult == null) { readAsyncResult = (ContextAwareResult) this.m_ReadAsyncResult; if (!readAsyncResult.InternalPeekCompleted) { readAsyncResult.InvokeCallback(); } } return readAsyncResult; } lock (readAsyncResult.StartPostingAsyncOp()) { this.SubmitRequest(true); readAsyncResult.FinishPostingAsyncOp(); } this.FinishRequestStage(RequestStage.CheckForError); return readAsyncResult; } catch (Exception exception) { if (Logging.On) { Logging.Exception(Logging.Web, this, "BeginGetResponse", exception); } throw; } finally { if (Logging.On) { Logging.Exit(Logging.Web, this, "BeginGetResponse", ""); } } return readAsyncResult; }