Exemple #1
0
        public API_ScriptCombiner CombineFiles()
        {
            if (baseFolder.dirExists().isFalse())
            {
                "[API_ScriptCombiner] baseFolder Not set".error();
                return(this);
            }
            using (var 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
                    allScripts     = new StringBuilder();
                    filesProcessed = GetScriptFileNames(setName);
                    foreach (string fileName in filesProcessed)
                    {
                        var fullPath = baseFolder.pathCombine(fileName.trim());

                        if (fullPath.contains(baseFolder).isFalse())
                        {
                            "[API_ScriptCombiner][CombineFiles] resolved full path ('{0}') did not contain baseFolder ('{1}')".format(fullPath, baseFolder);
                        }
                        if (fullPath.fileExists())
                        {
                            allScripts.AppendLine("\n\n/********************************** ");
                            allScripts.AppendLine(" *****    " + fileName);
                            allScripts.AppendLine(" **********************************/\n\n");
                            allScripts.AppendLine(File.ReadAllText(fullPath));
                        }
                    }

                    var codeToSend = allScripts.ToString();

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

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

                // Generate the response
                CombinedBytes = memoryStream.ToArray();
            }
            return(this);
        }
        public void ProcessRequest(HttpContext httpContext)
        {
            context = HttpContextFactory.Current;
            var request  = context.Request;
            var response = context.Response;

            response.Clear();

            if (context.sent304Redirect())
            {
                return;
            }
            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":
                    contentType = "text/css";
                    minifyCode  = false;
                    break;

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

                using (var 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
                        allScripts     = new StringBuilder();
                        filesProcessed = GetScriptFileNames(setName);
                        foreach (string fileName in filesProcessed)
                        {
                            var fullPath = context.Server.MapPath(fileName.trim());

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

                        var codeToSend = allScripts.ToString();

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

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

                    // Generate the response
                    var responseBytes = memoryStream.ToArray();
                    WriteBytes(responseBytes, isCompressed);
                }
            }
            catch (Exception ex)
            {
                ex.log();
                response.Write("//Error processing request" + ex.Message);
                response.End();
            }
        }
Exemple #3
0
        public API_ScriptCombiner CombineFiles()
        {
            if (baseFolder.dirExists().isFalse())
            {
                "[API_ScriptCombiner] baseFolder Not set".error();
                return this;
            }
            using (var 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
                    allScripts = new StringBuilder();
                    filesProcessed = GetScriptFileNames(setName);
                    foreach (string fileName in filesProcessed)
                    {
                        var fullPath = baseFolder.pathCombine(fileName.trim());

                        if(fullPath.contains(baseFolder).isFalse())
                            "[API_ScriptCombiner][CombineFiles] resolved full path ('{0}') did not contain baseFolder ('{1}')".format(fullPath, baseFolder);
                        if (fullPath.fileExists())
                        {
                            allScripts.AppendLine("\n\n/********************************** ");
                            allScripts.AppendLine(" *****    " + fileName);
                            allScripts.AppendLine(" **********************************/\n\n");
                            allScripts.AppendLine(File.ReadAllText(fullPath));
                        }
                    }

                    var codeToSend = allScripts.ToString();

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

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

                // Generate the response
                CombinedBytes = memoryStream.ToArray();
            }
            return this;
        }
Exemple #4
0
        public void ProcessRequest(HttpContext httpContext)
        {
            context = HttpContextFactory.Current;
            var request = context.Request;
            var response = context.Response;
            response.Clear();

            if (TMConfig.Current.TMSetup.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":
                        contentType = "text/css";
                        minifyCode = false;
                        break;
                    default:
                        contentType = "application/x-javascript";
                        break;
                }
                // Decide if browser supports compressed response
                bool isCompressed = CanGZip(context.Request);

                using (var 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
                        allScripts = new StringBuilder();
                        filesProcessed = GetScriptFileNames(setName);
                        foreach (string fileName in filesProcessed)
                        {
                            var fullPath = context.Server.MapPath(fileName.trim());

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

                        var codeToSend = allScripts.ToString();

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

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

                    // Generate the response
                    var responseBytes = memoryStream.ToArray();
                    WriteBytes(responseBytes, isCompressed);

                }
            }
            catch(Exception ex)
            {
                ex.log();
                response.Write("//Error processing request"+  ex.Message);
                response.End();
            }
        }