ScriptCombiner.ProcessRequest C# (CSharp) Method

ProcessRequest() public method

public ProcessRequest ( HttpContext __context ) : void
__context HttpContext
return void
    public void ProcessRequest(HttpContext __context)
    {
        this.context = HttpContextFactory.Current;
        var request = context.Request;
        var response = context.Response;
        response.Clear();

        if (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();
        }
    }

Usage Example

Example #1
0
        //[Test][Ignore("Race condition when running in paralell with other tests")]
        public void serverCode_defaultValues_and_EmptyRequest()
        {
            var scriptCombiner = new ScriptCombiner();

            scriptCombiner.ProcessRequest(null);

            Assert.AreEqual(scriptCombiner.setName, string.Empty, "[empty request] setName");
            Assert.AreEqual(scriptCombiner.version, string.Empty, "[empty request] version");
            Assert.IsNotNull(ScriptCombiner.MappingsLocation, "[empty request] mappingsLocation");

            var responseHtml = context.response_Read_All();

            Assert.AreEqual(EMPTY_RESPONSE, responseHtml, "[empty request] responseHtml should be empty");

            request.QueryString["s"] = "setName";
            request.QueryString["v"] = "version";
            scriptCombiner.ProcessRequest(null);
            Assert.AreEqual(scriptCombiner.setName, "setName", "setName value");
            Assert.AreEqual(scriptCombiner.version, "version", "setName value");

            //test test handshake
            request.QueryString["Hello"] = "TM";
            scriptCombiner.ProcessRequest(null);
            responseHtml = context.response_Read_All();
            Assert.AreEqual(responseHtml, "Good Morning", "handshake");
        }
All Usage Examples Of ScriptCombiner::ProcessRequest