Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

64
LINES

< > BotCompany Repo | #1029493 // WebSocketHTTPD [backup before extensions]

JavaX fragment (include)

1  
static new ThreadLocal<SS> WebSocketHTTPD_headers;
2  
static new ThreadLocal<WebSocketHTTPD> WebSocketHTTPD_current;
3  
4  
sclass WebSocketHTTPD extends NanoWebSocketServer {
5  
  *(int port, O factory) {
6  
    super(port, webSocketFactory(factory));
7  
    nanohttpd_socketTimeoutFix();
8  
  }
9  
10  
  // The following is all copied from #1011857
11  
  
12  
  O onServe, onEndServe;
13  
  volatile long requests;
14  
  bool printServes = true;
15  
  
16  
  sclass Serving {
17  
    S uri;
18  
    SS header, parms, files;
19  
  }
20  
  
21  
  Set<Serving> currentlyServing = synchroSet();
22  
23  
  public Response serve(S uri, Method method,
24  
    SS header, SS parms, SS files) ctex {
25  
    ++requests;
26  
    temp tempSetThreadLocal(WebSocketHTTPD_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  
      if (printServes)
33  
        print(formatDateAndTime() + " Serving URI: " + quote(header.get("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  
        WebSocketHTTPD_headers.set(header);
46  
        try {
47  
          html = callHtmlMethod2(getMainClass(), uri, parms);
48  
        } finally {
49  
          WebSocketHTTPD_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  
}

Author comment

Began life as a copy of #1013353

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: #1029493
Snippet name: WebSocketHTTPD [backup before extensions]
Eternal ID of this version: #1029493/3
Text MD5: 04006a374e9d0018716fe7be1c9b5d73
Author: stefan
Category: javax / http
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-08-15 14:39:13
Source code size: 1967 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 207 / 236
Version history: 2 change(s)
Referenced in: [show references]