When parsing a request, this module associate a pre-existing session to the request if there is one. It will also send the required cookie with the response if a session has been associated to the response object.
nitcorn :: sessions $ HttpRequest
A request received over HTTP, is build byHttpRequestParser
nitcorn :: sessions $ HttpRequestParser
Utility class to parse a request string and build aHttpRequest
nitcorn :: sessions $ HttpRequest
A request received over HTTP, is build byHttpRequestParser
nitcorn :: sessions $ HttpRequestParser
Utility class to parse a request string and build aHttpRequest
Serializable::inspect
to show more useful information
serialization :: serialization_core
Abstract services to serialize Nit objects to different formatscore :: union_find
union–find algorithm using an efficient disjoint-set data structureFileServer
action, which is a standard and minimal file server
restful
annotation documented at lib/nitcorn/restful.nit
root
to execute
# Automated session management
#
# When parsing a request, this module associate a pre-existing session
# to the request if there is one. It will also send the required cookie
# with the response if a session has been associated to the response object.
module sessions
import md5
import server_config
import http_request
import http_response
import token
# A server side session
class Session
# Hashed id used both client and server side to identify this `Session`
var id_hash: String is noinit
init
do
loop
var token = generate_token
if sys.sessions.keys.has(token) then continue
sys.sessions[token] = self
self.id_hash = token
break
end
end
end
redef class Sys
# Active sessions
var sessions = new HashMap[String, Session]
end
redef class HttpResponse
# A `Session` to associate with a response
var session: nullable Session = null is writable
redef fun finalize
do
super
var session = self.session
if session != null then
header["Set-Cookie"] = "nitcorn_session={session.id_hash}; HttpOnly"
else
# Make sure there are no cookie left client side
header["Set-Cookie"] = "nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT"
end
end
end
redef class HttpRequest
# The `Session` associated to this request
var session: nullable Session = null is writable
end
redef class HttpRequestParser
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
end
lib/nitcorn/sessions.nit:17,1--94,3