// Copyright 2014, Kevin Ko <kevin@faveset.com>

package com.faveset.khttpserver;

import java.io.IOException;
import sun.misc.Signal;
import sun.misc.SignalHandler;

import com.faveset.khttp.HttpHandler;
import com.faveset.khttp.HttpRequest;
import com.faveset.khttp.HttpResponseWriter;

import com.faveset.flags.BoolFlag;
import com.faveset.flags.Flags;
import com.faveset.log.OutputStreamLog;

public class HttpServer {
    private static BoolFlag mLogFlag =
        Flags.registerBool("log", false, "enable logging to stdout");

    public static void main(String[] args) throws IOException, IllegalArgumentException {
        Flags.parse(args);

        Flags flags = Flags.get();
        if (flags.getArgSize() < 1) {
            StringBuilder builder = new StringBuilder();
            builder.append("<port>\n");
            flags.writeHelp(builder);

            System.out.println(builder.toString());
            return;
        }

        int port = new Integer(flags.getArg(0));

        final com.faveset.khttp.HttpServer server = new com.faveset.khttp.HttpServer();

        OutputStreamLog log = null;
        if (mLogFlag.get()) {
            System.out.println("logging to stdout");
            log = new OutputStreamLog(System.out);
            server.setLog(log);
        }

        // Set up the signal handler for exiting the server.
        Signal.handle(new Signal("INT"), new SignalHandler() {
            @Override
            public void handle(Signal sig) {
                server.stop();
            }
        });

        // Register handlers.

        server.registerHandler("/", new HttpHandler() {
            public void onRequest(HttpRequest req, HttpResponseWriter w) {
                w.getHeadersBuilder().set("Content-Type", "text/html");
                w.write("<html><body>Hello</body></html>");
            }
        });
        server.listenAndServe("::", port);

        if (log != null) {
            log.close();
        }
    }
}

@@ -104,16 +104,198 @@
 eption;%0A
+import java.security.KeyManagementException;%0Aimport java.security.KeyStoreException;%0Aimport java.security.UnrecoverableKeyException;%0Aimport java.security.cert.CertificateException;%0A%0A
 import s
@@ -310,16 +310,16 @@
 Signal;%0A
-
 import s
@@ -531,16 +531,53 @@
 .Flags;%0A
+import com.faveset.flags.StringFlag;%0A
 import c
@@ -663,17 +663,17 @@
 oolFlag 
-m
+s
 LogFlag 
@@ -741,24 +741,366 @@
  stdout%22);%0A%0A
+    private static StringFlag sKeyStoreFlag = Flags.registerString(%22keystore%22, %22%22, %22keystore file%22);%0A%0A    private static StringFlag sTrustStoreFlag =%0A        Flags.registerString(%22truststore%22, %22%22, %22truststore file%22);%0A%0A    private static StringFlag sKeyPassFlag =%0A        Flags.registerString(%22keypass%22, %22%22, %22password for private key/cert%22);%0A%0A
     public s
@@ -1136,16 +1136,38 @@
 ) throws
+ CertificateException,
  IOExcep
@@ -1171,16 +1171,27 @@
 ception,
+%0A          
  Illegal
@@ -1207,16 +1207,86 @@
 xception
+, KeyManagementException, KeyStoreException, UnrecoverableKeyException
  %7B%0A     
@@ -1787,17 +1787,17 @@
     if (
-m
+s
 LogFlag.
@@ -1941,16 +1941,16 @@
 g(log);%0A
-
         
@@ -1944,32 +1944,382 @@
 og);%0A        %7D%0A%0A
+        if (!sKeyStoreFlag.get().isEmpty() &&%0A                !sTrustStoreFlag.get().isEmpty()) %7B%0A            System.out.println(%22using keystore (%22 + sKeyStoreFlag.get() + %22) truststore (%22 +%0A                    sTrustStoreFlag.get() + %22)%22);%0A            server.configureSSL(sKeyStoreFlag.get(), sTrustStoreFlag.get(), sKeyPassFlag.get());%0A        %7D%0A%0A%0A
         // Set u

Add keystore, truststore, and keypass flags for enabling SSL support.