Class: S3Proxy::App

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_proxy/app.rb

Defined Under Namespace

Modules: Errors

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ App

Returns a new instance of App.



6
7
8
# File 'lib/s3_proxy/app.rb', line 6

def initialize(options={})
  @options = options
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/s3_proxy/app.rb', line 10

def call(env)
  return Errors.method_not_allowed unless %w(GET HEAD).include?(env['REQUEST_METHOD'])
  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

  return Errors.not_found unless bucket && key

  req = {bucket: bucket, key: key}

  req[:if_match] = env['HTTP_IF_MATCH'] if env['HTTP_IF_MATCH']
  req[:if_none_match] = env['HTTP_IF_NONE_MATCH'] if env['HTTP_IF_NONE_MATCH']
  req[:if_modified_since] = env['HTTP_IF_MODIFIED_SINCE'] if env['HTTP_IF_MODIFIED_SINCE']
  req[:if_unmodified_since] = env['HTTP_IF_UNMODIFIED_SINCE'] if env['HTTP_UNMODIFIED_SINCE']

  head = s3.head_object(req)

  return Errors.not_found unless head

  case env['REQUEST_METHOD']
  when 'HEAD'
    gentle env, req, head
  when 'GET'
    if env['rack.hijack?']
      hijack env, req, head
    else
      gentle env, req, head
    end
  end

rescue Aws::S3::Errors::NoSuchKey, Aws::S3::Errors::NotFound
  return Errors.not_found

rescue Aws::S3::Errors::NotModified
  return Errors.not_modified

rescue Aws::S3::Errors::PreconditionFailed
  return Errors.precondition_failed

rescue NameError => e
  # https://github.com/aws/aws-sdk-core-ruby/pull/65
  raise e unless e.message == "wrong constant name 412Error"

  return Errors.precondition_failed
end