<commit_before>require 'ethon'

module UrlChecker
  module Checks
    module Reachability
      class << self
        def check(url)
          status = perform_request url
          status == :ok
        end

        protected

        def easy
          @easy ||= Ethon::Easy.new
        end

        def perform_request(url)
          easy.http_request url, :head, failonerror: true, followlocation: true, maxredirs: 5
          easy.perform
        end
      end
    end
  end
end<commit_msg>Check S3 public urls reachability by fetching a byte of data as S3 does not allow HEAD requests to public urls<commit_after>require 'ethon'

module UrlChecker
  module Checks
    module Reachability
      class << self
        def check(url)
          status = perform_request url
          status == :ok
        end

        protected

        def easy
          @easy ||= Ethon::Easy.new
        end

        def perform_request(url)
          if s3_url?(url)
            easy.http_request url, :get, failonerror: true, followlocation: true, maxredirs: 5, range: '0-1'
          else
            easy.http_request url, :head, failonerror: true, followlocation: true, maxredirs: 5
          end
          easy.perform
        end

        def s3_url?(url)
          !!(URI.parse(url).host =~ /s3\.amazonaws\.com$/)
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07481481481481482, 'fraction_numerical': 0.007407407407407408, 'mean_word_length': 1.7855670103092784, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4539465', 'n_tokens_mistral': 405, 'n_tokens_neox': 392, 'n_words': 121}