Uses 1658K of libraries. Click here for Pure Java version (40204L/267K).
1 | !7 |
2 | |
3 | concept Domain { |
4 | S domain; |
5 | S moduleLibID; |
6 | S proxyDestination; |
7 | S proxyCookieChecker; // a module lib ID |
8 | bool proxyRewritePort; |
9 | bool mustBeEnabled; |
10 | |
11 | toString { ret super.toString() + " " + domain; } |
12 | } |
13 | |
14 | // Eleu is so important, we don't use a shared lib |
15 | module Eleu3 > DynEleu { |
16 | transient CRUD<Domain> domainCRUD; |
17 | switchable bool verbose; |
18 | switchable bool debugNanoHTTPD; |
19 | switchable S sameSite; |
20 | |
21 | {} { enabled = false; } |
22 | |
23 | start { |
24 | NanoHTTPD_debug = debugNanoHTTPD; |
25 | bootstrapDBFrom(#1029618); |
26 | set flag defaultDefaultClassFinder_debug. |
27 | dbIndexingCI(Domain, 'domain); |
28 | assertNempty(list(Domain)); |
29 | uniqCI Domain(domain := "<default>"); |
30 | domainCRUD = new CRUD(Domain); |
31 | //dm_registerAs eleuWithCRUD(); |
32 | } |
33 | |
34 | visualize { |
35 | var c = jtabs( |
36 | "Main", super.visualize(), |
37 | "Domains" := domainCRUD.visualize()); |
38 | |
39 | tablePopupMenu(domainCRUD.table(), voidfunc(JPopupMenu menu, int row) { |
40 | Domain d = domainCRUD.selected(), ret if null; |
41 | if (nempty(d.moduleLibID)) |
42 | addMenuItem(menu, "Load module", rThreadEnter { dm_makeOrShow(d.moduleLibID) }); |
43 | }); |
44 | |
45 | ret c; |
46 | } |
47 | |
48 | @Override |
49 | S moduleForDomain(S domain) { |
50 | domain = dropAfterColon(domain); // drop port number |
51 | for (Domain d : domainsInMatchingOrder()) { |
52 | if (matchingDomain(d, domain)) { |
53 | S mod = findMod(d); |
54 | if (verbose) print("Domain matched: " + domain + " / " + d.domain + " => " + mod); |
55 | try answer mod; |
56 | } else |
57 | if (verbose) print("Domain not matched: " + domain + " / " + d.domain); |
58 | } |
59 | ret findMod(defaultDomain()); |
60 | } |
61 | |
62 | Domain defaultDomain() { |
63 | ret conceptWhereCI Domain(domain := "<default>"); |
64 | } |
65 | |
66 | S findMod(Domain d) { |
67 | if (d == null || empty(d.moduleLibID)) null; |
68 | ret d.mustBeEnabled |
69 | ? dm_findModuleWithParams(d.moduleLibID, enabled := true) |
70 | : dm_findModule(d.moduleLibID); |
71 | } |
72 | |
73 | ServeHttp_CookieHandler makeCookieHandler() { |
74 | ServeHttp_CookieHandler cookieHandler = super.makeCookieHandler(); |
75 | cookieHandler.metaParams = appendPrefixIfNempty("SameSite=", sameSite); |
76 | ret cookieHandler; |
77 | } |
78 | |
79 | void onWebServersStarted :: after { |
80 | forEach(webServers(), ws -> { |
81 | print("Augmenting web server: " + ws); |
82 | |
83 | ws.collectHeaderLines = true; |
84 | |
85 | // session is an instance of NanoHTTPD.IHTTPSession |
86 | ws.specialHandling = session -> ctex { |
87 | temp enter(); |
88 | S domain = dropPortFromHost(mapGet(session.getHeaders(), "host")); |
89 | |
90 | Domain d = first(domainsInMatchingOrder(), |
91 | _d -> matchingDomain(_d, domain)); |
92 | |
93 | printVars(+domain, +d); |
94 | if (d == null || empty(d.proxyDestination)) false; |
95 | print("Handling proxy request to " + d.proxyDestination); |
96 | HostAndPort hap = new(d.proxyDestination); |
97 | |
98 | if (nempty(d.proxyCookieChecker)) { |
99 | S cookieChecker = dm_findModule(d.proxyCookieChecker); |
100 | if (empty(cookieChecker)) |
101 | fail(print("Cookie checker " + d.proxyCookieChecker + " not found, aborting request")); |
102 | |
103 | NanoHTTPD.CookieHandler cookies = session.getCookies(); |
104 | |
105 | if (!isTrue(dm_call(cookieChecker, "checkCookie", cookies.read("cookie"), domain))) |
106 | fail(print("Cookie checker disapproves")); |
107 | else |
108 | print("Cookie checker approves. URI: " + session.getUri()); |
109 | } |
110 | |
111 | new HandleProxyRequest hpr; |
112 | hpr.hap = hap; |
113 | hpr.session = session; |
114 | hpr.rewritePort = d.proxyRewritePort; |
115 | |
116 | hpr.customizeRequest = proxyRequest -> { |
117 | proxyRequest.setOut = out -> { |
118 | proxyRequest.setOut_base(out); |
119 | proxyRequest.outProxied = out; // mod here |
120 | }; |
121 | |
122 | proxyRequest.setOutOut = outOut -> { |
123 | proxyRequest.setOutOut_base(outOut); |
124 | proxyRequest.outOutProxied = outOut; // mod here |
125 | }; |
126 | }; |
127 | |
128 | vmBus_send handlingProxyRequest(me(), hpr); |
129 | |
130 | ret true with hpr.run(); |
131 | }; |
132 | }); |
133 | } // onWebServersStarted |
134 | |
135 | bool matchingDomain(Domain d, S domain) { |
136 | ret domainIsUnder_extended(domain, d.domain); |
137 | } |
138 | |
139 | class WebRequest2 extends WebRequest { |
140 | *(NanoHTTPD.IHTTPSession httpSession, S uri, SS params) { |
141 | super(httpSession, uri, params); |
142 | } |
143 | |
144 | // TODO: change uri etc |
145 | /*public NanoHTTPD.Response proxy(S url, bool rewriteHost, bool rewritePort) { |
146 | url = dropPrefix("http://", url); |
147 | if (startsWith(url, "https://")) |
148 | new HandleProxyRequest hpr; |
149 | hpr.hap = hap; |
150 | hpr.session = httpSession; |
151 | hpr.rewriteHost = rewriteHost; |
152 | hpr.rewritePort = rewritePort; |
153 | hpr.run(); |
154 | null; |
155 | }*/ |
156 | } |
157 | |
158 | @Override |
159 | WebRequest createWebRequest(NanoHTTPD.IHTTPSession httpSession, |
160 | S uri, SS params) { |
161 | ret new WebRequest2(httpSession, uri, params); |
162 | } |
163 | |
164 | // API |
165 | |
166 | Cl<Domain> domainsInMatchingOrder() { |
167 | ret sortByCalculatedFieldICDesc( |
168 | filter(list(Domain), d -> !eqic(d.domain, "<default>")), |
169 | d -> reversed(d.domain)); |
170 | } |
171 | |
172 | void addDomain(S domain, S moduleLibID, bool mustBeEnabled) { |
173 | uniq Domain(+domain, +moduleLibID, +mustBeEnabled); |
174 | } |
175 | } |
Began life as a copy of #1029618
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): bhatertpkbcr, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034503 |
Snippet name: | Eleu with CRUD [fixing bug] |
Eternal ID of this version: | #1034503/6 |
Text MD5: | b737f95889deb962746581355e0520b1 |
Transpilation MD5: | 2a8bc0cac9a054c4c1d310b285a65786 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-02-10 01:02:21 |
Source code size: | 5419 bytes / 175 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 160 / 283 |
Version history: | 5 change(s) |
Referenced in: | [show references] |