Class: Yarn::RequestHandler
Instance Attribute Summary
#parser, #request, #response, #session
Instance Method Summary
collapse
#client_address, #extract_path, #initialize, #parse_request, #persistent?, #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
Instance Method Details
#execute_script(path) ⇒ Object
51
52
53
54
55
56
57
58
|
# File 'lib/yarn/request_handler.rb', line 51
def execute_script(path)
response = `ruby #{path} #{post_body}`
if !! ($?.to_s =~ /1$/)
raise ProcessingError
else
response
end
end
|
#get_mime_type(path) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/yarn/request_handler.rb', line 60
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_response ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/yarn/request_handler.rb', line 3
def prepare_response
path =
@response.["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
44
45
46
47
48
49
|
# File 'lib/yarn/request_handler.rb', line 44
def read_file(path)
file_contents = []
File.open(path).each { |line| file_contents << line }
file_contents
end
|
#serve_directory(path) ⇒ Object
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/yarn/request_handler.rb', line 33
def serve_directory(path)
@response.status = 200
if File.exists?("index.html") || File.exists?("/index.html")
@response.body = read_file "index.html"
@response.["Content-Type"] = "text/html"
else
@response.["Content-Type"] = "text/html"
@response.body << DirectoryLister.list(path)
end
end
|
#serve_file(path) ⇒ Object
22
23
24
25
26
|
# File 'lib/yarn/request_handler.rb', line 22
def serve_file(path)
@response.body << read_file(path)
@response.["Content-Type"] = get_mime_type path
@response.status = 200
end
|
#serve_ruby_file(path) ⇒ Object
28
29
30
31
|
# File 'lib/yarn/request_handler.rb', line 28
def serve_ruby_file(path)
@response.body << execute_script(path)
@response.status = 200
end
|