From 2423e76df9e7f2f41e890390aa08fbf394678407 Mon Sep 17 00:00:00 2001 From: Josh Vanderwillik Date: Thu, 10 Sep 2015 13:16:20 -0400 Subject: [PATCH 1/2] Query strings now cooperate with unix sockets Fixes issue #1766 --- request.js | 29 ++++++++++++++++++----------- tests/test-unix.js | 13 +++++++++++-- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/request.js b/request.js index 700b9e0a9..dd71c8880 100644 --- a/request.js +++ b/request.js @@ -256,17 +256,7 @@ Request.prototype.init = function (options) { // Support Unix Sockets if (self.uri.host === 'unix') { - // Get the socket & request paths from the URL - var unixParts = self.uri.path.split(':') - , host = unixParts[0] - , path = unixParts[1] - // Apply unix properties to request - self.socketPath = host - self.uri.pathname = path - self.uri.path = path - self.uri.host = host - self.uri.hostname = host - self.uri.isUnix = true + self.enableUnixSocket() } if (self.strictSSL === false) { @@ -1163,6 +1153,10 @@ Request.prototype.qs = function (q, clobber) { self.url = self.uri self.path = self.uri.path + if ( self.uri.host === 'unix' ) { + self.enableUnixSocket() + } + return self } Request.prototype.form = function (form) { @@ -1411,6 +1405,19 @@ Request.prototype.destroy = function () { self.response.destroy() } } +Request.prototype.enableUnixSocket = function () { + // Get the socket & request paths from the URL + var unixParts = this.uri.path.split(':') + , host = unixParts[0] + , path = unixParts[1] + // Apply unix properties to request + this.socketPath = host + this.uri.pathname = path + this.uri.path = path + this.uri.host = host + this.uri.hostname = host + this.uri.isUnix = true +} Request.defaultProxyHeaderWhiteList = Tunnel.defaultProxyHeaderWhiteList.slice() diff --git a/tests/test-unix.js b/tests/test-unix.js index 71395a6bc..14615920b 100644 --- a/tests/test-unix.js +++ b/tests/test-unix.js @@ -6,8 +6,10 @@ var request = require('../index') , rimraf = require('rimraf') , assert = require('assert') , tape = require('tape') + , url = require('url') var path = [null, 'test', 'path'].join('/') + , searchString = '?foo=bar' , socket = [__dirname, 'tmp-socket'].join('/') , expectedBody = 'connected' , statusCode = 200 @@ -15,7 +17,9 @@ var path = [null, 'test', 'path'].join('/') rimraf.sync(socket) var s = http.createServer(function(req, res) { - assert.equal(req.url, path, 'requested path is sent to server') + var incomingUrl = url.parse(req.url) + assert.equal(incomingUrl.pathname, path, 'requested path is sent to server') + assert.equal(incomingUrl.search, searchString, 'query string is sent to server') res.statusCode = statusCode res.end(expectedBody) }) @@ -27,7 +31,12 @@ tape('setup', function(t) { }) tape('unix socket connection', function(t) { - request('http://unix:' + socket + ':' + path, function(err, res, body) { + request({ + uri: 'http://unix:' + socket + ':' + path, + qs: { + foo: 'bar' + } + }, function(err, res, body) { t.equal(err, null, 'no error in connection') t.equal(res.statusCode, statusCode, 'got HTTP 200 OK response') t.equal(body, expectedBody, 'expected response body is received') From cf6df69cd7ed5e3cabf38eed4bbeb908915f0787 Mon Sep 17 00:00:00 2001 From: Josh Vanderwillik Date: Fri, 11 Sep 2015 10:31:15 -0400 Subject: [PATCH 2/2] More tests for unix socket + query string Also brings .enableUnixSocket calls in line with the rest of the code stylistically. --- request.js | 29 +++++++++++++++-------------- tests/test-unix.js | 29 +++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/request.js b/request.js index dd71c8880..af91a11d3 100644 --- a/request.js +++ b/request.js @@ -1153,7 +1153,7 @@ Request.prototype.qs = function (q, clobber) { self.url = self.uri self.path = self.uri.path - if ( self.uri.host === 'unix' ) { + if (self.uri.host === 'unix') { self.enableUnixSocket() } @@ -1240,6 +1240,20 @@ Request.prototype.getHeader = function (name, headers) { }) return result } +Request.prototype.enableUnixSocket = function () { + // Get the socket & request paths from the URL + var unixParts = this.uri.path.split(':') + , host = unixParts[0] + , path = unixParts[1] + // Apply unix properties to request + this.socketPath = host + this.uri.pathname = path + this.uri.path = path + this.uri.host = host + this.uri.hostname = host + this.uri.isUnix = true +} + Request.prototype.auth = function (user, pass, sendImmediately, bearer) { var self = this @@ -1405,19 +1419,6 @@ Request.prototype.destroy = function () { self.response.destroy() } } -Request.prototype.enableUnixSocket = function () { - // Get the socket & request paths from the URL - var unixParts = this.uri.path.split(':') - , host = unixParts[0] - , path = unixParts[1] - // Apply unix properties to request - this.socketPath = host - this.uri.pathname = path - this.uri.path = path - this.uri.host = host - this.uri.hostname = host - this.uri.isUnix = true -} Request.defaultProxyHeaderWhiteList = Tunnel.defaultProxyHeaderWhiteList.slice() diff --git a/tests/test-unix.js b/tests/test-unix.js index 14615920b..7a75023b3 100644 --- a/tests/test-unix.js +++ b/tests/test-unix.js @@ -8,7 +8,8 @@ var request = require('../index') , tape = require('tape') , url = require('url') -var path = [null, 'test', 'path'].join('/') +var rawPath = [null, 'raw', 'path'].join('/') + , queryPath = [null, 'query', 'path'].join('/') , searchString = '?foo=bar' , socket = [__dirname, 'tmp-socket'].join('/') , expectedBody = 'connected' @@ -18,8 +19,19 @@ rimraf.sync(socket) var s = http.createServer(function(req, res) { var incomingUrl = url.parse(req.url) - assert.equal(incomingUrl.pathname, path, 'requested path is sent to server') - assert.equal(incomingUrl.search, searchString, 'query string is sent to server') + switch (incomingUrl.pathname) { + case rawPath: + assert.equal(incomingUrl.pathname, rawPath, 'requested path is sent to server') + break + + case queryPath: + assert.equal(incomingUrl.pathname, queryPath, 'requested path is sent to server') + assert.equal(incomingUrl.search, searchString, 'query string is sent to server') + break + + default: + assert(false, 'A valid path was requested') + } res.statusCode = statusCode res.end(expectedBody) }) @@ -31,8 +43,17 @@ tape('setup', function(t) { }) tape('unix socket connection', function(t) { + request( 'http://unix:' + socket + ':' + rawPath, function(err, res, body) { + t.equal(err, null, 'no error in connection') + t.equal(res.statusCode, statusCode, 'got HTTP 200 OK response') + t.equal(body, expectedBody, 'expected response body is received') + t.end() + }) +}) + +tape('unix socket connection with qs', function(t) { request({ - uri: 'http://unix:' + socket + ':' + path, + uri: 'http://unix:' + socket + ':' + queryPath, qs: { foo: 'bar' }