Class: Rack::XapFile

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/xapper.rb

Constant Summary collapse

F =
::File

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ XapFile

Returns a new instance of XapFile.



228
229
230
231
232
233
# File 'lib/rack/xapper.rb', line 228

def initialize(options)
  @options = options.dup
  xap_name = "#{@options[:xap_name]}"
  xap_name = "#{xap_name}.xap" if xap_name !~ /\.xap$/
  @xap_path = @options[:root] + "/" + @options[:xap_path] + "/" + xap_name
end

Instance Method Details

#_call(env) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/rack/xapper.rb', line 241

def _call(env)
  @path_info = Utils.unescape(env["PATH_INFO"])
  return forbidden  if @path_info.include? ".."

  if dev_env? or xap_changed?
    FileUtils.rm(@xap_path) if F.exist?(@xap_path)
    XapBuilder.xap_to_disk @options
  end

  begin
    if F.file?(@xap_path) && F.readable?(@xap_path)
      [200, {
        "Date"           => F.mtime(@xap_path).httpdate,
        "Content-Type"   => Mime.mime_type(F.extname(@xap_path), 'application/x-zip-compressed'),
        "Cache-Control"  => "no-cache",
        "Expires"        => "-1",
        'Pragma'         => "no-cache",
        "Content-Length" => F.size?(@xap_path).to_s
      }, self]
    else
      raise Errno::EPERM
    end
  rescue SystemCallError
    not_found
  end
end

#call(env) ⇒ Object



235
236
237
# File 'lib/rack/xapper.rb', line 235

def call(env)
  dup._call(env)
end

#dev_env?Boolean

Returns:

  • (Boolean)


268
269
270
# File 'lib/rack/xapper.rb', line 268

def dev_env?
  (ENV['RACK_ENV'] || 'development').to_sym != :production
end

#eachObject



293
294
295
296
297
298
299
# File 'lib/rack/xapper.rb', line 293

def each
  F.open(@xap_path, "rb") { |file|
    while part = file.read(8192)
      yield part
    end
  }
end

#forbiddenObject



279
280
281
282
283
284
# File 'lib/rack/xapper.rb', line 279

def forbidden
  body = "Forbidden\n"
  [403, {"Content-Type" => "text/plain",
         "Content-Length" => body.size.to_s},
   [body]]
end

#not_foundObject



286
287
288
289
290
291
# File 'lib/rack/xapper.rb', line 286

def not_found
  body = "File not found: #{@path_info}\n"
  [404, {"Content-Type" => "text/plain",
     "Content-Length" => body.size.to_s},
   [body]]
end

#xap_changed?Boolean

Returns:

  • (Boolean)


272
273
274
275
276
277
# File 'lib/rack/xapper.rb', line 272

def xap_changed?
  XapBuilder.(@options)
  files = @options[:application_files] + @options[:extra_app_files] + @options[:assembly_files]
  xap_mtime = File.mtime(@xap_path)
  files.any?{ |f| File.mtime(f) > xap_mtime }      
end