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

136
LINES

< > BotCompany Repo | #1032719 // Eleu/CRUD/domain-based v2 - main web server module [moved to #1029618]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 2770K of libraries. Compilation Failed (26044L/142K).

!7

concept Domain {
  S domain;
  S moduleLibID;
  S proxyDestination;
  S proxyCookieChecker; // a module lib ID
  bool mustBeEnabled;
  
  toString { ret super.toString() + " " + domain; }
}

cm Eleu3 > DynEleu {
  transient CRUD<Domain> domainCRUD;
  switchable bool verbose;
  switchable bool debugNanoHTTPD;
  switchable S sameSite;
  
  start {
    NanoHTTPD_debug = debugNanoHTTPD;
    useDBOf(#1029618); // NOT WORKING?!
    dbIndexingCI(Domain, 'domain);
    uniqCI Domain(domain := "<default>");
    domainCRUD = new CRUD(Domain);
    dm_registerAs eleuWithCRUD();
  }
  
  visualize {
    var c = jtabs(
     "Main", super.visualize(),
      "Domains" := domainCRUD.visualize());
      
    tablePopupMenu(domainCRUD.table(), voidfunc(JPopupMenu menu, int row) {
      Domain d = domainCRUD.selected(), ret if null;
      if (nempty(d.moduleLibID))
        addMenuItem(menu, "Load module", rThreadEnter { dm_makeOrShow(d.moduleLibID) });
    });
    
    ret c;
  }
  
  @Override 
  S moduleForDomain(S domain) {
    domain = dropAfterColon(domain); // drop port number
    for (Domain d : domainsInMatchingOrder()) {
      if (matchingDomain(d, domain)) {
        S mod = findMod(d);
        if (verbose) print("Domain matched: " + domain + " / " + d.domain + " => " + mod); 
        try answer mod;
      } else
        if (verbose) print("Domain not matched: " + domain + " / " + d.domain); 
    }
    ret findMod(defaultDomain());
  }
  
  Domain defaultDomain() {
    ret conceptWhereCI Domain(domain := "<default>");
  }
  
  S findMod(Domain d) {
    if (d == null || empty(d.moduleLibID)) null;
    ret d.mustBeEnabled
      ? dm_findModuleWithParams(d.moduleLibID, enabled := true)
      : dm_findModule(d.moduleLibID);
  }

  ServeHttp_CookieHandler makeCookieHandler() {
    ServeHttp_CookieHandler cookieHandler = super.makeCookieHandler();
    cookieHandler.metaParams = appendPrefixIfNempty("SameSite=", sameSite);
    ret cookieHandler;
  }
  
  void onWebServersStarted :: after {
    forEach(webServers(), ws -> {
      print("Augmenting web server: " + ws);
      
      ws.collectHeaderLines = true;
      
      // session is an instance of NanoHTTPD.IHTTPSession
      ws.specialHandling = session -> ctex {
        temp enter();
        S domain = dropPortFromHost(mapGet(session.getHeaders(), "host"));

        Domain d = first(domainsInMatchingOrder(), 
          _d -> matchingDomain(_d, domain));
          
        printVars(+domain, +d);
        if (d == null || empty(d.proxyDestination)) false;
        print("Handling proxy request to " + d.proxyDestination);
        HostAndPort hap = new(d.proxyDestination);
        
        if (nempty(d.proxyCookieChecker)) {
          S cookieChecker = dm_findModule(d.proxyCookieChecker);
          if (empty(cookieChecker))
            fail(print("Cookie checker " + d.proxyCookieChecker + " not found, aborting request"));
            
          NanoHTTPD.CookieHandler cookies = session.getCookies();
          
          if (!isTrue(dm_call(cookieChecker, "checkCookie", cookies.read("cookie"), domain)))
            fail(print("Cookie checker disapproves"));
        }
        
        S headers = windowsLineBreaks(lines(concatLists(
          ll(session.getMethod() + " " + session.getFullURI() + " " + session.getProtocolVersion()),
          session.getHeaderLines()
        )));
        
        new StandaloneHttpProxy proxy;
        proxy.rewriteHostHeader = false;
        proxy.forwardServerAndPort = req -> hap;
        proxy.takeOverIncomingSocket(session.getSocket(),
          session.getInputStream(),
          session.getOutputStream(),
          toUtf8(printQuoted("HEADERS: ", headers))
        );
        true;
      };
    });
  } // onWebServersStarted
  
  bool matchingDomain(Domain d, S domain) {
    ret domainIsUnder_extended(domain, d.domain);
  }

  // API
  
  Cl<Domain> domainsInMatchingOrder() {
    ret sortByCalculatedFieldICDesc(
      filter(list(Domain), d -> !eqic(d.domain, "<default>")),
      d -> reversed(d.domain));
  }
  
  void addDomain(S domain, S moduleLibID, bool mustBeEnabled) {
    uniq Domain(+domain, +moduleLibID, +mustBeEnabled);
  }
}

Author comment

Began life as a copy of #1029618

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

-
Snippet ID: #1032719
Snippet name: Eleu/CRUD/domain-based v2 - main web server module [moved to #1029618]
Eternal ID of this version: #1032719/29
Text MD5: 0747e347f1af84733e8d3e693625af1b
Transpilation MD5: de25fd6911d3316baaa2b15c61df0d42
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-03 23:15:08
Source code size: 4317 bytes / 136 lines
Pitched / IR pitched: No / No
Views / Downloads: 251 / 600
Version history: 28 change(s)
Referenced in: