StaticFileHandler when using RemoteVirtualPathProvider

Feb 23, 2009 at 11:18 AM
I had to override ProcessRequestInternal in my own StaticFileHandler to get files from RemoteVirtualPathProvider to work. The problem is that the StaticFileHandler shipped with EPiServer calls method "ProcessVirtualFileEx" for files from RemoteVirtualPathProvider, the correct way to handle this kind of files is to call "ProcessVirtualFile" (I used reflector to grab these functions from EPiServer.Web.StaticFileHandler and adding the condition for "RemoteUnifiedFile").

protected override bool ProcessRequestInternal(HttpContext context)
{
            VirtualFile virtualFile = GenericHostingEnvironment.VirtualPathProvider.GetFile(context.Request.FilePath);
            if (virtualFile == null)
            {
                return ProcessBaseFile(context);
            }
            if (virtualFile.GetType().FullName == "EPiServer.Labs.RemoteProvider.RemoteUnifiedFile")
            {
                return ProcessVirtualFile(virtualFile, context);
            }

            VirtualFileEx virtualFileEx = virtualFile as VirtualFileEx;
            if (virtualFileEx != null)
            {
                return ProcessVirtualFileEx(virtualFileEx, context);
            }
            Type type = virtualFile.GetType();
            if ((type.Namespace == "System.Web.Hosting") && (type.Name == "MapPathBasedVirtualFile"))
            {
                return ProcessBaseFile(context);
            }
            return ProcessVirtualFile(virtualFile, context);
}

private bool ProcessBaseFile(HttpContext context)
{
            string physicalPath = context.Request.PhysicalPath;
            if (!File.Exists(physicalPath))
            {
                return false;
            }
            FileInfo info = new FileInfo(physicalPath);
            if (!this.NotModifiedHandling(context, info.LastWriteTime))
            {
                this.SetCachePolicy(context, info.LastWriteTimeUtc);
                context.Response.ContentType = MimeMapping.GetMimeMapping(info.Name);
                context.Response.StatusCode = 200;
                context.Response.TransmitFile(physicalPath);
            }
            return true;
}

private bool ProcessVirtualFile(VirtualFile virtualFile, HttpContext context)
{
            context.Response.ContentType = MimeMapping.GetMimeMapping(virtualFile.Name);
            context.Response.StatusCode = 200;
            using (Stream stream = virtualFile.Open())
            {
                StreamConsumer.CopyToEnd(stream, context.Response.OutputStream);
            }
            return true;
}

private bool ProcessVirtualFileEx(VirtualFileEx virtualFileEx, HttpContext context)
{
            if (!this.NotModifiedHandling(context, virtualFileEx.Changed))
            {
                this.SetCachePolicy(context, virtualFileEx.Changed.ToUniversalTime());
                if (virtualFileEx.LocalPath == null)
                {
                    return this.ProcessVirtualFile(virtualFileEx, context);
                }
                if (!File.Exists(virtualFileEx.LocalPath))
                {
                    context.Response.StatusCode = 0x194;
                    return true;
                }
                context.Response.ContentType = MimeMapping.GetMimeMapping(virtualFileEx.Name);
                context.Response.StatusCode = 200;
                context.Response.TransmitFile(virtualFileEx.LocalPath);
            }
            return true;
}