Created
July 11, 2014 07:32
-
-
Save jtyjty99999/76625f0219d1813ab992 to your computer and use it in GitHub Desktop.
gzip&cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var url = require("url"); | |
| var fs = require("fs"); | |
| var path = require("path"); | |
| var mime = require("./mime").types; | |
| var config = require("./config"); | |
| var utils = require("./utils"); | |
| var zlib = require("zlib"); | |
| var staticHandler = function () {}; | |
| staticHandler.prototype.dispatch = function (request, response) { | |
| response.setHeader("Server", "ICBU-SERVER"); | |
| var pathname = url.parse(request.url).pathname; | |
| if (pathname.slice(-1) === "/") { | |
| pathname = pathname + config.Welcome.file; | |
| } | |
| var realPath = path.join("static", path.normalize(pathname.replace(/\.\./g, ""))); | |
| var pathHandle = function (realPath) { | |
| fs.stat(realPath, function (err, stats) { | |
| if (err) { | |
| response.writeHead(404, "Not Found", {'Content-Type': 'text/plain'}); | |
| response.write("This request URL " + pathname + " was not found on this server."); | |
| response.end(); | |
| } else { | |
| if (stats.isDirectory()) { | |
| realPath = path.join(realPath, "/", config.Welcome.file); | |
| pathHandle(realPath); | |
| } else { | |
| var ext = path.extname(realPath); | |
| ext = ext ? ext.slice(1) : 'unknown'; | |
| var contentType = mime[ext] || "text/plain"; | |
| response.setHeader("Content-Type", contentType); | |
| response.setHeader('Content-Length', stats.size); | |
| var lastModified = stats.mtime.toUTCString(); | |
| response.setHeader("Last-Modified", lastModified); | |
| if (ext.match(config.Expires.fileMatch)) { | |
| var expires = new Date(); | |
| expires.setTime(expires.getTime() + config.Expires.maxAge * 1000); | |
| response.setHeader("Expires", expires.toUTCString()); | |
| response.setHeader("Cache-Control", "max-age=" + config.Expires.maxAge); | |
| } | |
| if (request.headers['if-modified-since'] && lastModified == request.headers['if-modified-since']) { | |
| response.writeHead(304, "Not Modified"); | |
| response.end(); | |
| } else { | |
| var compressHandle = function (raw, statusCode, reasonPhrase) { | |
| var stream = raw; | |
| var acceptEncoding = request.headers['accept-encoding'] || ""; | |
| var matched = ext.match(config.Compress.match); | |
| if (matched && acceptEncoding.match(/\bgzip\b/)) { | |
| response.setHeader("Content-Encoding", "gzip"); | |
| stream = raw.pipe(zlib.createGzip()); | |
| } else if (matched && acceptEncoding.match(/\bdeflate\b/)) { | |
| response.setHeader("Content-Encoding", "deflate"); | |
| stream = raw.pipe(zlib.createDeflate()); | |
| } | |
| response.writeHead(statusCode, reasonPhrase); | |
| stream.pipe(response); | |
| }; | |
| var raw = fs.createReadStream(realPath); | |
| compressHandle(raw, 200); | |
| } | |
| } | |
| } | |
| }); | |
| }; | |
| pathHandle(realPath); | |
| }; | |
| exports.staticHandler = staticHandler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment