Exemplo n.º 1
0
        // 对请求上下文进行处理
        public void ProcessRequest(HttpContext context)
        {
            if (string.IsNullOrEmpty(webStationName))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("网站未正确配置,请检查!");
                return;
            }
            // 1.获取网站根路径
            string bastPath      = AppDomain.CurrentDomain.BaseDirectory;
            string fileName      = Path.Combine(bastPath + "\\" + webStationName, context.Request.Url.TrimStart('/'));
            string fileExtension = Path.GetExtension(context.Request.Url);

            // 2.处理动态文件请求
            if (fileExtension.Equals(".aspx") || fileExtension.Equals(".ashx"))
            {
                string className = Path.GetFileNameWithoutExtension(context.Request.Url);
                System.Web.IHttpHandler handler = Assembly.Load(webStationName).CreateInstance(webStationName + "." + className) as System.Web.IHttpHandler;


                var request  = context.Request;
                var response = context.Response;
                System.Web.HttpRequest req = new System.Web.HttpRequest("", request.Url, "");

                //var webcontext = new System.Web.HttpContext(request, response);
                //handler.ProcessRequest(webcontext);

                //var cnt = (System.Web.HttpContext)context;
                //handler.ProcessRequest(cnt);

                //IHttpHandler handler = instance as IHttpHandler;
                //handler.ProcessRequest(context);

                return;
            }
            // 3.处理静态文件请求
            if (!File.Exists(fileName))
            {
                context.Response.StateCode        = "404";
                context.Response.StateDescription = "Not Found";
                context.Response.ContentType      = "text/html";
                string notExistHtml = Path.Combine(bastPath, webStationName + @"\404.html");
                context.Response.Body = File.ReadAllBytes(notExistHtml);
            }
            else
            {
                context.Response.StateCode        = "200";
                context.Response.StateDescription = "OK";
                context.Response.ContentType      = GetContenType(Path.GetExtension(context.Request.Url));
                context.Response.Body             = File.ReadAllBytes(fileName);
            }
        }
