Merge branch 'integration'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parents c12ace8, 6232c1a · Browse files · 1 file·+41 added−18 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+41 added−18 removed
- other:+41 added−18 removed
Provenance
- Commit
606da1c- Tree
117f503- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent 1
c12ace8- Parent 2
6232c1a
1 file changed+41 added−18 removed
modified:lib/express/http.js+41 added-18 removed
19 unmodified lines202122232424252626272829303132323334353637383435363738394041414243424344454647484950514849525354515255565754585960616212 unmodified lines75767778757980818283848586838788899091929394959697989910010110210310410510610710810911011111211311411519 unmodified lines * @param {number} redirects * @api private */ function request(method, url, data, headers, callback, redirects) {function request(method, url, data, headers, callback, redirects, first) { var buf = '', redirects = redirects || 3, redirects = typeof redirects !== 'number' ? 3 : redirects, url = parse(url), path = url.pathname || '/', search = url.search || '', hash = url.hash || '', port = url.port || 80, headers = { host: url.hostname }.merge(headers || {}), headers = (headers || {}).merge({ Host: url.hostname }), client = http.createClient(port, url.hostname) if (headers.redirect) redirects = headers.redirect, delete headers.redirect if (data) { data = queryString.stringify(data) if (typeof data === 'object' && 'redirects' in data) redirects = data.redirects, delete data.redirects if (first && data) { if (typeof data !== 'string') data = queryString.stringify(data) if (method === 'GET') search += (search ? '&' : '?') + data else headers['Content-Length'] = data.length, headers['Content-Type'] = 'application/x-www-form-urlencoded' else { if (!headers['Content-Length']) headers['Content-Length'] = data.length if (!headers['Content-Type']) headers['Content-Type'] = 'application/x-www-form-urlencoded' } } var req = client.request(method, path + search + hash, headers) if (data && method !== 'GET') req.write(data) req.addListener('response', function(res){ if (req.statusCode < 200 || req.statusCode >= 400) callback(new Error('request failed with status ' + res.statusCode + ' "' + http.STATUS_CODES[res.statusCode] + '"')) if (res.statusCode < 200 || res.statusCode >= 400) callback(new Error('request failed with status ' + res.statusCode + ' "' + http.STATUS_CODES[res.statusCode] + '"'), '', res) else if (res.statusCode >= 300 && res.statusCode < 400) if (--redirects) request(method, res.headers.location, headers, data, callback, redirects) if (redirects--) request(method, res.headers.location, data, headers, callback, redirects, false) else callback(new Error('maximum number of redirects reached')) callback(new Error('maximum number of redirects reached'), '', res) else { res.setBodyEncoding('utf8') res .addListener('data', function(chunk){ buf += chunk })12 unmodified lines */
function client(method) { return function() { var headers, data, var redirects, args = Array.prototype.slice.call(arguments), url = args.shift(), callback = args.pop(), data = args.shift(), headers = args.shift() if (typeof callback !== 'function') throw new TypeError('http client requires a callback function') return request(method.toUpperCase(), url, data, headers, callback) return request(method.toUpperCase(), url, data, headers, callback, redirects, true) }}
// --- Public API
/** * Examples: * * var http = require('express/http') * * http.get('http://google.com/search', { q: 'foobar' }, function(err, body, response){ * if (!err) sys.puts(body) * }) * * http.get('http://google.com/search?lang=en', { q: 'foobar' }, function(){ * // ... * }) * * http.post('http://localhost:8000', '<user>tj</user>', { 'Content-Type': 'text/xml' }, function(err, body){ * // ... * }) * */
exports.get = exports.view = client('get')exports.post = exports.create = client('post')exports.put = exports.update = client('put')exports.del = exports.destroy = client('delete')