[Pkg-javascript-commits] [node-static] 126/151: Add static gzip file support, where a gzip file with a matching name but adding .gz to the end can be served up to clients that support it. As part of that, changed the code so the Content-Type and Content-Length are sent even for an HTTP HEAD request.

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Tue Jan 7 23:18:03 UTC 2014


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch master
in repository node-static.

commit 16babca6ead382026d1dea24f9f3033393c75859
Author: Dobes Vandermeer <dobesv at gmail.com>
Date:   Wed Nov 2 13:11:08 2011 +0800

    Add static gzip file support, where a gzip file with a matching name but adding .gz to the end can be served up to clients that support it.  As part of that, changed the code so the Content-Type and Content-Length are sent even for an HTTP HEAD request.
---
 lib/node-static.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/lib/node-static.js b/lib/node-static.js
index e48263f..9300dc0 100644
--- a/lib/node-static.js
+++ b/lib/node-static.js
@@ -201,21 +201,78 @@ Server.prototype.serve = function (req, res, callback) {
     if (! callback) { return promise }
 };
 
+<<<<<<< HEAD
 Server.prototype.respond = function (pathname, status, _headers, files, stat, req, res, finish) {
     var mtime           = Date.parse(stat.mtime),
         key             = pathname || files[0],
         headers         = {},
         clientETag      = req.headers['if-none-match'],
         clientMTime     = Date.parse(req.headers['if-modified-since']);
+=======
+/* Check if we should consider sending a gzip version of the file based on the
+ * file content type and client's Accept-Encoding header value.
+ */
+this.Server.prototype.gzipOk = function(req, contentType) {
+    var enable = this.options.gzip;
+    if(enable && 
+        (typeof enable === 'boolean' || 
+            (contentType && (enable instanceof RegExp) && enable.test(contentType)))) {
+        var acceptEncoding = req.headers['accept-encoding'];
+        return acceptEncoding.indexOf("gzip") >= 0;
+    }
+    return false;
+}
+
+/* Send a gzipped version of the file if the options and the client indicate gzip is enabled and
+ * we find a .gz file matching the static resource requested. 
+ */
+this.Server.prototype.respondGzip = function(pathname, status, contentType, _headers, files, stat, req, res, finish) {
+    var that = this;
+    if(files.length == 1 && this.gzipOk(req)) {
+        var gzFile = files[0] + ".gz";
+        fs.stat(gzFile, function(e, gzStat) {
+            if(!e && gzStat.isFile()) {
+                //console.log('Serving', gzFile, 'to gzip-capable client instead of', files[0], 'new size is', gzStat.size, 'uncompressed size', stat.size);
+                var vary = _headers['Vary'];
+                _headers['Vary'] = (vary && vary != 'Accept-Encoding'?vary+', ':'')+'Accept-Encoding';
+                _headers['Content-Encoding'] = 'gzip';
+                stat.size = gzStat.size;
+                files = [gzFile];
+            } else {
+                // console.log('gzip file not found or error finding it', gzFile, String(e), stat.isFile());
+            }
+            that.respondNoGzip(pathname, status, contentType, _headers, files, stat, req, res, finish);
+        });
+    } else {
+        // Client doesn't want gzip or we're sending multiple files
+        that.respondNoGzip(pathname, status, contentType, _headers, files, stat, req, res, finish);
+    }
+}
+
+this.Server.prototype.respondNoGzip = function (pathname, status, contentType, _headers, files, stat, req, res, finish) {
+    var mtime   = Date.parse(stat.mtime),
+        key     = pathname || files[0],
+        headers = {};
+>>>>>>> Add static gzip file support, where a gzip file with a matching name but adding .gz to the end can be served up to clients that support it.  As part of that, changed the code so the Content-Type and Content-Length are sent even for an HTTP HEAD request.
 
     // Copy default headers
     for (var k in this.options.headers) {  headers[k] = this.options.headers[k] }
     // Copy custom headers
     for (var k in _headers) { headers[k] = _headers[k] }
 
+<<<<<<< HEAD
     headers['etag']          = JSON.stringify([stat.ino, stat.size, mtime].join('-'));
     headers['date']          = new(Date)().toUTCString();
     headers['last-modified'] = new(Date)(stat.mtime).toUTCString();
+=======
+    headers['Etag']          = JSON.stringify([stat.ino, stat.size, mtime].join('-'));
+    headers['Date']          = new(Date)().toUTCString();
+    headers['Last-Modified'] = new(Date)(stat.mtime).toUTCString();
+    headers['Content-Type']   = contentType;
+    headers['Content-Length'] = stat.size;
+    
+    for (var k in _headers) { headers[k] = _headers[k] }
+>>>>>>> Add static gzip file support, where a gzip file with a matching name but adding .gz to the end can be served up to clients that support it.  As part of that, changed the code so the Content-Type and Content-Length are sent even for an HTTP HEAD request.
 
     // Conditional GET
     // If the "If-Modified-Since" or "If-None-Match" headers
@@ -225,9 +282,12 @@ Server.prototype.respond = function (pathname, status, _headers, files, stat, re
         (!clientMTime || clientMTime >= mtime)) {
         finish(304, headers);
     } else {
+<<<<<<< HEAD
         headers['content-length'] = stat.size;
         headers['content-type']   = mime.lookup(files[0]);
                                    'application/octet-stream';
+=======
+>>>>>>> Add static gzip file support, where a gzip file with a matching name but adding .gz to the end can be served up to clients that support it.  As part of that, changed the code so the Content-Type and Content-Length are sent even for an HTTP HEAD request.
 
         res.writeHead(status, headers);
 
@@ -256,7 +316,22 @@ Server.prototype.respond = function (pathname, status, _headers, files, stat, re
     }
 };
 
+<<<<<<< HEAD
 Server.prototype.stream = function (pathname, files, buffer, res, callback) {
+=======
+this.Server.prototype.respond = function (pathname, status, _headers, files, stat, req, res, finish) {
+    var contentType = _headers['Content-Type'] || 
+                      mime.contentTypes[path.extname(files[0]).slice(1)] ||
+                      'application/octet-stream';
+    if(this.options.gzip) {
+        this.respondGzip(pathname, status, contentType, _headers, files, stat, req, res, finish);
+    } else {
+        this.respondNoGzip(pathname, status, contentType, _headers, files, stat, req, res, finish);
+    }
+}
+
+this.Server.prototype.stream = function (pathname, files, buffer, res, callback) {
+>>>>>>> Add static gzip file support, where a gzip file with a matching name but adding .gz to the end can be served up to clients that support it.  As part of that, changed the code so the Content-Type and Content-Length are sent even for an HTTP HEAD request.
     (function streamFile(files, offset) {
         var file = files.shift();
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-static.git



More information about the Pkg-javascript-commits mailing list