Skip to content

Commit

Permalink
Added tests for relative redirect location headers. Fixes content-len…
Browse files Browse the repository at this point in the history
…gth.
  • Loading branch information
slaskis committed Jul 15, 2011
1 parent 9a0cf05 commit d256939
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -68,6 +68,12 @@ Uses the [addressable module](https://github.com/publicclass/addressable) for pa


## History ## History


### 0.3.5

* [Fix] User Buffer.byteLength(body) only on strings, and body.length on Buffers.

* [Fix] Support for broken redirect implementations that give relative Location: headers.

### 0.3.4 ### 0.3.4


* [Fix] Use Buffer.byteLength(body) for Content-Length header as well, it's just better (and fixes a bug where the body was cut off). * [Fix] Use Buffer.byteLength(body) for Content-Length header as well, it's just better (and fixes a bug where the body was cut off).
Expand Down Expand Up @@ -141,7 +147,8 @@ Uses the [addressable module](https://github.com/publicclass/addressable) for pa
- Allow a REST interface? So if something is written to stdin it's sent as body to the url using POST by default, but definable with -X (curl style!) - Allow a REST interface? So if something is written to stdin it's sent as body to the url using POST by default, but definable with -X (curl style!)
- Pass command line arguments as options to the scheme functions. Ex. `open-uri -gzip http://google.com` sets opts.gzip to true. - Pass command line arguments as options to the scheme functions. Ex. `open-uri -gzip http://google.com` sets opts.gzip to true.


* More schemes support? Suggestions? * More schemes support? Suggestions?
- Support for opening a file descriptor? Basically a shortcut for fs.readFile(fd).
- S3 (using [knox module](https://github.com/LearnBoost/knox)), should list objects when path ends with "/" - S3 (using [knox module](https://github.com/LearnBoost/knox)), should list objects when path ends with "/"
- NNTP (using [node-nntp module](https://github.com/mscdex/node-nntp)) - NNTP (using [node-nntp module](https://github.com/mscdex/node-nntp))
- IRC (using [node-irc module](https://github.com/martynsmith/node-irc)), open("irc://bot@chat.freenode.net/nodejs") -> callback for each message? with a say function in the callback? ex: `function(err,from,to,msg,say){ if( to == "bot" && ~msg.indexOf("hello") ){ say(from+": HI!") } }` `this` could be the irc object so the socket can be closed... - IRC (using [node-irc module](https://github.com/martynsmith/node-irc)), open("irc://bot@chat.freenode.net/nodejs") -> callback for each message? with a say function in the callback? ex: `function(err,from,to,msg,say){ if( to == "bot" && ~msg.indexOf("hello") ){ say(from+": HI!") } }` `this` could be the irc object so the socket can be closed...
Expand Down
12 changes: 8 additions & 4 deletions lib/schemes/http.js
Expand Up @@ -46,15 +46,19 @@ module.exports = function http(uri,opts,output){
uri.method = opts.method || "GET"; uri.method = opts.method || "GET";


// Body Content-Length // Body Content-Length
if( typeof opts.body == "string" || Buffer.isBuffer( opts.body ) ) if( typeof opts.body == "string" )
uri.headers["content-length"] = Buffer.byteLength(opts.body); uri.headers["content-length"] = Buffer.byteLength( opts.body );
if( Buffer.isBuffer( opts.body ) )
uri.headers["content-length"] = opts.body.length;


var req = require(uri.scheme).request(uri,function(res){ var req = require(uri.scheme).request(uri,function(res){
// Follow Redirects // Follow Redirects
if( opts.follow !== false && res.statusCode > 299 && res.statusCode < 400 && res.headers.location ){ if( opts.follow !== false && res.statusCode >= 300 && res.statusCode < 400 && res.headers.location ){
if( res.headers.location[0] == '/' ) // It's an invalid 'relative' Location, prepend with current host
res.headers.location = uri.scheme + '://' + uri.authority + res.headers.location;
opts.headers.host = addressable.parse(res.headers.location).host; opts.headers.host = addressable.parse(res.headers.location).host;
return open(res.headers.location,opts,output); return open(res.headers.location,opts,output);
} }


// TODO How should other statusCodes be handled? (like 403, 404, 500), should they be returned as Errors? // TODO How should other statusCodes be handled? (like 403, 404, 500), should they be returned as Errors?


Expand Down
10 changes: 10 additions & 0 deletions test/test-open-uri.js
Expand Up @@ -221,6 +221,16 @@ exports["Stream a text file from ftp to a file"] = function(beforeExit){
}) })
} }


exports["GET a redirect with a relative Location"] = function(beforeExit){
var loaded = false;
open("http://golang.org/cmd/5a",function(err,go){
loaded = true
assert.ok(err)
console.log(go)
})
beforeExit(function(){assert.ok(loaded)})
}

exports["Chain it"] = function(beforeExit){ exports["Chain it"] = function(beforeExit){
var stream = writeStream(); var stream = writeStream();
var loaded = false; var loaded = false;
Expand Down

0 comments on commit d256939

Please sign in to comment.