Libraryless. Click here for Pure Java version (5335L/38K).
1 | static new ThreadLocal<SS> MyHTTPD_headers; |
2 | static new ThreadLocal<MyHTTPD> MyHTTPD_current; |
3 | |
4 | sclass MyHTTPD extends NanoHTTPD { |
5 | *(int port) { super(port); } |
6 | |
7 | O onServe, onEndServe; |
8 | volatile long requests; |
9 | transient IF0<AutoCloseable> enter; |
10 | |
11 | O serveFunction = func(S uri, SS parms) { |
12 | callHtmlMethod2(getMainClass(), uri, parms) |
13 | }; |
14 | |
15 | sclass Serving { |
16 | S uri; |
17 | SS header, parms, files; |
18 | } |
19 | |
20 | Set<Serving> currentlyServing = synchroSet(); |
21 | |
22 | public Response serve(S uri, Method method, |
23 | SS header, SS parms, SS files) ctex { |
24 | temp callF(enter); |
25 | ++requests; |
26 | temp tempSetThreadLocal(MyHTTPD_current, this); |
27 | Serving serving = nu(Serving.class, +uri, +header, +parms, +files); |
28 | currentlyServing.add(serving); |
29 | pcallF(onServe, serving); |
30 | try { |
31 | S remoteAddr = getClientIPFromHeaders(header); |
32 | S host = header.get("host"); |
33 | print(formatDateAndTime() + " Serving URI: " + quote((nempty(host) ? "//" + host : "") + uri) + " to: " + remoteAddr); |
34 | |
35 | /*if (nempty(remoteAddr) && |
36 | isTrue(callOpt(mc(), "isBlockedIP", remoteAddr))) { |
37 | print("BLOCKED IP."); |
38 | ret serve404(); |
39 | }*/ |
40 | |
41 | try { |
42 | Response response = cast callOpt(getMainClass(), "serve", uri, method, header, parms, files); |
43 | if (response != null) ret response; |
44 | O html; |
45 | MyHTTPD_headers.set(header); |
46 | try { |
47 | html = callF(serveFunction, uri, parms); |
48 | } finally { |
49 | MyHTTPD_headers.set(null); |
50 | } |
51 | |
52 | if (html != null) |
53 | ret html instanceof S ? serveHTML((S) html) : (NanoHTTPD.Response) html; |
54 | ret serve404(); |
55 | } catch (Throwable e) { |
56 | printStackTrace(e); |
57 | ret serveHTML("ERROR."); |
58 | } |
59 | } finally { |
60 | pcallF(onEndServe, serving); |
61 | currentlyServing.remove(serving); |
62 | } |
63 | } |
64 | } |
Began life as a copy of #1001648
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1011857 |
Snippet name: | MyHTTPD |
Eternal ID of this version: | #1011857/15 |
Text MD5: | 55c5079c6364aff621efe1cb3933b149 |
Transpilation MD5: | 32eaccd9d72bdaf6ea3ff4f514e833d5 |
Author: | stefan |
Category: | javax / networking |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-08-15 14:12:28 |
Source code size: | 1930 bytes / 64 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 631 / 1756 |
Version history: | 14 change(s) |
Referenced in: | [show references] |