Skip to content

Commit

Permalink
fix crash in #52
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Mar 7, 2014
1 parent 3b51043 commit 9c4c936
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
35 changes: 23 additions & 12 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,33 @@ module.exports.log_and_etagify = function(req, res, next) {

var _send = res.send
res.send = function(body) {
if (typeof(body) === 'string' || typeof(body) === 'object') {
res.header('Content-type', 'application/json')
try {
if (typeof(body) === 'string' || typeof(body) === 'object') {
res.header('Content-type', 'application/json')

if (typeof(body) === 'object' && body != null) {
if (body.error) {
res._sinopia_error = body.error
}
body = JSON.stringify(body, undefined, '\t') + '\n'
}

if (typeof(body) === 'object' && body != null) {
if (body.error) {
res._sinopia_error = body.error
// don't send etags with errors
if (!res.statusCode || (res.statusCode >= 200 && res.statusCode < 300)) {
res.header('ETag', '"' + md5sum(body) + '"')
}
body = JSON.stringify(body, undefined, '\t') + '\n'
} else {
// send(null), send(204), etc.
}

// don't send etags with errors
if (!res.statusCode || (res.statusCode >= 200 && res.statusCode < 300)) {
res.header('ETag', '"' + md5sum(body) + '"')
} catch(err) {
// if sinopia sends headers first, and then calls res.send()
// as an error handler, we can't report error properly,
// and should just close socket
if (err.message.match(/set headers after they are sent/)) {
return res.socket.destroy()
} else {
throw err
}
} else {
// send(null), send(204), etc.
}

res.send = _send
Expand Down
1 change: 1 addition & 0 deletions test/functional/config-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ users:
uplinks:
express:
url: http://localhost:55550/
timeout: 100
server2:
url: http://localhost:55552/
baduplink:
Expand Down
1 change: 1 addition & 0 deletions test/functional/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('Func', function() {
require('./tags')()
require('./mirror')()
require('./race')()
require('./racycrash')()
require('./security')()
require('./addtag')()
})
Expand Down
50 changes: 50 additions & 0 deletions test/functional/racycrash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var assert = require('assert')
, ex = module.exports

module.exports = function() {
var server = process.server
var express = process.express

describe('Racy', function() {
it('should not crash on error if client disconnects', function(_cb) {
express.get('/testexp-racycrash', function(_, res) {
res.send({
"name": "testexp-racycrash",
"versions": {
"0.1.0": {
"name": "testexp_tags",
"version": "0.1.0",
"dist": {
"shasum": "fake",
"tarball": "http://localhost:55550/testexp-racycrash/-/test.tar.gz"
}
}
}
})
})

express.get('/testexp-racycrash/-/test.tar.gz', function(_, res) {
res.header('content-length', 1e6)
res.write('test test test\n')
setTimeout(function() {
res.write('test test test\n')
res.socket.destroy()
cb()
}, 200)
})

server.request({uri:'/testexp-racycrash/-/test.tar.gz'}, function(err, res, body) {
assert.equal(body, 'test test test\n')
})

function cb() {
// test for NOT crashing
server.request({uri:'/testexp-racycrash'}, function(err, res, body) {
assert.equal(res.statusCode, 200)
_cb()
})
}
})
})
}

0 comments on commit 9c4c936

Please sign in to comment.