node.js create object from raw http request string node.js create object from raw http request string node.js node.js

node.js create object from raw http request string


Apparently, the http_parser module is a low-level callback-based parser. It will send whatever parts of the string it can parse to you via those callbacks, and it is up to you to make an IncomingMessage or whatever else you need from them.

I believe something like that might be what you are looking for:

var HTTPParser = process.binding('http_parser').HTTPParser;function parseMessage(request) {    var _message = {};    var _parser = new HTTPParser(HTTPParser.REQUEST);    _parser.onHeadersComplete = function(headers) {         _message = headers;     }    _parser.onBody = function(body, start, len) {        _message.data = body.slice(start, start+len);    }    var _result = _parser.execute(request, 0, request.length);    if (_result != request.length) {         _message.error = _result;     }    else {        _message.error = false;    }    return _message;}var request = Buffer("GET / HTTP/1.1\nHost: localhost\nContent-Length: 2\n\nHi\n\n");result = parseMessage(request);

Note that the particular IncomingMessage class is parameterized with a socket and generally built around the idea of it being used within a server. The code for parsing it is somewhat messy to be reused as-is (to my taste).


Old topic, but I'll say something.

Exporting HTTPParser in own stuff (module or app) is not as simple, 'cause http library uses many internal local functions and constructors. Besides, HTTPParser itself is binded from C library and some helpers.

As I know http.parsers was removed from Node > 4, so the only way is to import all necessary stuff from http library.

For node 0.x there are easy way to import:

var parser = require("http").parsers.alloc();parser.onIncoming = function(response) {  console.log(response);};function parse(data) {    var buffer = new Buffer(data);    parser.execute(buffer, 0, buffer.length);}/** * tests */parse("DELETE / HTTP/1.1\r\n");parse("user-agent: curl\r\n");parse("x-pingback:");parse("12023\r\n");parse("\r\n");//response{ _readableState:    { highWaterMark: 16384,     buffer: [],     length: 0,     pipes: null,     pipesCount: 0,     flowing: false,     ended: false,     endEmitted: false,     reading: false,     calledRead: false,     sync: true,     needReadable: false,     emittedReadable: false,     readableListening: false,     objectMode: false,     defaultEncoding: 'utf8',     ranOut: false,     awaitDrain: 0,     readingMore: false,     decoder: null,     encoding: null },  readable: true,  domain: null,  _events: {},  _maxListeners: 10,  socket: undefined,  connection: undefined,  httpVersion: '1.1',  complete: false,  headers: { 'user-agent': 'curl', 'x-pingback': '12023' },  trailers: {},  _pendings: [],  _pendingIndex: 0,  url: '/',  method: 'DELETE',  statusCode: null,  client: undefined,  _consuming: false,  _dumped: false,  httpVersionMajor: 1,  httpVersionMinor: 1,  upgrade: false }

More info here

Also, thanks to @KT for elegant solution