Skip to content

Commit

Permalink
Merge d66bba4 into c8d9223
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleytodd committed Feb 24, 2017
2 parents c8d9223 + d66bba4 commit dc055ba
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lib/application.js
Expand Up @@ -599,8 +599,8 @@ app.render = function render(name, options, callback) {
*/

app.listen = function listen() {
var server = http.createServer(this);
return server.listen.apply(server, arguments);
var server = this.createServer(this)
return server.listen.apply(server, arguments)
};

/**
Expand Down
44 changes: 39 additions & 5 deletions lib/express.js
Expand Up @@ -19,6 +19,30 @@ var Router = require('router');
var req = require('./request');
var res = require('./response');

/**
* Only reqire http when requested
*/
var http = {}
var _http
Object.defineProperty(http, 'IncomingMessage', {
get: function () {
_http = _http || require('http')
return _http.IncomingMessage
}
})
Object.defineProperty(http, 'ServerResponse', {
get: function () {
_http = _http || require('http')
return _http.ServerResponse
}
})
Object.defineProperty(http, 'createServer', {
get: function () {
_http = _http || require('http')
return _http.createServer
}
})

/**
* Expose `createApplication()`.
*/
Expand All @@ -32,16 +56,26 @@ exports = module.exports = createApplication;
* @api public
*/

function createApplication() {
function createApplication (opts) {
var options = opts || {}
options.reqProto = options.reqProto || http.IncomingMessage.prototype
options.resProto = options.resProto || http.ServerResponse.prototype
options.createServer = options.createServer || http.createServer

var app = function(req, res, next) {
app.handle(req, res, next);
};

mixin(app, EventEmitter.prototype, false);
mixin(app, proto, false);

app.request = { __proto__: req, app: app };
app.response = { __proto__: res, app: app };
app.createServer = opts.createServer
app.request = Object.create(req(opts.reqProto), {
app: { configurable: true, enumerable: true, writable: true, value: app }
})
app.response = Object.create(res(opts.resProto), {
app: { configurable: true, enumerable: true, writable: true, value: app }
})
app.init();
return app;
}
Expand All @@ -51,8 +85,8 @@ function createApplication() {
*/

exports.application = proto;
exports.request = req;
exports.response = res;
exports.request = req.proto
exports.response = res.proto

/**
* Expose constructors.
Expand Down
13 changes: 9 additions & 4 deletions lib/request.js
Expand Up @@ -16,19 +16,24 @@
var accepts = require('accepts');
var isIP = require('net').isIP;
var typeis = require('type-is');
var http = require('http');
var fresh = require('fresh');
var parseRange = require('range-parser');
var parse = require('parseurl');
var proxyaddr = require('proxy-addr');
var setPrototypeOf = require('setprototypeof')

/**
* Export with proto
*/
exports = module.exports = function (proto) {
return setPrototypeOf(req, proto)
}

/**
* Request prototype.
*/

var req = exports = module.exports = {
__proto__: http.IncomingMessage.prototype
};
var req = module.exports.proto = {}

/**
* Return request header.
Expand Down
21 changes: 13 additions & 8 deletions lib/response.js
Expand Up @@ -16,7 +16,6 @@ var contentDisposition = require('content-disposition');
var deprecate = require('depd')('express');
var encodeUrl = require('encodeurl');
var escapeHtml = require('escape-html');
var http = require('http');
var onFinished = require('on-finished');
var path = require('path');
var pathIsAbsolute = require('path-is-absolute');
Expand All @@ -25,21 +24,27 @@ var sign = require('cookie-signature').sign;
var normalizeType = require('./utils').normalizeType;
var normalizeTypes = require('./utils').normalizeTypes;
var setCharset = require('./utils').setCharset;
var statusCodes = http.STATUS_CODES;
var statusCodes = require('statuses/codes')
var cookie = require('cookie');
var send = require('send');
var extname = path.extname;
var mime = send.mime;
var resolve = path.resolve;
var vary = require('vary');
var setPrototypeOf = require('setprototypeof')

/**
* Export with proto
*/
exports = module.exports = function (proto) {
return setPrototypeOf(res, proto)
}

/**
* Response prototype.
*/

var res = module.exports = {
__proto__: http.ServerResponse.prototype
};
var res = module.exports.proto = {}

/**
* Module variables.
Expand Down Expand Up @@ -286,7 +291,7 @@ res.jsonp = function jsonp(obj) {
*/

res.sendStatus = function sendStatus(statusCode) {
var body = statusCodes[statusCode] || String(statusCode);
var body = statusCodes[statusCode] || String(statusCode)

this.statusCode = statusCode;
this.type('txt');
Expand Down Expand Up @@ -756,12 +761,12 @@ res.redirect = function redirect(url) {
// Support text/{plain,html} by default
this.format({
text: function(){
body = statusCodes[status] + '. Redirecting to ' + address;
body = statusCodes[status] + '. Redirecting to ' + address
},

html: function(){
var u = escapeHtml(address);
body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>';
body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>'
},

default: function(){
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -52,6 +52,8 @@
"router": "~1.1.5",
"send": "0.14.2",
"serve-static": "~1.11.2",
"setprototypeof": "~1.0.3",
"statuses": "~1.3.1",
"type-is": "~1.6.14",
"utils-merge": "1.0.0",
"vary": "~1.1.0"
Expand Down
9 changes: 4 additions & 5 deletions test/res.redirect.js
@@ -1,5 +1,4 @@

var http = require('http');
var express = require('..');
var request = require('supertest');
var utils = require('./support/utils');
Expand Down Expand Up @@ -104,7 +103,7 @@ describe('res', function(){
.set('Accept', 'text/html')
.expect('Content-Type', /html/)
.expect('Location', 'http://google.com')
.expect(302, '<p>' + http.STATUS_CODES[302] + '. Redirecting to <a href="http://google.com">http://google.com</a></p>', done);
.expect(302, '<p>Found. Redirecting to <a href="http://google.com">http://google.com</a></p>', done)
})

it('should escape the url', function(done){
Expand All @@ -120,7 +119,7 @@ describe('res', function(){
.set('Accept', 'text/html')
.expect('Content-Type', /html/)
.expect('Location', '%3Cla\'me%3E')
.expect(302, '<p>' + http.STATUS_CODES[302] + '. Redirecting to <a href="%3Cla&#39;me%3E">%3Cla&#39;me%3E</a></p>', done)
.expect(302, '<p>Found. Redirecting to <a href="%3Cla&#39;me%3E">%3Cla&#39;me%3E</a></p>', done)
})

it('should include the redirect type', function(done){
Expand Down Expand Up @@ -152,7 +151,7 @@ describe('res', function(){
.set('Accept', 'text/plain, */*')
.expect('Content-Type', /plain/)
.expect('Location', 'http://google.com')
.expect(302, http.STATUS_CODES[302] + '. Redirecting to http://google.com', done);
.expect(302, 'Found. Redirecting to http://google.com', done)
})

it('should encode the url', function(done){
Expand All @@ -168,7 +167,7 @@ describe('res', function(){
.set('Accept', 'text/plain, */*')
.expect('Content-Type', /plain/)
.expect('Location', 'http://example.com/?param=%3Cscript%3Ealert(%22hax%22);%3C/script%3E')
.expect(302, http.STATUS_CODES[302] + '. Redirecting to http://example.com/?param=%3Cscript%3Ealert(%22hax%22);%3C/script%3E', done)
.expect(302, 'Found. Redirecting to http://example.com/?param=%3Cscript%3Ealert(%22hax%22);%3C/script%3E', done)
})

it('should include the redirect type', function(done){
Expand Down

0 comments on commit dc055ba

Please sign in to comment.