joyent/node — Issue #2206: http.request(method: HEAD) + server keep-alive = uncatchable "Parse error"
Author: avz
Created: 2011-11-27 14:56:02+00:00
State: open

In the HEAD-request if the server responded ```Connection: keep-alive``` then ```http.request()``` throws a ```Parse error```, but a handler for ```error``` is not called.

server.js

```javascript
var net = require('net')

var server = net.createServer(function (socket) {
var headers = [
'HTTP/1.1 200 OK',
'Connection: keep-alive',
'Content-Length: 1' // required
]

socket.write(headers.join('\r\n') + '\r\n\r\n1')
socket.end()
})

server.listen(8888, '127.0.0.1')
```

client.js

```javascript
var http = require('http')

var options = {
host: 'localhost',
port: 8888,
path: '/',
method: 'HEAD' // GET-requests work properly
};

var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));

res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});

req.on('error', function(e) {
console.log('HANDLED: ' + e.message);
});

req.end();

process.on('uncaughtException', function(err) {
console.log('UNHANDLED: ' + err.stack)
})
```

result:

```
# node client.js
STATUS: 200
HEADERS: {"connection":"keep-alive","content-length":"1"}
UNHANDLED: Error: Parse Error
at Socket.ondata (http.js:1125:24)
at TCP.onread (net.js:334:27)
```

System info

```
# uname -srm
FreeBSD 8.2-STABLE amd64
# node -v
v0.6.3