Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions cgi.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,24 @@ function cgi(cgiBin, options) {
if (!options.nph) {
cgiResult = new CGIParser(cgiSpawn.stdout);

// When the parser encounters an error parsing the headers, then
// the 'error' event is emitted with an Error instance.
var headersHaveError = false;
cgiResult.on('error', function(err){
headersHaveError = true;
if (options.stderr) {
options.stderr.write(err.toString());
} else {
throw(err);
}
});

// When the blank line after the headers has been parsed, then
// the 'headers' event is emitted with a Headers instance.
cgiResult.on('headers', function(headers) {
// Prevent sending anything if there was an error parsing the headers
if (headersHaveError) return;

headers.forEach(function(header) {
// Don't set the 'Status' header. It's special, and should be
// used to set the HTTP response code below.
Expand Down
5 changes: 5 additions & 0 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function Parser(stream) {
allowFoldedHeaders: false
});
this._headerParser.on('headers', this._onHeadersComplete.bind(this));
this._headerParser.on('error', this._onHeadersError.bind(this));
}
require('util').inherits(Parser, StreamStack);
module.exports = Parser;
Expand All @@ -38,3 +39,7 @@ Parser.prototype._onHeadersComplete = function(headers, leftover) {
this._onData(leftover);
}
}

Parser.prototype._onHeadersError = function(err) {
this.emit('error', err);
}