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)

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

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

  // The following is all copied from #1011857
  
  O onServe, onEndServe;
  volatile long requests;
  bool printServes = true;
  
  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 {
    ++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);
      if (printServes)
        print(formatDateAndTime() + " Serving URI: " + quote(header.get("host") + uri) + " to: " + remoteAddr);
      
      /*if (nempty(remoteAddr) &&
        isTrue(callOpt(mc(), "isBlockedIP", remoteAddr))) {
        print("BLOCKED IP.");
        ret serve404();
      }*/
      
      try {
        Response response = cast callOpt(getMainClass(), "serve", uri, method, header, parms, files);
        if (response != null) ret response;
        O html;
        WebSocketHTTPD_headers.set(header);
        try {
          html = callHtmlMethod2(getMainClass(), uri, parms);
        } finally {
          WebSocketHTTPD_headers.set(null);
        }
        
        if (html != null)
          ret html instanceof S ? serveHTML((S) html) : (NanoHTTPD.Response) html;
        ret serve404();
      } catch (Throwable e) {
        printStackTrace(e);
        ret serveHTML("ERROR.");
      }
    } finally {
      pcallF(onEndServe, serving);
      currentlyServing.remove(serving);
    }
  }
}

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: 208 / 237
Version history: 2 change(s)
Referenced in: