Class: Dragonfly::Server

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/dragonfly/server.rb

Defined Under Namespace

Classes: JobNotAllowed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Server

Returns a new instance of Server



17
18
19
20
21
22
23
24
# File 'lib/dragonfly/server.rb', line 17

def initialize(app)
  @app = app
  @dragonfly_url = '/dragonfly'
  self.url_format = '/:job/:name'
  @fetch_file_whitelist = Whitelist.new
  @fetch_url_whitelist = Whitelist.new
  @verify_urls = true
end

Instance Attribute Details

#dragonfly_urlObject

Returns the value of attribute dragonfly_url



26
27
28
# File 'lib/dragonfly/server.rb', line 26

def dragonfly_url
  @dragonfly_url
end

#fetch_file_whitelistObject (readonly)

Returns the value of attribute fetch_file_whitelist



28
29
30
# File 'lib/dragonfly/server.rb', line 28

def fetch_file_whitelist
  @fetch_file_whitelist
end

#fetch_url_whitelistObject (readonly)

Returns the value of attribute fetch_url_whitelist



28
29
30
# File 'lib/dragonfly/server.rb', line 28

def fetch_url_whitelist
  @fetch_url_whitelist
end

#url_formatObject

Returns the value of attribute url_format



28
29
30
# File 'lib/dragonfly/server.rb', line 28

def url_format
  @url_format
end

#url_hostObject

Returns the value of attribute url_host



26
27
28
# File 'lib/dragonfly/server.rb', line 26

def url_host
  @url_host
end

#url_path_prefixObject

Returns the value of attribute url_path_prefix



26
27
28
# File 'lib/dragonfly/server.rb', line 26

def url_path_prefix
  @url_path_prefix
end

#verify_urlsObject

Returns the value of attribute verify_urls



26
27
28
# File 'lib/dragonfly/server.rb', line 26

def verify_urls
  @verify_urls
end

Instance Method Details

#add_to_fetch_file_whitelist(patterns) ⇒ Object



30
31
32
# File 'lib/dragonfly/server.rb', line 30

def add_to_fetch_file_whitelist(patterns)
  fetch_file_whitelist.push *patterns
end

#add_to_fetch_url_whitelist(patterns) ⇒ Object



34
35
36
# File 'lib/dragonfly/server.rb', line 34

def add_to_fetch_url_whitelist(patterns)
  fetch_url_whitelist.push *patterns
end

#before_serve(&block) ⇒ Object



47
48
49
# File 'lib/dragonfly/server.rb', line 47

def before_serve(&block)
  self.before_serve_callback = block
end

#call(env) ⇒ Object



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
# File 'lib/dragonfly/server.rb', line 51

def call(env)
  if dragonfly_url == env["PATH_INFO"]
    dragonfly_response
  elsif (params = url_mapper.params_for(env["PATH_INFO"], env["QUERY_STRING"])) && params['job']
    job = Job.deserialize(params['job'], app)
    validate_job!(job)
    job.validate_sha!(params['sha']) if verify_urls
    response = Response.new(job, env)
    catch(:halt) do
      if before_serve_callback && response.will_be_served?
        before_serve_callback.call(job, env)
      end
      response.to_response
    end
  else
    [404, {'Content-Type' => 'text/plain', 'X-Cascade' => 'pass'}, ['Not found']]
  end
rescue Job::NoSHAGiven => e
  [400, {"Content-Type" => 'text/plain'}, ["You need to give a SHA parameter"]]
rescue Job::IncorrectSHA => e
  [400, {"Content-Type" => 'text/plain'}, ["The SHA parameter you gave (#{e}) is incorrect"]]
rescue JobNotAllowed => e
  Dragonfly.warn(e.message)
  [403, {"Content-Type" => 'text/plain'}, ["Forbidden"]]
rescue Serializer::BadString, Serializer::MaliciousString, Job::InvalidArray => e
  Dragonfly.warn(e.message)
  [404, {'Content-Type' => 'text/plain'}, ['Not found']]
end

#url_for(job, opts = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dragonfly/server.rb', line 80

def url_for(job, opts={})
  opts = opts.dup
  host = opts.delete(:host) || url_host
  path_prefix = opts.delete(:path_prefix) || url_path_prefix
  params = job.url_attributes.extract(url_mapper.params_in_url)
  params.merge!(stringify_keys(opts))
  params['job'] = job.serialize
  params['sha'] = job.sha if verify_urls
  url = url_mapper.url_for(params)
  "#{host}#{path_prefix}#{url}"
end