Libraryless. Click here for Pure Java version (13531L/80K).
1 | sclass ServeHttp_CookieHandler { |
2 | int days = 365; |
3 | S cookieName = "cookie"; |
4 | bool verbose; |
5 | S cookieValue; |
6 | S metaParams; // e.g. "SameSite=Strict" |
7 | bool shareCookiesBetweenApexDomains; |
8 | |
9 | // returns cookie (may be empty if client doesn't accept cookies) |
10 | S handle() { |
11 | NanoHTTPD.IHTTPSession session = NanoHTTPD.currentSession!; |
12 | if (session == null) null; |
13 | NanoHTTPD.CookieHandler cookies = session.getCookies(); |
14 | if (empty(cookieValue)) cookieValue = cookies.read(cookieName); |
15 | S existingValue = cookieValue; |
16 | var headers= session.getHeaders(); |
17 | if (empty(cookieValue) && empty((S) mapGet(headers, "x-no-cookies"))) { |
18 | cookieValue = createNewCookie(); |
19 | if (verbose) |
20 | print("New cookie."); |
21 | } |
22 | |
23 | if (verbose) |
24 | print("IP " + session.remoteIp() + ", cookie: " + cookieValue); |
25 | |
26 | if (!eq_nullIfEmpty(existingValue, cookieValue)) { |
27 | var cookie = cookies.set(cookieName, cookieValue + prependIfNempty("; ", metaParams), days); |
28 | |
29 | S apexDomain = apexDomain(dropPortFromHost(session.getHeaders().get("host"))); |
30 | if (shareCookiesBetweenApexDomains && nempty(apexDomain)) |
31 | cookie.domain = apexDomain; |
32 | } |
33 | |
34 | ret cookieValue; |
35 | } |
36 | |
37 | swappable S createNewCookie() { |
38 | ret randomID(20); |
39 | } |
40 | } |
Began life as a copy of #1011963
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1027732 |
Snippet name: | ServeHttp_CookieHandler |
Eternal ID of this version: | #1027732/14 |
Text MD5: | 184234e2d693f18db3fd78a781db62e1 |
Transpilation MD5: | 2a227370ef5a86a9e61f3dec23a542f1 |
Author: | stefan |
Category: | eleu / javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2023-06-12 20:11:41 |
Source code size: | 1324 bytes / 40 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 289 / 673 |
Version history: | 13 change(s) |
Referenced in: | [show references] |