Low-level HTTP Parser for Whistle

0.5.0 · abandoned · verified Wed Apr 22

hparser is a low-level HTTP parser specifically designed to be used internally by the `whistle` network debugging and proxy tool. It provides capabilities to parse raw HTTP request and response data, handling the complexities of the HTTP protocol state machine. The package's current stable version is 0.5.0, with its last update approximately two years ago and it targets Node.js versions as old as `0.10.0` (released in 2013). This indicates a lack of active maintenance. Due to its specialized use-case within the `whistle` ecosystem and its abandoned status, it is not recommended for new projects or general-purpose HTTP parsing, especially given the availability of more modern and actively maintained alternatives.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing the `Parser`, feeding it a raw HTTP request buffer, and using its callback events to extract HTTP version, method, URL, headers, and body.

const Parser = require('hparser');
const { StringDecoder } = require('string_decoder');

const rawHttpRequest = Buffer.from(
  'GET /index.html HTTP/1.1\r\n'
  + 'Host: example.com\r\n'
  + 'User-Agent: test-client\r\n'
  + 'Accept: */*\r\n'
  + '\r\n'
);

const parser = new Parser();
const decoder = new StringDecoder('utf8');

let headers = {};
let bodyChunks = [];

parser.onHeadersComplete = function() {
  console.log('HTTP Version:', this.httpMajor + '.' + this.httpMinor);
  console.log('Method:', this.method);
  console.log('URL:', decoder.write(this.url));
  for (let i = 0; i < this.headers.length; i += 2) {
    const key = decoder.write(this.headers[i]);
    const value = decoder.write(this.headers[i + 1]);
    headers[key] = value;
  }
  console.log('Headers:', headers);
};

parser.onBody = function(buffer, start, len) {
  bodyChunks.push(buffer.slice(start, start + len));
};

parser.onMessageComplete = function() {
  console.log('Parsing complete!');
  console.log('Body:', Buffer.concat(bodyChunks).toString());
};

parser.execute(rawHttpRequest);
parser.finish();

view raw JSON →