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

43
LINES

< > BotCompany Repo | #1029492 // HTTP+HTTPS Server Include for DynModules [WebSocket version]

JavaX fragment (include)

1  
// needs a function O webServe(S uri, SS params) { ... }
2  
// and a function newWebSocket()
3  
4  
switchable int httpPort = 80;
5  
switchable int httpsPort = 443;
6  
transient autoDispose WebSocketHTTPD httpServer;
7  
transient autoDispose WebSocketHTTPD httpsServer;
8  
switchable bool redirectHttpToHttps;
9  
10  
transient new HTTPSpamBlocker1 spamBlocker;
11  
12  
void start_webServers(File keyStore, S passphrase) {
13  
  start_webServers(serveHttps_makeSSLSocketFactory(keyStore, passphrase));
14  
}
15  
16  
void start_webServers(SSLServerSocketFactory sslSocketFactory) ctex {
17  
  if (httpPort != 0) pcall {
18  
    WebSocketHTTPD _httpServer = new(httpPort, lambda1 newWebSocket);
19  
    _httpServer.enter = lambda0 enter;
20  
    _httpServer.serveFunction = func(S uri, SS parms) {
21  
      redirectHttpToHttps && !startsWith(uri, "/.well-known/")
22  
        ? serveRedirect("https://" + serveHttp_domainName()
23  
          + (httpsPort == 443 ? "" : ":" + httpsPort) + uri)
24  
        : webServe(uri, parms)
25  
    };
26  
    _httpServer.start();
27  
    print("HTTP server started on port " + _httpServer.getPort());
28  
    httpServer = _httpServer;
29  
  }
30  
  
31  
  if (httpsPort == 0 || sslSocketFactory == null) ret;
32  
  pcall {
33  
    WebSocketHTTPD _httpsServer = new(httpsPort, lambda1 newWebSocket);
34  
    _httpsServer.enter = lambda0 enter;
35  
    _httpsServer.makeSecure(sslSocketFactory);
36  
    _httpsServer.serveFunction = func(S uri, SS parms) { webServe(uri, parms) };
37  
    _httpsServer.start();
38  
    print("HTTPS server started on port " + _httpsServer.getPort());
39  
    httpsServer = _httpsServer;
40  
  }
41  
}
42  
43  
Cl<WebSocketHTTPD> webServers() { ret llNotNulls(httpServer, httpsServer); }

Author comment

Began life as a copy of #1027628

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: #1029492
Snippet name: HTTP+HTTPS Server Include for DynModules [WebSocket version]
Eternal ID of this version: #1029492/14
Text MD5: b3d22107ff9165928480c762a275b536
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-04-17 19:39:29
Source code size: 1638 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 293 / 835
Version history: 13 change(s)
Referenced in: [show references]