Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change when form parts go to files #518

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 16 additions & 7 deletions core/src/main/java/fi/iki/elonen/NanoHTTPD.java
Original file line number Diff line number Diff line change
Expand Up @@ -790,13 +790,7 @@ private void decodeMultipartFormData(ContentType contentType, ByteBuffer fbuf, M
parms.put(partName, values);
}

if (partContentType == null) {
// Read the part into a string
byte[] data_bytes = new byte[partDataEnd - partDataStart];
fbuf.get(data_bytes);

values.add(new String(data_bytes, contentType.getEncoding()));
} else {
if (partIsFile(fileName, partContentType)) {
// Read it into a file
String path = saveTmpFile(fbuf, partDataStart, partDataEnd - partDataStart, fileName);
if (!files.containsKey(partName)) {
Expand All @@ -809,6 +803,17 @@ private void decodeMultipartFormData(ContentType contentType, ByteBuffer fbuf, M
files.put(partName + count, path);
}
values.add(fileName);
} else {
// Read the part into a string
byte[] data_bytes = new byte[partDataEnd - partDataStart];
fbuf.get(data_bytes);

Matcher charsetMatcher = ContentType.CHARSET_PATTERN.matcher(partContentType == null ? "" : partContentType);
if (charsetMatcher.find()) {
values.add(new String(data_bytes, charsetMatcher.group(2)));
} else {
values.add(new String(data_bytes));
}
}
}
} catch (ResponseException re) {
Expand All @@ -825,6 +830,10 @@ private int scipOverNewLine(byte[] partHeaderBuff, int index) {
return ++index;
}

private boolean partIsFile(String fileName, String partContentType) {
return fileName != null || "application/octet-stream".equals(partContentType);
}

/**
* Decodes parameters in percent-encoded URI-format ( e.g.
* "name=Jack%20Daniels&pass=Single%20Malt" ) and adds them to given
Expand Down