<commit_before>module Reel
  # A connection to the HTTP server
  class Connection
    attr_reader :request
    
    # Attempt to read this much data
    BUFFER_SIZE = 4096
    
    def initialize(socket)
      @socket = socket
      @parser = Request::Parser.new
      @request = nil
    end
    
    def read_request
      return if @request
      
      until @parser.headers
        @parser << @socket.readpartial(BUFFER_SIZE)
      end
      
      @request = Request.new(@parser.http_method, @parser.url, @parser.http_version, @parser.headers)
    end
    
    def respond(response, body = nil)
      case response
      when Symbol
        response = Response.new(response, {'Connection' => 'close'}, body)
      when Response
      else raise TypeError, "invalid response: #{response.inspect}"
      end
      
      response.render(@socket)
    rescue Errno::ECONNRESET, Errno::EPIPE
      # The client disconnected early
    ensure
      # FIXME: Keep-Alive support
      @socket.close 
    end
  end
end<commit_msg>Store keepalive state<commit_after>module Reel
  # A connection to the HTTP server
  class Connection
    attr_reader :request
    
    # Attempt to read this much data
    BUFFER_SIZE = 4096
    
    def initialize(socket)
      @socket = socket
      @parser = Request::Parser.new
      @request = nil
      @keepalive = true
    end
    
    def read_request
      return if @request
      
      until @parser.headers
        @parser << @socket.readpartial(BUFFER_SIZE)
      end
      
      headers = {}
      @parser.headers.each do |header, value|
        headers[Http.canonicalize_header(header)] = value
      end
      @keepalive = false if headers['Connection'] == 'close'
      @request = Request.new(@parser.http_method, @parser.url, @parser.http_version, headers)
    end
    
    def respond(response, body = nil)
      case response
      when Symbol
        response = Response.new(response, {'Connection' => 'close'}, body)
      when Response
      else raise TypeError, "invalid response: #{response.inspect}"
      end
      
      response.render(@socket)
    rescue Errno::ECONNRESET, Errno::EPIPE
      # The client disconnected early
    ensure
      # FIXME: Keep-Alive support
      @socket.close 
    end
  end
end
{'fraction_non_alphanumeric': 0.08597883597883597, 'fraction_numerical': 0.003527336860670194, 'mean_word_length': 2.29796511627907, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '5734458', 'n_tokens_mistral': 653, 'n_tokens_neox': 641, 'n_words': 182}