<commit_before>require 'socket'
require 'timeout'
require 'thread'

class Capybara::Driver::Webkit
  class Connection
    attr_reader :port

    def initialize(options = {})
      @socket_class = options[:socket_class] || TCPSocket
      @stdout       = options.has_key?(:stdout) ?
                        options[:stdout] :
                        $stdout
      start_server
      connect
    end

    def puts(string)
      @socket.puts string
    end

    def print(string)
      @socket.print string
    end

    def gets
      @socket.gets
    end

    def read(length)
      @socket.read(length)
    end

    private

    def start_server
      pipe = fork_server
      @port = discover_port(pipe)
      @stdout_thread = Thread.new do
        Thread.current.abort_on_exception = true
        forward_stdout(pipe)
      end
    end

    def fork_server
      server_path = File.expand_path("../../../../../bin/webkit_server", __FILE__)
      pipe, @pid = server_pipe_and_pid(server_path)
      register_shutdown_hook
      pipe
    end

    def kill_process(pid)
      if RUBY_PLATFORM =~ /mingw32/
        Process.kill(9, pid)
      else
        Process.kill("INT", pid)
      end
    end

    def register_shutdown_hook
      @owner_pid = Process.pid
      at_exit do
        if Process.pid == @owner_pid
          kill_process(@pid)
        end
      end
    end

    def server_pipe_and_pid(server_path)
      cmdline = [server_path]
      pipe = IO.popen(cmdline.join(" "))
      [pipe, pipe.pid]
    end

    def discover_port(read_pipe)
      return unless IO.select([read_pipe], nil, nil, 10)
      ((read_pipe.first || '').match(/listening on port: (\d+)/) || [])[1].to_i
    end

    def forward_stdout(pipe)
      while pipe_readable?(pipe)
        line = pipe.readline
        if @stdout
          @stdout.write(line)
          @stdout.flush
        end
      end
    rescue EOFError
    end

    def connect<commit_msg>Reverted changes and bumped timeout<commit_after>require 'socket'
require 'timeout'
require 'thread'

class Capybara::Driver::Webkit
  class Connection
    WEBKIT_SERVER_START_TIMEOUT = 15

    attr_reader :port

    def initialize(options = {})
      @socket_class = options[:socket_class] || TCPSocket
      @stdout       = options.has_key?(:stdout) ?
                        options[:stdout] :
                        $stdout
      start_server
      connect
    end

    def puts(string)
      @socket.puts string
    end

    def print(string)
      @socket.print string
    end

    def gets
      @socket.gets
    end

    def read(length)
      @socket.read(length)
    end

    private

    def start_server
      pipe = fork_server
      @port = discover_port(pipe)
      @stdout_thread = Thread.new do
        Thread.current.abort_on_exception = true
        forward_stdout(pipe)
      end
    end

    def fork_server
      server_path = File.expand_path("../../../../../bin/webkit_server", __FILE__)
      pipe, @pid = server_pipe_and_pid(server_path)
      register_shutdown_hook
      pipe
    end

    def kill_process(pid)
      if RUBY_PLATFORM =~ /mingw32/
        Process.kill(9, pid)
      else
        Process.kill("INT", pid)
      end
    end

    def register_shutdown_hook
      @owner_pid = Process.pid
      at_exit do
        if Process.pid == @owner_pid
          kill_process(@pid)
        end
      end
    end

    def server_pipe_and_pid(server_path)
      cmdline = [server_path]
      pipe = IO.popen(cmdline.join(" "))
      [pipe, pipe.pid]
    end

    def discover_port(read_pipe)
      return unless IO.select([read_pipe], nil, nil, WEBKIT_SERVER_START_TIMEOUT)
      ((read_pipe.first || '').match(/listening on port: (\d+)/) || [])[1].to_i
    end

    def forward_stdout(pipe)
      while pipe_readable?(pipe)
        line = pipe.readline
        if @stdout
          @stdout.write(line)
          @stdout.flush
        end
      end
    rescue EOFError
    end

    def connect
{'fraction_non_alphanumeric': 0.09623642333922708, 'fraction_numerical': 0.0030310684516291994, 'mean_word_length': 1.979683972911964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '4028817', 'n_tokens_mistral': 1256, 'n_tokens_neox': 1220, 'n_words': 273}