Started fixing HTTP client
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent c3fec82 · Browse files · 1 file·+28 added−21 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+28 added−21 removed
- other:+28 added−21 removed
Provenance
- Commit
d5ca1ea- Tree
38c002f- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
c3fec82
1 file changed+28 added−21 removed
modified:lib/express/http.js+28 added-21 removed
1234567859101112133 unmodified lines17181920171821222324222526242728293031303233343335363738394041404142434445464243444546474849484950515051525354555456575859603 unmodified lines64656667666768697071727374757677787980818283
// 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') /**3 unmodified lines * @param {string} method * @param {string} url * @param {hash} headers * @param {hash} data * @param {Promise} promise * @return {Promise} * @param {function} fn * @api private */ function request(method, url, headers, data, promise) {function request(method, url, headers, data, fn) { var buf = '', promise = promise || new events.Promise, 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 + "'")) fn(new Error("client failed to " + method + " `" + url.href + "'")) }) 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) e(new Error('request failed with status ' + res.statusCode + ' "' + http.STATUS_CODES[res.statusCode] + '"')) else if (res.statusCode >= 300 && res.statusCode < 400) request(method, res.headers.location, headers, data, fn) 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_.3 unmodified lines * @api private */
function client(method) { return function(url, headers, data) { return request(method.toUpperCase(), url, headers, data) return function(url, headers, data, fn) { return request(method.toUpperCase(), url, headers, data, fn) }}
// --- Public API
exports.get = exports.view = client('get')exports.post = exports.create = client('post')exports.put = exports.update = client('put')exports.del = exports.destroy = client('delete')
exports.get('http://www.google.ca', {}, {}, function(e, content){ if (!e) require('sys').p(content.length)})