Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect http vs https from an Http.IncomingMessage in node.js

Tags:

Given an Http.IncomingMessage created by an http(s).Server in response to a request what's the correct / recommended way to detect if the request is http vs https?

Some random ideas that I have no idea are correct

  • Check the port

    // seems wrong - might be using a different port
    const isHTTPS = req.socket.address().port === 443;
    
  • Check if the socket has a cert

    // does no cert mean it wasn't https?
    const isHTTPS = req.socket.getPeerCertificate &&
         request.socket.getPeerCertificate() !== null;
    
  • Check if the socket has the function getPeerCertificate

    // does no func = !https?
    const isHTTPS = req.socket.getPeerCertificate !== undefined;
    
  • Check if the socket is a tls.TLSSocket

    const tls = require('tls');
    
    const isHTTPS = req.socket instanceof tls.TLSSocket;
    
  • other?

like image 960
gman Avatar asked Jun 01 '16 10:06

gman


1 Answers

req.socket will have a property called encrypted set to true for HTTPS connections.

like image 169
robertklep Avatar answered Sep 28 '22 03:09

robertklep