first_line
, header_fields
and body
of full_request
.
# Parse the `first_line`, `header_fields` and `body` of `full_request`.
fun parse_http_request(full_request: String): nullable HttpRequest
do
clear_data
var http_request = new HttpRequest
self.http_request = http_request
segment_http_request(full_request)
# Parse first line, looks like "GET dir/index.html?user=xymus HTTP/1.0"
if first_line.length < 3 then
print "HTTP error: request first line apprears invalid: {first_line}"
return null
end
http_request.method = first_line[0]
http_request.url = first_line[1]
http_request.http_version = first_line[2]
# GET args
if http_request.url.has('?') then
http_request.uri = first_line[1].substring(0, first_line[1].index_of('?'))
http_request.query_string = first_line[1].substring_from(first_line[1].index_of('?')+1)
var parse_url = parse_url
http_request.get_args = parse_url
http_request.all_args.add_all parse_url
else
http_request.uri = first_line[1]
end
# POST args
if http_request.method == "POST" or http_request.method == "PUT" then
http_request.body = body
var lines = body.split_with('&')
for line in lines do if not line.trim.is_empty then
var parts = line.split_once_on('=')
if parts.length > 1 then
var decoded = parts[1].replace('+', " ").from_percent_encoding
http_request.post_args[parts[0]] = decoded
http_request.all_args[parts[0]] = decoded
end
end
end
# Headers
for i in header_fields do
var temp_field = i.split_with(": ")
if temp_field.length == 2 then
http_request.header[temp_field[0]] = temp_field[1]
end
end
# Cookies
if http_request.header.keys.has("Cookie") then
var cookie = http_request.header["Cookie"]
for couple in cookie.split_with(';') do
var words = couple.trim.split_with('=')
if words.length != 2 then continue
http_request.cookie[words[0]] = words[1]
end
end
return http_request
end
lib/nitcorn/http_request.nit:117,2--182,4
redef fun parse_http_request(text)
do
var request = super
if request != null then
if request.cookie.keys.has("nitcorn_session") then
var id_hash = request.cookie["nitcorn_session"]
if sys.sessions.keys.has(id_hash) then
# Restore the session
request.session = sys.sessions[id_hash]
end
end
end
return request
end
lib/nitcorn/sessions.nit:79,2--93,4