Skip to content

Commit

Permalink
Added support for HEAD requests, closes socketio#557
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd-Eden committed Oct 8, 2011
1 parent fa5b518 commit 8107c1a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,13 @@ Static.prototype.write = function (path, req, res) {
function write (status, headers, content, encoding) {
try {
res.writeHead(status, headers || undefined);
res.end(content || '', encoding || undefined);

// only write content if it's not a HEAD request and we actually have
// some content to write (304's doesn't have content).
res.end(
req.method !== 'HEAD' && content ? content : ''
, encoding || undefined
);
} catch (e) {}
}

Expand Down Expand Up @@ -306,10 +312,10 @@ Static.prototype.write = function (path, req, res) {
if (versioned) {
var expires = self.manager.get('browser client expires')
headers['Cache-Control'] = 'public, max-age=' + expires;
headers['Date'] = new Date().toUTCString();
headers['Expires'] = new Date(Date.now() + (expires * 1000)).toUTCString();
}

// check if we can send gzip data
if (gzip && reply.gzip) {
headers['Content-Length'] = reply.gzip.length;
headers['Content-Encoding'] = 'gzip';
Expand Down
18 changes: 18 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ HTTPClient.prototype.post = function (path, data, opts, fn) {
return this.request(path, opts, fn);
};

/**
* Issue a HEAD request
*
* @api private
*/

HTTPClient.prototype.head = function (path, opts, fn) {
if ('function' == typeof opts) {
fn = opts;
opts = {};
}

opts = opts || {};
opts.method = 'HEAD';

return this.request(path, opts, fn);
};

/**
* Performs a handshake (GET) request
*
Expand Down
18 changes: 17 additions & 1 deletion test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,22 @@ module.exports = {
io.server.close();
done();
});
}
},

'test that HEAD requests work': function (done) {
var port = ++ports
, io = sio.listen(port)
, cl = client(port);

cl.head('/socket.io/socket.io.js', function (res, data) {
res.headers['content-type'].should.eql('application/javascript');
res.headers['content-length'].should.match(/([0-9]+)/);

data.should.eql('');

cl.end();
io.server.close()
done();
});
}
};

0 comments on commit 8107c1a

Please sign in to comment.