-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.rb
More file actions
63 lines (53 loc) · 1.39 KB
/
response.rb
File metadata and controls
63 lines (53 loc) · 1.39 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
61
62
63
#Class to hold the Response
class Response
attr_reader :version, :response_code, :headers, :body
RESPONSE_CODES = {
200 => "OK",
201 => "Created",
202 => "Accepted",
204 => "No Content",
301 => "Moved Permanently",
400 => "Bad Request",
401 => "Unauthorized",
403 => "Forbidden",
404 => "Not Found",
500 => "Internal Server Error",
}
def initialize(version, response_code, header, body)
@version = version
@response_code = response_code
@headers = HeadersCollection.new(header+"Content-Length: #{body.bytesize}")
#Since we are just appending the body in CGI cases, we have to format the
#body just in case the body is just an HTML file
if !body.start_with? "<"
@body = body
else
@body = "\r\n\r\n"+body
end
end
def to_s
@headers.parse
date = Time.new.strftime("%a, %d %b %Y %H:%M:%S %Z")
"#{@version} #{@response_code} #{RESPONSE_CODES[@response_code]}\r\n"+
"Server: server-12\r\nDate: #{date}\r\n#{@headers.to_s} #{@body}"
end
end
#Class to hold all of the Headers
class HeadersCollection
attr_reader :headers
def initialize(str)
@headers = {}
@lines = str
end
def parse
@lines.split("\r\n").each do |line|
temp = line.split(": ")
headers[temp[0]] = temp[1]
end
end
def to_s
headers.map { |key, value|
"#{key}: #{value}"
}.join("\r\n")
end
end