Skip to content

Commit

Permalink
Closed issue 82 : handling cookies - added tests too
Browse files Browse the repository at this point in the history
  • Loading branch information
alessioalex committed Nov 12, 2011
1 parent 739f841 commit 7daf841
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 793 deletions.
55 changes: 51 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var http = require('http')
, stream = require('stream')
, qs = require('querystring')
, mimetypes = require('./mimetypes')
, oauth = require('./oauth')
, uuid = require('./uuid')
, Cookie = require('./vendor/cookie')
, CookieJar = require('./vendor/cookie/jar')
, cookieJar = new CookieJar
Expand Down Expand Up @@ -177,6 +179,52 @@ Request.prototype.request = function () {
if (self.onResponse) self.on('error', function (e) {self.onResponse(e)})
if (self.callback) self.on('error', function (e) {self.callback(e)})

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) {
self.headers.authorization = "Basic " + toBase64(self.uri.auth.split(':').map(function(item){ return qs.unescape(item)}).join(':'))
Expand Down Expand Up @@ -530,11 +578,10 @@ request.del = function (options, callback) {
options.method = 'DELETE'
return request(options, callback)
}
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)
}
request.jar = function () {
return new CookieJar
}

Loading

0 comments on commit 7daf841

Please sign in to comment.