Merge pull request #17 from EvilDrW/master
accelerate parsing request body
This commit is contained in:
commit
2438e7a0fb
@ -16,8 +16,9 @@ import java.io.InputStream;
|
|||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Scanner;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class NanoHTTPDWebserver extends NanoHTTPD {
|
public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||||
|
|
||||||
@ -28,9 +29,20 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
|
|||||||
this.webserver = webserver;
|
this.webserver = webserver;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String inputStreamToString(InputStream inputStream) {
|
private String getBodyText(IHTTPSession session) {
|
||||||
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
|
Map<String, String> files = new HashMap<String, String>();
|
||||||
return s.hasNext() ? s.next() : "";
|
Method method = session.getMethod();
|
||||||
|
if (Method.PUT.equals(method) || Method.POST.equals(method)) {
|
||||||
|
try {
|
||||||
|
session.parseBody(files);
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
return "{}";
|
||||||
|
} catch (ResponseException re) {
|
||||||
|
return "{}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// get the POST body
|
||||||
|
return files.get("postData");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -51,7 +63,7 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
|
|||||||
private JSONObject createJSONRequest(String requestId, IHTTPSession session) throws JSONException {
|
private JSONObject createJSONRequest(String requestId, IHTTPSession session) throws JSONException {
|
||||||
JSONObject jsonRequest = new JSONObject();
|
JSONObject jsonRequest = new JSONObject();
|
||||||
jsonRequest.put("requestId", requestId);
|
jsonRequest.put("requestId", requestId);
|
||||||
jsonRequest.put("body", this.inputStreamToString(session.getInputStream()));
|
jsonRequest.put("body", this.getBodyText(session));
|
||||||
jsonRequest.put("headers", session.getHeaders());
|
jsonRequest.put("headers", session.getHeaders());
|
||||||
jsonRequest.put("method", session.getMethod());
|
jsonRequest.put("method", session.getMethod());
|
||||||
jsonRequest.put("path", session.getUri());
|
jsonRequest.put("path", session.getUri());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user