Comparing version 2.2.0 to 2.2.5
85
main.js
@@ -23,2 +23,7 @@ // Copyright 2010-2011 Mikeal Rogers | ||
, mimetypes = require('./mimetypes') | ||
, oauth = require('./oauth') | ||
, uuid = require('./uuid') | ||
, Cookie = require('./vendor/cookie') | ||
, CookieJar = require('./vendor/cookie/jar') | ||
, cookieJar = new CookieJar | ||
; | ||
@@ -139,2 +144,15 @@ | ||
if (self.jar === false) { | ||
// disable cookies | ||
var cookies = false; | ||
self._disableCookies = true; | ||
} else if (self.jar) { | ||
// fetch cookie from the user defined cookie jar | ||
var cookies = self.jar.get({ url: self.uri.href }) | ||
} else { | ||
// fetch cookie from the global cookie jar | ||
var cookies = cookieJar.get({ url: self.uri.href }) | ||
} | ||
if (cookies) {self.headers.Cookie = cookies} | ||
if (!self.uri.pathname) {self.uri.pathname = '/'} | ||
@@ -167,3 +185,49 @@ if (!self.uri.port) { | ||
if (self.form) { | ||
self.headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8' | ||
self.body = qs.stringify(self.form).toString('utf8') | ||
} | ||
if (self.oauth) { | ||
var form | ||
if (self.headers['content-type'] && | ||
self.headers['content-type'].slice(0, 'application/x-www-form-urlencoded'.length) === | ||
'application/x-www-form-urlencoded' | ||
) { | ||
form = qs.parse(self.body) | ||
} | ||
if (self.uri.query) { | ||
form = qs.parse(self.uri.query) | ||
} | ||
if (!form) form = {} | ||
var oa = {} | ||
for (i in form) oa[i] = form[i] | ||
for (i in self.oauth) oa['oauth_'+i] = self.oauth[i] | ||
if (!oa.oauth_version) oa.oauth_version = '1.0' | ||
if (!oa.oauth_timestamp) oa.oauth_timestamp = Math.floor( (new Date()).getTime() / 1000 ).toString() | ||
if (!oa.oauth_nonce) oa.oauth_nonce = uuid().replace(/-/g, '') | ||
oa.oauth_signature_method = 'HMAC-SHA1' | ||
var consumer_secret = oa.oauth_consumer_secret | ||
delete oa.oauth_consumer_secret | ||
var token_secret = oa.oauth_token_secret | ||
delete oa.oauth_token_secret | ||
var baseurl = self.uri.protocol + '//' + self.uri.host + self.uri.pathname | ||
var signature = oauth.hmacsign(self.method, baseurl, oa, consumer_secret, token_secret) | ||
// oa.oauth_signature = signature | ||
for (i in form) { | ||
if ( i.slice(0, 'oauth_') in self.oauth) { | ||
// skip | ||
} else { | ||
delete oa['oauth_'+i] | ||
} | ||
} | ||
self.headers.authorization = | ||
'OAuth '+Object.keys(oa).sort().map(function (i) {return i+'="'+encodeURIComponent(oa[i])+'"'}).join(',') | ||
self.headers.authorization += ',oauth_signature="'+encodeURIComponent(signature)+'"' | ||
} | ||
if (self.uri.auth && !self.headers.authorization) { | ||
@@ -364,2 +428,14 @@ self.headers.authorization = "Basic " + toBase64(self.uri.auth.split(':').map(function(item){ return qs.unescape(item)}).join(':')) | ||
} | ||
if (response.statusCode == 200 && response.headers['set-cookie'] && (!self._disableCookies)) { | ||
response.headers['set-cookie'].forEach(function(cookie) { | ||
if (self.jar) { | ||
// custom defined jar | ||
self.jar.add(new Cookie(cookie)); | ||
} else { | ||
// add to the global cookie jar if user don't define his own | ||
cookieJar.add(new Cookie(cookie)); | ||
} | ||
}); | ||
} | ||
self.callback(null, response, response.body) | ||
@@ -484,2 +560,4 @@ }) | ||
de.del = def(request.del) | ||
de.cookie = def(request.cookie) | ||
de.jar = def(request.jar) | ||
return de | ||
@@ -512,1 +590,8 @@ } | ||
} | ||
request.jar = function () { | ||
return new CookieJar | ||
} | ||
request.cookie = function (str) { | ||
if (typeof str !== 'string') throw new Error("The cookie function only accepts STRING as param") | ||
return new Cookie(str) | ||
} |
{ "name" : "request" | ||
, "description" : "Simplified HTTP request client." | ||
, "tags" : ["http", "simple", "util", "utility"] | ||
, "version" : "2.2.0" | ||
, "version" : "2.2.5" | ||
, "author" : "Mikeal Rogers <mikeal.rogers@gmail.com>" | ||
@@ -6,0 +6,0 @@ , "repository" : |
@@ -96,2 +96,50 @@ # Request -- Simplified HTTP request method | ||
## OAuth Signing | ||
```javascript | ||
// Twitter OAuth | ||
var qs = require('querystring') | ||
, oauth = | ||
{ callback: 'http://mysite.com/callback/' | ||
, consumer_key: CONSUMER_KEY | ||
, consumer_secret: CONSUMER_SECRET | ||
} | ||
, url = 'https://api.twitter.com/oauth/request_token' | ||
; | ||
request.post({url:url, oauth:oauth}, function (e, r, body) { | ||
// Assume by some stretch of magic you aquired the verifier | ||
var access_token = qs.parse(body) | ||
, oauth = | ||
{ consumer_key: CONSUMER_KEY | ||
, consumer_secret: CONSUMER_SECRET | ||
, token: access_token.oauth_token | ||
, verifier: VERIFIER | ||
, token_secret: access_token.oauth_token_secret | ||
} | ||
, url = 'https://api.twitter.com/oauth/access_token' | ||
; | ||
request.post({url:url, oauth:oauth}, function (e, r, body) { | ||
var perm_token = qs.parse(body) | ||
, oauth = | ||
{ consumer_key: CONSUMER_KEY | ||
, consumer_secret: CONSUMER_SECRET | ||
, token: perm_token.oauth_token | ||
, token_secret: perm_token.oauth_token_secret | ||
} | ||
, url = 'https://api.twitter.com/1/users/show.json?' | ||
, params = | ||
{ screen_name: perm_token.screen_name | ||
, user_id: perm_token.user_id | ||
} | ||
; | ||
url += qs.stringify(params) | ||
request.get({url:url, oauth:oauth, json:true}, function (e, r, user) { | ||
console.log(user) | ||
}) | ||
}) | ||
}) | ||
``` | ||
### request(options, callback) | ||
@@ -115,3 +163,5 @@ | ||
* `proxy` - An HTTP proxy to be used. Support proxy Auth with Basic Auth the same way it's supported with the `url` parameter by embedding the auth info in the uri. | ||
* `oauth` - Options for OAuth HMAC-SHA1 signing, see documentation above. | ||
* `strictSSL` - Set to `true` to require that SSL certificates be valid. Note: to use your own certificate authority, you need to specify an agent that was created with that ca as an option. | ||
* `jar` - Set to `false` if you don't want cookies to be remembered for future use or define your custom cookie jar (see examples section) | ||
@@ -168,4 +218,18 @@ | ||
``` | ||
### request.cookie | ||
Function that creates a new cookie. | ||
```javascript | ||
request.cookie('cookie_string_here') | ||
``` | ||
### request.jar | ||
Function that creates a new cookie jar. | ||
```javascript | ||
request.jar() | ||
``` | ||
## Examples: | ||
@@ -197,1 +261,29 @@ | ||
``` | ||
Cookies are enabled by default (so they can be used in subsequent requests). To disable cookies set jar to false (either in defaults or in the options sent). | ||
```javascript | ||
var request = request.defaults({jar: false}) | ||
request('http://www.google.com', function () { | ||
request('http://images.google.com') | ||
}) | ||
``` | ||
If you to use a custom cookie jar (instead of letting request use its own global cookie jar) you do so by setting the jar default or by specifying it as an option: | ||
```javascript | ||
var j = request.jar() | ||
var request = request.defaults({jar:j}) | ||
request('http://www.google.com', function () { | ||
request('http://images.google.com') | ||
}) | ||
``` | ||
OR | ||
```javascript | ||
var j = request.jar() | ||
var cookie = request.cookie('your_cookie_here') | ||
j.add(cookie) | ||
request({url: 'http://www.google.com', jar: j}, function () { | ||
request('http://images.google.com') | ||
}) | ||
``` |
@@ -30,2 +30,2 @@ var server = require('./server') | ||
console.log("All tests passed.") | ||
console.log("All tests passed.") |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102990
20
1411
286
2