<commit_before>require "net/http"

module Selenium
  module WebDriver
    module Remote
      class DefaultHttpClient
        CONTENT_TYPE    = "application/json"
        DEFAULT_HEADERS = { "Accept" => CONTENT_TYPE }

        class RetryException < StandardError; end

        def initialize(url)
          @server_url = url
        end

        def call(verb, url, *args)
          response = nil
          url      = @server_url.merge(url) unless url.kind_of?(URI)
          headers  = DEFAULT_HEADERS.dup

          if args.any?
            headers.merge!("Content-Type" => "#{CONTENT_TYPE}; charset=utf-8")
            payload = args.to_json
            puts "   >>> #{payload}" if $DEBUG
          end

          begin
            request = Net::HTTP.const_get(verb.to_s.capitalize).new(url.path, headers)

            # TODO: should be checking against a maximum redirect count
            http.request(request, payload) do |res|
              if res.kind_of? Net::HTTPRedirection
                verb, payload = :get, nil
                url           = URI.parse(res["Location"])
                raise RetryException
              else
                response = create_response(res)
              end
            end

            response
          rescue RetryException
            retry
          end
        end

        private

        def http
          # ignoring SSL for now
          @http ||= Net::HTTP.new @server_url.host, @server_url.port
        end

        def create_response(res)
          puts "<- #{res.body}\n" if $DEBUG
          if res.content_type == CONTENT_TYPE
            Response.new do |r|
              r.code         = res.code.to_i
              r.payload      = JSON.parse(res.body)
            end
          elsif res.code == '204'
            Response.new { |r| r.code = res.code.to_i }
          else
            raise "Unexpected content type: #{res.content_type.inspect} (#{res.code})\n#{res.body}"
          end
        end
<commit_msg>JariBakken: Strip trailing \0s from the remote server response<commit_after>require "net/http"

module Selenium
  module WebDriver
    module Remote
      class DefaultHttpClient
        CONTENT_TYPE    = "application/json"
        DEFAULT_HEADERS = { "Accept" => CONTENT_TYPE }

        class RetryException < StandardError; end

        def initialize(url)
          @server_url = url
        end

        def call(verb, url, *args)
          response = nil
          url      = @server_url.merge(url) unless url.kind_of?(URI)
          headers  = DEFAULT_HEADERS.dup

          if args.any?
            headers.merge!("Content-Type" => "#{CONTENT_TYPE}; charset=utf-8")
            payload = args.to_json
            puts "   >>> #{payload}" if $DEBUG
          end

          begin
            request = Net::HTTP.const_get(verb.to_s.capitalize).new(url.path, headers)

            # TODO: should be checking against a maximum redirect count
            http.request(request, payload) do |res|
              if res.kind_of? Net::HTTPRedirection
                verb, payload = :get, nil
                url           = URI.parse(res["Location"])
                raise RetryException
              else
                response = create_response(res)
              end
            end

            response
          rescue RetryException
            retry
          end
        end

        private

        def http
          # ignoring SSL for now
          @http ||= Net::HTTP.new @server_url.host, @server_url.port
        end

        def create_response(res)
          puts "<- #{res.body}\n" if $DEBUG
          if res.content_type == CONTENT_TYPE
            Response.new do |r|
              r.code         = res.code.to_i
              r.payload      = JSON.parse(res.body.strip)
            end
          elsif res.code == '204'
            Response.new { |r| r.code = res.code.to_i }
          else
            raise "Unexpected content type: #{res.content_type.inspect} (#{res.code})\n#{res.body}"
          end
        end

{'fraction_non_alphanumeric': 0.09212813508815496, 'fraction_numerical': 0.0022349143282840824, 'mean_word_length': 1.4818237831176833, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '5327366', 'n_tokens_mistral': 1125, 'n_tokens_neox': 1093, 'n_words': 272}