Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Almost ready for v0.4.

  • Loading branch information...
commit 16f3f9c9322372511c117a4a8339b4d9800a875a 1 parent 5edd53e
@SaltwaterC authored
View
1  CHANGELOG.md
@@ -6,6 +6,7 @@
* Deprecated options.nogzip in favor of options.nocompress.
* New option: noua for tuning off the default User-Agent header.
* New option: progress for passing a callback indicating the download progress. [#8](https://github.com/SaltwaterC/http-get/issues/8)
+ * New option: debug for reporting the protocol violations that are silently dropped. For node.js 0.6.18+ the gzip / deflate content is decoded even when the client did not send the accept-encoding header. With this option, this situation is reported as warning in STDERR.
* The 4xx errors attach the body of the HTTP(S) response, if possible. [#7](https://github.com/SaltwaterC/http-get/issues/7)
* If the Last-Modified header is set, then for the file downloads it keeps the timestamp as the mtime value. [#6](https://github.com/SaltwaterC/http-get/issues/6)
View
4 README.md
@@ -21,7 +21,9 @@ Either manually clone this repository into your node_modules directory, or the r
* [node.js](http://nodejs.org/) v0.6.11+ for general usage. Previous versions are broken. Invalid domain names hang the event loop [#2688](https://github.com/joyent/node/pull/2688).
* [node.js](http://nodejs.org/) v0.6.18+ with zlib bindings for using the transparent gzip / deflate decompression. Previous versions are broken. They don't have proper error reporting [#3230](https://github.com/joyent/node/issues/3230). node.js v0.6.11 - v0.6.17 may be used, but options.nocompress is forced as true.
-node.js v0.4 is **NOT** supported due to lack of zlib bindings. Only the v0.3 versions support node.js v0.4.
+This library is not recommended under node.js v0.6.17 due to [this issue](https://groups.google.com/forum/#!topic/nodejs/6euYfwMmx1Y).
+
+node.js v0.4 is **NOT** supported due to lack of zlib bindings. Only http-get v0.3 versions supports node.js v0.4.
## Contributor
View
4 lib/http-get.js
@@ -106,7 +106,9 @@ var get = function (options, file, cb, reqId) {
if (options.nocompress && res.headers['content-encoding']) {
if (semver.satisfies(process.version, '>=0.6.18')) {
- console.error('Warning: the server sent %s content without being requested for %s.', res.headers['content-encoding'], u.format(url));
+ if (options.debug) {
+ console.error('Warning: the server sent %s content without being requested for %s.', res.headers['content-encoding'], u.format(url));
+ }
} else {
var err = tools.formattedError('The server sent %s content without being requested.', res.headers['content-encoding']);
err.code = res.statusCode;
View
67 tests/includes/common.js
@@ -5,6 +5,7 @@ var fs = require('fs');
var p = require('path');
var http = require('http');
var https = require('https');
+var zlib = require('zlib');
var options = {
host: '127.0.0.1',
@@ -35,15 +36,12 @@ options.secureUrl = u.format({
pathname: '/'
});
-options.gzipBuffer = new Buffer([0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x03, 0x4b, 0xcb, 0xcf, 0x07, 0x00, 0x21, 0x65, 0x73, 0x8c,
- 0x03, 0x00, 0x00, 0x00]);
-
exports.options = options;
var createFooServer = function (secure, cb) {
var srvCb = function (req, res) {
var gzip = false;
+ var deflate = false;
if (req.headers.foo) {
res.setHeader('foo', req.headers.foo);
}
@@ -52,14 +50,39 @@ var createFooServer = function (secure, cb) {
if (accept.indexOf('gzip') != -1) {
gzip = true;
res.setHeader('content-encoding', 'gzip');
+ } else if (accept.indexOf('deflate') != -1) {
+ deflate = true;
}
}
switch (req.url) {
case '/404':
res.writeHead(404, {'content-type': 'text/plain'});
- res.write('Not Found');
- res.end();
+ if ( ! gzip && ! deflate) {
+ res.write('Not Found');
+ res.end();
+ } else {
+ if (gzip) {
+ zlib.gzip('Not Found', function (err, compressed) {
+ if ( ! err) {
+ res.write(compressed);
+ } else {
+ res.writeHead(500, {'content-type': 'text/plain'});
+ }
+ res.end();
+ });
+ }
+ if (deflate) {
+ zlib.deflate('Not Found', function (err, compressed) {
+ if ( ! err) {
+ res.write(compressed);
+ } else {
+ res.writeHead(500, {'content-type': 'text/plain'});
+ }
+ res.end();
+ });
+ }
+ }
return;
break;
@@ -69,14 +92,34 @@ var createFooServer = function (secure, cb) {
}
if (req.method != 'HEAD') {
- if ( ! gzip) {
+ if ( ! gzip && ! deflate) {
res.write('foo');
+ res.end();
} else {
- res.write(options.gzipBuffer);
+ if (gzip) {
+ zlib.gzip('foo', function (err, compressed) {
+ if ( ! err) {
+ res.write(compressed);
+ } else {
+ res.writeHead(500, {'content-type': 'text/plain'});
+ }
+ res.end();
+ });
+ }
+ if (deflate) {
+ zlib.deflate('foo', function (err, compressed) {
+ if ( ! err) {
+ res.write(compressed);
+ } else {
+ res.writeHead(500, {'content-type': 'text/plain'});
+ }
+ res.end();
+ });
+ }
}
+ } else {
+ res.end();
}
-
- res.end();
};
if (secure) {
var server = https.createServer({
@@ -96,10 +139,10 @@ exports.createFooServer = createFooServer;
var merge = function (obj1, obj2) {
var obj3 = {};
- for (attrname in obj1) {
+ for (var attrname in obj1) {
obj3[attrname] = obj1[attrname];
}
- for (attrname in obj2) {
+ for (var attrname in obj2) {
obj3[attrname] = obj2[attrname];
}
return obj3;
View
2  tests/request-broken-dns-name.js
@@ -1,5 +1,3 @@
-process.exit(0); // XXX ignores this test till a proper node v0.6.11 is available
-
var http = require('../');
var assert = require('assert');
View
4 tests/request-headers.js
@@ -10,10 +10,6 @@ common.executeTests(function (err, res) {
assert.ifError(err);
assert.deepEqual(res.code, 200);
assert.deepEqual(res.headers['content-type'], 'text/plain');
- try {
- require('gzbz2');
- assert.deepEqual(res.headers['content-encoding'], 'gzip');
- } catch (e) {}
assert.deepEqual(res.headers['foo'], 'bar');
assert.deepEqual(res.buffer, 'foo');
},
View
4 tests/request-no-prefix.js
@@ -15,10 +15,6 @@ var server = common.createFooServer(false, function () {
assert.ifError(err);
assert.deepEqual(res.code, 200);
assert.deepEqual(res.headers['content-type'], 'text/plain');
- try {
- require('gzbz2');
- assert.deepEqual(res.headers['content-encoding'], 'gzip');
- } catch (e) {}
assert.deepEqual(res.buffer, 'foo');
server.close();
});
View
4 tests/response-buffer.js
@@ -10,10 +10,6 @@ common.executeTests(function (err, res) {
assert.ifError(err);
assert.deepEqual(res.code, 200);
assert.deepEqual(res.headers['content-type'], 'text/plain');
- try {
- require('gzbz2');
- assert.deepEqual(res.headers['content-encoding'], 'gzip');
- } catch (e) {}
assert.deepEqual(res.buffer, 'foo');
});
View
4 tests/response-file-stream.js
@@ -12,10 +12,6 @@ common.executeTests(function (err, res) {
assert.ifError(err);
assert.deepEqual(res.code, 200);
assert.deepEqual(res.headers['content-type'], 'text/plain');
- try {
- require('gzbz2');
- assert.deepEqual(res.headers['content-encoding'], 'gzip');
- } catch (e) {}
fs.stat(res.file, function (err) {
assert.ifError(err);
fs.readFile(res.file, function (err, data) {
View
11 tests/response-not-requested-gzip.js
@@ -3,6 +3,7 @@ var hg = require('../');
var semver = require('semver');
var assert = require('assert');
+var zlib = require('zlib');
var common = require('./includes/common.js');
var http = require('http');
@@ -12,8 +13,14 @@ var callback = false;
var server = http.createServer(function (req, res) {
res.setHeader('content-encoding', 'gzip');
res.writeHead(200, {'content-type': 'text/plain'});
- res.write(common.options.gzipBuffer);
- res.end();
+ zlib.gzip('foo', function (err, compressed) {
+ if ( ! err) {
+ res.write(compressed);
+ } else {
+ res.writeHead(500, {'content-type': 'text/plain'});
+ }
+ res.end();
+ });
});
server.listen(common.options.port, common.options.host, function () {
Please sign in to comment.
Something went wrong with that request. Please try again.