-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require "../server" | ||
|
||
class HTTP::Server::RequestProcessor | ||
def initialize(&@handler : HTTP::Handler::Proc) | ||
@wants_close = false | ||
end | ||
|
||
def initialize(@handler : HTTP::Handler | HTTP::Handler::Proc) | ||
@wants_close = false | ||
end | ||
|
||
def close | ||
@wants_close = true | ||
end | ||
|
||
def process(input, output, error = STDERR) | ||
must_close = true | ||
response = Response.new(output) | ||
|
||
begin | ||
until @wants_close | ||
request = HTTP::Request.from_io(input) | ||
|
||
# EOF | ||
break unless request | ||
|
||
if request.is_a?(HTTP::Request::BadRequest) | ||
response.respond_with_error("Bad Request", 400) | ||
response.close | ||
return | ||
end | ||
|
||
response.version = request.version | ||
response.reset | ||
response.headers["Connection"] = "keep-alive" if request.keep_alive? | ||
context = Context.new(request, response) | ||
|
||
begin | ||
@handler.call(context) | ||
rescue ex | ||
response.respond_with_error | ||
response.close | ||
error.puts "Unhandled exception on HTTP::Handler" | ||
ex.inspect_with_backtrace(error) | ||
return | ||
end | ||
|
||
if response.upgraded? | ||
must_close = false | ||
return | ||
end | ||
|
||
response.output.close | ||
output.flush | ||
|
||
break unless request.keep_alive? | ||
end | ||
rescue ex : Errno | ||
# IO-related error, nothing to do | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
asterite
Member
|
||
ensure | ||
input.close if must_close | ||
This comment has been minimized.
Sorry, something went wrong.
ysbaddaden
Contributor
|
||
end | ||
end | ||
end |
I'm not sure I agree, if my OS or program is stressed out and starts to throw IO errors at me, or if I hit some limit like maximum open file descriptors or if something took the socket down beneath my feet, I do want to know.