示例#1
0
    public void ProcessRequest(HttpContext __context)
    {
        this.context = HttpContextFactory.Current;
        var request  = context.Request;
        var response = context.Response;

        response.Clear();

        if (TMConfig.Current.TMDebugAndDev.Enable302Redirects && send304Redirect())
        {
            context.Response.StatusCode        = 304;
            context.Response.StatusDescription = "Not Modified";
            return;
        }
        setCacheHeaders();

        try
        {
            minifyCode  = true;
            ignoreCache = true;
            if (request.QueryString["Hello"] == "TM")
            {
                response.Write("Good Morning");
                return;
            }

            // Read setName, version from query string
            setName = XssEncoder.UrlEncode(request.QueryString["s"]) ?? string.Empty;
            version = XssEncoder.UrlEncode(request.QueryString["v"]) ?? string.Empty;

            if (setName == string.Empty)
            {
                response.Write("//nothing to do");
                return;
            }

            if (request.QueryString["dontMinify"] == "true")
            {
                minifyCode = false;
            }

            switch (request.QueryString["ct"])
            {
            case "css":
                this.contentType = "text/css";
                minifyCode       = false;
                break;

            default:
                this.contentType = "application/x-javascript";
                break;
            }
            // Decide if browser supports compressed response
            bool isCompressed = this.CanGZip(context.Request);

            using (MemoryStream memoryStream = new MemoryStream(8092))
            {
                // Decide regular stream or gzip stream based on whether the response can be compressed or not
                //using (Stream writer = isCompressed ?  (Stream)(new GZipStream(memoryStream, CompressionMode.Compress)) : memoryStream)
                using (Stream writer = isCompressed ? (Stream)(new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(memoryStream)) : memoryStream)
                {
                    // Read the files into one big string
                    this.allScripts     = new StringBuilder();
                    this.filesProcessed = GetScriptFileNames(setName);
                    foreach (string fileName in this.filesProcessed)
                    {
                        var fullPath = context.Server.MapPath(fileName.trim());

                        if (fullPath.fileExists())
                        {
                            this.allScripts.AppendLine("\n\n/********************************** ");
                            this.allScripts.AppendLine(" *****    " + fileName);
                            this.allScripts.AppendLine(" **********************************/\n\n");
                            this.allScripts.AppendLine(File.ReadAllText(fullPath));
                        }
                    }

                    var codeToSend = this.allScripts.ToString();

                    if (minifyCode)
                    {
                        // Minify the combined script files and remove comments and white spaces
                        var minifier = new JavaScriptMinifier();
                        this.minifiedCode = minifier.Minify(codeToSend);
                        codeToSend        = this.minifiedCode;
                    }

                    // Send minfied string to output stream
                    byte[] bts = Encoding.UTF8.GetBytes(codeToSend);
                    writer.Write(bts, 0, bts.Length);
                }

                // Generate the response
                byte[] responseBytes = memoryStream.ToArray();
                this.WriteBytes(responseBytes, isCompressed);
            }
        }
        catch (Exception ex)
        {
            ex.log();
            response.Write("//Error processing request" + ex.Message);
            response.End();
        }
    }