[Pkg-javascript-commits] [sockjs-client] 14/434: Simple server shall compress output and support if-modified-since.

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:46:57 UTC 2014


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

tonnerre-guest pushed a commit to branch master
in repository sockjs-client.

commit 2e4452c9638be04ef152769a41b36fc80ccc919c
Author: Marek Majkowski <majek04 at gmail.com>
Date:   Sun Jul 24 17:30:36 2011 +0100

    Simple server shall compress output and support if-modified-since.
---
 bin/simple_http_server.js | 88 +++++++++++++++++++++++++++++++++++++----------
 package.json              |  3 +-
 2 files changed, 71 insertions(+), 20 deletions(-)

diff --git a/bin/simple_http_server.js b/bin/simple_http_server.js
index d5e13fb..1df7105 100644
--- a/bin/simple_http_server.js
+++ b/bin/simple_http_server.js
@@ -2,6 +2,8 @@ var http = require('http');
 var url = require('url');
 var fs = require('fs');
 var path = require('path');
+var compress = require('compress');
+
 
 var port = 8000;
 var host = "0.0.0.0";
@@ -17,29 +19,77 @@ var mimetypes = {
     jpg: 'image/jpeg',
     gif: 'image/gif'
 };
+
+var compressable_ct = ['text', 'application'];
+
+var req_fin = function(statusCode, req, res, content, encoding) {
+    var gz = '';
+    if (!encoding) encoding = 'utf-8';
+    if (compress && content &&
+        'accept-encoding' in req.headers &&
+        req.headers['accept-encoding'].split(',').indexOf('gzip') !== -1 &&
+        compressable_ct.indexOf(res.getHeader('Content-Type').split('/')[0]) !== -1) {
+        var gzip = new compress.Gzip;
+        gzip.init();
+        content = gzip.deflate(content, encoding) + gzip.end();
+        res.setHeader('Content-Encoding', 'gzip');
+        gz = '(gzip)';
+        encoding = 'binary';
+    }
+    // According to: http://code.google.com/speed/page-speed/docs/caching.html
+    // 'vary' effectively disables caching for IE. IE users are
+    // screwed anyway.
+    res.setHeader('Vary', 'Accept-Encoding');
+
+    var cl = '';
+    if (content) {
+        cl = content.length;
+        res.setHeader('Content-Length', content.length);
+    } else {
+        content = '';
+    }
+    try {
+        res.writeHead(statusCode);
+        res.end(content, encoding);
+    } catch (x) {}
+    console.log(req.method, req.url, statusCode, cl, gz);
+};
+
+var fs_decorator = function(req, res, cb) {
+    return function (err, data) {
+        if (!err) {
+            try {
+                cb(data);
+            } catch (x) {
+                err = true;
+                req_fin(500, req, res);
+                console.log('error', x.stack);
+            }
+        } else {
+            // file doesn't exist or can't read it.
+            req_fin(404, req, res);
+        }
+    };
+};
 var handler = function(req, res) {
     var filename = url.parse(req.url).pathname.slice(1) || 'index.html';
-    var cb = function (error, content) {
-        try{
-            if (error) {
-                console.log(req.method, filename, 404);
-                res.writeHead(404);
-                res.end();
-            } else {
-                console.log(req.method, filename, 200, content.length);
-                res.setHeader('Content-length', content.length);
-                var mimetype = mimetypes[path.extname(filename).slice(1)] || 'text/plain';
-                mimetype += '; charset=UTF-8';
-                res.setHeader('Content-type', mimetype);
-                res.writeHead(200, res.headers);
-                res.write(content);
-                res.end();
-            }
-        } catch (x) {
-            console.log(req.method, filename, "(closed)");
+    var cb1, cb2;
+    cb1 = function(stats) {
+        var last_modified = stats.mtime.toGMTString();
+        if (req.headers['if-modified-since'] === last_modified) {
+            req_fin(304, req, res);
+            return;
         }
+        res.setHeader('Last-Modified', last_modified);
+        fs.readFile(filename, fs_decorator(req, res, cb2));
+    };
+    cb2 = function (content) {
+        var mimetype = mimetypes[path.extname(filename).slice(1)] || 'text/plain';
+        mimetype += '; charset=UTF-8';
+        res.setHeader('Content-Type', mimetype);
+        req_fin(200, req, res, content);
     };
-    fs.readFile(filename, cb);
+    fs.stat(filename, fs_decorator(req, res, cb1));
 };
 
 console.log(" [*] Listening on", host + ':' + port);
diff --git a/package.json b/package.json
index cbfb875..daf87e2 100644
--- a/package.json
+++ b/package.json
@@ -4,6 +4,7 @@
         "dependencies": {
                 "uglify-js": "1.0.6",
                 "coffee-script": "1.1.1",
-                "optparse": "1.0.1"
+                "optparse": "1.0.1",
+                "compress": "0.1.9"
         }
 }

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



More information about the Pkg-javascript-commits mailing list