Support multi-line headers (lines will be joined with a space)
Support repeating headers
Installation
npm install http-headers --save
Usage
var net = require('net')var httpHeaders = require('http-headers')// create TCP servernet.createServer(function (c) { var buffers = [] c.on('data', buffers.push.bind(buffers)) c.on('end', function () { var data = Buffer.concat(buffers) // parse incoming data as an HTTP request and extra HTTP headers console.log(httpHeaders(data)) })}).listen(8080)
http.ServerReponse support
If given an instance of http.ServerResponse, the reponse headers is
automatically extracted, parsed and returned:
If you've ever needed to log or in another way access the headers sent
to the client on a http.ServerResponse in Node.js, you know it's not
as easy as with the http.IncomingMessage headers (which you just
access via request.headers['content-type']).
Response headers are not directly available on the response object.
Instead all headers are preprocessed as a string on the private
response._header property and needs to be processed in order to be
available as an object.
This module makes the task super simple.
API
The http-headers module exposes a single parser function:
httpHeaders(data[, onlyHeaders])
Arguments:
data - A string, buffer or instance of http.ServerReponse
onlyHeaders - An optional boolean. If true, only the headers
object will be returned. Defaults to false
Request example
If given a request as input:
GET /foo HTTP/1.1Date: Tue, 10 Jun 2014 07:19:27 GMTConnection: keep-aliveTransfer-Encoding: chunkedHello World