Created
March 27, 2016 00:20
-
-
Save johnllao/232d200aa3674959fab0 to your computer and use it in GitHub Desktop.
Quick HTTP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package quickhttp; | |
import com.sun.net.httpserver.HttpContext; | |
public interface QuickHandler { | |
byte[] handle(final HttpContext ctx); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package quickhttp; | |
import com.sun.net.httpserver.HttpContext; | |
public class QuickHost { | |
public static void main(String[] args) { | |
System.out.println("Quick HTTP (version 1.0"); | |
final QuickServer server = QuickServer.create(9998); | |
server.register("/quickhttp/help/", "text/plain", new HelpHandler()) | |
.start(); | |
} | |
static class HelpHandler implements QuickHandler { | |
public byte[] handle(HttpContext ctx) { | |
String output = ""; | |
final long mb = 1024 * 1024; | |
final Runtime runtime = Runtime.getRuntime(); | |
output += String.format("free memory: %s MB\n", runtime.freeMemory() / mb); | |
output += String.format("max memory: %s MB\n", runtime.maxMemory() / mb); | |
output += String.format("total memory: %s MB\n", runtime.totalMemory() / mb); | |
return output.getBytes(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package quickhttp; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.net.InetSocketAddress; | |
import com.sun.net.httpserver.Headers; | |
import com.sun.net.httpserver.HttpExchange; | |
import com.sun.net.httpserver.HttpHandler; | |
import com.sun.net.httpserver.HttpServer; | |
public class QuickServer { | |
private HttpServer server = null; | |
public static QuickServer create(final int port) { | |
return new QuickServer(port); | |
} | |
private QuickServer(final int port) { | |
final InetSocketAddress address = new InetSocketAddress(port); | |
try { | |
this.server = HttpServer.create(address, 0); | |
} | |
catch (IOException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
public QuickServer register(final String path, final String contentType, final QuickHandler handler) { | |
this.server.createContext(path, new HttpHandler() { | |
public void handle(HttpExchange ex) throws IOException { | |
byte[] response = handler.handle(ex.getHttpContext()); | |
final Headers headers = ex.getResponseHeaders(); | |
headers.add("Content-type", contentType); | |
ex.sendResponseHeaders(200, response.length); | |
final OutputStream output = ex.getResponseBody(); | |
output.write(response); | |
output.flush(); | |
output.close(); | |
} | |
}); | |
return this; | |
} | |
public QuickServer start() { | |
this.server.setExecutor(null); | |
this.server.start(); | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment