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).

static new ThreadLocal<SS> WebSocketHTTPD_headers;
static new ThreadLocal<WebSocketHTTPD> WebSocketHTTPD_current;

sclass WebSocketHTTPD extends NanoWebSocketServer {
  *(int port, WebSocketFactory factory) {
    this(port, (O) factory);
  }
  
  *(S hostname, int port, WebSocketFactory factory) {
    super(hostname, port, webSocketFactory(factory));
    nanohttpd_socketTimeoutFix();
  }
    
  *(int port, O factory) {
    super(port, webSocketFactory(factory));
    nanohttpd_socketTimeoutFix();
  }

  // The following is all copied from #1011857 (replacing MyHTTPD* with WebSocketHTTPD*)
  
  O onServe, onEndServe;
  volatile long requests;
  transient IF0<AutoCloseable> enter;
  //bool callMainClassServeFunction; // very old stuff
  
  O serveFunction = func(S uri, SS parms) {
    callHtmlMethod2(getMainClass(), uri, parms)
  };
  
  sclass Serving {
    S uri;
    SS header, parms, files;
  }
  
  Set<Serving> currentlyServing = synchroSet();

  public Response serve(S uri, Method method,
    SS header, SS parms, SS files) ctex {
    temp callF(enter);
    ++requests;
    temp tempSetThreadLocal(WebSocketHTTPD_current, this);
    Serving serving = nu(Serving.class, +uri, +header, +parms, +files);
    currentlyServing.add(serving);
    pcallF(onServe, serving);
    try {
      S remoteAddr = getClientIPFromHeaders(header);
      S host = header.get("host");
      print(formatDateAndTime() + " Serving URI: " + quote((nempty(host) ? "//" + host : "") + uri) + " to: " + remoteAddr);
      
      /*if (nempty(remoteAddr) &&
        isTrue(callOpt(mc(), "isBlockedIP", remoteAddr))) {
        print("BLOCKED IP.");
        ret serve404();
      }*/
      
      try {
        /*if (callMainClassServeFunction) {
          virtual Response originalResponse = callOpt(getMainClass(), "serve", uri, method, header, parms, files);
          if (originalResponse != null && !originalResponse instanceof Response)
            fail("Bad response type for " + uri + ": " + className(originalResponse));
          Response response = cast originalResponse;
          if (response != null) ret response;
        }*/
        
        O html;
        WebSocketHTTPD_headers.set(header);
        try {
          html = callF(serveFunction, uri, parms);
        } finally {
          WebSocketHTTPD_headers.set(null);
        }
        
        if (html != null) {
          if (html cast NanoHTTPD.Response)
            ret html;
          else
            ret serveHTML(strOrNull(html));
        }
        
        ret serve404();
      } catch (Throwable e) {
        printStackTrace(e);
        ret serveHTML("ERROR.");
      }
    } finally {
      pcallF(onEndServe, serving);
      currentlyServing.remove(serving);
    }
  }
}

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: 650 / 1352
Version history: 25 change(s)
Referenced in: #1029493 - WebSocketHTTPD [backup before extensions]
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)