1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Automated session management
19 # When parsing a request, this module associate a pre-existing session
20 # to the request if there is one. It will also send the required cookie
21 # with the response if a session has been associated to the response object.
31 # A server side session
34 # Hashed id used both client and server side to identify this `Session`
35 var id_hash
: String is noinit
40 var token
= generate_token
41 if sys
.sessions
.keys
.has
(token
) then continue
43 sys
.sessions
[token
] = self
52 var sessions
= new HashMap[String, Session]
55 redef class HttpResponse
56 # A `Session` to associate with a response
57 var session
: nullable Session = null is writable
63 var session
= self.session
64 if session
!= null then
65 header
["Set-Cookie"] = "nitcorn_session={session.id_hash}; HttpOnly"
67 # Make sure there are no cookie left client side
68 header
["Set-Cookie"] = "nitcorn_session=; HttpOnly; expires=Thu, 01 Jan 1970 00:00:00 GMT"
73 redef class HttpRequest
74 # The `Session` associated to this request
75 var session
: nullable Session = null is writable
78 redef class HttpRequestParser
79 redef fun parse_http_request
(text
)
82 if request
!= null then
83 if request
.cookie
.keys
.has
("nitcorn_session") then
84 var id_hash
= request
.cookie
["nitcorn_session"]
86 if sys
.sessions
.keys
.has
(id_hash
) then
88 request
.session
= sys
.sessions
[id_hash
]