Parsing HTTP Response String / Buffer

I am trying to parse an HTTP response in form Stringor Bufferin Object.

The result will be Object, as an answer to the native httpmodule.

I tried to import my own HTTP parser, but the results were too crude for my use:

var HTTPParser = process.binding('http_parser').HTTPParser;

var parser = new HTTPParser(HTTPParser.RESPONSE);

parser.onHeadersComplete = function(res) {
    console.log('onHeadersComplete');
    console.log(res);
};

parser.execute(data, 0, data.length);

which will return something like this:

onHeadersComplete
{
   headers: 
   [ 'X-Powered-By',
     'Express',
     'Content-Type',
     'text/plain',
     'Content-Length',
     '2',
     'Date',
     'Sat, 19 Apr 2014 20:16:45 GMT',
     'Connection',
     'keep-alive' ],
  statusCode: 200,
  versionMajor: 1,
  versionMinor: 1,
  shouldKeepAlive: true,
  upgrade: false 
}

For my use case, two things are missing:

  • display header names associated with header values
  • body analysis response

    • Does anyone know how to achieve this?

Thank you in advance for your help!

+4
source share
3 answers

You must complete all callbacks.

var HTTPParser = require('http-parser-js').HTTPParser;
var parser = new HTTPParser(HTTPParser.RESPONSE);
parser.onHeadersComplete = function(res) {
    console.log(res.headers);
};
parser.onBody = function(chunk, offset, length) {
    console.log("body", chunk.toString(), offset, length)
}
parser.onMessageComplete = function() {

}
parser.execute(new Buffer('HTTP/1.1 200 OK\r\nContent-Type: text/plain; xya\r\nContent-Length: 11\r\n\r\nhello world'))
+4
source

http.IncomingMessage(socket)._addHeaderLines(headersArr, headersNumber)

var msg=new http.IncomingMessage()

msg._addHeaderLines(['X-Powered-By',
     'Express',
     'Content-Type',
     'text/plain',
     'Content-Length',
     '2',
     'Date',
     'Sat, 19 Apr 2014 20:16:45 GMT',
     'Connection',
     'keep-alive' ], 10);
console.log(msg);

:

IncomingMessage {
  ...
  headers:
   { 'x-powered-by': 'Express',
     'content-type': 'text/plain',
     'content-length': '2',
     date: 'Sat, 19 Apr 2014 20:16:45 GMT',
     connection: 'keep-alive' },
  rawHeaders:
   [ 'X-Powered-By',
     'Express',
     'Content-Type',
     'text/plain',
     'Content-Length',
     '2',
     'Date',
     'Sat, 19 Apr 2014 20:16:45 GMT',
     'Connection',
     'keep-alive' ],
  ...
 }

Node : https://github.com/nodejs/node/blob/master/lib/_http_common.js

0

I am also trying to learn node.js and I just started yesterday. Since this is javascript, here is my suggested solution.

I assume that the object in question here is a res argument.

var headers = {};
for (index = 0; index < res.headers.length; index++) {
    headers[res.headers[index].replace('-', '')] = res.headers[++index];
}

// or this
for (index = 0; index < res.headers.length; index+=2) {
    var headerName = res.headers[index].replace('-', '');
    headers[headerName] = res.headers[index + 1];
}

// Note that we removed the '-' in the name since it an invalid character for property name.

// Access sample.
console.log(headers.ContentType);
console.log(headers['ContentType']);

Hope this helps you.

-1
source

Source: https://habr.com/ru/post/1537236/


All Articles