0

i was in the process of answering this question

Retrieving the http.ServerResponse body

im trying to capture the raw string of an https request and response, for http it works fine

    var http = require('http');
    var util = require('util');
    
    var req,res;
    
    var server = http.createServer(onrequest);
    
    server.listen(81);

    server.on('connection',socket=>{
    
          console.log('***   connection   ***');
          
          req   = '';
          res   = '';
          
          socket.on('data',data=>{

                req  += data.toString();

          });
          
          var write   =   socket.write

          socket.write=function(data,encoding,callback){

                res  += data.toString();
  
                write.apply(socket,arguments);

          }//write
    
    });
  
    function onrequest(request,response){

          console.log('*** request : '+request.url+' ***');
          
          response.on('finish',()=>{
          
                console.log('---   request   ---');
                console.log('['+req+']');
                console.log();
                console.log('---   response   ---');
                console.log('['+res+']');
                
          });
          
          request.on('data',data=>{});

          request.on('end',()=>response.end('Hi.  This is my http response.'));

    }//onresquest

simplifying things using a single curl request

curl http://localhost:81/

gives the required data, congrats i thought onto my project and .. nope

when using https with the following code

    var key; // = .....
    var cert; // = ....
    
    var server = https.createServer({key,cert},onrequest);
    

and again using curl

curl -k https://localhost:81/

no data is logged, the problem is this

on the connection event the socket passed is of type "socket" presumably a net.socket

the socket that is passed to the onrequest function is of type "tlssocket"

so i thought maybe i could use the underlying "duplex" which they do both share, however when i try to add the on data event to the duplex, with either

//on connection
var duplex = socket.__proto__;
duplex.on('data',data=>{

or

duplex.on.call(duplex,'data',data=>

it fails at Node.js JavaScript runtime

does anybody know how to get further than this?

related questions :

Get raw HTTP response in NodeJS

How can I get the raw HTTP message body using the request library in Node.js?

1 Answers1

0

okey dokey

the solution is to use the "secureConnection" event, like so :

var https = require('https');

var req,res;

var key;    // = ...
var cert;   // = ...

var server = https.createServer({key,cert},onrequest);

server.listen(81);

server.on('secureConnection',socket=>{

      console.log('***   connection   ***');
      
      req   = '';
      res   = '';
      
      socket.on('data',data=>{

            req  += data.toString();

      });
      
      var write   =   socket.write

      socket.write=function(data,encoding,callback){

            res  += data.toString();

            write.apply(socket,arguments);

      }//write

});


function onrequest(request,response){

      console.log('*** request : '+request.url+' ***');
      
      response.on('finish',()=>{
      
            console.log('---   request   ---');
            console.log('['+req+']');
            console.log();
            console.log('---   response   ---');
            console.log('['+res+']');
            
      });
      
      request.on('data',data=>{});

      request.on('end',()=>response.end('Hi.  This is my http response.'));

}//onresquest

this can be seen implemented

github: simple-server - no dependencies