Exemplo n.º 2
0
        public void ProcessRequest(System.Web.HttpContext context)
        {
            var checker = new XamlBuildCheck()
            {
                Context = context
            };

            if (handler != null || checker.NeedsBuilding())
            {
                if (handler == null)
                {
                    try {
#if Silversite
                        var handlerInfo = Services.Lazy.Types.Info("XamlImageConverter.XamlImageHandler");
                        handlerInfo.Load();
                        handler = handlerInfo.New <IHttpHandler>();
#else
                        //AppDomain.CurrentDomain.AppendPrivatePath("Lazy\\Awesomium");
                        lock (Lock) {
                            if (!AssemblyResolverInitialized)
                            {
                                int resolving = 0;
                                AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
                                    try {
                                        lock (Lock) {
                                            resolving++;
                                            if (resolving > 1)
                                            {
                                                return(null);
                                            }
                                            var aname = new AssemblyName(args.Name);
                                            aname.CodeBase = context.Request.MapPath("~/Bin/Lazy/" + aname.Name + ".dll");
                                            var n = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(l => l.GetName().Name == args.Name) ?? System.Reflection.Assembly.Load(aname);
                                            return(n);
                                        }
                                    } catch {
                                        return(null);
                                    } finally {
                                        resolving--;
                                    }
                                };
                                AssemblyResolverInitialized = true;
                            }
                        }
                        var aname2 = new AssemblyName("XamlImageConverter");
                        var a      = Assembly.Load(aname2);
                        var type   = a.GetType("XamlImageConverter.XamlImageHandler");

                        handler = (System.Web.IHttpHandler)Activator.CreateInstance(type);
#endif
                    } catch {
                        context.Response.StatusCode = 500;
                        context.ApplicationInstance.CompleteRequest();
                    }
                }
#if Silversite
                context.Application.Lock();
                context.Application["XamlImageConverter.Configuration.UseService"]     = Configuration.UseService;
                context.Application["XamlImageConverter.Configuration.Log"]            = Configuration.Log;
                context.Application["XamlImageConverter.Configuration.Cache"]          = Configuration.Cache;
                context.Application["XamlImageConverter.Configuration.SeparateDomain"] = Configuration.SeparateDomain;
                context.Application["XamlImageConverter.Configuration.GCLevel"]        = Configuration.GCLevel;
                context.Application.UnLock();
#endif
                handler.ProcessRequest(context);
            }
            else
            {
                var filename = context.Request.AppRelativeCurrentExecutionFilePath;
                var image    = context.Request.QueryString["Image"] ?? context.Request.QueryString["File"] ?? context.Request.QueryString["Filename"];
                var ext      = Path.GetExtension(filename).ToLower();
                if (ext == ".xaml" || ext == ".psd" || ext == ".svg" || ext == ".svgz")
                {
                    var exts = context.Request.QueryString.GetValues(null);
                    if (exts != null && exts.Length > 0)
                    {
                        ext   = exts[0];
                        image = image ?? filename + "." + ext;
                    }
                }
                var name = System.IO.Path.GetFileName(image);
                image = context.Server.MapPath(image);
                ext   = System.IO.Path.GetExtension(image);
                if (!string.IsNullOrEmpty(ext))
                {
                    ext = ext.Substring(1).ToLower();
                }

                switch (ext)
                {
                case "bmp": context.Response.ContentType = "image/bmp"; break;

                case "png": context.Response.ContentType = "image/png"; break;

                case "jpg":
                case "jpeg":    context.Response.ContentType = "image/jpeg"; break;

                case "tif":
                case "tiff": context.Response.ContentType = "image/tiff"; break;

                case "gif": context.Response.ContentType = "image/gif"; break;

                case "wdp": context.Response.ContentType = "image/vnd.ms-photo"; break;

                case "pdf":
                    context.Response.AppendHeader("content-disposition", "attachment; filename=" + name);
                    context.Response.ContentType = "application/pdf"; break;

                case "xps":
                    context.Response.AppendHeader("content-disposition", "attachment; filename=" + name);
                    context.Response.ContentType = "application/vnd.ms-xpsdocument"; break;

                case "eps":
                case "ps":
                    context.Response.AppendHeader("content-disposition", "attachment; filename=" + name);
                    context.Response.ContentType = "application/postscript"; break;

                case "psd":
                    context.Response.AppendHeader("content-disposition", "attachment; filename=" + name);
                    context.Response.ContentType = "image/photoshop"; break;

                case "svg":
                case "svgz": context.Response.ContentType = "image/svg+xml"; break;

                case "xaml": context.Response.ContentType = "application/xaml+xml"; break;

                default: break;
                }

                if (System.IO.File.Exists(image))
                {
                    context.Response.WriteFile(image);
                }
                else
                {
                    context.Response.StatusCode = 404;
                }
                context.ApplicationInstance.CompleteRequest();
            }
        }
        public void ProcessRequest(System.Web.HttpContext context)
        {
            var checker = new XamlBuildCheck() { Context = context };

            if (handler != null || checker.NeedsBuilding()) {
                if (handler == null) {
                    try {
            #if Silversite
                        var handlerInfo = Services.Lazy.Types.Info("XamlImageConverter.XamlImageHandler");
                        handlerInfo.Load();
                        handler = handlerInfo.New<IHttpHandler>();
            #else
                        var a = Assembly.LoadFrom(context.Server.MapPath("~/Bin/Lazy/XamlImageConverter.dll"));
                        var type = a.GetType("XamlImageConverter.XamlImageHandler");
                        handler = (System.Web.IHttpHandler)Activator.CreateInstance(type);
            #endif
                    } catch {
                        context.Response.StatusCode = 500;
                        context.ApplicationInstance.CompleteRequest();
                    }
                }
            #if Silversite
                context.Application.Lock();
                context.Application["XamlImageConverter.Configuration.UseService"] = Configuration.UseService;
                context.Application["XamlImageConverter.Configuration.Log"] = Configuration.Log;
                context.Application["XamlImageConverter.Configuration.Cache"] = Configuration.Cache;
                context.Application["XamlImageConverter.Configuration.SeparateDomain"] = Configuration.SeparateDomain;
                context.Application["XamlImageConverter.Configuration.GCLevel"] = Configuration.GCLevel;
                context.Application.UnLock();
            #endif
                handler.ProcessRequest(context);
            } else {
                var filename = context.Request.AppRelativeCurrentExecutionFilePath;
                var image = context.Request.QueryString["Image"] ?? context.Request.QueryString["File"] ?? context.Request.QueryString["Filename"];
                var ext = Path.GetExtension(filename).ToLower();
                if (ext == ".xaml" || ext == ".psd" || ext == ".svg" || ext == ".svgz") {
                    var exts = context.Request.QueryString.GetValues(null);
                    if (exts != null && exts.Length > 0) {
                        ext = exts[0];
                        image = image ?? filename + "." + ext;
                    }
                }
                var name = System.IO.Path.GetFileName(image);
                image = context.Server.MapPath(image);
                ext = System.IO.Path.GetExtension(image);
                if (!string.IsNullOrEmpty(ext)) ext = ext.Substring(1).ToLower();

                switch (ext) {
                    case "bmp": context.Response.ContentType = "image/bmp"; break;
                    case "png": context.Response.ContentType = "image/png"; break;
                    case "jpg":
                    case "jpeg":	context.Response.ContentType = "image/jpeg"; break;
                    case "tif":
                    case "tiff": context.Response.ContentType = "image/tiff"; break;
                    case "gif": context.Response.ContentType = "image/gif"; break;
                    case "wdp": context.Response.ContentType = "image/vnd.ms-photo"; break;
                    case "pdf":
                        context.Response.AppendHeader("content-disposition", "attachment; filename=" + name);
                        context.Response.ContentType = "application/pdf"; break;
                    case "xps":
                        context.Response.AppendHeader("content-disposition", "attachment; filename=" + name);
                        context.Response.ContentType = "application/vnd.ms-xpsdocument"; break;
                    case "eps":
                    case "ps":
                        context.Response.AppendHeader("content-disposition", "attachment; filename=" + name);
                        context.Response.ContentType = "application/postscript"; break;
                    case "psd":
                        context.Response.AppendHeader("content-disposition", "attachment; filename=" + name);
                        context.Response.ContentType = "image/photoshop"; break;
                    case "svg":
                    case "svgz": context.Response.ContentType = "image/svg+xml"; break;
                    case "xaml": context.Response.ContentType = "application/xaml+xml"; break;
                    default: break;
                }

                if (System.IO.File.Exists(image)) context.Response.WriteFile(image);
                else {
                    context.Response.StatusCode = 404;
                }
                context.ApplicationInstance.CompleteRequest();
            }
        }