228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
|
(sdat-set-session-key! self new-key)
(sdat-set-session-id! self new-sid)
(sdat-set-session-cookie! self (session:make-cookie self)))
(sdat-set-session-id! self sid))))
(define (session:make-cookie self)
;; (list (conc "session_key=" (sdat-get-session-key self) "; Path=/; Domain=." (sdat-get-domain self) "; Max-Age=" (* 86400 14) "; Version=1")))
;; According to
;; http://www.codemarvels.com/2010/11/apache-rewriterule-set-a-cookie-on-localhost/
;; Here are the 2 (often left out) requirements to set a cookie using
;; httpd-F˘s rewrite rule (mod_rewrite), while working on localhost:-A
;;
;; Use the IP 127.0.0.1 instead of localhost/machine-name as the
;; domain; e.g. [CO=someCookie:someValue:127.0.0.1:2:/], which says
;; create a cookie -Y´someCookieˇ with value ´someValueˇ for the
;; domain ´127.0.0.1$B!m(B having a life time of 2 mins, for any path in
;; the domain (path=/). (Obviously you will have to run the
;; application with this value in the URL)
;;
;; To make a session cookie, limit the flag statement to just three
;; attributes: name, value and domain. e.g
;; [CO=someCookie:someValue:127.0.0.1] %G–%@ Any further
;; settings, apache writes anˇ expiresˇ attribute for the set-cookie
;; header, which makes the cookie a persistent one (not really
;; persistent, as the expires value set is the current server time
;; %G–%@ so you don-F-F˘t even get to see your cookie!)-A
(list (string-substitute
";" "; "
(car (construct-cookie-string
;; warning! messing up this itty bitty bit of code will cost much time!
`(("session_key" ,(sdat-get-session-key self)
expires: ,(+ (current-seconds) (* 14 86400))
max-age: (* 14 86400)
;; max-age: (* 14 86400)
path: "/" ;;
domain: ,(string-append "." (sdat-get-domain self))
version: 1)) 0)))))
;; look up a given session key and return the id if found, #f if not found
(define (session:get-id self session-key)
;; (let ((session-key (sdat-get-session-key self)))
|
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
-
-
-
-
+
+
+
+
|
(dbi:for-each-row (lambda (tuple)
(set! status #t))
conn (string-append "INSERT INTO sessions (session_key) VALUES ('" tmpkey "')"))
tmpkey))
;; returns session key IFF it is in the HTTP_COOKIE
(define (session:extract-session-key self)
(let ((http-session (get-environment-variable "HTTP_COOKIE")))
(if http-session
(session:extract-key-from-param self (list http-session) "session_key")
#f)))
(let ((http-cookie (get-environment-variable "HTTP_COOKIE")))
(if http-cookie
(session:extract-key-from-param self (string-split-fields ";\\s+" http-cookie infix:) "session_key")
)))
(define (session:get-session-id self session-key)
(let ((query "SELECT id FROM sessions WHERE session_key=?;")
(result #f))
;; (pg:query-for-each (lambda (tuple)
;; (set! result (vector-ref tuple 0))) ;; (vector-ref tuple 0)))
;; (s:sqlparam query session-key)
|
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
+
+
|
(define (session:extract-key self key)
(let ((params (sdat-get-params self)))
(session:extract-key-from-param self params key)))
(define (session:extract-key-from-param self params key)
(let ((r1 (regexp (string-append "^" key "=([^=]+)$"))))
(err:log "INFO: Looking for " key " in " params)
(if (< (length params) 1) #f
(let loop ((head (car params))
(tail (cdr params)))
(let ((match (string-match r1 head)))
(cond
(match
(let ((session-key (list-ref match 1)))
(err:log "INFO: Found session key=" session-key)
(sdat-set-session-key! self (list-ref match 1))
session-key))
((null? tail)
#f)
(else
(loop (car tail)
(cdr tail)))))))))
|