/// <include file='doc\DiscoveryRequestHandler.uex' path='docs/doc[@for="DiscoveryRequestHandler.ProcessRequest"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public void ProcessRequest(HttpContext context)
        {
            TraceMethod method = Tracing.On ? new TraceMethod(this, "ProcessRequest") : null;

            if (Tracing.On)
            {
                Tracing.Enter("IHttpHandler.ProcessRequest", method, Tracing.Details(context.Request));
            }

            new PermissionSet(PermissionState.Unrestricted).Demand();
            // string cacheKey;

            string physicalPath = context.Request.PhysicalPath;

            if (CompModSwitches.DynamicDiscoverySearcher.TraceVerbose)
            {
                Debug.WriteLine("DiscoveryRequestHandle: handling " + physicalPath);
            }

            // Check to see if file exists locally.
            if (File.Exists(physicalPath))
            {
                DynamicDiscoveryDocument dynDisco = null;
                FileStream stream = null;
                try {
                    stream = new FileStream(physicalPath, FileMode.Open, FileAccess.Read);
                    XmlTextReader xmlReader = new XmlTextReader(stream);
                    xmlReader.WhitespaceHandling = WhitespaceHandling.Significant;
                    xmlReader.XmlResolver        = null;
                    xmlReader.DtdProcessing      = DtdProcessing.Prohibit;
                    if (xmlReader.IsStartElement("dynamicDiscovery", DynamicDiscoveryDocument.Namespace))
                    {
                        stream.Position = 0;
                        dynDisco        = DynamicDiscoveryDocument.Load(stream);
                    }
                }
                finally {
                    if (stream != null)
                    {
                        stream.Close();
                    }
                }

                if (dynDisco != null)
                {
                    string[] excludeList        = new string[dynDisco.ExcludePaths.Length];
                    string   discoFileDirectory = Path.GetDirectoryName(physicalPath);
                    string   discoFileName      = Path.GetFileName(physicalPath);

                    for (int i = 0; i < excludeList.Length; i++)
                    {
                        // Exclude list now consists of relative paths, so this transformation not needed.
                        // excludeList[i] = Path.Combine(discoFileDirectory, dynDisco.ExcludePaths[i].Path);
                        excludeList[i] = dynDisco.ExcludePaths[i].Path;
                    }

                    // Determine start url path for search
                    DynamicDiscoSearcher searcher;
                    Uri    searchStartUrl    = context.Request.Url;
                    string escapedUri        = RuntimeUtils.EscapeUri(searchStartUrl);
                    string searchStartUrlDir = GetDirPartOfPath(escapedUri);    // URL path without file name
                    string strLocalPath      = GetDirPartOfPath(searchStartUrl.LocalPath);

                    if (strLocalPath.Length == 0 ||                           // no subdir present, host only
                        CompModSwitches.DynamicDiscoveryVirtualSearch.Enabled // virtual search forced (for test suites).
                        )
                    {
                        discoFileName = GetFilePartOfPath(escapedUri);
                        searcher      = new DynamicVirtualDiscoSearcher(discoFileDirectory, excludeList, searchStartUrlDir);
                    }
                    else
                    {
                        searcher = new DynamicPhysicalDiscoSearcher(discoFileDirectory, excludeList, searchStartUrlDir);
                    }

                    if (CompModSwitches.DynamicDiscoverySearcher.TraceVerbose)
                    {
                        Debug.WriteLine("*** DiscoveryRequestHandler.ProcessRequest() - startDir: " + searchStartUrlDir + " discoFileName :" + discoFileName);
                    }
                    searcher.Search(discoFileName);

                    DiscoveryDocument discoFile = searcher.DiscoveryDocument;

                    MemoryStream memStream = new MemoryStream(1024);
                    StreamWriter writer    = new StreamWriter(memStream, new UTF8Encoding(false));
                    discoFile.Write(writer);
                    memStream.Position = 0;
                    byte[] data      = new byte[(int)memStream.Length];
                    int    bytesRead = memStream.Read(data, 0, data.Length);
                    context.Response.ContentType = ContentType.Compose("text/xml", Encoding.UTF8);
                    context.Response.OutputStream.Write(data, 0, bytesRead);
                }
                else
                {
                    // Else, just return the disco file
                    context.Response.ContentType = "text/xml";
                    context.Response.WriteFile(physicalPath);
                }
                if (Tracing.On)
                {
                    Tracing.Exit("IHttpHandler.ProcessRequest", method);
                }
                return;
            }
            if (Tracing.On)
            {
                Tracing.Exit("IHttpHandler.ProcessRequest", method);
            }

            // Else, file is not found
            throw new HttpException(404, Res.GetString(Res.WebPathNotFound, context.Request.Path));
        }
        public void ProcessRequest(HttpContext context)
        {
            TraceMethod caller = Tracing.On ? new TraceMethod(this, "ProcessRequest", new object[0]) : null;

            if (Tracing.On)
            {
                Tracing.Enter("IHttpHandler.ProcessRequest", caller, Tracing.Details(context.Request));
            }
            new PermissionSet(PermissionState.Unrestricted).Demand();
            string physicalPath = context.Request.PhysicalPath;
            bool   traceVerbose = System.ComponentModel.CompModSwitches.DynamicDiscoverySearcher.TraceVerbose;

            if (File.Exists(physicalPath))
            {
                DynamicDiscoveryDocument document = null;
                FileStream input = null;
                try
                {
                    input = new FileStream(physicalPath, FileMode.Open, FileAccess.Read);
                    XmlTextReader reader = new XmlTextReader(input)
                    {
                        WhitespaceHandling = WhitespaceHandling.Significant,
                        XmlResolver        = null,
                        DtdProcessing      = DtdProcessing.Prohibit
                    };
                    if (reader.IsStartElement("dynamicDiscovery", "urn:schemas-dynamicdiscovery:disco.2000-03-17"))
                    {
                        input.Position = 0L;
                        document       = DynamicDiscoveryDocument.Load(input);
                    }
                }
                finally
                {
                    if (input != null)
                    {
                        input.Close();
                    }
                }
                if (document != null)
                {
                    DynamicDiscoSearcher searcher;
                    string[]             excludedUrls = new string[document.ExcludePaths.Length];
                    string directoryName = Path.GetDirectoryName(physicalPath);
                    string fileName      = Path.GetFileName(physicalPath);
                    for (int i = 0; i < excludedUrls.Length; i++)
                    {
                        excludedUrls[i] = document.ExcludePaths[i].Path;
                    }
                    Uri    url           = context.Request.Url;
                    string str           = Uri.EscapeUriString(url.ToString()).Replace("#", "%23");
                    string dirPartOfPath = GetDirPartOfPath(str);
                    if ((GetDirPartOfPath(url.LocalPath).Length == 0) || System.ComponentModel.CompModSwitches.DynamicDiscoveryVirtualSearch.Enabled)
                    {
                        fileName = GetFilePartOfPath(str);
                        searcher = new DynamicVirtualDiscoSearcher(directoryName, excludedUrls, dirPartOfPath);
                    }
                    else
                    {
                        searcher = new DynamicPhysicalDiscoSearcher(directoryName, excludedUrls, dirPartOfPath);
                    }
                    bool flag2 = System.ComponentModel.CompModSwitches.DynamicDiscoverySearcher.TraceVerbose;
                    searcher.Search(fileName);
                    DiscoveryDocument discoveryDocument = searcher.DiscoveryDocument;
                    MemoryStream      stream            = new MemoryStream(0x400);
                    StreamWriter      writer            = new StreamWriter(stream, new UTF8Encoding(false));
                    discoveryDocument.Write(writer);
                    stream.Position = 0L;
                    byte[] buffer = new byte[(int)stream.Length];
                    int    count  = stream.Read(buffer, 0, buffer.Length);
                    context.Response.ContentType = ContentType.Compose("text/xml", Encoding.UTF8);
                    context.Response.OutputStream.Write(buffer, 0, count);
                }
                else
                {
                    context.Response.ContentType = "text/xml";
                    context.Response.WriteFile(physicalPath);
                }
                if (Tracing.On)
                {
                    Tracing.Exit("IHttpHandler.ProcessRequest", caller);
                }
            }
            else
            {
                if (Tracing.On)
                {
                    Tracing.Exit("IHttpHandler.ProcessRequest", caller);
                }
                throw new HttpException(0x194, System.Web.Services.Res.GetString("WebPathNotFound", new object[] { context.Request.Path }));
            }
        }
        /// <include file='doc\DiscoveryRequestHandler.uex' path='docs/doc[@for="DiscoveryRequestHandler.ProcessRequest"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public void ProcessRequest(HttpContext context) {
            TraceMethod method = Tracing.On ? new TraceMethod(this, "ProcessRequest") : null;
            if (Tracing.On) Tracing.Enter("IHttpHandler.ProcessRequest", method, Tracing.Details(context.Request));

            new PermissionSet(PermissionState.Unrestricted).Demand();
            // string cacheKey;

            string physicalPath = context.Request.PhysicalPath;
            if ( CompModSwitches.DynamicDiscoverySearcher.TraceVerbose ) Debug.WriteLine("DiscoveryRequestHandle: handling " + physicalPath);

            // Check to see if file exists locally.
            if (File.Exists(physicalPath)) {
                DynamicDiscoveryDocument dynDisco = null;
                FileStream stream = null;
                try {
                    stream = new FileStream(physicalPath, FileMode.Open, FileAccess.Read);
                    XmlTextReader xmlReader = new XmlTextReader(stream);
                    xmlReader.WhitespaceHandling = WhitespaceHandling.Significant;
                    xmlReader.XmlResolver = null;
                    xmlReader.DtdProcessing = DtdProcessing.Prohibit;
                    if (xmlReader.IsStartElement("dynamicDiscovery", DynamicDiscoveryDocument.Namespace)) {
                        stream.Position = 0;
                        dynDisco = DynamicDiscoveryDocument.Load(stream);
                    }
                }
                finally {
                    if (stream != null) {
                        stream.Close();
                    }
                }

                if (dynDisco != null) {
                    string[] excludeList = new string[dynDisco.ExcludePaths.Length];
                    string discoFileDirectory = Path.GetDirectoryName(physicalPath);
                    string discoFileName = Path.GetFileName(physicalPath);

                    for (int i = 0; i < excludeList.Length; i++) {
                         // Exclude list now consists of relative paths, so this transformation not needed.
                         // excludeList[i] = Path.Combine(discoFileDirectory, dynDisco.ExcludePaths[i].Path);
                         excludeList[i] = dynDisco.ExcludePaths[i].Path;
                         }

                    // Determine start url path for search
                    DynamicDiscoSearcher searcher;
                    Uri searchStartUrl = context.Request.Url;
                    string escapedUri = RuntimeUtils.EscapeUri(searchStartUrl);
                    string searchStartUrlDir = GetDirPartOfPath( escapedUri );  // URL path without file name
                    string strLocalPath = GetDirPartOfPath(searchStartUrl.LocalPath);

                    if ( strLocalPath.Length == 0 ||       // no subdir present, host only
                         CompModSwitches.DynamicDiscoveryVirtualSearch.Enabled    // virtual search forced (for test suites).
                       ) {
                       discoFileName = GetFilePartOfPath( escapedUri );
                       searcher = new DynamicVirtualDiscoSearcher( discoFileDirectory, excludeList, searchStartUrlDir);
                    }
                    else
                        searcher = new DynamicPhysicalDiscoSearcher(discoFileDirectory, excludeList, searchStartUrlDir);

                    if ( CompModSwitches.DynamicDiscoverySearcher.TraceVerbose ) Debug.WriteLine( "*** DiscoveryRequestHandler.ProcessRequest() - startDir: " + searchStartUrlDir + " discoFileName :" + discoFileName);
                    searcher.Search(discoFileName);

                    DiscoveryDocument discoFile = searcher.DiscoveryDocument;

                    MemoryStream memStream = new MemoryStream(1024);
                    StreamWriter writer = new StreamWriter(memStream, new UTF8Encoding(false));
                    discoFile.Write(writer);
                    memStream.Position = 0;
                    byte[] data = new byte[(int)memStream.Length];
                    int bytesRead = memStream.Read(data, 0, data.Length);
                    context.Response.ContentType = ContentType.Compose("text/xml", Encoding.UTF8);
                    context.Response.OutputStream.Write(data, 0, bytesRead);
                }
                else {
                    // Else, just return the disco file
                    context.Response.ContentType = "text/xml";
                    context.Response.WriteFile(physicalPath);
                }
                if (Tracing.On) Tracing.Exit("IHttpHandler.ProcessRequest", method);
                return;
            }
            if (Tracing.On) Tracing.Exit("IHttpHandler.ProcessRequest", method);

            // Else, file is not found
            throw new HttpException(404, Res.GetString(Res.WebPathNotFound, context.Request.Path));
        }
 public void ProcessRequest(HttpContext context)
 {
     TraceMethod caller = Tracing.On ? new TraceMethod(this, "ProcessRequest", new object[0]) : null;
     if (Tracing.On)
     {
         Tracing.Enter("IHttpHandler.ProcessRequest", caller, Tracing.Details(context.Request));
     }
     new PermissionSet(PermissionState.Unrestricted).Demand();
     string physicalPath = context.Request.PhysicalPath;
     bool traceVerbose = System.ComponentModel.CompModSwitches.DynamicDiscoverySearcher.TraceVerbose;
     if (File.Exists(physicalPath))
     {
         DynamicDiscoveryDocument document = null;
         FileStream input = null;
         try
         {
             input = new FileStream(physicalPath, FileMode.Open, FileAccess.Read);
             XmlTextReader reader = new XmlTextReader(input) {
                 WhitespaceHandling = WhitespaceHandling.Significant,
                 XmlResolver = null,
                 DtdProcessing = DtdProcessing.Prohibit
             };
             if (reader.IsStartElement("dynamicDiscovery", "urn:schemas-dynamicdiscovery:disco.2000-03-17"))
             {
                 input.Position = 0L;
                 document = DynamicDiscoveryDocument.Load(input);
             }
         }
         finally
         {
             if (input != null)
             {
                 input.Close();
             }
         }
         if (document != null)
         {
             DynamicDiscoSearcher searcher;
             string[] excludedUrls = new string[document.ExcludePaths.Length];
             string directoryName = Path.GetDirectoryName(physicalPath);
             string fileName = Path.GetFileName(physicalPath);
             for (int i = 0; i < excludedUrls.Length; i++)
             {
                 excludedUrls[i] = document.ExcludePaths[i].Path;
             }
             Uri url = context.Request.Url;
             string str = Uri.EscapeUriString(url.ToString()).Replace("#", "%23");
             string dirPartOfPath = GetDirPartOfPath(str);
             if ((GetDirPartOfPath(url.LocalPath).Length == 0) || System.ComponentModel.CompModSwitches.DynamicDiscoveryVirtualSearch.Enabled)
             {
                 fileName = GetFilePartOfPath(str);
                 searcher = new DynamicVirtualDiscoSearcher(directoryName, excludedUrls, dirPartOfPath);
             }
             else
             {
                 searcher = new DynamicPhysicalDiscoSearcher(directoryName, excludedUrls, dirPartOfPath);
             }
             bool flag2 = System.ComponentModel.CompModSwitches.DynamicDiscoverySearcher.TraceVerbose;
             searcher.Search(fileName);
             DiscoveryDocument discoveryDocument = searcher.DiscoveryDocument;
             MemoryStream stream = new MemoryStream(0x400);
             StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(false));
             discoveryDocument.Write(writer);
             stream.Position = 0L;
             byte[] buffer = new byte[(int) stream.Length];
             int count = stream.Read(buffer, 0, buffer.Length);
             context.Response.ContentType = ContentType.Compose("text/xml", Encoding.UTF8);
             context.Response.OutputStream.Write(buffer, 0, count);
         }
         else
         {
             context.Response.ContentType = "text/xml";
             context.Response.WriteFile(physicalPath);
         }
         if (Tracing.On)
         {
             Tracing.Exit("IHttpHandler.ProcessRequest", caller);
         }
     }
     else
     {
         if (Tracing.On)
         {
             Tracing.Exit("IHttpHandler.ProcessRequest", caller);
         }
         throw new HttpException(0x194, System.Web.Services.Res.GetString("WebPathNotFound", new object[] { context.Request.Path }));
     }
 }