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

64
LINES

< > BotCompany Repo | #1011857 - MyHTTPD

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

Libraryless. Click here for Pure Java version (5335L/38K).

static new ThreadLocal<SS> MyHTTPD_headers;
static new ThreadLocal<MyHTTPD> MyHTTPD_current;

sclass MyHTTPD extends NanoHTTPD {
  *(int port) { super(port); }
  
  O onServe, onEndServe;
  volatile long requests;
  transient IF0<AutoCloseable> enter;
  
  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(MyHTTPD_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 {
        Response response = cast callOpt(getMainClass(), "serve", uri, method, header, parms, files);
        if (response != null) ret response;
        O html;
        MyHTTPD_headers.set(header);
        try {
          html = callF(serveFunction, uri, parms);
        } finally {
          MyHTTPD_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 #1001648

download  show line numbers  debug dex   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, 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: 369 / 1217
Version history: 14 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1011857 & http://1011857.tinybrain.de