diff --git a/main.js b/main.js index fa9b8ad2f..ba5293ede 100644 --- a/main.js +++ b/main.js @@ -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 @@ -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(':')) @@ -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 -} - diff --git a/uuid.js b/uuid.js index 5d8d93c50..1d83bd50a 100644 --- a/uuid.js +++ b/uuid.js @@ -1,790 +1,19 @@ - - - - - - - - - uuid.js at master from mikeal/request - GitHub - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
-
- - -
-

- mikeal / - request -

- - - - - - -
- - - - - - - - - - - - - - - - - - - - -
- - - - - - - -

Latest commit to the master branch

- -
-

- Removing irrelevant comments. - -

-
- commit 8869b2e88c - -
- - mikeal - authored - -
-
-
- - -
- - - -
-
- - -
-
-
-
- Txt - 100644 - 19 lines (14 sloc) - 0.632 kb -
- -
-
- - - - - -
-
1
-2
-3
-4
-5
-6
-7
-8
-9
-10
-11
-12
-13
-14
-15
-16
-17
-18
-19
-
-
-
module.exports = function () {
  var s = [], itoh = '0123456789ABCDEF';
 
  // Make array of random hex digits. The UUID only has 32 digits in it, but we
  // allocate an extra items to make room for the '-'s we'll be inserting.
  for (var i = 0; i <36; i++) s[i] = Math.floor(Math.random()*0x10);
 
  // Conform to RFC-4122, section 4.4
  s[14] = 4; // Set 4 high bits of time_high field to version
  s[19] = (s[19] & 0x3) | 0x8; // Specify 2 high bits of clock sequence
 
  // Convert to hex chars
  for (var i = 0; i <36; i++) s[i] = itoh[s[i]];
 
  // Insert '-'s
  s[8] = s[13] = s[18] = s[23] = '-';
 
  return s.join('');
}
-
-
- -
-
-
-
- -
- - - -
- -
- - - - - - - - -
-

Markdown Cheat Sheet

- -
- -
-
-

Format Text

-

Headers

-
-# This is an <h1> tag
-## This is an <h2> tag
-###### This is an <h6> tag
-

Text styles

-
-*This text will be italic*
-_This will also be italic_
-**This text will be bold**
-__This will also be bold__
-
-*You **can** combine them*
-
-
-
-

Lists

-

Unordered

-
-* Item 1
-* Item 2
-  * Item 2a
-  * Item 2b
-

Ordered

-
-1. Item 1
-2. Item 2
-3. Item 3
-   * Item 3a
-   * Item 3b
-
-
-

Miscellaneous

-

Images

-
-![GitHub Logo](/images/logo.png)
-Format: ![Alt Text](url)
-
-

Links

-
-http://github.com - automatic!
-[GitHub](http://github.com)
-

Blockquotes

-
-As Kanye West said:
-> We're living the future so
-> the present is our past.
-
-
-
-
- -

Code Examples in Markdown

-
-

Syntax highlighting with GFM

-
-```javascript
-function fancyAlert(arg) {
-  if(arg) {
-    $.facebox({div:'#foo'})
-  }
+module.exports = function () {
+  var s = [], itoh = '0123456789ABCDEF';
+ 
+  // Make array of random hex digits. The UUID only has 32 digits in it, but we
+  // allocate an extra items to make room for the '-'s we'll be inserting.
+  for (var i = 0; i <36; i++) s[i] = Math.floor(Math.random()*0x10);
+ 
+  // Conform to RFC-4122, section 4.4
+  s[14] = 4;  // Set 4 high bits of time_high field to version
+  s[19] = (s[19] & 0x3) | 0x8;  // Specify 2 high bits of clock sequence
+ 
+  // Convert to hex chars
+  for (var i = 0; i <36; i++) s[i] = itoh[s[i]];
+ 
+  // Insert '-'s
+  s[8] = s[13] = s[18] = s[23] = '-';
+ 
+  return s.join('');
 }
-```
-
-
-

Or, indent your code 4 spaces

-
-Here is a Python code example
-without syntax highlighting:
-
-    def foo:
-      if not bar:
-        return true
-
-
-

Inline code for comments

-
-I think you should use an
-`<addr>` element here instead.
-
-
- -
- - -
- -
-

Something went wrong with that request. Please try again. Dismiss

-
- - - - - - -