Class: Kaizen::KZNServer

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

Class Method Summary collapse

Class Method Details

.call(env) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/kaizen.rb', line 336

def self.call(env)
  requested_file = request_path(Rack::Request.new(env).path)

  response = Rack::Response.new

  response['Content-Type']  = mime_for(requested_file)
  response['Cache-Control'] = "no-cache, no-store, must-revalidate"
  response['Pragma']        = "no-cache"
  response['Expires']       = "0"

  if File.file?(requested_file)
    response.write(File.read(requested_file))
    response.status = 200
  else
    response.status = 404
  end

  response.finish
end

.mime_for(file) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/kaizen.rb', line 277

def self.mime_for(file)
  supported_mimes = {
    ".avi"   => :"video/x-msvideo",
    ".bmp"   => :"image/bmp",
    ".css"   => :"text/css",
    ".csv"   => :"text/csv",
    ".doc"   => :"application/msword",
    ".docm"  => :"application/vnd.ms-word.document.macroEnabled.12",
    ".docx"  => :"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    ".flv"   => :"video/x-flv",
    ".gz"    => :"application/x-gzip",
    ".htm"   => :"text/html",
    ".html"  => :"text/html",
    ".ico"   => :"image/x-icon",
    ".jpe"   => :"image/jpeg",
    ".jpeg"  => :"image/jpeg",
    ".jpg"   => :"image/jpeg",
    ".js"    => :"application/x-javascript",
    ".m4a"   => :"audio/m4a",
    ".m4v"   => :"video/x-m4v",
    ".mov"   => :"video/quicktime",
    ".movie" => :"video/x-sgi-movie",
    ".mp2"   => :"video/mpeg",
    ".mp2v"  => :"video/mpeg",
    ".mp3"   => :"audio/mpeg",
    ".mp4"   => :"video/mp4",
    ".mp4v"  => :"video/mp4",
    ".mpa"   => :"video/mpeg",
    ".mpeg"  => :"video/mpeg",
    ".mpg"   => :"video/mpeg",
    ".png"   => :"image/png",
    ".rar"   => :"application/octet-stream",
    ".tar"   => :"application/x-tar",
    ".tif"   => :"image/tiff",
    ".tiff"  => :"image/tiff",
    ".ttf"   => :"application/octet-stream",
    ".tts"   => :"video/vnd.dlna.mpeg-tts",
    ".txt"   => :"text/plain",
    ".wav"   => :"audio/wav",
    ".wsdl"  => :"text/xml",
    ".xhtml" => :"application/xhtml+xml",
    ".xml"   => :"text/xml",
    ".xsd"   => :"text/xml",
    ".xsf"   => :"text/xml",
    ".xsl"   => :"text/xml",
    ".xslt"  => :"text/xml",
    ".xsn"   => :"application/octet-stream",
    ".zip"   => :"application/x-zip-compressed"
  }

  ext = File.extname(file)

  if supported_mimes.key?(ext)
    supported_mimes[ext].to_s
  else
    'text/html'
  end
end

.request_path(path) ⇒ Object



267
268
269
270
271
272
273
274
275
# File 'lib/kaizen.rb', line 267

def self.request_path(path)
  parts = path.split('/')

  if parts.size == 0
    parts << "index.html"
  end

  return "#{@@server_path}/#{parts.join('/')}"
end

.set_load_path(p) ⇒ Object



263
264
265
# File 'lib/kaizen.rb', line 263

def self.set_load_path(p)
  @@server_path = p
end