Mercurial Hosting > luan
changeset 1141:14927c34cbb7
webserver - add examples/Headers
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 30 Jan 2018 00:55:17 -0700 |
parents | bf03d687eaff |
children | 0f59eab45f3d |
files | src/luan/webserver/examples/Example.java src/luan/webserver/examples/Headers.java |
diffstat | 2 files changed, 33 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/webserver/examples/Example.java Mon Jan 29 22:21:59 2018 -0700 +++ b/src/luan/webserver/examples/Example.java Tue Jan 30 00:55:17 2018 -0700 @@ -39,10 +39,10 @@ } public static void fancy() throws IOException { - Handler handler = new Example(); Map<String,Handler> map = new HashMap<String,Handler>(); - map.put("/hello",handler); - handler = new MapHandler(map); + map.put( "/hello", new Example() ); + map.put( "/headers", new Headers() ); + Handler handler = new MapHandler(map); handler = new ListHandler( handler, new FileHandler() ); handler = new SafeHandler(handler); handler = new LogHandler(handler);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/webserver/examples/Headers.java Tue Jan 30 00:55:17 2018 -0700 @@ -0,0 +1,30 @@ +package luan.webserver.examples; + +import java.io.Writer; +import java.io.OutputStreamWriter; +import java.io.IOException; +import java.util.Map; +import luan.webserver.Handler; +import luan.webserver.Request; +import luan.webserver.Response; +import luan.webserver.ResponseOutputStream; + + +public final class Headers implements Handler { + + public Response handle(Request request) { + Response response = new Response(); + response.headers.put( "Content-Type", "text/plain; charset=UTF-8" ); + try { + Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) ); + for( Map.Entry<String,String> entry : request.headers.entrySet() ) { + writer.write(entry.getKey()+": "+entry.getValue()+"\n"); + } + writer.close(); + } catch(IOException e) { + throw new RuntimeException(e); + } + return response; + } + +}