Skip to content
Myr

Merge branch 'features/https'

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parents cd519e9, 54eac36 · Browse files · 6 files+515 added−465 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+515 added−465 removed

  • tests:+2 added−2 removed
  • other:+513 added−463 removed

6 files+515 added−465 removed

modified:lib/express.js+10 added-5 removed
added:lib/http.js+463 added
12/*!3 * Express - HTTPServer4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var qs = require('qs')13 , connect = require('connect')14 , router = connect.router15 , methods = router.methods.concat(['del', 'all'])16 , view = require('./view')17 , url = require('url')18 , utils = connect.utils;1920/**21 * Initialize a new `HTTPServer` with optional `middleware`.22 *23 * @param {Array} middleware24 * @api public25 */2627var Server = exports = module.exports = function HTTPServer(middleware){28 connect.HTTPServer.call(this, []);29 this.init(middleware);30};3132/**33 * Inherit from `connect.HTTPServer`.34 */3536Server.prototype.__proto__ = connect.HTTPServer.prototype;3738/**39 * Initialize the server.40 *41 * @param {Array} middleware42 * @api private43 */4445Server.prototype.init = function(middleware){46 var self = this;47 this.settings = {};48 this.redirects = {};49 this.isCallbacks = {};50 this.viewHelpers = {};51 this.dynamicViewHelpers = {};52 this.errorHandlers = [];5354 // Default "home" to / 55 this.set('home', '/');5657 // Set "env" to NODE_ENV, defaulting to "development"58 this.set('env', process.env.NODE_ENV || 'development');5960 // Expose objects to each other61 this.use(function(req, res, next){62 req.query = req.query || {};63 res.headers = { 'X-Powered-By': 'Express' };64 req.app = res.app = self;65 req.res = res;66 res.req = req;67 req.next = next;68 // Assign req.params.get69 if (req.url.indexOf('?') > 0) {70 var query = url.parse(req.url).query;71 req.query = qs.parse(query);72 }73 next();74 });7576 // Apply middleware77 if (middleware) {78 middleware.forEach(function(fn){79 self.use(fn);80 });81 }8283 // Use router, expose as app.get(), etc84 var fn = router(function(app){ self.routes = app; });85 this.__defineGetter__('router', function(){86 this.__usedRouter = true;87 return fn;88 });8990 // Default production configuration91 this.configure('production', function(){92 this.enable('cache views');93 });94};9596/**97 * Proxy in order to register error handlers.98 */99100Server.prototype.listen = function(){101 this.registerErrorHandlers();102 connect.HTTPServer.prototype.listen.apply(this, arguments);103};104105/**106 * Proxy in order to register error handlers.107 */108109Server.prototype.listenFD = function(){110 this.registerErrorHandlers();111 connect.HTTPServer.prototype.listenFD.apply(this, arguments);112};113114/**115 * When using the vhost() middleware register error handlers.116 */117118Server.prototype.onvhost = function(){119 this.registerErrorHandlers();120};121122/**123 * Register error handlers. This is automatically124 * called from within `Server#listen()` and `Server#listenFD()`.125 *126 * @return {Server} for chaining127 * @api public128 */129130Server.prototype.registerErrorHandlers = function(){131 this.errorHandlers.forEach(function(fn){132 this.use(function(err, req, res, next){133 fn.apply(this, arguments);134 });135 }, this);136 return this;137};138139/**140 * Proxy `connect.HTTPServer#use()` to apply settings to141 * mounted applications.142 *143 * @param {String|Function|Server} route144 * @param {Function|Server} middleware145 * @return {Server} for chaining146 * @api public147 */148149Server.prototype.use = function(route, middleware){150 if (typeof route !== 'string') {151 middleware = route, route = '/';152 }153154 connect.HTTPServer.prototype.use.call(this, route, middleware);155156 // Mounted an app157 if (middleware instanceof Server) {158 // Home is /:route/:home159 var app = middleware160 , home = app.set('home');161 if (home === '/') home = '';162 app.set('home', (app.route || '') + home);163 app.parent = this;164 // Mounted hook165 if (app.__mounted) app.__mounted.call(app, this);166 }167168 return this;169};170171/**172 * Assign a callback `fn` which is called173 * when this `Server` is passed to `Server#use()`.174 *175 * Examples:176 *177 * var app = express.createServer(),178 * blog = express.createServer();179 *180 * blog.mounted(function(parent){181 * // parent is app182 * // "this" is blog183 * });184 *185 * app.use(blog);186 *187 * @param {Function} fn188 * @return {Server} for chaining189 * @api public190 */191192Server.prototype.mounted = function(fn){193 this.__mounted = fn;194 return this;195};196197/**198 * See: view.register.199 *200 * @return {Server} for chaining201 * @api public202 */203204Server.prototype.register = function(){205 view.register.apply(this, arguments);206 return this;207};208209/**210 * Register the given view helpers `obj`. This method211 * can be called several times to apply additional helpers.212 *213 * @param {Object} obj214 * @return {Server} for chaining215 * @api public216 */217218Server.prototype.helpers = 219Server.prototype.locals = function(obj){220 utils.merge(this.viewHelpers, obj);221 return this;222};223224/**225 * Register the given dynamic view helpers `obj`. This method226 * can be called several times to apply additional helpers.227 *228 * @param {Object} obj229 * @return {Server} for chaining230 * @api public231 */232233Server.prototype.dynamicHelpers = function(obj){234 utils.merge(this.dynamicViewHelpers, obj);235 return this;236};237238/**239 * Map the given param placeholder `name`(s) to the given callback `fn`.240 *241 * Param mapping is used to provide pre-conditions to routes242 * which us normalized placeholders. For example ":user_id" may243 * attempt to load the user from the database, where as ":num" may 244 * pass the value through `parseInt(num, 10)`.245 *246 * When the callback function accepts only a single argument, the247 * value of placeholder is passed:248 *249 * app.param('page', function(n){ return parseInt(n, 10); });250 *251 * After which "/users/:page" would automatically provide us with252 * an integer for `req.params.page`. If desired we could use the callback 253 * signature shown below, and immediately `next(new Error('invalid page'))`254 * when `parseInt` fails.255 *256 * Alternatively the callback may accept the request, response, next, and257 * the value, acting like middlware:258 *259 * app.param('userId', function(req, res, next, id){260 * User.find(id, function(err, user){261 * if (err) {262 * next(err);263 * } else if (user) {264 * req.user = user;265 * next();266 * } else {267 * next(new Error('failed to load user'));268 * }269 * });270 * });271 *272 * Now every time ":userId" is present, the associated user object273 * will be loaded and assigned before the route handler is invoked.274 *275 * @param {String|Array} name276 * @param {Function} fn277 * @return {Server} for chaining278 * @api public279 */280281Server.prototype.param = function(name, fn){282 if (Array.isArray(name)) {283 name.forEach(function(name){284 this.param(name, fn);285 }, this);286 } else {287 if (':' == name[0]) name = name.substr(1);288 this.routes.param(name, fn);289 }290 return this;291};292293/**294 * Assign a custom exception handler callback `fn`.295 * These handlers are always _last_ in the middleware stack.296 *297 * @param {Function} fn298 * @return {Server} for chaining299 * @api public300 */301302Server.prototype.error = function(fn){303 this.errorHandlers.push(fn);304 return this;305};306307/**308 * Register the given callback `fn` for the given `type`.309 *310 * @param {String} type311 * @param {Function} fn312 * @return {Server} for chaining313 * @api public314 */315316Server.prototype.is = function(type, fn){317 if (!fn) return this.isCallbacks[type];318 this.isCallbacks[type] = fn;319 return this;320};321322/**323 * Assign `setting` to `val`, or return `setting`'s value.324 * Mounted servers inherit their parent server's settings.325 *326 * @param {String} setting327 * @param {String} val328 * @return {Server|Mixed} for chaining, or the setting value329 * @api public330 */331332Server.prototype.set = function(setting, val){333 if (val === undefined) {334 if (this.settings.hasOwnProperty(setting)) {335 return this.settings[setting];336 } else if (this.parent) {337 return this.parent.set(setting);338 }339 } else {340 this.settings[setting] = val;341 return this;342 }343};344345/**346 * Check if `setting` is enabled.347 *348 * @param {String} setting349 * @return {Boolean}350 * @api public351 */352353Server.prototype.enabled = function(setting){354 return !!this.set(setting);355};356357/**358 * Check if `setting` is disabled.359 *360 * @param {String} setting361 * @return {Boolean}362 * @api public363 */364365Server.prototype.disabled = function(setting){366 return !this.set(setting);367};368369/**370 * Enable `setting`.371 *372 * @param {String} setting373 * @return {Server} for chaining374 * @api public375 */376377Server.prototype.enable = function(setting){378 return this.set(setting, true);379};380381/**382 * Disable `setting`.383 *384 * @param {String} setting385 * @return {Server} for chaining386 * @api public387 */388389Server.prototype.disable = function(setting){390 return this.set(setting, false);391};392393/**394 * Redirect `key` to `url`.395 *396 * @param {String} key397 * @param {String} url398 * @return {Server} for chaining399 * @api public400 */401402Server.prototype.redirect = function(key, url){403 this.redirects[key] = url;404 return this;405};406407/**408 * Configure callback for the given `env`.409 *410 * @param {String} env411 * @param {Function} fn412 * @return {Server} for chaining413 * @api public414 */415416Server.prototype.configure = function(env, fn){417 if (typeof env === 'function') {418 fn = env, env = 'all';419 }420 if ('all' == env || env == this.settings.env) {421 fn.call(this);422 }423 return this;424};425426// Generate routing methods427428function generateMethod(method){429 Server.prototype[method] = function(path, fn){430 var self = this;431432 // Ensure router is mounted433 if (!this.__usedRouter) {434 this.use(this.router);435 }436437 // Route specific middleware support438 if (arguments.length > 2) {439 var args = Array.prototype.slice.call(arguments, 1);440 fn = args.pop();441 (function stack(middleware){442 middleware.forEach(function(fn){443 if (Array.isArray(fn)) {444 stack(fn);445 } else {446 self[method](path, fn);447 }448 });449 })(args);450 }451452 // Generate the route453 this.routes[method](path, fn);454 return this;455 };456 return arguments.callee;457}458459methods.forEach(generateMethod);460461// Alias delete as "del"462463Server.prototype.del = Server.prototype.delete;
added:lib/https.js+40 added
deleted:lib/server.js-458 removed
12/*!3 * Express - Server4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var queryString = require('querystring')13 , connect = require('connect')14 , router = connect.router15 , methods = router.methods.concat(['del', 'all'])16 , view = require('./view')17 , url = require('url')18 , utils = connect.utils;1920/**21 * Initialize a new `Server` with optional `middleware`.22 *23 * @param {Array} middleware24 * @api public25 */2627var Server = exports = module.exports = function Server(middleware){28 var self = this;29 this.settings = {};30 this.redirects = {};31 this.isCallbacks = {};32 this.viewHelpers = {};33 this.dynamicViewHelpers = {};34 this.errorHandlers = [];35 connect.HTTPServer.call(this, []);3637 // Default "home" to / 38 this.set('home', '/');3940 // Set "env" to NODE_ENV, defaulting to "development"41 this.set('env', process.env.NODE_ENV || 'development');4243 // Expose objects to each other44 this.use(function(req, res, next){45 req.query = req.query || {};46 res.headers = { 'X-Powered-By': 'Express' };47 req.app = res.app = self;48 req.res = res;49 res.req = req;50 req.next = next;51 // Assign req.params.get52 if (req.url.indexOf('?') > 0) {53 var query = url.parse(req.url).query;54 req.query = exports.parseQueryString(query);55 }56 next();57 });5859 // Apply middleware60 if (middleware) {61 middleware.forEach(function(fn){62 self.use(fn);63 });64 }6566 // Use router, expose as app.get(), etc67 var fn = router(function(app){ self.routes = app; });68 this.__defineGetter__('router', function(){69 this.__usedRouter = true;70 return fn;71 });7273 // Default production configuration74 this.configure('production', function(){75 this.enable('cache views');76 });77};7879/**80 * Inherit from `connect.HTTPServer`.81 */8283Server.prototype.__proto__ = connect.HTTPServer.prototype;8485/**86 * Support swappable querystring parsers.87 */8889exports.parseQueryString = queryString.parse;9091/**92 * Proxy in order to register error handlers.93 */9495Server.prototype.listen = function(){96 this.registerErrorHandlers();97 connect.HTTPServer.prototype.listen.apply(this, arguments);98};99100/**101 * Proxy in order to register error handlers.102 */103104Server.prototype.listenFD = function(){105 this.registerErrorHandlers();106 connect.HTTPServer.prototype.listenFD.apply(this, arguments);107};108109/**110 * When using the vhost() middleware register error handlers.111 */112113Server.prototype.onvhost = function(){114 this.registerErrorHandlers();115};116117/**118 * Register error handlers. This is automatically119 * called from within `Server#listen()` and `Server#listenFD()`.120 *121 * @return {Server} for chaining122 * @api public123 */124125Server.prototype.registerErrorHandlers = function(){126 this.errorHandlers.forEach(function(fn){127 this.use(function(err, req, res, next){128 fn.apply(this, arguments);129 });130 }, this);131 return this;132};133134/**135 * Proxy `connect.HTTPServer#use()` to apply settings to136 * mounted applications.137 *138 * @param {String|Function|Server} route139 * @param {Function|Server} middleware140 * @return {Server} for chaining141 * @api public142 */143144Server.prototype.use = function(route, middleware){145 if (typeof route !== 'string') {146 middleware = route, route = '/';147 }148149 connect.HTTPServer.prototype.use.call(this, route, middleware);150151 // Mounted an app152 if (middleware instanceof Server) {153 // Home is /:route/:home154 var app = middleware155 , home = app.set('home');156 if (home === '/') home = '';157 app.set('home', (app.route || '') + home);158 app.parent = this;159 // Mounted hook160 if (app.__mounted) app.__mounted.call(app, this);161 }162163 return this;164};165166/**167 * Assign a callback `fn` which is called168 * when this `Server` is passed to `Server#use()`.169 *170 * Examples:171 *172 * var app = express.createServer(),173 * blog = express.createServer();174 *175 * blog.mounted(function(parent){176 * // parent is app177 * // "this" is blog178 * });179 *180 * app.use(blog);181 *182 * @param {Function} fn183 * @return {Server} for chaining184 * @api public185 */186187Server.prototype.mounted = function(fn){188 this.__mounted = fn;189 return this;190};191192/**193 * See: view.register.194 *195 * @return {Server} for chaining196 * @api public197 */198199Server.prototype.register = function(){200 view.register.apply(this, arguments);201 return this;202};203204/**205 * Register the given view helpers `obj`. This method206 * can be called several times to apply additional helpers.207 *208 * @param {Object} obj209 * @return {Server} for chaining210 * @api public211 */212213Server.prototype.helpers = 214Server.prototype.locals = function(obj){215 utils.merge(this.viewHelpers, obj);216 return this;217};218219/**220 * Register the given dynamic view helpers `obj`. This method221 * can be called several times to apply additional helpers.222 *223 * @param {Object} obj224 * @return {Server} for chaining225 * @api public226 */227228Server.prototype.dynamicHelpers = function(obj){229 utils.merge(this.dynamicViewHelpers, obj);230 return this;231};232233/**234 * Map the given param placeholder `name`(s) to the given callback `fn`.235 *236 * Param mapping is used to provide pre-conditions to routes237 * which us normalized placeholders. For example ":user_id" may238 * attempt to load the user from the database, where as ":num" may 239 * pass the value through `parseInt(num, 10)`.240 *241 * When the callback function accepts only a single argument, the242 * value of placeholder is passed:243 *244 * app.param('page', function(n){ return parseInt(n, 10); });245 *246 * After which "/users/:page" would automatically provide us with247 * an integer for `req.params.page`. If desired we could use the callback 248 * signature shown below, and immediately `next(new Error('invalid page'))`249 * when `parseInt` fails.250 *251 * Alternatively the callback may accept the request, response, next, and252 * the value, acting like middlware:253 *254 * app.param('userId', function(req, res, next, id){255 * User.find(id, function(err, user){256 * if (err) {257 * next(err);258 * } else if (user) {259 * req.user = user;260 * next();261 * } else {262 * next(new Error('failed to load user'));263 * }264 * });265 * });266 *267 * Now every time ":userId" is present, the associated user object268 * will be loaded and assigned before the route handler is invoked.269 *270 * @param {String|Array} name271 * @param {Function} fn272 * @return {Server} for chaining273 * @api public274 */275276Server.prototype.param = function(name, fn){277 if (Array.isArray(name)) {278 name.forEach(function(name){279 this.param(name, fn);280 }, this);281 } else {282 if (':' == name[0]) name = name.substr(1);283 this.routes.param(name, fn);284 }285 return this;286};287288/**289 * Assign a custom exception handler callback `fn`.290 * These handlers are always _last_ in the middleware stack.291 *292 * @param {Function} fn293 * @return {Server} for chaining294 * @api public295 */296297Server.prototype.error = function(fn){298 this.errorHandlers.push(fn);299 return this;300};301302/**303 * Register the given callback `fn` for the given `type`.304 *305 * @param {String} type306 * @param {Function} fn307 * @return {Server} for chaining308 * @api public309 */310311Server.prototype.is = function(type, fn){312 if (!fn) return this.isCallbacks[type];313 this.isCallbacks[type] = fn;314 return this;315};316317/**318 * Assign `setting` to `val`, or return `setting`'s value.319 * Mounted servers inherit their parent server's settings.320 *321 * @param {String} setting322 * @param {String} val323 * @return {Server|Mixed} for chaining, or the setting value324 * @api public325 */326327Server.prototype.set = function(setting, val){328 if (val === undefined) {329 if (this.settings.hasOwnProperty(setting)) {330 return this.settings[setting];331 } else if (this.parent) {332 return this.parent.set(setting);333 }334 } else {335 this.settings[setting] = val;336 return this;337 }338};339340/**341 * Check if `setting` is enabled.342 *343 * @param {String} setting344 * @return {Boolean}345 * @api public346 */347348Server.prototype.enabled = function(setting){349 return !!this.set(setting);350};351352/**353 * Check if `setting` is disabled.354 *355 * @param {String} setting356 * @return {Boolean}357 * @api public358 */359360Server.prototype.disabled = function(setting){361 return !this.set(setting);362};363364/**365 * Enable `setting`.366 *367 * @param {String} setting368 * @return {Server} for chaining369 * @api public370 */371372Server.prototype.enable = function(setting){373 return this.set(setting, true);374};375376/**377 * Disable `setting`.378 *379 * @param {String} setting380 * @return {Server} for chaining381 * @api public382 */383384Server.prototype.disable = function(setting){385 return this.set(setting, false);386};387388/**389 * Redirect `key` to `url`.390 *391 * @param {String} key392 * @param {String} url393 * @return {Server} for chaining394 * @api public395 */396397Server.prototype.redirect = function(key, url){398 this.redirects[key] = url;399 return this;400};401402/**403 * Configure callback for the given `env`.404 *405 * @param {String} env406 * @param {Function} fn407 * @return {Server} for chaining408 * @api public409 */410411Server.prototype.configure = function(env, fn){412 if (typeof env === 'function') {413 fn = env, env = 'all';414 }415 if ('all' == env || env == this.settings.env) {416 fn.call(this);417 }418 return this;419};420421// Generate routing methods422423function generateMethod(method){424 Server.prototype[method] = function(path, fn){425 var self = this;426427 // Ensure router is mounted428 if (!this.__usedRouter) {429 this.use(this.router);430 }431432 // Route specific middleware support433 if (arguments.length > 2) {434 var args = Array.prototype.slice.call(arguments, 1);435 fn = args.pop();436 (function stack(middleware){437 middleware.forEach(function(fn){438 if (Array.isArray(fn)) {439 stack(fn);440 } else {441 self[method](path, fn);442 }443 });444 })(args);445 }446447 // Generate the route448 this.routes[method](path, fn);449 return this;450 };451 return arguments.callee;452}453454methods.forEach(generateMethod);455456// Alias delete as "del"457458Server.prototype.del = Server.prototype.delete;
modified:test/express.test.js+1 added-1 removed
9 unmodified lines
1011module.exports = {12 'test inheritance': function(){13 var server = express.createServer();14 server.should.be.an.instanceof(connect.Server);14 server.should.be.an.instanceof(connect.HTTPServer);15 },16 17 'test connect middleware autoloaders': function(){18 express.errorHandler.should.equal(connect.errorHandler);
507 unmodified lines
modified:test/view.test.js+1 added-1 removed
389 unmodified lines
390 session: function(req, res){391 ++calls;392 req.should.be.a('object');393 res.should.be.a('object');394 this.should.be.an.instanceof(express.Server);394 this.should.equal(app);395 return req.session;396 }397 });398
505 unmodified lines