Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 29 additions & 24 deletions core/src/main/java/org/nanohttpd/protocols/http/HTTPSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ public long getBodySize() {
}

@Override
public void parseBody(Map<String, String> files) throws IOException, ResponseException {
public void parseBody(Map<String, String> files, Map<String, List<String>> form) throws IOException, ResponseException {
RandomAccessFile randomAccessFile = null;
try {
long size = getBodySize();
Expand Down Expand Up @@ -635,30 +635,35 @@ public void parseBody(Map<String, String> files) throws IOException, ResponseExc

// If the method is POST, there may be parameters
// in data section, too, read it:
if (Method.POST.equals(this.method)) {
ContentType contentType = new ContentType(this.headers.get("content-type"));
if (contentType.isMultipart()) {
String boundary = contentType.getBoundary();
if (boundary == null) {
throw new ResponseException(Status.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but boundary missing. Usage: GET /example/file.html");
}
decodeMultipartFormData(contentType, fbuf, this.parms, files);
} else {
byte[] postBytes = new byte[fbuf.remaining()];
fbuf.get(postBytes);
String postLine = new String(postBytes, contentType.getEncoding()).trim();
// Handle application/x-www-form-urlencoded
if ("application/x-www-form-urlencoded".equalsIgnoreCase(contentType.getContentType())) {
decodeParms(postLine, this.parms);
} else if (postLine.length() != 0) {
// Special case for raw POST data => create a
// special files entry "postData" with raw content
// data
files.put("postData", postLine);
switch (this.method) {
case POST: {
ContentType contentType = new ContentType(this.headers.get("content-type"));
if (form == null)
form = this.parms;
if (contentType.isMultipart()) {
String boundary = contentType.getBoundary();
if (boundary == null) {
throw new ResponseException(Status.BAD_REQUEST, "BAD REQUEST: Content type is multipart/form-data but boundary missing. Usage: GET /example/file.html");
}
decodeMultipartFormData(contentType, fbuf, form, files);
} else {
byte[] postBytes = new byte[fbuf.remaining()];
fbuf.get(postBytes);
String postLine = new String(postBytes, contentType.getEncoding()).trim();
// Handle application/x-www-form-urlencoded
if ("application/x-www-form-urlencoded".equalsIgnoreCase(contentType.getContentType())) {
decodeParms(postLine, form);
} else if (postLine.length() != 0) {
// Special case for raw POST data => create a
// special files entry "postData" with raw content
// data
files.put("postData", postLine);
}
}
}
} else if (Method.PUT.equals(this.method)) {
files.put("content", saveTmpFile(fbuf, 0, fbuf.limit(), null));
break; }
case PUT:
files.put("content", saveTmpFile(fbuf, 0, fbuf.limit(), null));
break;
}
} finally {
NanoHTTPD.safeClose(randomAccessFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ public interface IHTTPSession {
* Adds the files in the request body to the files map.
*
* @param files
* map to modify
* map to receive local paths of uploaded files.
* @param form
* map to receive &lt;form&gt; parameters when POST method is used.
*/
void parseBody(Map<String, String> files) throws IOException, ResponseException;
void parseBody(Map<String, String> files, Map<String, List<String>> form) throws IOException, ResponseException;

/**
* Get the remote ip address of the requester.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ public Response serve(IHTTPSession session) {
Method method = session.getMethod();
if (Method.PUT.equals(method) || Method.POST.equals(method)) {
try {
session.parseBody(files);
session.parseBody(files, session.getParameters());
} catch (IOException ioe) {
return Response.newFixedLengthResponse(Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage());
} catch (ResponseException re) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public Response serve(IHTTPSession session) {
this.header = session.getHeaders();
this.files = new HashMap<String, String>();
try {
session.parseBody(this.files);
session.parseBody(this.files, session.getParameters());
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -229,7 +229,7 @@ public Response serve(IHTTPSession session) {
StringBuilder responseMsg = new StringBuilder();

try {
session.parseBody(this.files);
session.parseBody(this.files, session.getParameters());
for (String key : files.keySet()) {
responseMsg.append(key);
}
Expand Down Expand Up @@ -285,7 +285,7 @@ public Response serve(IHTTPSession session) {
String responseMsg = "pass";

try {
session.parseBody(this.files);
session.parseBody(this.files, session.getParameters());
for (String key : files.keySet()) {
if (!(new File(files.get(key))).exists()) {
responseMsg = "fail";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Response serve(IHTTPSession session) {

try {
Map<String, String> files = new HashMap<String, String>();
session.parseBody(files);
session.parseBody(files, session.getParameters());
sb.append("<h3>Files</h3><p><blockquote>").append(toString(files)).append("</blockquote></p>");
} catch (Exception e) {
e.printStackTrace();
Expand Down