Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1413 from simov/fix-basic-auth
Fix basic auth
- Loading branch information
Showing
6 changed files
with
351 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use strict' | ||
|
||
var uuid = require('node-uuid') | ||
, CombinedStream = require('combined-stream') | ||
, isstream = require('isstream') | ||
|
||
|
||
function Multipart (request) { | ||
this.request = request | ||
this.boundary = uuid() | ||
this.chunked = false | ||
this.body = null | ||
} | ||
|
||
Multipart.prototype.isChunked = function (options) { | ||
var self = this | ||
, chunked = false | ||
, parts = options.data || options | ||
|
||
if (!parts.forEach) { | ||
throw new Error('Argument error, options.multipart.') | ||
} | ||
|
||
if (self.request.getHeader('transfer-encoding') === 'chunked') { | ||
chunked = true | ||
} | ||
|
||
if (options.chunked !== undefined) { | ||
chunked = options.chunked | ||
} | ||
|
||
if (!chunked) { | ||
parts.forEach(function (part) { | ||
if(typeof part.body === 'undefined') { | ||
throw new Error('Body attribute missing in multipart.') | ||
} | ||
if (isstream(part.body)) { | ||
chunked = true | ||
} | ||
}) | ||
} | ||
|
||
return chunked | ||
} | ||
|
||
Multipart.prototype.setHeaders = function (chunked) { | ||
var self = this | ||
|
||
if (chunked && !self.request.hasHeader('transfer-encoding')) { | ||
self.request.setHeader('transfer-encoding', 'chunked') | ||
} | ||
|
||
var header = self.request.getHeader('content-type') | ||
var contentType = (!header || header.indexOf('multipart') === -1) | ||
? 'multipart/related' | ||
: header.split(';')[0] | ||
|
||
self.request.setHeader('content-type', contentType + '; boundary=' + self.boundary) | ||
} | ||
|
||
Multipart.prototype.build = function (parts, chunked) { | ||
var self = this | ||
var body = chunked ? new CombinedStream() : [] | ||
|
||
function add (part) { | ||
return chunked ? body.append(part) : body.push(new Buffer(part)) | ||
} | ||
|
||
if (self.request.preambleCRLF) { | ||
add('\r\n') | ||
} | ||
|
||
parts.forEach(function (part) { | ||
var preamble = '--' + self.boundary + '\r\n' | ||
Object.keys(part).forEach(function (key) { | ||
if (key === 'body') { return } | ||
preamble += key + ': ' + part[key] + '\r\n' | ||
}) | ||
preamble += '\r\n' | ||
add(preamble) | ||
add(part.body) | ||
add('\r\n') | ||
}) | ||
add('--' + self.boundary + '--') | ||
|
||
if (self.request.postambleCRLF) { | ||
add('\r\n') | ||
} | ||
|
||
return body | ||
} | ||
|
||
Multipart.prototype.onRequest = function (options) { | ||
var self = this | ||
|
||
var chunked = self.isChunked(options) | ||
, parts = options.data || options | ||
|
||
self.setHeaders(chunked) | ||
self.chunked = chunked | ||
self.body = self.build(parts, chunked) | ||
} | ||
|
||
exports.Multipart = Multipart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
'use strict' | ||
|
||
var url = require('url') | ||
var isUrl = /^https?:/ | ||
|
||
function Redirect (request) { | ||
this.request = request | ||
this.followRedirect = true | ||
this.followRedirects = true | ||
this.followAllRedirects = false | ||
this.allowRedirect = function () {return true} | ||
this.maxRedirects = 10 | ||
this.redirects = [] | ||
this.redirectsFollowed = 0 | ||
} | ||
|
||
Redirect.prototype.onRequest = function () { | ||
var self = this | ||
, request = self.request | ||
|
||
if (request.maxRedirects !== undefined) { | ||
self.maxRedirects = request.maxRedirects | ||
} | ||
if (typeof request.followRedirect === 'function') { | ||
self.allowRedirect = request.followRedirect | ||
} | ||
if (request.followRedirect !== undefined) { | ||
self.followRedirects = !!request.followRedirect | ||
} | ||
if (request.followAllRedirects !== undefined) { | ||
self.followAllRedirects = request.followAllRedirects | ||
} | ||
if (self.followRedirects || self.followAllRedirects) { | ||
self.redirects = self.redirects || [] | ||
} | ||
} | ||
|
||
Redirect.prototype.redirectTo = function (response) { | ||
var self = this | ||
, request = self.request | ||
|
||
var redirectTo = null | ||
if (response.statusCode >= 300 && response.statusCode < 400 && response.caseless.has('location')) { | ||
var location = response.caseless.get('location') | ||
// debug('redirect', location) | ||
|
||
if (self.followAllRedirects) { | ||
redirectTo = location | ||
} else if (self.followRedirects) { | ||
switch (request.method) { | ||
case 'PATCH': | ||
case 'PUT': | ||
case 'POST': | ||
case 'DELETE': | ||
// Do not follow redirects | ||
break | ||
default: | ||
redirectTo = location | ||
break | ||
} | ||
} | ||
} else if (response.statusCode === 401) { | ||
var authHeader = request._auth.onResponse(response) | ||
if (authHeader) { | ||
request.setHeader('authorization', authHeader) | ||
redirectTo = request.uri | ||
} | ||
} | ||
return redirectTo | ||
} | ||
|
||
Redirect.prototype.onResponse = function (response) { | ||
var self = this | ||
, request = self.request | ||
|
||
var redirectTo = self.redirectTo(response) | ||
if (!redirectTo || !self.allowRedirect.call(request, response)) { | ||
return false | ||
} | ||
|
||
|
||
// debug('redirect to', redirectTo) | ||
|
||
// ignore any potential response body. it cannot possibly be useful | ||
// to us at this point. | ||
if (request._paused) { | ||
response.resume() | ||
} | ||
|
||
if (self.redirectsFollowed >= self.maxRedirects) { | ||
request.emit('error', new Error('Exceeded maxRedirects. Probably stuck in a redirect loop ' + request.uri.href)) | ||
return false | ||
} | ||
self.redirectsFollowed += 1 | ||
|
||
if (!isUrl.test(redirectTo)) { | ||
redirectTo = url.resolve(request.uri.href, redirectTo) | ||
} | ||
|
||
var uriPrev = request.uri | ||
request.uri = url.parse(redirectTo) | ||
|
||
// handle the case where we change protocol from https to http or vice versa | ||
if (request.uri.protocol !== uriPrev.protocol) { | ||
request._updateProtocol() | ||
} | ||
|
||
self.redirects.push( | ||
{ statusCode : response.statusCode | ||
, redirectUri: redirectTo | ||
} | ||
) | ||
if (self.followAllRedirects && response.statusCode !== 401 && response.statusCode !== 307) { | ||
request.method = 'GET' | ||
} | ||
// request.method = 'GET' // Force all redirects to use GET || commented out fixes #215 | ||
delete request.src | ||
delete request.req | ||
delete request.agent | ||
delete request._started | ||
if (response.statusCode !== 401 && response.statusCode !== 307) { | ||
// Remove parameters from the previous response, unless this is the second request | ||
// for a server that requires digest authentication. | ||
delete request.body | ||
delete request._form | ||
if (request.headers) { | ||
request.removeHeader('host') | ||
request.removeHeader('content-type') | ||
request.removeHeader('content-length') | ||
if (request.uri.hostname !== request.originalHost.split(':')[0]) { | ||
// Remove authorization if changing hostnames (but not if just | ||
// changing ports or protocols). This matches the behavior of curl: | ||
// https://github.com/bagder/curl/blob/6beb0eee/lib/http.c#L710 | ||
request.removeHeader('authorization') | ||
} | ||
} | ||
} | ||
|
||
request.emit('redirect') | ||
|
||
request.init() | ||
|
||
return true | ||
} | ||
|
||
exports.Redirect = Redirect |
Oops, something went wrong.