-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.rb
More file actions
39 lines (35 loc) · 1 KB
/
worker.rb
File metadata and controls
39 lines (35 loc) · 1 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
require 'socket'
require_relative 'logger'
require_relative 'httpd_config'
require_relative 'resource'
require_relative 'response_factory'
#Class to handle a single request
class Worker
#client (a reference to the stream that the server received a new request on)
def initialize (client, config, mime, logger)
@client = client
@config = config
@logger = logger
@mime = mime
end
def parse_stream
puts "Connection Received"
request_obj = Request.new(@client)
begin
request_obj.parse
rescue
response_obj = Response.new("HTTP 1.1", 400, "Content-Type: text/html\r\n",
File.read("public_html/400.html"))
@logger.write(request_obj, response_obj)
@client.puts response_obj
@client.close
return
end
resource_obj = Resource.new(request_obj.uri, @config, @mime)
response_obj = ResponseFactory.create(request_obj, resource_obj)
@logger.write(request_obj, response_obj)
@client.puts response_obj
@client.close
return
end
end