-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse_factory.rb
More file actions
60 lines (54 loc) · 1.97 KB
/
response_factory.rb
File metadata and controls
60 lines (54 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require_relative 'request'
require_relative 'resource'
require_relative 'response'
require_relative 'htaccess_checker'
#Class to generate the responses, based on certain conditions
class ResponseFactory
def self.create(request, resource)
resolved_uri = resource.resolve
htaccess_checker = HtaccessChecker.new("public_html/protected/.htaccess",
request.headers)
if htaccess_checker.protected?
if !htaccess_checker.can_authorize?
return Response.new("HTTP/1.1", 401, "Content-Type: text/html\r\nWWW-Authenticate: Basic\r\n",
File.read("public_html/401.html"))
else
if !htaccess_checker.authorized?
return Response.new("HTTP/1.1", 403, "Content-Type: text/html\r\n",
File.read("public_html/403.html"))
end
end
end
if(!File.exist?(resolved_uri))
return Response.new("HTTP/1.1", 404, "Content-Type: text/html\r\n",
File.read("public_html/404.html"))
end
#check script alias, if true do IO.popen
if(resource.script?)
IO.popen([{'ENV'=>'value'}, resolved_uri+"perl_env"]) {
|script|
return Response.new("HTTP/1.1", 200, "", script.read)
}
end
case request.method
when "GET"
return Response.new("HTTP/1.1", 200, "Content-Type: text/html\r\n",
File.read(resolved_uri))
when "HEAD"
return Response.new("HTTP/1.1", 200, "Content-Type: text/html\r\n",
"")
when "POST"
#Not sure how to implement
return Response.new("HTTP/1.1", 200, "Content-Type: text/html\r\n", "")
when "PUT"
#Not tested fully
File.open(resolved_uri, 'a') {|file| file.write(request.body)}
return Response.new("HTTP/1.1", 201, "Content-Type: text/html\r\n",
File.read("public_html/put.html"))
when "DELETE"
File.delete(resolved_uri)
return Response.new("HTTP/1.1", 204, "Content-Type: text/html\r\n",
File.read("public_html/delete.html"))
end
end
end