/
request_processor.cr
98 lines (80 loc) · 2.73 KB
/
request_processor.cr
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require "./handler"
require "log"
class HTTP::Server::RequestProcessor
Log = ::Log.for("http.server")
# Maximum permitted size of the request line in an HTTP request.
property max_request_line_size = HTTP::MAX_REQUEST_LINE_SIZE
# Maximum permitted combined size of the headers in an HTTP request.
property max_headers_size = HTTP::MAX_HEADERS_SIZE
def initialize(&@handler : HTTP::Handler::HandlerProc)
@wants_close = false
end
def initialize(@handler : HTTP::Handler | HTTP::Handler::HandlerProc)
@wants_close = false
end
def close : Nil
@wants_close = true
end
def process(input, output) : Nil
response = Response.new(output)
begin
until @wants_close
request = HTTP::Request.from_io(
input,
max_request_line_size: max_request_line_size,
max_headers_size: max_headers_size,
)
# EOF
break unless request
response.reset
if request.is_a?(HTTP::Status)
response.respond_with_status(request)
return
end
response.version = request.version
response.headers["Connection"] = "keep-alive" if request.keep_alive?
context = Context.new(request, response)
Log.with_context do
@handler.call(context)
rescue ex : ClientError
Log.debug(exception: ex.cause) { ex.message }
rescue ex
Log.error(exception: ex) { "Unhandled exception on HTTP::Handler" }
unless response.closed?
unless response.wrote_headers?
response.respond_with_status(:internal_server_error)
end
end
return
ensure
response.output.close
end
output.flush
# If there is an upgrade handler, hand over
# the connection to it and return
if upgrade_handler = response.upgrade_handler
upgrade_handler.call(output)
return
end
break unless request.keep_alive?
# Don't continue if the handler set `Connection` header to `close`
break unless HTTP.keep_alive?(response)
# The request body is either FixedLengthContent or ChunkedContent.
# In case it has not entirely been consumed by the handler, the connection is
# closed the connection even if keep alive was requested.
case body = request.body
when FixedLengthContent
if body.read_remaining > 0
# Close the connection if there are bytes remaining
break
end
when ChunkedContent
# Close the connection if the IO has still bytes to read.
break unless body.closed?
end
end
rescue IO::Error
# IO-related error, nothing to do
end
end
end