Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3872100
commit d4bcf60
Showing
4 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package duckutil.webserver; | ||
|
||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import com.sun.net.httpserver.HttpServer; | ||
import duckutil.TaskMaster; | ||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.util.logging.Logger; | ||
|
||
public class DuckWebServer | ||
{ | ||
private static final Logger logger = Logger.getLogger("duckutil.webserver"); | ||
private final HttpServer http_server; | ||
private final WebHandler handler; | ||
|
||
public DuckWebServer(String web_host, int web_port, WebHandler handler, int threads) | ||
throws IOException | ||
{ | ||
InetSocketAddress listen = new InetSocketAddress(web_port); | ||
if (web_host != null) | ||
{ | ||
listen = new InetSocketAddress(web_host, web_port); | ||
} | ||
|
||
this.handler = handler; | ||
|
||
http_server = HttpServer.create(listen, 16); | ||
http_server.createContext("/", new RootHandler()); | ||
http_server.setExecutor(TaskMaster.getBasicExecutor(threads,"web_server")); | ||
http_server.start(); | ||
} | ||
|
||
public class RootHandler implements HttpHandler | ||
{ | ||
public void handle(HttpExchange t) throws IOException | ||
{ | ||
try(WebContext ctx = new WebContext(t)) | ||
{ | ||
try | ||
{ | ||
handler.handle(ctx); | ||
} | ||
catch(Exception e) | ||
{ | ||
ctx.setException(e); | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package duckutil.webserver; | ||
|
||
import com.sun.net.httpserver.HttpExchange; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.PrintStream; | ||
import java.net.URI; | ||
import java.util.logging.Logger; | ||
|
||
public class WebContext implements AutoCloseable | ||
{ | ||
private static final Logger logger = Logger.getLogger("duckutil.webserver"); | ||
|
||
private final HttpExchange http_exchange; | ||
|
||
private int http_code=500; | ||
private String content_type="text/plain"; | ||
private int output_size = -1; | ||
|
||
private ByteArrayOutputStream output_buffer = new ByteArrayOutputStream(); | ||
private PrintStream print_out = new PrintStream(output_buffer); | ||
|
||
private boolean out_written; | ||
|
||
// Things that needs to be set | ||
// size | ||
// content-type | ||
// http code | ||
|
||
public WebContext(HttpExchange t) | ||
{ | ||
this.http_exchange = t; | ||
logger.info("Request - " + getURI()); | ||
|
||
} | ||
|
||
public URI getURI() | ||
{ | ||
return http_exchange.getRequestURI(); | ||
} | ||
public String getHost() | ||
{ | ||
return http_exchange.getRequestHeaders().get("Host").get(0); | ||
} | ||
|
||
public InputStream getRequestBody() { return http_exchange.getRequestBody(); } | ||
|
||
public void setHttpCode(int code) { http_code = code; } | ||
public void setContentType(String type) { content_type = type; } | ||
public void setOutputSize(int size) { output_size = size; } | ||
|
||
public void setException(Throwable t) | ||
{ | ||
setHttpCode(500); | ||
setContentType("text/plain"); | ||
out().println(t); | ||
|
||
} | ||
|
||
public PrintStream out(){return print_out; } | ||
|
||
public void writeHeaders() | ||
throws java.io.IOException | ||
{ | ||
if (output_size < 0) | ||
{ | ||
output_size = output_buffer.toByteArray().length; | ||
} | ||
|
||
logger.info(String.format("HTTP result: %s code:%d sz:%d", content_type, http_code, output_size)); | ||
http_exchange.getResponseHeaders().add("Content-type",content_type); | ||
http_exchange.sendResponseHeaders( http_code, output_size); | ||
|
||
} | ||
|
||
/** | ||
* If this is used, do setOutputSize() then writeHeaders() first | ||
*/ | ||
public OutputStream getOutStream() | ||
{ | ||
out_written=true; | ||
return http_exchange.getResponseBody(); | ||
} | ||
|
||
public void close() | ||
{ | ||
try | ||
{ | ||
if (!out_written) | ||
{ | ||
writeHeaders(); | ||
byte[] data = output_buffer.toByteArray(); | ||
OutputStream out = http_exchange.getResponseBody(); | ||
out.write(data); | ||
out.close(); | ||
out_written = true; | ||
} | ||
} | ||
catch(java.io.IOException e) | ||
{ | ||
logger.info(e.toString()); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package duckutil.webserver; | ||
|
||
public interface WebHandler | ||
{ | ||
public void handle(WebContext t) throws Exception; | ||
|
||
} | ||
|
||
|