aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java
diff options
context:
space:
mode:
authorritchie <ritchie@gmx.at>2015-05-25 08:38:15 +0200
committerritchie <ritchie@gmx.at>2015-05-25 08:38:15 +0200
commit731ecb0ecb91cc31d3f12a657c4a34b40fa18f3f (patch)
tree03d532b2ef979a2cdd724196483af18ea2f8c829 /core/src/main/java
parent5e6d76fe9c6bf67e7cdeb6e310952af639614030 (diff)
downloadnanohttpd-731ecb0ecb91cc31d3f12a657c4a34b40fa18f3f.tar.gz
use regex for content disposition
Diffstat (limited to 'core/src/main/java')
-rw-r--r--core/src/main/java/fi/iki/elonen/NanoHTTPD.java54
1 files changed, 28 insertions, 26 deletions
diff --git a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
index 055c606..4675f6f 100644
--- a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
+++ b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
@@ -58,6 +58,8 @@ import java.util.StringTokenizer;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;
import javax.net.ssl.KeyManager;
@@ -440,6 +442,18 @@ public abstract class NanoHTTPD {
}
}
+ private static final String CONTENT_DISPOSITION_REGEX = "([ |\t]*Content-Disposition[ |\t]*:)(.*)";
+
+ private static final Pattern CONTENT_DISPOSITION_PATTERN = Pattern.compile(CONTENT_DISPOSITION_REGEX, Pattern.CASE_INSENSITIVE);
+
+ private static final String CONTENT_TYPE_REGEX = "([ |\t]*content-type[ |\t]*:)(.*)";
+
+ private static final Pattern CONTENT_TYPE_PATTERN = Pattern.compile(CONTENT_TYPE_REGEX, Pattern.CASE_INSENSITIVE);
+
+ private static final String CONTENT_DISPOSITION_ATTRIBUTE_REGEX = "[ |\t]*([a-zA-Z]*)[ |\t]*=[ |\t]*['|\"]([^\"^']*)['|\"]";
+
+ private static final Pattern CONTENT_DISPOSITION_ATTRIBUTE_PATTERN = Pattern.compile(CONTENT_DISPOSITION_ATTRIBUTE_REGEX);
+
protected class HTTPSession implements IHTTPSession {
public static final int BUFSIZE = 8192;
@@ -568,38 +582,26 @@ public abstract class NanoHTTPD {
}
String part_name = null, file_name = null, content_type = null;
-
// Parse the reset of the header lines
mpline = in.readLine();
while (mpline != null && mpline.trim().length() > 0) {
- int hdr_sep_i = mpline.indexOf(':');
- if (hdr_sep_i != -1) {
- String key = mpline.substring(0, hdr_sep_i).trim().toLowerCase(Locale.US);
- String value = mpline.substring(hdr_sep_i + 1).trim();
-
- // Parse content-disposition. Example:
- // Content-Disposition: form-data; name="file1";
- // filename="a.txt"
- if (key.equals("content-disposition")) {
- StringTokenizer st = new StringTokenizer(value, ";");
- while (st.hasMoreTokens()) {
- String token = st.nextToken().trim();
- int cd_sep_i = token.indexOf('=');
- if (cd_sep_i != -1) {
- String cd_key = token.substring(0, cd_sep_i).trim().toLowerCase(Locale.US);
- String cd_val = token.substring(cd_sep_i + 1).trim();
- String cd_val_no_quotes = cd_val.substring(1, cd_val.length() - 1);
- if (cd_key.equals("name")) {
- part_name = cd_val_no_quotes;
- } else if (cd_key.equals("filename")) {
- file_name = cd_val_no_quotes;
- }
- }
+ Matcher matcher = CONTENT_DISPOSITION_PATTERN.matcher(mpline);
+ if (matcher.matches()) {
+ String attributeString = matcher.group(2);
+ matcher = CONTENT_DISPOSITION_ATTRIBUTE_PATTERN.matcher(attributeString);
+ while (matcher.find()) {
+ String key = matcher.group(1);
+ if (key.equalsIgnoreCase("name")) {
+ part_name = matcher.group(2);
+ } else if (key.equalsIgnoreCase("filename")) {
+ file_name = matcher.group(2);
}
- } else if (key.equals("content-type")) {
- content_type = value;
}
}
+ matcher = CONTENT_TYPE_PATTERN.matcher(mpline);
+ if (matcher.matches()) {
+ content_type = matcher.group(2).trim();
+ }
mpline = in.readLine();
}