path = url.pathname || '/',
port = url.port || 80,
headers = process.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 + "'"))
})
client.request(method, path, headers).finish(function(response){
if (response.statusCode < 200 && response.statusCode >= 400)
promise.emitError(new Error('request failed with status ' + response.statusCode))
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, promise)
else {
response.setBodyEncoding('utf8')
})
return promise
}
exports.get = function(url, headers, fn) {
if (typeof headers == 'function') fn = headers
request('GET', url, headers)
.addErrback(fn)
.addCallback(fn)
exports.get = function(url, headers) {
return request('GET', url, headers)
}
exports.get('http://google.com', function(content){
require('sys').p(content)
})
exports.post = function(url, headers) {
return request('POST', url, headers)
}
exports.post('http://google.com')
.addCallback(function(content, response){
puts(content)
p(response.headers)
})