Conversation
|
Yeah, it will only work if the TCP connection is closed after the end of the HTTP request. If the socket is reused my example is not intended to work. The reason why I wasn't just using the first One thing I'd like to change in your PR is that you're using A more complete example though, that will work in all cases, would probably be if we bundled up chunks until we saw a double line-break ( net.createServer(function (c) {
var data = ''
c.on('data', onData)
function onData (chunk) {
data += chunk.toString()
// A double line-break indicates that we've consumed all the headers
if (/\r\n\r\n/.test(data)) {
c.removeListener('data', onData)
// parse incoming data as an HTTP request and extra HTTP headers
console.log(httpHeaders(data))
}
}
}).listen(8080) |
|
Below code will make the example only accept ONE TIME request? |
|
Yes. What is your use-case? Do you want to get headers for multiple HTTP requests on the same TCP connection? |
|
yah, that's why I need use Socket instead of HTTP, most of time http is one time connection and cannot accept another request, but in my case I need the socket keep listening and more request will come later. |
|
Ah I see. Node.js can actually be configured to use keep-alive connections. You need to use a custom agent. So you might not need this module. But if you want to use this module, you need to write your own state logic so you know if you're inside the HTTP body or not. If you are inside the body you shouldn't try to parse it as headers. Only when you're outside of the HTTP body should you try to parse the chunks as headers 😃 |
The net socket usage is wrong, see: Event: 'end'
Socket
endevent is for FIN package, that inform client to end connection, not for data receive end.