Class: Skypager::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/skypager/proxy.rb

Defined Under Namespace

Modules: Errors Classes: RequestProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Proxy

Returns a new instance of Proxy.



14
15
16
17
18
19
# File 'lib/skypager/proxy.rb', line 14

def initialize(options={})
  @options            = options
  @bucket             = options[:bucket]
  @access_key_id      = options.fetch(:access_key_id) { Skypager.config.aws_access_key_id }
  @secret_access_key  = options.fetch(:secret_access_key) { Skypager.config.aws_secret_access_key }
end

Instance Attribute Details

#access_key_idObject (readonly)

Returns the value of attribute access_key_id.



9
10
11
# File 'lib/skypager/proxy.rb', line 9

def access_key_id
  @access_key_id
end

#bucketObject (readonly)

Returns the value of attribute bucket.



9
10
11
# File 'lib/skypager/proxy.rb', line 9

def bucket
  @bucket
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/skypager/proxy.rb', line 9

def options
  @options
end

#secret_access_keyObject (readonly)

Returns the value of attribute secret_access_key.



9
10
11
# File 'lib/skypager/proxy.rb', line 9

def secret_access_key
  @secret_access_key
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/skypager/proxy.rb', line 21

def call(env)
  if @options[:bucket]
    s3_call(env)
  elsif @options[:host] || @options[:port]
    proxy_call(env)
  end
end

#proxy_call(env) ⇒ Object



29
30
31
# File 'lib/skypager/proxy.rb', line 29

def proxy_call(env)
  request_proxy.call(env)
end

#request_proxyObject



37
38
39
40
# File 'lib/skypager/proxy.rb', line 37

def request_proxy
  o = @options
  @request_proxy ||= RequestProxy.new.tap {|p| p.set_config(o) }
end

#s3Object



33
34
35
# File 'lib/skypager/proxy.rb', line 33

def s3
  @s3 ||= Aws::S3.new(access_key_id: access_key_id, secret_access_key: secret_access_key, region: "us-east-1")
end

#s3_call(env) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/skypager/proxy.rb', line 42

def s3_call(env)
  return Errors.method_not_allowed unless %w(GET HEAD).include?(env['REQUEST_METHOD'])
  return Errors.not_found if env['PATH_INFO'].empty?

  rack_request = Rack::Request.new(env)
  key = "#{rack_request.path}".without_leading_slash

  return Errors.not_found unless bucket && key

  path = key.without_leading_slash
  last = path.split("/").last

  req = {bucket: bucket, key: path}

  req[:key]                   = "index.html" if req[:key].to_s.length == 0

  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