protected override void DoProtocolSpecificBeginProcess()
 {
     if (!base.ClientRequest.IsAuthenticated)
     {
         try
         {
             if (this.IsSimpleSoapRequest())
             {
                 base.ParseClientRequest <bool>(new Func <Stream, bool>(this.ParseRequest), 81820);
             }
         }
         catch (FormatException innerException)
         {
             throw new HttpException(400, "FormatException parsing Autodiscover request", innerException);
         }
         catch (XmlException innerException2)
         {
             throw new HttpException(400, "XmlException parsing Autodiscover request", innerException2);
         }
         if (!base.IsWsSecurityRequest && !base.IsDomainBasedRequest && !ProtocolHelper.IsAutodiscoverV2Request(base.ClientRequest.Url.AbsolutePath))
         {
             throw new HttpProxyException(HttpStatusCode.Unauthorized, HttpProxySubErrorCode.UnauthenticatedRequest, "Unauthenticated AutoDiscover request.");
         }
     }
 }
        protected override UriBuilder GetClientUrlForProxy()
        {
            string text = base.ClientRequest.Url.ToString();
            string uri  = text;

            if (this.isExplicitLogonRequest && !ProtocolHelper.IsAutodiscoverV2Request(base.ClientRequest.Url.AbsoluteUri))
            {
                string text2 = "/" + this.explicitLogonAddress;
                int    num   = text.IndexOf(text2);
                if (num != -1)
                {
                    uri = text.Substring(0, num) + text.Substring(num + text2.Length);
                }
            }
            return(new UriBuilder(uri));
        }
        protected override AnchorMailbox ResolveAnchorMailbox()
        {
            if (this.skipTargetBackEndCalculation)
            {
                base.Logger.Set(HttpProxyMetadata.RoutingHint, "OrgRelationship-Anonymous");
                return(new AnonymousAnchorMailbox(this));
            }
            string text;

            if (ProtocolHelper.IsAutodiscoverV2PreviewRequest(base.ClientRequest.Url.AbsolutePath))
            {
                text = base.ClientRequest.Params["Email"];
            }
            else if (ProtocolHelper.IsAutodiscoverV2Version1Request(base.ClientRequest.Url.AbsolutePath))
            {
                int num = base.ClientRequest.Url.AbsolutePath.LastIndexOf('/');
                text = base.ClientRequest.Url.AbsolutePath.Substring(num + 1);
            }
            else
            {
                text = this.TryGetExplicitLogonNode(ExplicitLogonNode.Second);
            }
            string text2;

            if (ProtocolHelper.TryGetValidNormalizedExplicitLogonAddress(text, out text2))
            {
                this.isExplicitLogonRequest = true;
                this.explicitLogonAddress   = text;
                base.Logger.Set(HttpProxyMetadata.RoutingHint, "ExplicitLogon-SMTP");
                SmtpAnchorMailbox smtpAnchorMailbox = new SmtpAnchorMailbox(text2, this);
                if (this.preferAnchorMailboxHeader)
                {
                    string text3 = base.ClientRequest.Headers[Constants.AnchorMailboxHeaderName];
                    if (!string.IsNullOrEmpty(text3) && !StringComparer.OrdinalIgnoreCase.Equals(text3, text2) && SmtpAddress.IsValidSmtpAddress(text3))
                    {
                        base.Logger.Set(HttpProxyMetadata.RoutingHint, "AnchorMailboxHeader-SMTP");
                        smtpAnchorMailbox = new SmtpAnchorMailbox(text3, this);
                    }
                }
                if (ProtocolHelper.IsAutodiscoverV2Request(base.ClientRequest.Url.AbsolutePath))
                {
                    smtpAnchorMailbox.FailOnDomainNotFound = false;
                }
                return(smtpAnchorMailbox);
            }
            return(base.ResolveAnchorMailbox());
        }
        private IHttpHandler SelectHandlerForUnauthenticatedRequest(HttpContext httpContext)
        {
            IHttpHandler result;

            try
            {
                IHttpHandler httpHandler = null;
                if (HttpProxyGlobals.ProtocolType == ProtocolType.Autodiscover)
                {
                    if (httpContext.Request.HttpMethod.Equals("GET", StringComparison.OrdinalIgnoreCase))
                    {
                        if (ProtocolHelper.IsOAuthMetadataRequest(httpContext.Request.Url.AbsolutePath))
                        {
                            httpHandler = new AuthMetadataHttpHandler(httpContext);
                        }
                        else if (ProtocolHelper.IsAutodiscoverV2Request(httpContext.Request.Url.AbsolutePath))
                        {
                            httpHandler = new AutodiscoverProxyRequestHandler();
                        }
                    }
                    else
                    {
                        httpHandler = new AutodiscoverProxyRequestHandler();
                    }
                }
                else if (HttpProxyGlobals.ProtocolType == ProtocolType.Ews)
                {
                    string httpMethod = httpContext.Request.HttpMethod;
                    if (!httpMethod.Equals("GET", StringComparison.OrdinalIgnoreCase) && !httpMethod.Equals("HEAD", StringComparison.OrdinalIgnoreCase))
                    {
                        httpHandler = new EwsProxyRequestHandler();
                    }
                }
                else if (HttpProxyGlobals.ProtocolType == ProtocolType.Ecp)
                {
                    if (EDiscoveryExportToolProxyRequestHandler.IsEDiscoveryExportToolProxyRequest(httpContext.Request))
                    {
                        httpHandler = new EDiscoveryExportToolProxyRequestHandler();
                    }
                    else if (BEResourceRequestHandler.CanHandle(httpContext.Request))
                    {
                        httpHandler = new BEResourceRequestHandler();
                    }
                    else if (EcpProxyRequestHandler.IsCrossForestDelegatedRequest(httpContext.Request))
                    {
                        httpHandler = new EcpProxyRequestHandler
                        {
                            IsCrossForestDelegated = true
                        };
                    }
                    else if (!httpContext.Request.Path.StartsWith("/ecp/auth/", StringComparison.OrdinalIgnoreCase) && !httpContext.Request.Path.Equals("/ecp/ping.ecp", StringComparison.OrdinalIgnoreCase))
                    {
                        httpHandler = new Return401RequestHandler();
                    }
                }
                else if (HttpProxyGlobals.ProtocolType == ProtocolType.RpcHttp)
                {
                    httpHandler = new RpcHttpRequestHandler();
                }
                else if (HttpProxyGlobals.ProtocolType == ProtocolType.Xrop)
                {
                    httpHandler = new XRopProxyRequestHandler();
                }
                else if (HttpProxyGlobals.ProtocolType == ProtocolType.E4e)
                {
                    httpHandler = new E4eProxyRequestHandler();
                }
                else if (AnonymousCalendarProxyRequestHandler.IsAnonymousCalendarRequest(httpContext.Request))
                {
                    httpHandler = new AnonymousCalendarProxyRequestHandler();
                }
                else if (HttpProxyGlobals.ProtocolType == ProtocolType.Owa && WopiRequestPathHandler.IsWopiRequest(httpContext.Request, AuthCommon.IsFrontEnd))
                {
                    httpHandler = new WopiProxyRequestHandler();
                }
                else if (OwaExtensibilityProxyRequestHandler.IsOwaExtensibilityRequest(httpContext.Request))
                {
                    httpHandler = new OwaExtensibilityProxyRequestHandler();
                }
                else if (OwaCobrandingRedirProxyRequestHandler.IsCobrandingRedirRequest(httpContext.Request))
                {
                    httpHandler = new OwaCobrandingRedirProxyRequestHandler();
                }
                else if (HttpProxyGlobals.ProtocolType == ProtocolType.Owa && OwaResourceProxyRequestHandler.CanHandle(httpContext.Request))
                {
                    httpHandler = new OwaResourceProxyRequestHandler();
                }
                else if (HttpProxyGlobals.ProtocolType == ProtocolType.PowerShellGateway)
                {
                    httpHandler = new PsgwProxyRequestHandler();
                }
                result = httpHandler;
            }
            finally
            {
                long currentLatency = LatencyTracker.FromHttpContext(httpContext).GetCurrentLatency(LatencyTrackerKey.ProxyModuleLatency);
                if (currentLatency > 100L)
                {
                    RequestDetailsLoggerBase <RequestDetailsLogger> .SafeAppendGenericInfo(RequestDetailsLoggerBase <RequestDetailsLogger> .GetCurrent(httpContext), "SelectHandler", currentLatency);
                }
            }
            return(result);
        }
        private void OnPostAuthenticateInternal(HttpApplication httpApplication)
        {
            HttpContext context = httpApplication.Context;

            if (!context.Request.IsAuthenticated)
            {
                bool   flag         = false;
                bool   flag2        = false;
                string value        = string.Empty;
                string httpMethod   = context.Request.HttpMethod;
                string absolutePath = context.Request.Url.AbsolutePath;
                if (httpMethod.Equals("GET", StringComparison.OrdinalIgnoreCase) || httpMethod.Equals("HEAD", StringComparison.OrdinalIgnoreCase))
                {
                    if (HttpProxyGlobals.ProtocolType != ProtocolType.Autodiscover || (!ProtocolHelper.IsOAuthMetadataRequest(absolutePath) && !ProtocolHelper.IsAutodiscoverV2Request(absolutePath)))
                    {
                        flag2 = true;
                        flag  = true;
                        value = "AutodiscoverEwsDiscovery";
                    }
                }
                else if (!ProtocolHelper.IsAnyWsSecurityRequest(context.Request.Url.AbsolutePath) && HttpProxyGlobals.ProtocolType != ProtocolType.Autodiscover)
                {
                    flag2 = true;
                    value = "AnonymousRequestDisallowed";
                }
                if (!string.IsNullOrEmpty(value))
                {
                    context.Items[AnonymousRequestFilterModule.AnonymousRequestFilterModuleLoggingKey] = value;
                }
                if (flag)
                {
                    AutodiscoverEwsDiscoveryResponseHelper.AddEndpointEnabledHeaders(context.Response);
                }
                if (flag2)
                {
                    context.Response.StatusCode        = 401;
                    context.Response.StatusDescription = "Anonymous Request Disallowed";
                    httpApplication.CompleteRequest();
                }
            }
        }
 protected override void OnInitializingHandler()
 {
     base.OnInitializingHandler();
     if (!base.ClientRequest.IsAuthenticated)
     {
         base.IsWsSecurityRequest = base.ClientRequest.IsAnyWsSecurityRequest();
         if (base.IsWsSecurityRequest && !AutodiscoverEwsWebConfiguration.WsSecurityEndpointEnabled)
         {
             throw new HttpException(404, "WS-Security endpoint is not supported");
         }
     }
     if (base.ClientRequest.Url.ToString().EndsWith("autodiscover.xml", StringComparison.OrdinalIgnoreCase) || ProtocolHelper.IsAutodiscoverV2Request(base.ClientRequest.Url.AbsolutePath))
     {
         base.PreferAnchorMailboxHeader = true;
     }
 }