Home | History | Annotate | Download | only in debug
      1 package fi.iki.elonen.debug;
      2 
      3 import fi.iki.elonen.NanoHTTPD;
      4 import fi.iki.elonen.ServerRunner;
      5 
      6 import java.util.HashMap;
      7 import java.util.List;
      8 import java.util.Map;
      9 
     10 public class DebugServer extends NanoHTTPD {
     11     public DebugServer() {
     12         super(8080);
     13     }
     14 
     15     public static void main(String[] args) {
     16         ServerRunner.run(DebugServer.class);
     17     }
     18 
     19     @Override public Response serve(IHTTPSession session) {
     20         Map<String, List<String>> decodedQueryParameters =
     21             decodeParameters(session.getQueryParameterString());
     22 
     23         StringBuilder sb = new StringBuilder();
     24         sb.append("<html>");
     25         sb.append("<head><title>Debug Server</title></head>");
     26         sb.append("<body>");
     27         sb.append("<h1>Debug Server</h1>");
     28 
     29         sb.append("<p><blockquote><b>URI</b> = ").append(
     30             String.valueOf(session.getUri())).append("<br />");
     31 
     32         sb.append("<b>Method</b> = ").append(
     33             String.valueOf(session.getMethod())).append("</blockquote></p>");
     34 
     35         sb.append("<h3>Headers</h3><p><blockquote>").
     36             append(toString(session.getHeaders())).append("</blockquote></p>");
     37 
     38         sb.append("<h3>Parms</h3><p><blockquote>").
     39             append(toString(session.getParms())).append("</blockquote></p>");
     40 
     41         sb.append("<h3>Parms (multi values?)</h3><p><blockquote>").
     42             append(toString(decodedQueryParameters)).append("</blockquote></p>");
     43 
     44         try {
     45             Map<String, String> files = new HashMap<String, String>();
     46             session.parseBody(files);
     47             sb.append("<h3>Files</h3><p><blockquote>").
     48                 append(toString(files)).append("</blockquote></p>");
     49         } catch (Exception e) {
     50             e.printStackTrace();
     51         }
     52 
     53         sb.append("</body>");
     54         sb.append("</html>");
     55         return new Response(sb.toString());
     56     }
     57 
     58     private String toString(Map<String, ? extends Object> map) {
     59         if (map.size() == 0) {
     60             return "";
     61         }
     62         return unsortedList(map);
     63     }
     64 
     65     private String unsortedList(Map<String, ? extends Object> map) {
     66         StringBuilder sb = new StringBuilder();
     67         sb.append("<ul>");
     68         for (Map.Entry entry : map.entrySet()) {
     69             listItem(sb, entry);
     70         }
     71         sb.append("</ul>");
     72         return sb.toString();
     73     }
     74 
     75     private void listItem(StringBuilder sb, Map.Entry entry) {
     76         sb.append("<li><code><b>").append(entry.getKey()).
     77             append("</b> = ").append(entry.getValue()).append("</code></li>");
     78     }
     79 }
     80