utopia 0.9.52 → 0.9.53

Sign up to get free protection for your applications and to get access to all the features.
@@ -130,11 +130,29 @@ module Utopia
130
130
  return result
131
131
  end
132
132
 
133
+ # Accelerate the response if sendfile is configured.
134
+ def sendfile(request, response)
135
+ if @sendfile.respond_to?(:call)
136
+ response = @sendfile.call(request.env, response)
137
+ elsif @sendfile && request.env['HTTP_X_SENDFILE_MODE'] == "X-Sendfile"
138
+ response[1]["X-Sendfile"] = response[2].to_path
139
+ response[2] = []
140
+ end
141
+
142
+ return response
143
+ end
144
+
133
145
  public
134
146
  def initialize(app, options = {})
135
147
  @app = app
136
148
  @root = options[:root] || Utopia::Middleware::default_root
137
149
 
150
+ if options.key? :sendfile
151
+ @sendfile = options[:sendfile]
152
+ else
153
+ @sendfile = (UTOPIA_ENV == :production)
154
+ end
155
+
138
156
  if options[:types]
139
157
  @extensions = load_mime_types(options[:types])
140
158
  else
@@ -148,7 +166,9 @@ module Utopia
148
166
  end
149
167
 
150
168
  def fetch_file(path)
169
+ # We need file_path to be an absolute path for X-Sendfile to work correctly.
151
170
  file_path = File.join(@root, path.components)
171
+
152
172
  if File.exist?(file_path)
153
173
  return FileReader.new(file_path)
154
174
  else
@@ -201,12 +221,13 @@ module Utopia
201
221
  "Last-Modified" => file.mtime_date,
202
222
  "Content-Type" => @extensions[ext],
203
223
  "Cache-Control" => @cache_control,
204
- "ETag" => file.etag
224
+ "ETag" => file.etag,
205
225
  }
206
226
 
207
227
  if file.modified?(env)
208
228
  response_headers["Content-Length"] = file.size.to_s
209
- return [200, response_headers, file]
229
+
230
+ return sendfile(request, [200, response_headers, file])
210
231
  else
211
232
  return [304, response_headers, []]
212
233
  end
@@ -6,7 +6,7 @@ module Utopia
6
6
  module VERSION
7
7
  MAJOR = 0
8
8
  MINOR = 9
9
- TINY = 52
9
+ TINY = 53
10
10
 
11
11
  STRING = [MAJOR, MINOR, TINY].join('.')
12
12
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: utopia
3
3
  version: !ruby/object:Gem::Version
4
- hash: 83
4
+ hash: 81
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 52
10
- version: 0.9.52
9
+ - 53
10
+ version: 0.9.53
11
11
  platform: ruby
12
12
  authors:
13
13
  - Samuel Williams