You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

simple-http-proxy

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-http-proxy - npm Package Compare versions

Comparing version

to
0.5.5

52

index.js

@@ -5,4 +5,5 @@

*/
var url = require('url')
, debug = require('debug')("simple-http-proxy")
, debug = require('debug')('simple-http-proxy')
, protocols = {

@@ -13,2 +14,11 @@ http: require('http'),

/**
* Proxy an endpoint with options
*
* @param {String} endpoint
* @param {Object} opts
* @return {Function}
* @api public
*/
module.exports = function(endpoint, opts) {

@@ -19,5 +29,11 @@ if(!opts) opts = {};

// Should we keep the trailing slash on a root request?
var trailingSlash = endpoint[endpoint.length-1] === '/';
// If we've got a trailing slash remove it
if (trailingSlash) parsedUrl.pathname = parsedUrl.pathname.slice(0, parsedUrl.pathname.length-1);
return function simpleHttpProxy(req, res, next) {
// Get our forwarding info
var hostInfo = req.headers.host.split(":");
var hostInfo = req.headers.host.split(':');

@@ -30,4 +46,4 @@ // Remove the host header

// Should we keep the trailing slash?
var trailingSlash = req.originalUrl[req.originalUrl.length-1] === "/";
// Resolve the url
var path = parsedUrl.pathname + (!trailingSlash && req.url === '/' ? '' : req.url);

@@ -39,3 +55,3 @@ // Setup the options

headers: req.headers,
path: url.resolve(parsedUrl.pathname, trailingSlash ? req.url : req.url.substring(1)),
path: path,
method: req.method

@@ -47,13 +63,13 @@ };

// Get the path at which the middleware is mounted
var resPath = req.originalUrl.replace(req.url, "");
var resPath = req.originalUrl.replace(req.url, '');
// We'll need to add a / if it's not on there
if(resPath.indexOf("/") !== 0) resPath = url.resolve("/", resPath);
if(resPath.indexOf('/') !== 0) resPath = '/' + resPath;
// Pass along our headers
options.headers[opts.xforward.proto || "x-forwarded-proto"] = req.connection.encrypted ? "https" : "http";
options.headers[opts.xforward.host || "x-forwarded-host"] = hostInfo[0];
options.headers[opts.xforward.path || "x-forwarded-path"] = resPath;
options.headers[opts.xforward.proto || 'x-forwarded-proto'] = req.connection.encrypted ? 'https' : 'http';
options.headers[opts.xforward.host || 'x-forwarded-host'] = hostInfo[0];
options.headers[opts.xforward.path || 'x-forwarded-path'] = resPath;
if (hostInfo[1]) options.headers[opts.xforward.port || "x-forwarded-port"] = hostInfo[1];
if (hostInfo[1]) options.headers[opts.xforward.port || 'x-forwarded-port'] = hostInfo[1];
}

@@ -73,18 +89,18 @@

*/
if(~["POST", "DELETE"].indexOf(req.method) && options.headers['transfer-encoding'] != 'chunked') {
if(~['POST', 'DELETE'].indexOf(req.method) && options.headers['transfer-encoding'] != 'chunked') {
options.headers['content-length'] = options.headers['content-length'] || '0';
}
debug("sending proxy request", options);
debug('sending proxy request', options);
// Make the request with the correct protocol
var request = protocols[(parsedUrl.protocol || 'http').replace(":", "")].request(options, function(response) {
debug("got response");
var request = protocols[(parsedUrl.protocol || 'http').replace(':', '')].request(options, function(response) {
debug('got response');
// Send down the statusCode and headers
debug("sending head", response.statusCode, response.headers);
debug('sending head', response.statusCode, response.headers);
res.writeHead(response.statusCode, response.headers);
// Pipe the response
debug("piping response");
debug('piping response');
response.pipe(res);

@@ -102,3 +118,3 @@ });

res.status(504);
next(new Error("Proxy to '"+endpoint+"' timed out"));
next(new Error('Proxy to "'+endpoint+'" timed out'));
});

@@ -105,0 +121,0 @@

{
"name": "simple-http-proxy",
"version": "0.5.4",
"version": "0.5.5",
"description": "Simple proxy middleware",

@@ -5,0 +5,0 @@ "main": "index.js",

/**
* Module dependencies
*/
var express = require("express");
var express = require('express');
var app = module.exports = express();
app.use(require("connect-base")());
app.enable('strict routing');
app.use(require('connect-base')());
app.get('/timeout', function(req, res, next) {

@@ -16,4 +19,8 @@ setTimeout(function() {

app.get('/trailing-slash/', function(req, res, next) {
res.send('slash');
});
app.use(function(req, res, next) {
res.send(req.base);
});

@@ -1,12 +0,12 @@

var should = require("should")
, proxyApp = require("./app")
, express = require("express")
, request = require("supertest")
, proxy = require("..");
var should = require('should')
, proxyApp = require('./app')
, express = require('express')
, request = require('supertest')
, proxy = require('..');
var app = express();
app.use("/google", proxy("https://www.google.com"));
app.use('/google', proxy('https://www.google.com'));
describe("simple-http-proxy", function(){
describe('simple-http-proxy', function(){

@@ -19,7 +19,8 @@ var server, uri;

uri = "http://"+address.address+":"+address.port;
uri = 'http://'+address.address+':'+address.port;
app.use("/proxy", proxy(uri));
app.use("/xforward", proxy(uri, {xforward: true}));
app.use("/timeout", proxy(uri+'/timeout', {timeout:100}));
app.use('/proxy', proxy(uri));
app.use('/xforward', proxy(uri, {xforward: true}));
app.use('/timeout', proxy(uri+'/timeout', {timeout:100}));
app.use('/trailing-slash', proxy(uri+'/trailing-slash/'));
done();

@@ -33,5 +34,5 @@ });

it("should proxy an app", function(done) {
it('should proxy an app', function(done) {
request(app)
.get("/proxy")
.get('/proxy')
.expect(uri)

@@ -45,5 +46,5 @@ .end(function(err, res) {

it("should impose a timeout", function(done) {
it('should impose a timeout', function(done) {
request(app)
.get("/timeout")
.get('/timeout')
.expect(504)

@@ -58,5 +59,5 @@ .end(function(err, res) {

it("should send xforward headers", function(done) {
it('should send xforward headers', function(done) {
request(app)
.get("/xforward")
.get('/xforward')
.expect(/http:\/\/127.0.0.1:[0-9]+\/xforward/)

@@ -70,5 +71,5 @@ .end(function(err, res) {

it("should proxy an external site", function(done) {
it('should proxy an external site', function(done) {
request(app)
.get("/google")
.get('/google')
.expect(200)

@@ -81,2 +82,14 @@ .end(function(err, res) {

});
it('should proxy correctly with a trailing slash', function(done) {
request(app)
.get('/trailing-slash')
.expect(200)
.expect('slash')
.end(function(err, res) {
if(err) return done(err);
if(!res.ok) return done(new Error(res.text));
done();
});
});
});