function requestHandler(filterRules) {
  const filters = Filters(filterRules);

  return (req, res) => {
    const logContext = {
      url: req.url,
      method: req.method,
      headers: req.headers,
      requestId: req.headers['snyk-request-id'] || uuid(),
    };

    logger.debug(logContext, 'received request over HTTP connection');
    filters(req, (error, result) => {
      if (error) {
        const reason = 'Request does not match any accept rule, blocking HTTP request';
        error.reason = reason;
        logContext.error = error;
        logger.warn(logContext, reason);
        // TODO: respect request headers, block according to content-type
        return res
          .status(401)
          .send({ message: error.message, reason, url: req.url });
      }

      req.url = result.url;
      logContext.ioUrl = result.url;

      // check if this is a streaming request for binary data
      if (result.stream) {
        const streamingID = uuid();
        const streamBuffer = new stream.PassThrough();
        logContext.streamingID = streamingID;
        logger.debug(logContext, 'sending stream request over websocket connection');

        streams.set(streamingID, {
          response : res,
          streamBuffer
        });
        streamBuffer.pipe(res);

        res.locals.io.send('request', {
          url: req.url,
          method: req.method,
          body: req.body,
          headers: req.headers,
          streamingID,
        }, () => {
          // Streaming requests should not be handled using the emit function
          // but rather by sending 'chunk' messages
          const msg = 'Broker client does not support streaming requests';
          logger.error(logContext, msg);
          return res.status(501).send({message: msg});
        });

        return;
      }

      logger.debug(logContext, 'sending request over websocket connection');

      // relay the http request over the websocket, handle websocket response
      res.locals.io.send('request', {
        url: req.url,
        method: req.method,
        body: req.body,
        headers: req.headers,
        streamingID : '',
      }, ioResponse => {
        logContext.ioStatus = ioResponse.status;
        logContext.ioHeaders = ioResponse.headers;

        const logMsg = 'sending response back to HTTP connection';
        if (ioResponse.status <= 200) {
          logger.debug(logContext, logMsg);
        } else {
          logger.info(logContext, logMsg);
        }

        const httpResponse = res
          .status(ioResponse.status)
          .set(ioResponse.headers);

        // keep chunked http requests without content-length header
        const isChunked = undefsafe(ioResponse, 'headers.transfer-encoding') === 'chunked';
        if (isChunked) {
          httpResponse.write(ioResponse.body);
          httpResponse.end();
        } else {
          httpResponse.send(ioResponse.body);
        }
      });
    });
  };
}
// 5. Send response over HTTP conn
5. Send response over HTTP conn
5 . Send response over HTTP conn
function requestHandler(filterRules) {
  const filters = Filters(filterRules);
  return (req, res) => {
    const logContext = {
      url: req.url,
      method: req.method,
      headers: req.headers,
      requestId: req.headers['snyk-request-id'] || uuid(),
    };
    logger.debug(logContext, 'received request over HTTP connection');
    filters(req, (error, result) => {
      if (error) {
        const reason = 'Request does not match any accept rule, blocking HTTP request';
        error.reason = reason;
        logContext.error = error;
        logger.warn(logContext, reason);
        return res
          .status(401)
          .send({ message: error.message, reason, url: req.url });
      }
      req.url = result.url;
      logContext.ioUrl = result.url;
      if (result.stream) {
        const streamingID = uuid();
        const streamBuffer = new stream.PassThrough();
        logContext.streamingID = streamingID;
        logger.debug(logContext, 'sending stream request over websocket connection');
        streams.set(streamingID, {
          response : res,
          streamBuffer
        });
        streamBuffer.pipe(res);
        res.locals.io.send('request', {
          url: req.url,
          method: req.method,
          body: req.body,
          headers: req.headers,
          streamingID,
        }, () => {
          const msg = 'Broker client does not support streaming requests';
          logger.error(logContext, msg);
          return res.status(501).send({message: msg});
        });
        return;
      }
      logger.debug(logContext, 'sending request over websocket connection');
      res.locals.io.send('request', {
        url: req.url,
        method: req.method,
        body: req.body,
        headers: req.headers,
        streamingID : '',
      }, ioResponse => {
        logContext.ioStatus = ioResponse.status;
        logContext.ioHeaders = ioResponse.headers;
        const logMsg = 'sending response back to HTTP connection';
        if (ioResponse.status <= 200) {
          logger.debug(logContext, logMsg);
        } else {
          logger.info(logContext, logMsg);
        }
        const httpResponse = res
          .status(ioResponse.status)
          .set(ioResponse.headers);
        const isChunked = undefsafe(ioResponse, 'headers.transfer-encoding') === 'chunked';
        if (isChunked) {
          httpResponse.write(ioResponse.body);
          httpResponse.end();
        } else {
          httpResponse.send(ioResponse.body);
        }
      });
    });
  };
}
function requestHandler ( filterRules ) { const filters = Filters ( filterRules ) ; return ( req , res ) => { const logContext = { url : req . url , method : req . method , headers : req . headers , requestId : req . headers [ 'snyk-request-id' ] || uuid ( ) , } ; logger . debug ( logContext , 'received request over HTTP connection' ) ; filters ( req , ( error , result ) => { if ( error ) { const reason = 'Request does not match any accept rule, blocking HTTP request' ; error . reason = reason ; logContext . error = error ; logger . warn ( logContext , reason ) ; return res . status ( 401 ) . send ( { message : error . message , reason , url : req . url } ) ; } req . url = result . url ; logContext . ioUrl = result . url ; if ( result . stream ) { const streamingID = uuid ( ) ; const streamBuffer = new stream . PassThrough ( ) ; logContext . streamingID = streamingID ; logger . debug ( logContext , 'sending stream request over websocket connection' ) ; streams . set ( streamingID , { response : res , streamBuffer } ) ; streamBuffer . pipe ( res ) ; res . locals . io . send ( 'request' , { url : req . url , method : req . method , body : req . body , headers : req . headers , streamingID , } , ( ) => { const msg = 'Broker client does not support streaming requests' ; logger . error ( logContext , msg ) ; return res . status ( 501 ) . send ( { message : msg } ) ; } ) ; return ; } logger . debug ( logContext , 'sending request over websocket connection' ) ; res . locals . io . send ( 'request' , { url : req . url , method : req . method , body : req . body , headers : req . headers , streamingID : '' , } , ioResponse => { logContext . ioStatus = ioResponse . status ; logContext . ioHeaders = ioResponse . headers ; const logMsg = 'sending response back to HTTP connection' ; if ( ioResponse . status <= 200 ) { logger . debug ( logContext , logMsg ) ; } else { logger . info ( logContext , logMsg ) ; } const httpResponse = res . status ( ioResponse . status ) . set ( ioResponse . headers ) ; const isChunked = undefsafe ( ioResponse , 'headers.transfer-encoding' ) === 'chunked' ; if ( isChunked ) { httpResponse . write ( ioResponse . body ) ; httpResponse . end ( ) ; } else { httpResponse . send ( ioResponse . body ) ; } } ) ; } ) ; } ; }
5.
5 .
// TODO: respect request headers, block according to content-type // check if this is a streaming request for binary data // Streaming requests should not be handled using the emit function // but rather by sending 'chunk' messages // relay the http request over the websocket, handle websocket response // keep chunked http requests without content-length header