From 59921b2ac8103d20d3374e289dcbd3a1d41865b0 Mon Sep 17 00:00:00 2001 From: Sam Olsen Date: Sun, 11 Aug 2013 10:50:28 -0700 Subject: [PATCH] Concatenate POST body data instead of using Array.prototype.join. This is faster on V8. http://jsperf.com/array-join-vs-string-connect --- bin/mk-server | 2 +- lib/router.js | 54 ++++++++++++++-------- src/router.coffee | 5 +- test/common/public/scgi/hellonode/hello.js | 2 +- test/common/scgi.js | 2 +- test/common/test_router.coffee | 2 +- test/common/test_router.js | 38 +++++++++++---- test/uploader/server.js | 7 +-- 8 files changed, 74 insertions(+), 38 deletions(-) diff --git a/bin/mk-server b/bin/mk-server index cea5cca..59e0087 100755 --- a/bin/mk-server +++ b/bin/mk-server @@ -1,6 +1,6 @@ #!/usr/bin/env node -// Generated by CoffeeScript 1.4.0 +// Generated by CoffeeScript 1.6.3 (function() { var cs, filename, fs, full_filename, js, text, _ref, _ref1; diff --git a/lib/router.js b/lib/router.js index ad35daf..4f1e1ed 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1,9 +1,9 @@ -// Generated by CoffeeScript 1.4.0 +// Generated by CoffeeScript 1.6.3 (function() { var Router; Router = function(options) { - var default_options, dispatch, domain, escaped_icon, fs, mime_types, net, path_tools, querystring, spawn, urlparse, _bodyparser, _dirlist_template, _extend, _make_request_wrapper, _multipartparser, _parsePattern, _pushRoute; + var default_options, dispatch, domain, escaped_icon, fs, http, mime_types, net, path_tools, querystring, spawn, urlparse, _bodyparser, _dirlist_template, _extend, _make_request_wrapper, _multipartparser, _parsePattern, _pushRoute; if (options == null) { options = {}; } @@ -14,6 +14,7 @@ spawn = require('child_process').spawn; domain = require('domain'); net = require('net'); + http = require('http'); mime_types = { '': 'application/octet-stream', '.bin': 'application/octet-stream', @@ -110,7 +111,7 @@ var wrapper; wrapper = function(req, res) { var body, contentType, mp_index; - body = []; + body = ''; contentType = 'application/x-www-form-urlencoded'; if (req.headers['content-type']) { contentType = req.headers['content-type']; @@ -120,10 +121,10 @@ req.setEncoding('binary'); } req.on('data', function(chunk) { - return body.push(chunk); + return body += chunk; }); return req.on('end', function() { - body = body.join(''); + var e; if (contentType === 'text/plain') { body = body.replace('\r\n', ''); } @@ -131,7 +132,8 @@ req.body = _extend(req.body, req.post); try { return cb(req, res); - } catch (e) { + } catch (_error) { + e = _error; return dispatch._500(req, res, req.url, e.toString()); } }); @@ -139,7 +141,7 @@ return wrapper; }; dispatch = function(req, res) { - var args, full_path, home_page, index, m, method, param, parsed, pathname, route, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2; + var args, error, full_path, home_page, index, m, method, param, parsed, pathname, route, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2; parsed = urlparse(req.url); pathname = parsed.pathname; req.get = parsed.query != null ? querystring.parse(parsed.query) : {}; @@ -174,7 +176,8 @@ if (fs.existsSync(full_path)) { return dispatch["static"]("/" + home_page, req, res); } - } catch (error) { + } catch (_error) { + error = _error; if (!!dispatch.logging) { dispatch.log(error.toString()); } @@ -256,10 +259,12 @@ return resp; }; _bodyparser = function(body) { + var e; if (body.indexOf('=') !== -1) { try { return querystring.parse(body); - } catch (e) { + } catch (_error) { + e = _error; if (!!dispatch.logging) { dispatch.log(e); } @@ -267,7 +272,8 @@ } try { return JSON.parse(body); - } catch (e) { + } catch (_error) { + e = _error; if (!!dispatch.logging) { dispatch.log(e); } @@ -284,11 +290,13 @@ var full_path; full_path = "" + dispatch.static_route + (unescape(pathname)); return fs.exists(full_path, function(exists) { + var e; if (exists) { if (((pathname.indexOf("" + dispatch.cgi_dir + "/") !== -1) || (pathname.match(/\.php$/))) && (pathname.substr(-1) !== "/") && (dispatch.serve_cgi === true)) { try { return dispatch.cgi(pathname, req, res); - } catch (e) { + } catch (_error) { + e = _error; if (!!dispatch.logging) { dispatch.log(e.toString()); } @@ -372,7 +380,7 @@ return env; }; dispatch.cgi = function(pathname, req, res) { - var body, child, d, data, env, full_path, isPHP, prepareChild, respbuffer, urlobj; + var body, child, d, data, e, env, full_path, isPHP, prepareChild, respbuffer, urlobj; urlobj = urlparse(req.url); respbuffer = ''; full_path = "" + dispatch.static_route + (unescape(pathname)); @@ -408,7 +416,7 @@ } child.stderr.pipe(process.stderr); child.stdout.on('data', function(data) { - var arrdata, elem, pair, _i, _len, _results; + var arrdata, e, elem, pair, _i, _len, _results; arrdata = data.toString().split('\n'); _results = []; for (_i = 0, _len = arrdata.length; _i < _len; _i++) { @@ -419,7 +427,8 @@ pair = elem.split(/:\s+/); try { _results.push(res.setHeader(pair[0], pair[1])); - } catch (e) { + } catch (_error) { + e = _error; if (!!dispatch.logging) { _results.push(dispatch.log("Error setting response header: " + e.message)); } else { @@ -431,12 +440,14 @@ return _results; }); child.stdout.on('end', function(moredata) { + var e; try { if (!!moredata) { respbuffer += moredata; } return res.end(respbuffer); - } catch (e) { + } catch (_error) { + e = _error; if (!!dispatch.logging) { return dispatch.log("Error terminating response: " + e.message); } @@ -450,7 +461,7 @@ return body.push(chunk); }); req.on('end', function() { - var child, d, data; + var child, d, data, e; body = body.join(''); req.post = _bodyparser(body); req.body = _extend(req.body, req.post); @@ -474,7 +485,8 @@ child.stdin.write("" + data + "\n"); return child.stdin.end(); }); - } catch (e) { + } catch (_error) { + e = _error; if (!!dispatch.logging) { return dispatch.log("Child process input error: " + e.message); } @@ -501,7 +513,8 @@ child.stdin.write("" + data + "\n"); return child.stdin.end(); }); - } catch (e) { + } catch (_error) { + e = _error; if (!!dispatch.logging) { dispatch.log("Child process input error: " + e.message); } @@ -622,6 +635,11 @@ }); return d.run(getData); }; + dispatch.proxy_pass = function(url, response) { + return http.get(url, function(res) { + return res.pipe(response); + }); + }; dispatch.directory = function(fpath, path, res) { var resp; resp = _dirlist_template; diff --git a/src/router.coffee b/src/router.coffee index 0c86cfe..06b5c99 100644 --- a/src/router.coffee +++ b/src/router.coffee @@ -102,16 +102,15 @@ Router = (options = {}) -> _make_request_wrapper = (cb) -> wrapper = (req, res) -> - body = [] + body = '' contentType = 'application/x-www-form-urlencoded' if req.headers['content-type'] contentType = req.headers['content-type'] mp_index = contentType.indexOf('multipart/form-data') req.setEncoding('binary') if (mp_index isnt -1) req.on 'data', (chunk) -> - body.push chunk + body += chunk req.on 'end', () -> - body = body.join('') if contentType is 'text/plain' body = body.replace('\r\n', '') req.post = if mp_index is -1 then _bodyparser(body) else _multipartparser(body, contentType) diff --git a/test/common/public/scgi/hellonode/hello.js b/test/common/public/scgi/hellonode/hello.js index 79ed921..138a0b9 100644 --- a/test/common/public/scgi/hellonode/hello.js +++ b/test/common/public/scgi/hellonode/hello.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.4.0 +// Generated by CoffeeScript 1.6.3 (function() { var fs, handler, net, server; diff --git a/test/common/scgi.js b/test/common/scgi.js index 9bec5f5..bea2479 100644 --- a/test/common/scgi.js +++ b/test/common/scgi.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.4.0 +// Generated by CoffeeScript 1.6.3 (function() { var Router, argv, child, childPath, domain, fs, http, net, querystring, router, server, spawn; diff --git a/test/common/test_router.coffee b/test/common/test_router.coffee index 3765fb2..304c70d 100755 --- a/test/common/test_router.coffee +++ b/test/common/test_router.coffee @@ -1,7 +1,7 @@ #!/usr/bin/env coffee -Router = require '../../src/router' +Router = require '../../lib/router' http = require 'http' diff --git a/test/common/test_router.js b/test/common/test_router.js index 7ea180e..7dbfdda 100755 --- a/test/common/test_router.js +++ b/test/common/test_router.js @@ -1,14 +1,10 @@ #!/usr/bin/env node -// Generated by CoffeeScript 1.4.0 +// Generated by CoffeeScript 1.6.3 (function() { var Router, argv, http, router, server; - try { - Router = require('../../src/router'); - } catch (e) { - Router = require('../../lib/router'); - } + Router = require('../../lib/router'); http = require('http'); @@ -33,7 +29,7 @@ }); router.post("/users", function(req, res) { - var key, val, _ref, _ref1, _ref2; + var e, key, val, _ref, _ref1, _ref2; router.log("\n\nBody of request is: " + (req.body.toString()) + "\nRequest content type is: " + req.headers['content-type']); router.log("\nRequest Headers"); _ref = req.headers; @@ -49,7 +45,8 @@ val = _ref1[key]; router.log("" + key + ": " + val); } - } catch (e) { + } catch (_error) { + e = _error; res.write("Looks like you did something dumb: " + (e.toString()) + "\n"); } _ref2 = req.body; @@ -72,7 +69,7 @@ }); router.post("/showrequest", function(req, res) { - var key, stri, val; + var e, key, stri, val; res.writeHead(200, { 'Content-type': 'text/plain' }); @@ -87,7 +84,8 @@ router.log(stri); } res.write(stri); - } catch (e) { + } catch (_error) { + e = _error; res.write("NASTY ERROR: " + e.message + "\n"); } } @@ -101,6 +99,26 @@ return res.end("Request vars discovery\n
\n

