Fixed pathname encoding and keep-alive

This commit is contained in:
Gregory Schier 2016-10-11 09:57:06 -07:00
parent 3d5408bfc3
commit 741278a05b
3 changed files with 18 additions and 14 deletions

View File

@ -17,6 +17,7 @@ describe('buildRequestConfig()', () => {
expect(config).toEqual({ expect(config).toEqual({
body: '', body: '',
followAllRedirects: true, followAllRedirects: true,
forever: true,
gzip: true, gzip: true,
headers: {host: ''}, headers: {host: ''},
maxRedirects: 20, maxRedirects: 20,
@ -37,7 +38,7 @@ describe('buildRequestConfig()', () => {
parameters: [{name: 'foo bar', value: 'hello&world'}], parameters: [{name: 'foo bar', value: 'hello&world'}],
method: 'POST', method: 'POST',
body: 'foo=bar', body: 'foo=bar',
url: 'http://foo.com:3332/★/foo%20bar?bar=baz', url: 'http://foo.com:3332/★/hi@gmail.com/foo%20bar?bar=baz',
authentication: { authentication: {
username: 'user', username: 'user',
password: 'pass' password: 'pass'
@ -49,6 +50,7 @@ describe('buildRequestConfig()', () => {
expect(config).toEqual({ expect(config).toEqual({
body: 'foo=bar', body: 'foo=bar',
followAllRedirects: true, followAllRedirects: true,
forever: true,
gzip: true, gzip: true,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -61,7 +63,8 @@ describe('buildRequestConfig()', () => {
rejectUnauthorized: true, rejectUnauthorized: true,
time: true, time: true,
timeout: 0, timeout: 0,
url: 'http://foo.com:3332/%E2%98%85/foo%20bar?bar=baz&foo%20bar=hello%26world' url: 'http://foo.com:3332/%E2%98%85/hi%40gmail.com/' +
'foo%20bar?bar=baz&foo%20bar=hello%26world'
}) })
}) })
}); });

View File

@ -106,7 +106,7 @@ describe('prepareUrlForSending()', () => {
it('encodes pathname mixed encoding', () => { it('encodes pathname mixed encoding', () => {
const url = util.prepareUrlForSending('https://google.com/foo bar baz%20qux/100%/foo%25'); const url = util.prepareUrlForSending('https://google.com/foo bar baz%20qux/100%/foo%25');
expect(url).toBe('https://google.com/foo%20bar%20baz%20qux/100%25/foo%2525'); expect(url).toBe('https://google.com/foo%20bar%20baz%20qux/100%25/foo%25');
}); });
it('leaves already encoded pathname', () => { it('leaves already encoded pathname', () => {

View File

@ -60,30 +60,30 @@ export function flexibleEncodeComponent (str) {
// Sometimes spaces screw things up because of url.parse // Sometimes spaces screw things up because of url.parse
str = str.replace(/%20/g, ' '); str = str.replace(/%20/g, ' ');
let decodedPathname; let decoded;
try { try {
decodedPathname = decodeURIComponent(str); decoded = decodeURIComponent(str);
} catch (e) { } catch (e) {
// Malformed (probably not encoded) so assume it's decoded already // Malformed (probably not encoded) so assume it's decoded already
decodedPathname = str; decoded = str;
} }
return encodeURIComponent(decodedPathname); return encodeURIComponent(decoded);
} }
export function flexibleEncode (str) { export function flexibleEncode (str) {
// Sometimes spaces screw things up because of url.parse // Sometimes spaces screw things up because of url.parse
str = str.replace(/%20/g, ' '); str = str.replace(/%20/g, ' ');
let decodedPathname; let decoded;
try { try {
decodedPathname = decodeURI(str); decoded = decodeURI(str);
} catch (e) { } catch (e) {
// Malformed (probably not encoded) so assume it's decoded already // Malformed (probably not encoded) so assume it's decoded already
decodedPathname = str; decoded = str;
} }
return encodeURI(decodedPathname); return encodeURI(decoded);
} }
export function prepareUrlForSending (url) { export function prepareUrlForSending (url) {
@ -96,9 +96,10 @@ export function prepareUrlForSending (url) {
// 1. Pathname // // 1. Pathname //
// ~~~~~~~~~~~ // // ~~~~~~~~~~~ //
parsedUrl.pathname = flexibleEncode( if (parsedUrl.pathname) {
parsedUrl.pathname || '' const segments = parsedUrl.pathname.split('/');
); parsedUrl.pathname = segments.map(flexibleEncodeComponent).join('/');
}
// ~~~~~~~~~~~~~~ // // ~~~~~~~~~~~~~~ //
// 2. Querystring // // 2. Querystring //