0

I am trying to get a malformed HTTP response using node. Since the response is malformed, I cannot use request normally (it would give the HPE_INVALID_CONSTANT error)

From this answer https://stackoverflow.com/a/23543522/1748450 I can get the raw HTTP response using the net module like so:

var net = require('net');

var host = '192.168.1.1',
    port = 80,
    socket = net.connect(port, host, function() {

    var request = "GET / HTTP/1.1\r\nHost: " + host + "\r\n\r\n",
        rawResponse = "";

    // send http request:
    socket.end(request);

    // assume utf-8 encoding:
    socket.setEncoding('utf-8');

    // collect raw http message:
    socket.on('data', function(chunk) {
        rawResponse += chunk;
    });
    socket.on('end', function(){
        console.log(rawResponse);
    });
});

However, this only works with getting the response from the host's root page (192.168.1.1). The page I'm trying to get the response from is actually 192.168.1.1/admin/landingpage.fwd.

If I try to edit host to that URL then I get this error:

events.js:187
      throw er; // Unhandled 'error' event
      ^

Error: getaddrinfo ENOTFOUND 192.168.1.1/admin/landingpage.fwd
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26)
Emitted 'error' event on Socket instance at:
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: '192.168.1.1/admin/landingpage.fwd'
}

Is this possible to fetch this using the net module in the above example?

If not possible, what other way can I use to get the raw HTTP response from that URL?

Chin
  • 16,446
  • 27
  • 96
  • 148
  • Does this answer your question? [Expressjs raw body](https://stackoverflow.com/questions/9920208/expressjs-raw-body) – Neil VanLandingham Dec 24 '19 at 21:16
  • no, I'm not using express, and this is a one-off script, not something about request handling on the server side – Chin Dec 24 '19 at 21:47
  • Why not just put the path in your http request as in: `var request = "GET /admin/landingpage.fwd HTTP/1.1\r\nHost: " + host + "\r\n\r\n",`? That's where the path goes in an http request. – jfriend00 Dec 24 '19 at 22:39
  • @jfriend00 that solved it, thanks so much! You can make an answer and I'll accept it. – Chin Dec 25 '19 at 19:23

1 Answers1

1

You can just put the path in your http request as in:

var request = "GET /admin/landingpage.fwd HTTP/1.1\r\nHost: " + host + "\r\n\r\n",? 

That's where the path goes in an http request.

jfriend00
  • 580,699
  • 78
  • 809
  • 825