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.



11
12
13
14
# File 'lib/skypager/proxy.rb', line 11

def initialize(options={})
  @options            = options
  @bucket             = options[:bucket]
end

Instance Attribute Details

#access_key_idObject (readonly)

Returns the value of attribute access_key_id.



6
7
8
# File 'lib/skypager/proxy.rb', line 6

def access_key_id
  @access_key_id
end

#bucketObject (readonly)

Returns the value of attribute bucket.



6
7
8
# File 'lib/skypager/proxy.rb', line 6

def bucket
  @bucket
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/skypager/proxy.rb', line 6

def options
  @options
end

#secret_access_keyObject (readonly)

Returns the value of attribute secret_access_key.



6
7
8
# File 'lib/skypager/proxy.rb', line 6

def secret_access_key
  @secret_access_key
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/skypager/proxy.rb', line 16

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

#proxy_call(env) ⇒ Object



24
25
26
# File 'lib/skypager/proxy.rb', line 24

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

#request_proxyObject



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

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

#s3Object



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

def s3
  Datapimp::Sync.amazon.storage
end

#s3_call(env) ⇒ Object



37
38
39
40
41
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
# File 'lib/skypager/proxy.rb', line 37

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

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

  return Errors.not_found('no-bucket-no-key') 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']

  begin
    head = s3.head_object(bucket, key, req)
  rescue => e
    return Errors.not_found('could not head ' + bucket.to_s + ' ' + key.to_s + ' ' + e.message)
  end

  return Errors.not_found('no-head') 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 Excon::Errors::NotFound
  return Errors.not_found('rescue')

rescue Excon::Errors::NotModified
  return Errors.not_modified

rescue Excon::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