<commit_before>var spdy = require('spdy'),
    http = require('http'),
    path = require('path'),
    util = require('util'),
    net = require('net'),
    url = require('url'),
    fs = require('fs');

var SPDYProxy = function(options) {
  var self = this;

  this.setAuthHandler = function(handler) {
    self._authHandler = handler;
    console.log('AuthHandler'.green, handler.friendly_name.yellow,
                'will be used.'.green);
  }

  this.setLogHandler = function(handler) {
    self._logHandler = handler;
    console.log('Requests will be logged into file'.green, handler._filename.yellow);
  }

  function logHeaders(headers) {
    for (var i in headers)
      console.log(' > '.grey + i.cyan + ': ' + headers[i]);
    console.log();
  }
  
  function logRequest(req) {
    console.log(req.method.green + ' ' + req.url.yellow);
    logHeaders(req.headers);
  }

  function synReply(socket, code, reason, headers, cb) {
    try {
      if (socket._handle instanceof spdy.handle) {
        var handle = socket._handle;
        handle._stream.respond(code, headers, function (err) {
          cb.call();
        });
          
      
      /*
      // SPDY socket
      if(socket._lock){
        socket._lock(function() {
          var socket = this;
          stream.respond(code, headers, function (err) {});
          this._spdyState.framer.replyFrame(
            this._spdyState.id, code, reason, headers,
            function (err, frame) {
              socket.connection.write(frame);
              socket._unlock();
              cb.call();
            }
          );
        });
        */

      // Chrome used raw SSL instead of SPDY when issuing CONNECT for
      // WebSockets. Hence, to support WS we must fallback to regular
      // HTTPS tunelling: https://github.com/igrigorik/node-spdyproxy/issues/26
      } else {
        console.log("Fallback for WebSockets");
        var statusLine = 'HTTP/1.1 ' + code + ' ' + reason + '\r\n';
        var headerLines = '';
        for(key in headers){
            headerLines += key + ': ' + headers[key] + '\r\n';
        }
        socket.write(statusLine + headerLines + '\r\n', 'UTF-8', cb);
      }
    } catch(error) {
      console.error("Error: ".red, error);
      console.error(error.stack);
      cb.call();
    }
  }

  function handlePlain(req, res) {
    var path = req.headers.path || url.parse(req.url).path;
    var requestOptions = {
      host: req.headers.host.split(':')[0],
      port: req.headers.host.split(':')[1] || 80,
      path: path,
      method: req.method,
      headers: req.headers
    };
    if (options.localAddress) {
      requestOptions.localAddress = options.localAddress;
    }

    var rreq = http.request(requestOptions, function(rres) {
      rres.headers['proxy-agent'] = 'SPDY Proxy ' + options.version;

      if (options.verbose) {
        console.log("HTTP/" + rres.httpVersion + " " + rres.statusCode);
        logHeaders(rres.headers);
      }
      
      // write out headers to handle redirects
      res.writeHead(rres.statusCode, '', rres.headers);
      rres.pipe(res);

      // Res could not write, but it could close connection
      res.pipe(rres);
    });

    rreq.on('error', function(e) {
      console.log("Client error: " + e.message);
      res.writeHead(502, 'Proxy fetch failed');
      res.end();
    });

    req.pipe(rreq);

    // Just in case if socket will be shutdown before http.request will connect
    // to the server.
    res.on('close', function() {
      rreq.abort();
    });
  }

  function handleSecure(req, socket) {
    var requestOptions = {
      host: req.url.split(':')[0],
      port: req.url.split(':')[1] || 443,
    };
    if (options.localAddress) {<commit_msg>Remove invalid headers from response.<commit_after>var spdy = require('spdy'),
    http = require('http'),
    path = require('path'),
    util = require('util'),
    net = require('net'),
    url = require('url'),
    fs = require('fs');

var SPDYProxy = function(options) {
  var self = this;

  this.setAuthHandler = function(handler) {
    self._authHandler = handler;
    console.log('AuthHandler'.green, handler.friendly_name.yellow,
                'will be used.'.green);
  }

  this.setLogHandler = function(handler) {
    self._logHandler = handler;
    console.log('Requests will be logged into file'.green, handler._filename.yellow);
  }

  function logHeaders(headers) {
    for (var i in headers)
      console.log(' > '.grey + i.cyan + ': ' + headers[i]);
    console.log();
  }
  
  function logRequest(req) {
    console.log(req.method.green + ' ' + req.url.yellow);
    logHeaders(req.headers);
  }

  function synReply(socket, code, reason, headers, cb) {
    try {
      if (socket._handle instanceof spdy.handle) {
        var handle = socket._handle;
        handle._stream.respond(code, headers, function (err) {
          cb.call();
        });
          
      
      /*
      // SPDY socket
      if(socket._lock){
        socket._lock(function() {
          var socket = this;
          stream.respond(code, headers, function (err) {});
          this._spdyState.framer.replyFrame(
            this._spdyState.id, code, reason, headers,
            function (err, frame) {
              socket.connection.write(frame);
              socket._unlock();
              cb.call();
            }
          );
        });
        */

      // Chrome used raw SSL instead of SPDY when issuing CONNECT for
      // WebSockets. Hence, to support WS we must fallback to regular
      // HTTPS tunelling: https://github.com/igrigorik/node-spdyproxy/issues/26
      } else {
        console.log("Fallback for WebSockets");
        var statusLine = 'HTTP/1.1 ' + code + ' ' + reason + '\r\n';
        var headerLines = '';
        for(key in headers){
            headerLines += key + ': ' + headers[key] + '\r\n';
        }
        socket.write(statusLine + headerLines + '\r\n', 'UTF-8', cb);
      }
    } catch(error) {
      console.error("Error: ".red, error);
      console.error(error.stack);
      cb.call();
    }
  }

  function handlePlain(req, res) {
    var path = req.headers.path || url.parse(req.url).path;
    var requestOptions = {
      hostname: req.headers.host.split(':')[0],
      port: req.headers.host.split(':')[1] || 80,
      path: path,
      method: req.method,
      headers: req.headers
    };
    if (options.localAddress) {
      requestOptions.localAddress = options.localAddress;
    }

    var rreq = http.request(requestOptions, function(rres) {
      rres.headers['proxy-agent'] = 'SPDY Proxy ' + options.version;

      if (options.verbose) {
        console.log("HTTP/" + rres.httpVersion + " " + rres.statusCode);
        logHeaders(rres.headers);
      }

      // remove invalid headers
      delete rres.headers["connection"];
      delete rres.headers["keep-alive"];
      delete rres.headers["proxy-connection"];
      delete rres.headers["transfer-encoding"];
      
      // write out headers to handle redirects
      res.writeHead(rres.statusCode, rres.statusMessage || '', rres.headers);
      rres.pipe(res);

      // Res could not write, but it could close connection
      res.pipe(rres);
    });

    rreq.on('error', function(e) {
      console.log("Client error: " + e.message);
      res.writeHead(502, 'Proxy fetch failed');
      res.end();
    });

    req.pipe(rreq);

    // Just in case if socket will be shutdown before http.request will connect
    // to the server.
    res.on('close', function() {
      rreq.abort();
    });
  }

  function handleSecure(req, socket) {
    var requestOptions = {
      host: req.url.split(':')[0],
      port: req.url.split(':')[1] || 443,
    };
    if (options.localAddress) {
{'fraction_non_alphanumeric': 0.14886187273667872, 'fraction_numerical': 0.004397309881013968, 'mean_word_length': 2.2180607573866, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7434917', 'n_tokens_mistral': 2414, 'n_tokens_neox': 2350, 'n_words': 518}