joyent/node — Issue #2457: http response not emitting data
Author: legege
Created: 2012-01-03 12:41:49+00:00
State: open

I'm trying to proxy a MJPEG HTTP stream and I found an issue with the http module or the http parser. A MJPEG HTTP response doesn't have a Content-Length header, and should normally, read the response body until EOF. However, I found that in particular cases, the http module doesn't read until EOF, and simply close the connection after having received the headers.

Here is the code I'm using to proxy the request:

```javascript
var http = require('http');
var sys = require('util');
var url = require('url');

var mjpegUrl = "http://admin:admin@192.168.1.31/cgi/mjpg/mjpg.cgi";

var server = http.createServer(function(req, res) {
var options = url.parse(mjpegUrl);

var mjpegRequest = http.request(options, function(mjpegResponse) {
console.log("...response received");
res.writeHead(200, {
'Content-Type': mjpegResponse.headers['content-type'],
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Expires': 'Mon, 01 Jul 1980 00:00:00 GMT',
'Pragma': 'no-cache'
});

console.log("...response");
mjpegResponse.setEncoding(null);
mjpegResponse.on('data', function(chunk) {
//console.log("...data");
res.write(chunk, 'binary');
});
mjpegResponse.on('end', function () {
console.log("...end");
res.end();
});
mjpegResponse.on('close', function () {
console.log("...close");
});
});
mjpegRequest.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
mjpegRequest.end();
});
server.listen(5080);
```
The problem occurs when proxying a MJPEG stream from a TRENDNet TV-IP422 camera. It works fine with an Axis camera.

I have wireshark traces of the issue. The only difference I see from a working response versus this one, is with the way TCP segments are sent back (on TV-IP422, the first segment is for the HTTP response status code, the next is for HTTP response headers, the next is for the MJPEG stream, etc; on Axis, the first segment is for HTTP response status code + headers + first part of MJPEG stream).

I can expose the camera on the Internet, please contact me.