Skip to content
Snippets Groups Projects
Commit 557b7777 authored by srosse's avatar srosse
Browse files

OO-2154: replace the method getSubmittedFilename from Servlet API by the...

OO-2154: replace the method getSubmittedFilename from Servlet API by the implementation of undertow.io
parent a01c462b
No related branches found
No related tags found
No related merge requests found
......@@ -52,6 +52,7 @@ import org.olat.core.gui.components.form.flexible.FormItemContainer;
import org.olat.core.gui.components.form.flexible.elements.Submit;
import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.Event;
import org.olat.core.gui.media.ServletUtil;
import org.olat.core.gui.translator.Translator;
import org.olat.core.logging.AssertException;
import org.olat.core.logging.LogDelegator;
......@@ -324,7 +325,7 @@ public class Form extends LogDelegator {
for(Part part:req.getParts()) {
String name = part.getName();
String contentType = part.getContentType();
String fileName = part.getSubmittedFileName();
String fileName = getSubmittedFileName(part);
if(StringHelper.containsNonWhitespace(fileName)) {
File tmpFile = new File(WebappHelper.getTmpDir(), "upload-" + CodeHelper.getGlobalForeverUniqueID());
part.write(tmpFile.getAbsolutePath());
......@@ -349,6 +350,19 @@ public class Form extends LogDelegator {
}
}
private String getSubmittedFileName(Part part) {
final String disposition = part.getHeader("Content-Disposition");
if (disposition != null) {
if (disposition.startsWith("form-data")) {
String fileName = ServletUtil.extractQuotedValueFromHeader(disposition, "filename");
if (fileName != null) {
return fileName;
}
}
}
return null;
}
private boolean isMultipartContent(HttpServletRequest request) {
if (!"POST".equalsIgnoreCase(request.getMethod())) {
return false;
......
......@@ -628,6 +628,76 @@ public class ServletUtil {
return (normalized);
}
/**
* Extracts a quoted value from a header that has a given key. For instance if the header is
* <p>
* content-disposition=form-data; name="my field"
* and the key is name then "my field" will be returned without the quotes.
*
* @param header The header
* @param key The key that identifies the token to extract
* @return The token, or null if it was not found
*/
public static String extractQuotedValueFromHeader(final String header, final String key) {
int keypos = 0;
int pos = -1;
boolean inQuotes = false;
for (int i = 0; i < header.length() - 1; ++i) { //-1 because we need room for the = at the end
//TODO: a more efficient matching algorithm
char c = header.charAt(i);
if (inQuotes) {
if (c == '"') {
inQuotes = false;
}
} else {
if (key.charAt(keypos) == c) {
keypos++;
} else if (c == '"') {
keypos = 0;
inQuotes = true;
} else {
keypos = 0;
}
if (keypos == key.length()) {
if (header.charAt(i + 1) == '=') {
pos = i + 2;
break;
} else {
keypos = 0;
}
}
}
}
if (pos == -1) {
return null;
}
int end;
int start = pos;
if (header.charAt(start) == '"') {
start++;
for (end = start; end < header.length(); ++end) {
char c = header.charAt(end);
if (c == '"') {
break;
}
}
return header.substring(start, end);
} else {
//no quotes
for (end = start; end < header.length(); ++end) {
char c = header.charAt(end);
if (c == ' ' || c == '\t' || c == ';') {
break;
}
}
return header.substring(start, end);
}
}
//fxdiff FXOLAT-118: accept range to deliver videos for iPad
protected static class Range {
public long start;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment