Created
March 17, 2015 15:35
-
-
Save HakurouKen/3f4bff570f54d5abae64 to your computer and use it in GitHub Desktop.
A basic lib to process client request for Node.js
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 http = require('http'), | |
https = require('https'), | |
iconv = require('iconv-lite'), | |
url = require('url'), | |
querystring = require('querystring'), | |
fs = require('fs'); | |
var request = module.exports = {}; | |
function parseUrl(urlStr){ | |
var _url = url.parse(urlStr); | |
_url.port = _url.port || ( _url.protocol === 'https:' ? 443 : 80 ); | |
return _url; | |
} | |
function getProtocol(u){ | |
if (typeof u === 'string'){ | |
return u.substring(0,u.indexOf(':')) === 'https' ? https : http; | |
} else if( typeof u === 'object'){ | |
return u.protocol === 'https:' ? https : http; | |
} | |
return http; | |
} | |
function type(o){ | |
return Object.prototype.toString.call(o).slice(8,-1).toLowerCase(); | |
} | |
function extend(deep,dest,src){ | |
var key, | |
copy; | |
if( deep !== true && deep !== false ){ | |
src = dest; | |
dest = deep; | |
deep = false; | |
} | |
for( key in src ){ | |
if( src.hasOwnProperty(key) ){ | |
copy = src[key]; | |
if( deep ){ | |
// window.window === window | |
if( dest === copy ){ | |
continue; | |
} | |
if( type(copy) === 'object' ){ | |
dest[key] = arguments.callee(dest[key] || {}, copy); | |
} else if( type(copy) === 'array' ){ | |
dest[key] = arguments.callee(dest[key] || [], copy); | |
} else { | |
dest[key] = copy; | |
} | |
} else { | |
dest[key] = copy; | |
} | |
} | |
} | |
return dest; | |
} | |
function _send(protocol,data,options,charset,callback){ | |
var req = protocol.request(options,function(res){ | |
var data; | |
if( charset === 'gbk' ){ | |
res.setEncoding('binary'); | |
} else { | |
res.setEncoding(charset); | |
} | |
res.on('data',function(chunk){ | |
data += chunk; | |
}); | |
res.on('end',function(){ | |
if( charset === 'gbk' ){ | |
data = iconv.decode(new Buffer(data,'binary'),'gbk'); | |
} | |
callback(data,res); | |
}); | |
}); | |
if( data != null ){ | |
if( type(data) === 'object' ){ | |
req.write(querystring.stringify(data)); | |
} else { | |
req.write(data); | |
} | |
} | |
req.end(); | |
return req; | |
} | |
request.get = function(urlStr,callback,headers,charset){ | |
var _url = parseUrl(urlStr), | |
protocol = getProtocol(_url), | |
_charset,_headers; | |
_charset = (type(charset) === 'string') ? charset : 'utf8'; | |
if( type(headers) === 'string' && charset === undefined ){ | |
_charset = headers; | |
} | |
_headers = (type(headers) === 'object') ? headers : {}; | |
_headers['content-length'] = 0; | |
var options = { | |
host: _url.host, | |
port: _url.port, | |
path: _url.path, | |
method: 'GET', | |
headers: _headers | |
}; | |
return _send(protocol,null,options,_charset,callback); | |
}; | |
request.post = function(urlStr,data,callback,headers,charset){ | |
var _url = parseUrl(urlStr), | |
protocol = getProtocol(_url), | |
postData = querystring.stringify(data), | |
_charset,_headers; | |
_charset = (type(charset) === 'string') ? charset : 'utf8'; | |
if( type(headers) === 'string' && charset === undefined ){ | |
_charset = headers; | |
} | |
_headers = (type(headers) === 'object') ? headers : {}; | |
_headers['content-length'] = postData.length; | |
var options = { | |
host: _url.host, | |
port: _url.port, | |
path: _url.path, | |
method: 'POST', | |
headers: _headers | |
}; | |
return _send(protocol,postData,options,_charset,callback); | |
}; | |
request.getFile = function(urlStr,output,callback,headers){ | |
var _url = parseUrl(urlStr), | |
protocol = getProtocol(_url); | |
if( callback === undefined || type(callback) === 'object' ){ | |
headers = callback; | |
callback = function(){}; | |
} | |
var options = { | |
host: _url.host, | |
port: _url.port, | |
path: _url.path, | |
headers: headers | |
}; | |
protocol.get(options,function(res){ | |
var content = ''; | |
res.setEncoding('binary'); | |
res.on('data',function(chunk){ | |
content += chunk; | |
}); | |
res.on('end',function(){ | |
var data = new Buffer(content,'binary'); | |
fs.writeFile(output,data,function(err){ | |
callback(err); | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment