nitcorn :: HttpServer :: defaultinit
# A server handling a single connection
class HttpServer
super HTTPConnection
# The associated `HttpFactory`
var factory: HttpFactory
private var parser = new HttpRequestParser is lazy
# Human readable address of the remote client
var remote_address: String
redef fun read_http_request(str)
do
var request_object = parser.parse_http_request(str.to_s)
if request_object != null then delegate_answer request_object
end
# Answer to a request
fun delegate_answer(request: HttpRequest)
do
# Find target virtual host
var virtual_host = null
if request.header.keys.has("Host") then
var host = request.header["Host"]
if host.index_of(':') == -1 then host += ":80"
for vh in factory.config.virtual_hosts do
for i in vh.interfaces do if i.to_s == host then
virtual_host = vh
break label
end
end label
end
# Use default virtual host if none already responded
if virtual_host == null then virtual_host = factory.config.default_virtual_host
# Get a response from the virtual host
var response
if virtual_host != null then
var route = virtual_host.routes[request.uri]
if route != null then
# include uri parameters in request
request.uri_params = route.parse_params(request.uri)
var handler = route.handler
var root = route.resolve_path(request)
var turi
if root != null then
turi = ("/" + request.uri.substring_from(root.length)).simplify_path
else turi = request.uri
# Delegate the responsibility to respond to the `Action`
handler.prepare_respond_and_close(request, turi, self)
return
else response = new HttpResponse(404)
else response = new HttpResponse(404)
respond response
close
end
# Send back `response` to the client
fun respond(response: HttpResponse)
do
response.render.write_to(self)
for path in response.files do write_file path
end
end
lib/nitcorn/reactor.nit:29,1--97,3