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

61
LINES

< > BotCompany Repo | #1002344 // Test makeSecure in NanoHTTPD (enabling HTTPS)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (2685L/21K/54K).

!759

!include #1001651 // class NanoHTTP

//static int port = 443; // standard https
static int port = 8765;
  
  static class MyHTTPD extends NanoHTTPD {
    public MyHTTPD(int port) {
      super(port);
    }
  
    public Response serve(String uri, Method method,
      Map<String, String> header, Map<String, String> parms,
      Map<String, String> files) {
      print("Serving URI: " + quote(uri));
      try {
        Response response = cast callOpt(getMainClass(), "serve", uri, method, header, parms, files);
        if (response != null) ret response;
        S html = callHtmlMethod(getMainClass(), uri);
        if (html != null)
          ret serveHTML(html);
        ret serve404();
      } catch (Throwable e) {
        e.printStackTrace();
        ret serveHTML("ERROR.");
      }
    }
  }

  static MyHTTPD serveHttp_server;

static SSLServerSocketFactory makeSSLSocketFactory(File keystoreFile, char[] passphrase) throws IOException {
        SSLServerSocketFactory res = null;
        try {
            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            InputStream keystoreStream = new FileInputStream(keystoreFile);
            keystore.load(keystoreStream, passphrase);
            // TODO: close keystoreStream??
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(keystore);
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyManagerFactory.init(keystore, passphrase);
            SSLContext ctx = SSLContext.getInstance("TLS");
            ctx.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
            res = ctx.getServerSocketFactory();
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
        return res;
    }

p {
  File keystoreFile = new File("/home/stefan/dev/keystore-ssl/keystore.jks");
  SSLServerSocketFactory factory = makeSSLSocketFactory(keystoreFile, args[0].toCharArray());
  
  serveHttp_server = new MyHTTPD(port);
  serveHttp_server.makeSecure(factory);
  serveHttp_server.start();
  System.out.println("HTTPS server started (listening on port " + port + "!)");
}

Author comment

Began life as a copy of #760

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002344
Snippet name: Test makeSecure in NanoHTTPD (enabling HTTPS)
Eternal ID of this version: #1002344/1
Text MD5: f20f780088c346c8e41d6312e872c2e1
Transpilation MD5: 7cccdd831543749ee827405e643b95d1
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-01-07 20:07:57
Source code size: 2388 bytes / 61 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1713 / 1497
Referenced in: [show references]