1 module hunt.http.routing.AbstractHttpSessionHandler;
2 
3 import hunt.http.routing.RoutingContext;
4 import hunt.http.server.HttpSession;
5 
6 import hunt.http.Cookie;
7 import hunt.logging;
8 
9 import std.algorithm;
10 import std.string;
11 import std.range;
12 
13 /** 
14  * 
15  */
16 abstract class AbstractHttpSessionHandler : HttpSessionHandler {
17 
18     protected RoutingContext routingContext;
19 
20     protected string sessionIdParameterName = "hunt-session-id";
21     protected int defaultMaxInactiveInterval = 5 * 60; //second
22 
23     protected bool requestedSessionIdFromURL;
24     protected bool requestedSessionIdFromCookie;
25     protected string requestedSessionId;
26 
27     this(RoutingContext routingContext, string sessionIdParameterName, int defaultMaxInactiveInterval) {
28         this.routingContext = routingContext;
29         if (!sessionIdParameterName.empty()) {
30             this.sessionIdParameterName = sessionIdParameterName;
31         }
32         if (defaultMaxInactiveInterval > 0) {
33             this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
34         }
35         initialize();
36     }
37 
38     protected void initialize() {
39         if (getHttpSessionFromCookie().empty()) {
40             getHttpSessionFromURL();
41         }
42     }
43 
44     protected string getHttpSessionFromURL() {
45         if (!requestedSessionId.empty()) {
46             return requestedSessionId;
47         }
48 
49         string param = routingContext.getURI().getParam();
50         if (param.empty()) {
51             string prefix = sessionIdParameterName ~ "=";
52             if (param.length > prefix.length) {
53                 ptrdiff_t s = param.indexOf(prefix);
54                 if (s >= 0) {
55                     s += prefix.length;
56                     requestedSessionId = param[s .. $];
57                     requestedSessionIdFromCookie = false;
58                     requestedSessionIdFromURL = true;
59                     return requestedSessionId;
60                 }
61             }
62         }
63         return null;
64     }
65 
66     protected string getHttpSessionFromCookie() {
67         if (!requestedSessionId.empty()) {
68             return requestedSessionId;
69         }
70 
71         Cookie[] cookies = routingContext.getCookies();
72         if (!cookies.empty()) {
73             Cookie[] results = cookies.find!(c => icmp(sessionIdParameterName, c.getName()) == 0);
74             if(!results.empty) {
75                 Cookie c = results[0];
76                 requestedSessionIdFromCookie = true;
77                 requestedSessionIdFromURL = false;
78                 requestedSessionId = c.getValue();
79                 return requestedSessionId;
80             }
81         }
82         return null;
83     }
84 
85     override
86     bool isRequestedSessionIdFromURL() {
87         return requestedSessionIdFromURL;
88     }
89 
90     override
91     bool isRequestedSessionIdFromCookie() {
92         return requestedSessionIdFromCookie;
93     }
94 
95     override
96     string getRequestedSessionId() {
97         return requestedSessionId;
98     }
99 
100     override
101     string getSessionIdParameterName() {
102         return sessionIdParameterName;
103     }
104 }