Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added tests for relative redirect location headers. Fixes content-len…

…gth.
  • Loading branch information...
commit d2569399ea3276f15258581e3d71eccdb7948134 1 parent 9a0cf05
Robert Sköld slaskis authored
Showing with 26 additions and 5 deletions.
  1. +8 −1 README.md
  2. +8 −4 lib/schemes/http.js
  3. +10 −0 test/test-open-uri.js
9 README.md
View
@@ -68,6 +68,12 @@ Uses the [addressable module](https://github.com/publicclass/addressable) for pa
## 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
* [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).
@@ -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!)
- 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 "/"
- 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...
12 lib/schemes/http.js
View
@@ -46,15 +46,19 @@ module.exports = function http(uri,opts,output){
uri.method = opts.method || "GET";
// Body Content-Length
- if( typeof opts.body == "string" || Buffer.isBuffer( opts.body ) )
- uri.headers["content-length"] = Buffer.byteLength(opts.body);
+ if( typeof opts.body == "string" )
+ 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){
// 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;
return open(res.headers.location,opts,output);
- }
+ }
// TODO How should other statusCodes be handled? (like 403, 404, 500), should they be returned as Errors?
10 test/test-open-uri.js
View
@@ -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){
var stream = writeStream();
var loaded = false;
Please sign in to comment.
Something went wrong with that request. Please try again.