-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.rb
More file actions
63 lines (56 loc) · 1.36 KB
/
request.rb
File metadata and controls
63 lines (56 loc) · 1.36 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
require 'timeout'
#Class to parse and hold the client's request
class Request
attr_reader :method, :uri, :version, :headers, :body
def initialize(socket)
@socket_content = socket
@headers = {}
end
def parse
parse_first_line
parse_headers
parse_body
end
def parse_first_line
#parse the method
line = @socket_content.gets.split()
@method = line[0]
#Checks to see if user forgot to put in a slash at the end of a directory
#Ignores if uri ends with filename, if includes "."
if line[1].include?(".")
@uri = line[1]
elsif line[1].end_with?("/")
@uri = line[1]
else
@uri = line[1]+"/"
end
@version = line[2]
end
def parse_headers
#parse the header
line = @socket_content.gets
while (line != "\r\n")
key, value = line.chomp!.split(': ', 2)
@headers[key] = value
line = @socket_content.gets
end
end
def parse_body
#parse body
#Using timeouts just in case there is no body and the stream takes up 0.5
#seconds to read when there's nothing there
begin
timeout(0.5) do
line = @socket_content.gets
puts line.is_instance_of? String
while (line != "")
puts line
@body += line
line = @socket_content.gets
end
end
rescue Timeout::Error
@body = ""
end
end
end