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

90
LINES

< > BotCompany Repo | #1013353 // WebSocketHTTPD - extends NanoWebSocketServer

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (13365L/82K).

1  
static new ThreadLocal<SS> WebSocketHTTPD_headers;
2  
static new ThreadLocal<WebSocketHTTPD> WebSocketHTTPD_current;
3  
4  
sclass WebSocketHTTPD extends NanoWebSocketServer {
5  
  *(int port, WebSocketFactory factory) {
6  
    this(port, (O) factory);
7  
  }
8  
  
9  
  *(S hostname, int port, WebSocketFactory factory) {
10  
    super(hostname, port, webSocketFactory(factory));
11  
    nanohttpd_socketTimeoutFix();
12  
  }
13  
    
14  
  *(int port, O factory) {
15  
    super(port, webSocketFactory(factory));
16  
    nanohttpd_socketTimeoutFix();
17  
  }
18  
19  
  // The following is all copied from #1011857 (replacing MyHTTPD* with WebSocketHTTPD*)
20  
  
21  
  O onServe, onEndServe;
22  
  volatile long requests;
23  
  transient IF0<AutoCloseable> enter;
24  
  //bool callMainClassServeFunction; // very old stuff
25  
  
26  
  O serveFunction = func(S uri, SS parms) {
27  
    callHtmlMethod2(getMainClass(), uri, parms)
28  
  };
29  
  
30  
  sclass Serving {
31  
    S uri;
32  
    SS header, parms, files;
33  
  }
34  
  
35  
  Set<Serving> currentlyServing = synchroSet();
36  
37  
  public Response serve(S uri, Method method,
38  
    SS header, SS parms, SS files) ctex {
39  
    temp callF(enter);
40  
    ++requests;
41  
    temp tempSetThreadLocal(WebSocketHTTPD_current, this);
42  
    Serving serving = nu(Serving.class, +uri, +header, +parms, +files);
43  
    currentlyServing.add(serving);
44  
    pcallF(onServe, serving);
45  
    try {
46  
      S remoteAddr = getClientIPFromHeaders(header);
47  
      S host = header.get("host");
48  
      print(formatDateAndTime() + " Serving URI: " + quote((nempty(host) ? "//" + host : "") + uri) + " to: " + remoteAddr);
49  
      
50  
      /*if (nempty(remoteAddr) &&
51  
        isTrue(callOpt(mc(), "isBlockedIP", remoteAddr))) {
52  
        print("BLOCKED IP.");
53  
        ret serve404();
54  
      }*/
55  
      
56  
      try {
57  
        /*if (callMainClassServeFunction) {
58  
          virtual Response originalResponse = callOpt(getMainClass(), "serve", uri, method, header, parms, files);
59  
          if (originalResponse != null && !originalResponse instanceof Response)
60  
            fail("Bad response type for " + uri + ": " + className(originalResponse));
61  
          Response response = cast originalResponse;
62  
          if (response != null) ret response;
63  
        }*/
64  
        
65  
        O html;
66  
        WebSocketHTTPD_headers.set(header);
67  
        try {
68  
          html = callF(serveFunction, uri, parms);
69  
        } finally {
70  
          WebSocketHTTPD_headers.set(null);
71  
        }
72  
        
73  
        if (html != null) {
74  
          if (html cast NanoHTTPD.Response)
75  
            ret html;
76  
          else
77  
            ret serveHTML(strOrNull(html));
78  
        }
79  
        
80  
        ret serve404();
81  
      } catch (Throwable e) {
82  
        printStackTrace(e);
83  
        ret serveHTML("ERROR.");
84  
      }
85  
    } finally {
86  
      pcallF(onEndServe, serving);
87  
      currentlyServing.remove(serving);
88  
    }
89  
  }
90  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1013353
Snippet name: WebSocketHTTPD - extends NanoWebSocketServer
Eternal ID of this version: #1013353/26
Text MD5: b6d1fb9ad8b34c9288853b2f334d5440
Transpilation MD5: 199049db9b27e62f28f3042baaf9865c
Author: stefan
Category: javax / http
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-16 22:26:07
Source code size: 2831 bytes / 90 lines
Pitched / IR pitched: No / No
Views / Downloads: 649 / 1352
Version history: 25 change(s)
Referenced in: [show references]