Name:

\n

Age:   

\n

\n
"); }); + router.get("/google", function(req, res) { + return router.proxy_pass("http://www.google.com.ar", res); + }); + + router.get("/testing", function(req, res) { + return router.proxy_pass("http://testing.savos.ods.org/", res); + }); + + router.get("/testing/:route", function(req, res) { + return router.proxy_pass("http://testing.savos.ods.org/" + req.params.route + "/", res); + }); + + router.get("/login", function(req, res) { + res.setHeader("WWW-Authenticate", 'Basic realm="node-simple-router"'); + res.writeHead(200, { + 'Content-type': 'text/html' + }); + return res.end("Login here."); + }); + /* End of example routes */ diff --git a/test/uploader/server.js b/test/uploader/server.js index 87f33bd..4bc4aeb 100755 --- a/test/uploader/server.js +++ b/test/uploader/server.js @@ -1,8 +1,8 @@ #!/usr/bin/env node -// Generated by CoffeeScript 1.4.0 +// Generated by CoffeeScript 1.6.3 (function() { - var Router, argv, fs, http, router, server; + var Router, argv, e, fs, http, router, server; fs = require('fs'); @@ -24,7 +24,8 @@ try { Router = require('../../src/router'); - } catch (e) { + } catch (_error) { + e = _error; Router = require('../../lib/router'); }