private void ProxyLoginWriteCompleteCallback(IAsyncResult iar) { AirSyncDiagnostics.TraceFunction <Uri, string>(ExTraceGlobals.RequestsTracer, this, "ProxyLoginWriteCompleteCallback: {0}, {1}.", this.remoteUri, this.userName); lock (this) { if (this.requestWasTimedOut) { AirSyncDiagnostics.TraceDebug(ExTraceGlobals.RequestsTracer, this, "ProxyHandler.ProxyLoginWriteCompleteCallback() Request was timed out while waiting for lock!"); return; } } try { this.UnregisterTimeoutWaitHandle(); this.backEndRequestStream.EndWrite(iar); this.pendingAsyncOperation = ProxyHandler.PendingOperationState.ProxyLoginGettingResponse; IAsyncResult asyncResult = this.backEndWebRequest.BeginGetResponse(new AsyncCallback(this.ProxyLoginGetResponseCompleteCallback), null); if (!asyncResult.CompletedSynchronously) { this.RegisterTimeoutWaitHandle(asyncResult, (int)GlobalSettings.ProxyHandlerShortTimeout.TotalMilliseconds); } } catch (Exception ex) { if (this.backEndWebRequest != null) { this.backEndWebRequest.Abort(); this.backEndWebRequest = null; } this.HandleException(ex); } AirSyncDiagnostics.TraceFunction <Uri, string>(ExTraceGlobals.RequestsTracer, this, "ProxyLoginWriteCompleteCallback: completed {0}, {1}.", this.remoteUri, this.userName); }
private void ProxyLoginGetStreamCompleteCallback(IAsyncResult iar) { AirSyncDiagnostics.TraceFunction <Uri, string>(ExTraceGlobals.RequestsTracer, this, "ProxyLoginRequestCompleteCallback: {0}, {1}.", this.remoteUri, this.userName); lock (this) { if (this.requestWasTimedOut) { AirSyncDiagnostics.TraceDebug(ExTraceGlobals.RequestsTracer, this, "ProxyHandler.ProxyLoginGetStreamCompleteCallback() Request was timed out while waiting for lock!"); return; } } try { this.UnregisterTimeoutWaitHandle(); this.backEndRequestStream = this.backEndWebRequest.EndGetRequestStream(iar); this.pendingAsyncOperation = ProxyHandler.PendingOperationState.ProxyLoginWritingCsc; IAsyncResult asyncResult = this.backEndRequestStream.BeginWrite(this.proxyInfo.CscBuffer, 0, this.proxyInfo.CscBuffer.Length, new AsyncCallback(this.ProxyLoginWriteCompleteCallback), null); if (!asyncResult.CompletedSynchronously) { this.RegisterTimeoutWaitHandle(asyncResult, (int)GlobalSettings.ProxyHandlerShortTimeout.TotalMilliseconds); } } catch (Exception ex) { AirSyncUtility.ExceptionToStringHelper arg = new AirSyncUtility.ExceptionToStringHelper(ex); AirSyncDiagnostics.TraceError <AirSyncUtility.ExceptionToStringHelper>(ExTraceGlobals.RequestsTracer, this, "ProxyLoginRequestCompleteCallback caught an exception\r\n{0}", arg); if (this.backEndWebRequest != null) { this.backEndWebRequest.Abort(); this.backEndWebRequest = null; } this.HandleException(ex); } AirSyncDiagnostics.TraceFunction <Uri, string>(ExTraceGlobals.RequestsTracer, this, "ProxyLoginRequestCompleteCallback: completed {0}, {1}.", this.remoteUri, this.userName); }
private void ProxyLoginGetResponseCompleteCallback(IAsyncResult iar) { AirSyncDiagnostics.TraceFunction <Uri, string>(ExTraceGlobals.RequestsTracer, this, "ProxyLoginGetResponseCompleteCallback: {0}, {1}.", this.remoteUri, this.userName); lock (this) { if (this.requestWasTimedOut) { AirSyncDiagnostics.TraceDebug(ExTraceGlobals.RequestsTracer, this, "ProxyHandler.ProxyLoginGetResponseCompleteCallback() Request was timed out while waiting for lock!"); return; } } try { this.TryUnregisterTimeoutWaitHandle(); this.httpWebResponse = (HttpWebResponse)this.backEndWebRequest.EndGetResponse(iar); } catch (WebException ex) { this.HandleException(ex); return; } finally { if (this.backEndRequestStream != null) { this.backEndRequestStream.Flush(); this.backEndRequestStream.Dispose(); this.backEndRequestStream = null; } if (this.httpWebResponse != null) { this.httpWebResponse.Close(); } } if (this.httpWebResponse == null) { this.HandleException(new WebException("Received null response object.")); } else { if (this.httpWebResponse.StatusCode != HttpStatusCode.OK) { this.HandleException(new WebException("Received HTTP status " + this.httpWebResponse.StatusCode + " from second CAS.")); return; } this.backEndWebRequest = this.CopyClientRequest(this.frontEndHttpContext.Request, this.cachedUri); this.cachedRequestBody.Seek(0L, SeekOrigin.Begin); this.clientRequestStream = this.cachedRequestBody; this.cachedRequestBody = null; this.pendingAsyncOperation = ProxyHandler.PendingOperationState.BackEndGetRequestStream; IAsyncResult asyncResult = this.backEndWebRequest.BeginGetRequestStream(new AsyncCallback(this.BackEndGetRequestStream), null); if (!asyncResult.CompletedSynchronously) { this.RegisterTimeoutWaitHandle(asyncResult, (int)GlobalSettings.ProxyHandlerShortTimeout.TotalMilliseconds); } AirSyncDiagnostics.TraceFunction <Uri, string>(ExTraceGlobals.RequestsTracer, this, "ProxyLoginGetResponseCompleteCallback: completed {0}, {1}.", this.remoteUri, this.userName); return; } }
private void IssueProxyLoginRequest() { AirSyncDiagnostics.TraceDebug <Uri, string>(ExTraceGlobals.RequestsTracer, this, "IssueProxyLoginRequest() invoked against {0} for {1}.", this.remoteUri, this.userName); HttpWebRequest httpWebRequest = this.CreateWebRequest(new UriBuilder(this.proxyInfo.RemoteUri) { Query = "cmd=ProxyLogin" }.Uri); httpWebRequest.Method = "POST"; httpWebRequest.ContentType = "text/xml"; httpWebRequest.ContentLength = (long)this.proxyInfo.CscBuffer.Length; httpWebRequest.Headers["X-EAS-Proxy"] = this.proxyInfo.AdditionalHeaders["X-EAS-Proxy"]; this.backEndWebRequest = httpWebRequest; this.issueTime = ExDateTime.Now; this.pendingAsyncOperation = ProxyHandler.PendingOperationState.BackEndGetRequestStream; IAsyncResult asyncResult = this.backEndWebRequest.BeginGetRequestStream(new AsyncCallback(this.ProxyLoginGetStreamCompleteCallback), null); if (!asyncResult.CompletedSynchronously) { this.RegisterTimeoutWaitHandle(asyncResult, (int)GlobalSettings.ProxyHandlerShortTimeout.TotalMilliseconds); } AirSyncDiagnostics.TraceFunction <Uri, string>(ExTraceGlobals.RequestsTracer, this, "IssueProxyLoginRequest() completed: {0}, {1}.", this.remoteUri, this.userName); }
private void BackEndGetResponse(IAsyncResult asynchronousResult) { lock (this) { if (this.requestWasTimedOut) { AirSyncDiagnostics.TraceDebug(ExTraceGlobals.RequestsTracer, this, "ProxyHandler.BackEndGetResponse() Request was timed out while waiting for lock!"); string text = this.remoteUri.Host.ToString(); AirSyncDiagnostics.LogPeriodicEvent(AirSyncEventLogConstants.Tuple_ProxyResultTimedOut, text, new string[] { text }); return; } } try { AirSyncDiagnostics.TraceDebug <Uri, string>(ExTraceGlobals.RequestsTracer, this, "ProxyHandler.BackEndGetResponse called for serverName: {0}, user {1}", this.remoteUri, this.userName); this.UnregisterTimeoutWaitHandle(); try { this.httpWebResponse = (HttpWebResponse)this.backEndWebRequest.EndGetResponse(asynchronousResult); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { HttpWebResponse httpWebResponse = ex.Response as HttpWebResponse; if (httpWebResponse == null) { this.HandleException(ex); return; } if (httpWebResponse.StatusCode == (HttpStatusCode)441) { httpWebResponse.Close(); if (!this.proxyInfo.AttemptProxyLogin) { AirSyncDiagnostics.TraceDebug <string>(ExTraceGlobals.RequestsTracer, this, "BackEndGetResponse(): authentication failure for {0}.", this.userName); this.TryUnregisterTimeoutWaitHandle(); this.frontEndHttpContext.Response.StatusCode = 403; this.frontEndResult.InvokeCallback(); return; } AirSyncDiagnostics.Assert(!this.proxyInfo.RequiresImpersonation && this.proxyInfo.AdditionalHeaders != null); AirSyncDiagnostics.TraceDebug <string>(ExTraceGlobals.RequestsTracer, this, "BackEndGetResponse(): proxy login required for {0}.", this.userName); if (this.proxyLoginAttempts > 0) { AirSyncDiagnostics.TraceFunction(ExTraceGlobals.RequestsTracer, this, "BackEndGetResponse(): too many login attempts."); this.protocolLogger.SetValue(ProtocolLoggerData.Error, "TooManyProxyLoginAttempts"); this.TryUnregisterTimeoutWaitHandle(); this.frontEndHttpContext.Response.StatusCode = 403; this.frontEndResult.InvokeCallback(); return; } this.proxyLoginAttempts++; this.IssueProxyLoginRequest(); return; } else { if (httpWebResponse.Headers != null) { WebHeaderCollection headers = httpWebResponse.Headers; string text2 = headers["WWW-Authenticate"]; if (text2 != null && text2.IndexOf("Negotiate ") == -1) { if (this.HasBeenProxiedByMServ) { this.protocolLogger.SetValue(ProtocolLoggerData.Error, "Datacenter Config issue upon MServ proxy"); } else { this.protocolLogger.SetValue(ProtocolLoggerData.Error, "NTLM not on the destination CAS"); string text3 = this.proxyInfo.RemoteUri.Host.ToString(); AirSyncDiagnostics.LogPeriodicEvent(AirSyncEventLogConstants.Tuple_SecondCasFailureNTLM, text3, new string[] { text3 }); } } } this.httpWebResponse = httpWebResponse; } } else { if (ex.Status == WebExceptionStatus.TrustFailure) { AirSyncDiagnostics.TraceDebug <string>(ExTraceGlobals.RequestsTracer, this, "SSL Certification failed for {0}.", this.proxyInfo.RemoteUri.Host); this.protocolLogger.SetValue(ProtocolLoggerData.Error, "SSL Certification Failed."); string text4 = this.proxyInfo.RemoteUri.Host.ToString(); AirSyncDiagnostics.LogPeriodicEvent(AirSyncEventLogConstants.Tuple_SecondCasFailureSSL, text4, new string[] { text4 }); this.HandleException(ex); return; } this.HandleException(ex); return; } } if (GlobalSettings.ProxyHeaders != null && GlobalSettings.ProxyHeaders.Length > 0) { this.frontEndHttpContext.Response.ContentType = string.Empty; for (int i = 0; i < this.httpWebResponse.Headers.Count; i++) { string text5 = this.httpWebResponse.Headers.Keys[i]; for (int j = 0; j < GlobalSettings.ProxyHeaders.Length; j++) { if (string.Equals(text5, GlobalSettings.ProxyHeaders[j], StringComparison.OrdinalIgnoreCase)) { this.frontEndHttpContext.Response.AppendHeader(text5, this.httpWebResponse.Headers[text5]); } } } } this.backEndResponseStream = this.httpWebResponse.GetResponseStream(); this.frontEndHttpContext.Response.StatusCode = (int)this.httpWebResponse.StatusCode; this.frontEndHttpContext.Response.StatusDescription = this.httpWebResponse.StatusDescription; this.pendingAsyncOperation = ProxyHandler.PendingOperationState.BackEndReadContent; IAsyncResult asyncResult = this.backEndResponseStream.BeginRead(this.asyncBuffer, 0, this.asyncBuffer.Length, new AsyncCallback(this.BackEndReadContent), null); if (!asyncResult.CompletedSynchronously) { this.RegisterTimeoutWaitHandle(asyncResult, (int)GlobalSettings.ProxyHandlerShortTimeout.TotalMilliseconds); } } catch (Exception ex2) { AirSyncUtility.ExceptionToStringHelper arg = new AirSyncUtility.ExceptionToStringHelper(ex2); AirSyncDiagnostics.TraceError <AirSyncUtility.ExceptionToStringHelper>(ExTraceGlobals.RequestsTracer, this, "ProxyHandler.BackEndGetResponse caught an exception\r\n{0}", arg); if (this.backEndWebRequest != null) { this.backEndWebRequest.Abort(); this.backEndWebRequest = null; } this.HandleException(ex2); } }