Class: Yarn::RequestHandler

Inherits:
AbstractHandler show all
Defined in:
lib/yarn/request_handler.rb

Overview

handler for static and dynamic requests

Instance Attribute Summary

Attributes inherited from AbstractHandler

#parser, #request, #response, #session

Instance Method Summary collapse

Methods inherited from AbstractHandler

#client_address, #extract_path, #initialize, #parse_request, #post_body, #read_request, #request_path, #return_response, #run, #set_common_headers

Methods included from ErrorPage

#serve_404_page, #serve_500_page

Methods included from Logging

#debug, #log, #output, #timestamp

Constructor Details

This class inherits a constructor from Yarn::AbstractHandler

Instance Method Details

#execute_script(path) ⇒ Object

Evaluates the contents of a ruby file and returns the output.



60
61
62
63
64
65
66
67
# File 'lib/yarn/request_handler.rb', line 60

def execute_script(path)
  response = `ruby #{path} #{post_body}`
  if !! ($?.to_s =~ /1$/)
    raise ProcessingError
  else
    response
  end
end

#get_mime_type(path) ⇒ Object

Determines the MIME type based on the file extension.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/yarn/request_handler.rb', line 70

def get_mime_type(path)
  return false unless path.include? '.'
  filetype = path.split('.').last

  return case
    when ["html", "htm"].include?(filetype)
      "text/html"
    when "txt" == filetype 
      "text/plain"
    when "css" == filetype
      "text/css"
    when "js" == filetype
      "text/javascript"
    when ["png", "jpg", "jpeg", "gif", "tiff"].include?(filetype)
      "image/#{filetype}"
    when ["zip","pdf","postscript","x-tar","x-dvi"].include?(filetype)
      "application/#{filetype}"
    else false
  end
end

#prepare_responseObject

Determines whether to serve a directory listing, the contetents of a file, or an error page.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/yarn/request_handler.rb', line 7

def prepare_response
  path = extract_path

  @response.headers["Content-Type"] = "text/html"

  begin
    if File.directory?(path)
      serve_directory(path)
    elsif File.exists?(path)
      path =~ /.*\.rb$/ ? serve_ruby_file(path) : serve_file(path)
    else
      serve_404_page
    end
  rescue ProcessingError
    log "An error occured processing #{path}"
    serve_500_page
  end
end

#read_file(path) ⇒ Object

Reads the contents of a file into an Array.



52
53
54
55
56
57
# File 'lib/yarn/request_handler.rb', line 52

def read_file(path)
  file_contents = []
  File.open(path).each { |line| file_contents << line }

  file_contents
end

#serve_directory(path) ⇒ Object

Sets the reponse for a directory listing.



40
41
42
43
44
45
46
47
48
49
# File 'lib/yarn/request_handler.rb', line 40

def serve_directory(path)
  @response.status = 200
  if File.exists?("index.html") || File.exists?("/index.html")
    @response.body = read_file "index.html"
    @response.headers["Content-Type"] = "text/html"
  else
    @response.headers["Content-Type"] = "text/html"
    @response.body << DirectoryLister.list(path)
  end
end

#serve_file(path) ⇒ Object

Sets the response for a static file.



27
28
29
30
31
# File 'lib/yarn/request_handler.rb', line 27

def serve_file(path)
  @response.body << read_file(path)
  @response.headers["Content-Type"] = get_mime_type path
  @response.status = 200
end

#serve_ruby_file(path) ⇒ Object

Sets the response for a dynamic file.



34
35
36
37
# File 'lib/yarn/request_handler.rb', line 34

def serve_ruby_file(path)
  @response.body << execute_script(path)
  @response.status = 200
end