Updated HTTP client. Closes #221
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Merge branch 'update-http-client'
authored by visionmedia · committed by visionmedia · parents bfff008, 60393f0 · Browse files · 1 file·+45 added−34 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+45 added−34 removed
- other:+45 added−34 removed
Provenance
- Commit
8ee66dc- Tree
34119ac- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent 1
bfff008- Parent 2
60393f0
1 file changed+45 added−34 removed
modified:lib/express/http.js+45 added-34 removed
1234567859101112131011141516171518171819202122232422252624272829303132303334323334353637383940414240414243444546434445464748495051525348495051545556575859546061626358596465666267686970676869707172737475767778798081828384858687777879888990
// Express - HTTP - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
/** * Module dependencies. */
var http = require('http'), events = require('events'), utils = require('express/utils'), parse = require('url').parse, queryString = require('querystring') /** * Request using the given _method_, _url_ * followed by optional _headers_, and _data_. * Mega super awesome private request utility. * * @param {string} method * @param {string} url * @param {hash} headers * @param {hash} data * @param {Promise} promise * @return {Promise} * @param {hash} headers * @param {function} fn * @param {number} redirects * @api private */ function request(method, url, headers, data, promise) {function request(method, url, data, headers, fn, redirects) { var buf = '', promise = promise || new events.Promise, redirects = redirects || 3, url = parse(url), path = url.pathname || '/', search = url.search || '', hash = url.hash || '', port = url.port || 80, headers = process.mixin(headers, { host: url.hostname }), headers = utils.mixin(headers, { host: url.hostname }), client = http.createClient(port, url.hostname) client.addListener('error', function(e){ promise.emitError(new Error("client failed to " + method + " `" + url.href + "'")) }) if (headers.redirect) redirects = headers.redirect, delete headers.redirect if (data) { data = queryString.stringify(data) headers['content-length'] = data.length headers['content-type'] = 'application/x-www-form-urlencoded' } var request = client.request(method, path + search + hash, headers) if (data) request.write(data) request.close(function(response){ if (response.statusCode < 200 || response.statusCode >= 400) promise.emitError(new Error('request failed with status ' + response.statusCode + ' "' + http.STATUS_CODES[response.statusCode] + '"')) else if (response.statusCode >= 300 && response.statusCode < 400) request(method, response.headers.location, headers, data, promise) var req = client.request(method, path + search + hash, headers) if (data) req.write(data) req.addListener('response', function(res){ if (req.statusCode < 200 || req.statusCode >= 400) fn(new Error('request failed with status ' + res.statusCode + ' "' + http.STATUS_CODES[res.statusCode] + '"')) else if (res.statusCode >= 300 && res.statusCode < 400) if (--redirects) request(method, res.headers.location, headers, data, fn, redirects) else fn(new Error('maximum number of redirects reached')) else { response.setBodyEncoding('utf8') response .addListener('body', function(chunk){ buf += chunk }) .addListener('complete', function(){ promise.emitSuccess(buf, response) }) res.setBodyEncoding('utf8') res .addListener('data', function(chunk){ buf += chunk }) .addListener('end', function(){ fn(null, buf, res) }) } }) return promise req.close()}
/** * Return HTTP Client function for the given _method_, * which optionally may _allowData_ to be passed. * Return HTTP Client function for the given _method_. * * @param {string} method * @param {bool} allowData * @return {function} * @api private */
function client(method, allowData) { return function(url, headers, data) { if (allowData) data = data || {} return request(method.toUpperCase(), url, headers, data)function client(method) { return function() { var headers, data, args = Array.prototype.slice.call(arguments), url = args.shift(), fn = args.pop(), data = args.shift(), headers = args.shift() if (typeof fn !== 'function') throw new TypeError('http client requires a callback function') return request(method.toUpperCase(), url, data, headers, fn) }}
// --- Public API
exports.get = exports.view = client('get')exports.post = exports.create = client('post', true)exports.put = exports.update = client('put', true)exports.del = exports.destroy = client('delete', true)exports.post = exports.create = client('post')exports.put = exports.update = client('put')exports.del = exports.destroy = client('delete')