<commit_before>require 'aws-sdk-core'
require 'stringio'

module S3Proxy
  class App
    def initialize(options={})
      @options = options
    end

    def call(env)
      return Errors.method_not_allowed unless env['REQUEST_METHOD'] == 'GET'
      return Errors.not_found if env['PATH_INFO'].empty?

      _, bucket, key = env['PATH_INFO'].split('/', 3)
      path = {bucket: bucket, key: key}

      head = s3.head_object(path)
      return Errors.not_found unless head

      if env['rack.hijack?']
        hijack env, path, head
      else
        gentle env, path, head
      end

    rescue Aws::S3::Errors::NoSuchKey
      return Errors.not_found
    end

    private<commit_msg>When set proxy and port in .s3cfg, s3cmd uses this proxy as a forward proxy.So get bucket name from HOST header.<commit_after>require 'aws-sdk-core'
require 'stringio'

module S3Proxy
  class App
    def initialize(options={})
      @options = options
    end

    def call(env)
      return Errors.method_not_allowed unless env['REQUEST_METHOD'] == 'GET'
      return Errors.not_found if env['PATH_INFO'].empty?

      # When used as a forward proxy
      if env['HTTP_HOST'] =~ /(.+)\.s3\.amazonaws\.com/
        bucket = $1
        _, key = env['PATH_INFO'].split('/', 2)
      else
        _, bucket, key = env['PATH_INFO'].split('/', 3)
      end
      path = {bucket: bucket, key: key}

      head = s3.head_object(path)
      return Errors.not_found unless head

      if env['rack.hijack?']
        hijack env, path, head
      else
        gentle env, path, head
      end

    rescue Aws::S3::Errors::NoSuchKey
      return Errors.not_found
    end

    private
{'fraction_non_alphanumeric': 0.11452682338758288, 'fraction_numerical': 0.007836045810729355, 'mean_word_length': 2.50210970464135, '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': '3263231', 'n_tokens_mistral': 540, 'n_tokens_neox': 526, 'n_words': 149}