Permalink
Browse files

Parsing POST request data.

  • Loading branch information...
1 parent 0da8411 commit e19a8b229d672c9033947f5ea9c1d3b05eee83ba @marshall007 marshall007 committed Jul 19, 2011
Showing with 22 additions and 24 deletions.
  1. +22 −24 src/com/sparkedia/valrix/netstats/NetServWorker.java
@@ -7,12 +7,10 @@
import com.sun.net.httpserver.HttpHandler;
import java.io.*;
-//import java.util.HashMap;
-//import java.util.Map;
public class NetServWorker implements HttpHandler {
private Netstats plugin;
- //private Gson jsonStr = new Gson();
+// private Gson jsonStr = new Gson();
public NetServWorker (Netstats plugin) {
this.plugin = plugin;
@@ -27,14 +25,20 @@ public void handle(HttpExchange exchange) throws IOException {
"For proper usage, please see the <a href=\"https://github.com/phyreman/Netstats\">documentation</a>.";
try {
Headers responseHeaders = exchange.getResponseHeaders();
- responseHeaders.set("Content-Type", "text/html; charset=UTF-8");
String method = exchange.getRequestMethod();
String path = exchange.getRequestURI().getPath();
- if (!path.startsWith("/data.json")) { responseStatus = 404; }
- else if (!method.startsWith("POST")) { responseStatus = 400; }
+ if (!path.startsWith("/data.json")) {
+ responseHeaders.set("Content-Type", "text/html; charset=UTF-8");
+ responseStatus = 404;
+ }
+ else if (!method.startsWith("POST")) {
+ responseHeaders.set("Content-Type", "text/html; charset=UTF-8");
+ responseStatus = 400;
+ }
else {
+ responseHeaders.set("Content-Type", "text/json; charset=UTF-8");
plugin.log.info("["+plugin.pName+"] Attempting to parse query string...");
// read the query string from the request body
String query;
@@ -49,8 +53,8 @@ public void handle(HttpExchange exchange) throws IOException {
} finally {
in.close();
}
- //content = buildResponse(query);
- content = query;
+ content = buildResponse(query);
+ plugin.log.info("["+plugin.pName+"]\n"+content);
}
plugin.log.info("["+plugin.pName+"] "+method+" on "+path);
plugin.log.info("["+plugin.pName+"] "+responseStatus);
@@ -59,34 +63,29 @@ public void handle(HttpExchange exchange) throws IOException {
e.printStackTrace();
}
try {
+ plugin.log.info("["+plugin.pName+"]\n"+content);
responseContent = content.getBytes("UTF-8");
exchange.sendResponseHeaders(responseStatus, responseContent.length);
response.write(responseContent);
+ response.flush();
} catch (Exception e) {
plugin.log.warning("["+plugin.pName+"] Error sending HTTP response.");
e.printStackTrace();
}
exchange.close();
}
- /*
+
private String buildResponse(String request) throws Exception {
- Map<String, String> arguments = new HashMap<String, String>();
- String ret = "Array (\n";
- for (String k : request.split("&"))
- for (String v : k.split("="))
- arguments.put(k, v);
-
- for (String k : arguments.keySet()) {
- ret += "\t[";
- ret += k;
- ret += "] => ";
- ret += arguments.get(k);
- ret += "\n";
+ //Map<String, String> arguments = new HashMap<String, String>();
+ String ret = "{";
+ for (String k : request.split("&")) {
+ String [] v = k.split("=");
+ ret += "\""+v[0]+"\": \""+v[1]+"\", ";
}
+ ret += "}";
- ret += ")";
-
+ plugin.log.info("["+plugin.pName+"]\n"+ret);
return ret;
// if (inputArgs.containsKey("player")) {
@@ -104,7 +103,6 @@ private String buildResponse(String request) throws Exception {
// "\n\tquit";
// }
}
- */
public String ping () {
return "pong";

0 comments on commit e19a8b2

Please sign in to comment.