bin/instrument.js
function handler(req, res) {
    // Support both the ?url= mode and the X-WTF-URL header.
    var targetUrl;
    if (req.headers['x-wtf-url']) {
      targetUrl = req.headers['x-wtf-url'];
    } else {
      var parsedUrl = url.parse(req.url, true);
      var query = parsedUrl.query;
      targetUrl = query.url;
    }
    if (!targetUrl || targetUrl.indexOf('http') != 0) {
      res.writeHead(404, ['Content-Type', 'text/plain']);
      res.end();
      return;
    }

    res.on('error', function(e) {
      console.log('ERROR (source): ' + e);
    });

    var targetModule = targetUrl.indexOf('https') == 0 ? https : http;
    targetModule.get(targetUrl, function(originalRes) {
      // Eat errors, otherwise the app will die.
      originalRes.on('error', function(e) {
        console.log('ERROR (target): ' + e);
      });

      // Pull out content type to see if we are interested in it.
      var supportedContentType = false;
      var contentType = originalRes.headers['content-type'];
      if (contentType && contentType.indexOf(';') != 0) {
        contentType = contentType.substr(0, contentType.indexOf(';'));
      }
      switch (contentType) {
        case 'text/javascript':
        case 'application/javascript':
          supportedContentType = true;
          break;
      }

      // Also parse the URL - some servers don't return content type for some
      // reason.
      var supportedPath = false;
      var contentUrl = url.parse(targetUrl);
      if (path.extname(contentUrl.pathname) == '.js') {
        supportedPath = true;
      }

      if (supportedContentType || supportedPath) {
        var headers = [];
        for (var key in originalRes.headers) {
          if (key == 'content-length') {
            continue;
          }
          headers.push([key, originalRes.headers[key]]);
        }
        res.writeHead(originalRes.statusCode, headers);
        injectStream(targetUrl, originalRes, res);
      } else {
        console.log('Pass-through: ' + targetUrl, contentType);
        res.writeHead(originalRes.statusCode, originalRes.headers);
        originalRes.pipe(res);
      }
    }).on('error', function(e) {
      console.log(e);
      res.writeHead(404, ['Content-Type', 'text/plain']);
      res.end(e.message);
    });
  }
function handler(req, res) {
    // Support both the ?url= mode and the X-WTF-URL header.
    var targetUrl;
    if (req.headers['x-wtf-url']) {
      targetUrl = req.headers['x-wtf-url'];
    } else {
      var parsedUrl = url.parse(req.url, true);
      var query = parsedUrl.query;
      targetUrl = query.url;
    }
    if (!targetUrl || targetUrl.indexOf('http') != 0) {
      res.writeHead(404, ['Content-Type', 'text/plain']);
      res.end();
      return;
    }

    res.on('error', function(e) {
      console.log('ERROR (source): ' + e);
    });

    var targetModule = targetUrl.indexOf('https') == 0 ? https : http;
    targetModule.get(targetUrl, function(originalRes) {
      // Eat errors, otherwise the app will die.
      originalRes.on('error', function(e) {
        console.log('ERROR (target): ' + e);
      });

      // Pull out content type to see if we are interested in it.
      var supportedContentType = false;
      var contentType = originalRes.headers['content-type'];
      if (contentType && contentType.indexOf(';') != 0) {
        contentType = contentType.substr(0, contentType.indexOf(';'));
      }
      switch (contentType) {
        case 'text/javascript':
        case 'application/javascript':
          supportedContentType = true;
          break;
      }

      // Also parse the URL - some servers don't return content type for some
      // reason.
      var supportedPath = false;
      var contentUrl = url.parse(targetUrl);
      if (path.extname(contentUrl.pathname) == '.js') {
        supportedPath = true;
      }

      if (supportedContentType || supportedPath) {
        var headers = [];
        for (var key in originalRes.headers) {
          if (key == 'content-length') {
            continue;
          }
          headers.push([key, originalRes.headers[key]]);
        }
        res.writeHead(originalRes.statusCode, headers);
        injectStream(targetUrl, originalRes, res);
      } else {
        console.log('Pass-through: ' + targetUrl, contentType);
        res.writeHead(originalRes.statusCode, originalRes.headers);
        originalRes.pipe(res);
      }
    }).on('error', function(e) {
      console.log(e);
      res.writeHead(404, ['Content-Type', 'text/plain']);
      res.end(e.message);
    });
  }
function handler ( req , res ) { // Support both the ?url= mode and the X-WTF-URL header. var targetUrl ; if ( req . headers [ 'x-wtf-url' ] ) { targetUrl = req . headers [ 'x-wtf-url' ] ; } else { var parsedUrl = url . parse ( req . url , true ) ; var query = parsedUrl . query ; targetUrl = query . url ; } if ( ! targetUrl || targetUrl . indexOf ( 'http' ) != 0 ) { res . writeHead ( 404 , [ 'Content-Type' , 'text/plain' ] ) ; res . end ( ) ; return ; } res . on ( 'error' , function ( e ) { console . log ( 'ERROR (source): ' + e ) ; } ) ; var targetModule = targetUrl . indexOf ( 'https' ) == 0 ? https : http ; targetModule . get ( targetUrl , function ( originalRes ) { // Eat errors, otherwise the app will die. originalRes . on ( 'error' , function ( e ) { console . log ( 'ERROR (target): ' + e ) ; } ) ; // Pull out content type to see if we are interested in it. var supportedContentType = false ; var contentType = originalRes . headers [ 'content-type' ] ; if ( contentType && contentType . indexOf ( ';' ) != 0 ) { contentType = contentType . substr ( 0 , contentType . indexOf ( ';' ) ) ; } switch ( contentType ) { case 'text/javascript' : case 'application/javascript' : supportedContentType = true ; break ; } // Also parse the URL - some servers don't return content type for some // reason. var supportedPath = false ; var contentUrl = url . parse ( targetUrl ) ; if ( path . extname ( contentUrl . pathname ) == '.js' ) { supportedPath = true ; } if ( supportedContentType || supportedPath ) { var headers = [ ] ; for ( var key in originalRes . headers ) { if ( key == 'content-length' ) { continue ; } headers . push ( [ key , originalRes . headers [ key ] ] ) ; } res . writeHead ( originalRes . statusCode , headers ) ; injectStream ( targetUrl , originalRes , res ) ; } else { console . log ( 'Pass-through: ' + targetUrl , contentType ) ; res . writeHead ( originalRes . statusCode , originalRes . headers ) ; originalRes . pipe ( res ) ; } } ) . on ( 'error' , function ( e ) { console . log ( e ) ; res . writeHead ( 404 , [ 'Content-Type' , 'text/plain' ] ) ; res . end ( e . message ) ; } ) ; }
Handles both HTTP and HTTPS requests.
Handles both HTTP and HTTPS requests .