Kong / mashape-oauth

OAuth Modules for Node.js - Supporting RSA, HMAC, PLAINTEXT, 2,3-Legged, 1.0a, Echo, XAuth, and 2.0
http://oauthbible.com
MIT License
1.77k stars 186 forks source link

Oauth not working request with headers.Accept='application/pdf' #8

Closed mgilgar closed 10 years ago

mgilgar commented 10 years ago

I am trying to GET some content in 'application/pdf' format (PDF 1.4). I specify in the headers headers.Accept='application/pdf' and I get content that display an empty file in any PDF viewer but the file is not empty. Have a look at this code in lib/oauth.js:

if ($this.clientOptions.detectResponseContentType && utils.isBinaryContent(response)) { data = new Buffer(0); type = 1; output = response; } else if (response.headers['content-encoding'] === 'gzip') { var gunzip = zlib.createGunzip(); data = new Buffer(0); type = 2; response.pipe(gunzip); output = gunzip; } else { response.setEncoding('utf8'); data = ""; output = response; }

$this.clientOptions.detectResponseContentType is undefined and it is not documented what it does or how to set it up anyway. In regards to isBinaryContent function it returns false for PDF. So it is the else what it is run, so we run this line: response.setEncoding('utf8')

I have fixed it locally by adding this: if ($this.clientOptions.detectResponseContentType && utils.isBinaryContent(response)) { data = new Buffer(0); type = 1; output = response; } else if (response.headers['content-encoding'] === 'gzip') { var gunzip = zlib.createGunzip(); data = new Buffer(0); type = 2; response.pipe(gunzip); output = gunzip; } else if (headers.Accept=='application/pdf') { data = new Buffer(0); type = 1; output = response; } else { response.setEncoding('utf8'); data = ""; output = response; }

This is more a hack than a solution but I am not sure is PDF is binary file format and I am not sure how do you use $this.clientOptions.detectResponseContentType.

nijikokun commented 10 years ago

I check for text based entities instead of binary now. And you can set that through the setClientOptions function.

I don't think I documented this because it's very advanced.

OAuth.prototype.setClientOptions = function (options) {
  this.clientOptions = utils.extend(this._clientOptions, options);
};
mgilgar commented 10 years ago

You missed a closing parethensis:

utils.isBinaryContent = function (response) { return (!response.headers || !response.headers["content-type"]) ? false : (response.headers["content-type"].match(/^(text|html|json|doc|xml|html|rss|form|javascript|ecmascript)/) ? false : true; };

Should be: : true); };

nijikokun commented 10 years ago

Ah thank you

On Mon, Mar 3, 2014 at 1:51 PM, Miguel notifications@github.com wrote:

You missed a closing parethensis:

utils.isBinaryContent = function (response) { return (!response.headers !response.headers["content-type"]) ? false
(response.headers["content-type"].match(/^(text html json doc xml html rss form javascript ecmascript)/)

? false : true; };

Should be: : true); };

Reply to this email directly or view it on GitHubhttps://github.com/Mashape/mashape-oauth/issues/8#issuecomment-36564516 .