Created
September 30, 2011 07:15
-
-
Save ishiduca/1252940 to your computer and use it in GitHub Desktop.
httpClientRequestのラッパー
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
var url = require('url'); | |
var http = require('http'); | |
var path = require('path'); | |
// httpRequest('POST', 'http://xxx.com/post', { 'connection' : 'keep-alive' }, "mode=write&data=foo", function (res, req) { | |
// console.log(res.statusCode); | |
// console.log(JSON.stringify(res.headers)); | |
// console.log(JSON.stringify(req)); | |
// | |
// var data = ''; | |
// res.on('data', function (chunk) { data += chunk; }); | |
// res.on('close', function (err) { throw err; }); | |
// res.on('end', functon () { console.log(data); }); | |
// }); | |
function httpRequest (/* method, uri, headers, body, callback */) { | |
var slice = Array.prototype.slice; | |
var args = slice.apply(arguments); | |
if (args.length < 3) { | |
throw { | |
name : 'Arguments Error', | |
message : 'arugments: method, uri [, headers, body ], callback' | |
}; | |
} | |
var method = args.shift(); | |
var uriStr = args.shift(); | |
var callback = args.pop (); | |
var headers = args.shift() || {}; | |
var body = args.shift() || null; | |
if (! (typeof method === 'string' && method.match(/^(get|post|delete|head|put)$/i))) { | |
throw { | |
name : 'type error', | |
message : '"method" must be "get", "post", "delete", "head", "put"' | |
}; | |
} | |
var uriReg = /^(http(s)?):\/\/([0-9.\-A-Z$a-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/; | |
if (! (typeof uriStr === 'string' && uriStr.match(uriReg))) { | |
throw { | |
name : '"uri" type error', | |
message: 'something different "' + uriStr + '"' | |
}; | |
} | |
var uri = url.parse(uriStr); | |
var query = uri['search'] || ''; | |
var path = (! uri['pathname']) ? '/' | |
: (uri['pathname'].slice(0, 1) !== '/') ? [ '/', uri['pathname'] ].join('') | |
: uri['pathname']; | |
var options = { | |
'method': method.toUpperCase(), | |
'host' : uri['hostname'], | |
'port' : (uri['port']) ? uri['port'] | |
: (uri['protocol'] === 'https') ? '443' | |
: '80', | |
'path' : [ path, query ].join('') | |
}; | |
if (method === 'POST') { | |
if (typeof body !== 'string' && ! typeof body !== 'undefined' && typeof body === 'object') { | |
body = require('querystring').stringify(body); | |
} | |
headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded'; | |
headers['content-length'] = (body) ? body.length : 0; | |
} | |
if (this['referer']) headers['referer'] = this['referer']; | |
if (this['cookie']) headers['cookie'] = this['cookie']; | |
options['headers'] = headers; | |
var req = http.request(options); | |
req.on('error', function (e) { | |
console.log([ e.name, e.message ].join(': ')); | |
}); | |
var that = this; | |
req.on('response', function (response) { | |
var statusCode = response.statusCode; | |
var _location = response['headers']['location'] || undefined; | |
var _cookie = (response['headers']['set-cookie']) | |
? (response['headers']['set-cookie'])[0] : that['cookie']; | |
if (_cookie) that['cookie'] = _cookie; | |
that['referer'] = uriStr; | |
if (that['redirect'] && statusCode >= 300 && statusCode < 400 && _location) { | |
var _headers = { 'referer' : uriStr }; | |
if (_cookie) _headers['cookie'] = _cookie; | |
httpRequest('GET', _location, _headers, function (response) { | |
callback(response, [ _location, _headers ]); | |
}); | |
} else { | |
callback(response, [ uriStr, headers, body ]); | |
} | |
}); | |
if (body) req.write(body); | |
req.end(); | |
} | |
exports.httpRequest = httpRequest; | |
// var client = new HttpClient; | |
// client.request(...); | |
function HttpClient () { | |
this.cookie = undefined; | |
this.referer = undefined; | |
this.redirect = true; | |
} | |
(function () { | |
HttpClient.prototype.request = httpRequest; | |
})(); | |
exports.HttpClient = HttpClient; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment