Handling bad incoming request (keep a check assertion to help during debugging period)
This commit is contained in:
@@ -26,6 +26,7 @@ feature {NONE} -- Initialization
|
||||
|
||||
reset
|
||||
do
|
||||
has_error := False
|
||||
create method.make_empty
|
||||
create uri.make_empty
|
||||
create request_header.make_empty
|
||||
@@ -48,7 +49,14 @@ feature -- Execution
|
||||
end
|
||||
|
||||
analyze_request_message (client_socket)
|
||||
process_request (Current, client_socket)
|
||||
if has_error then
|
||||
check catch_bad_incoming_connection: False end
|
||||
if is_verbose then
|
||||
log ("ERROR: invalid HTTP incoming request")
|
||||
end
|
||||
else
|
||||
process_request (Current, client_socket)
|
||||
end
|
||||
reset
|
||||
end
|
||||
|
||||
@@ -57,6 +65,7 @@ feature -- Request processing
|
||||
process_request (a_handler: HTTP_CONNECTION_HANDLER; a_socket: TCP_STREAM_SOCKET)
|
||||
-- Process request ...
|
||||
require
|
||||
no_error: not has_error
|
||||
a_handler_attached: a_handler /= Void
|
||||
a_uri_attached: a_handler.uri /= Void
|
||||
a_method_attached: a_handler.method /= Void
|
||||
@@ -74,6 +83,9 @@ feature -- Access
|
||||
request_header_map : HASH_TABLE [STRING,STRING]
|
||||
-- Contains key:value of the header
|
||||
|
||||
has_error: BOOLEAN
|
||||
-- Error occurred during `analyze_request_message'
|
||||
|
||||
method: STRING
|
||||
-- http verb
|
||||
|
||||
@@ -85,10 +97,12 @@ feature -- Access
|
||||
--| unused for now
|
||||
|
||||
remote_info: detachable TUPLE [addr: STRING; hostname: STRING; port: INTEGER]
|
||||
-- Information related to remote client
|
||||
|
||||
feature -- Parsing
|
||||
|
||||
analyze_request_message (a_socket: TCP_STREAM_SOCKET)
|
||||
-- Analyze message extracted from `a_socket' as HTTP request
|
||||
require
|
||||
input_readable: a_socket /= Void and then a_socket.is_open_read
|
||||
local
|
||||
@@ -97,23 +111,31 @@ feature -- Parsing
|
||||
line : detachable STRING
|
||||
k, val: STRING
|
||||
txt: STRING
|
||||
l_is_verbose: BOOLEAN
|
||||
do
|
||||
create txt.make (64)
|
||||
line := next_line (a_socket)
|
||||
if line /= Void then
|
||||
analyze_request_line (line)
|
||||
txt.append (line)
|
||||
txt.append_character ('%N')
|
||||
request_header := txt
|
||||
|
||||
request_header := txt
|
||||
if attached next_line (a_socket) as l_request_line and then not l_request_line.is_empty then
|
||||
txt.append (l_request_line)
|
||||
txt.append_character ('%N')
|
||||
analyze_request_line (l_request_line)
|
||||
else
|
||||
has_error := True
|
||||
end
|
||||
|
||||
l_is_verbose := is_verbose
|
||||
|
||||
if not has_error or l_is_verbose then
|
||||
-- if `is_verbose' we can try to print the request, even if it is a bad HTTP request
|
||||
from
|
||||
line := next_line (a_socket)
|
||||
until
|
||||
line = Void or end_of_stream
|
||||
loop
|
||||
n := line.count
|
||||
if is_verbose then
|
||||
print ("%N" + line)
|
||||
if l_is_verbose then
|
||||
log (line)
|
||||
end
|
||||
pos := line.index_of (':',1)
|
||||
if pos > 0 then
|
||||
@@ -139,26 +161,26 @@ feature -- Parsing
|
||||
end
|
||||
|
||||
analyze_request_line (line: STRING)
|
||||
-- Analyze `line' as a HTTP request line
|
||||
require
|
||||
line /= Void
|
||||
valid_line: line /= Void and then not line.is_empty
|
||||
local
|
||||
pos, next_pos: INTEGER
|
||||
do
|
||||
if is_verbose then
|
||||
print ("%N## Parse HTTP request line ##")
|
||||
print ("%N")
|
||||
print (line)
|
||||
log ("%N## Parse HTTP request line ##")
|
||||
log (line)
|
||||
end
|
||||
pos := line.index_of (' ', 1)
|
||||
method := line.substring (1, pos - 1)
|
||||
next_pos := line.index_of (' ', pos + 1)
|
||||
uri := line.substring (pos + 1, next_pos - 1)
|
||||
version := line.substring (next_pos + 1, line.count)
|
||||
ensure
|
||||
not_void_method: method /= Void
|
||||
has_error := method.is_empty
|
||||
end
|
||||
|
||||
next_line (a_socket: TCP_STREAM_SOCKET): detachable STRING
|
||||
-- Next line fetched from `a_socket' is available.
|
||||
require
|
||||
is_readable: a_socket.is_open_read
|
||||
do
|
||||
|
||||
Reference in New Issue
Block a user