long uploadMax,
long fileUploadMax,
long lengthMax)
throws IOException
{
MultipartStream ms = new MultipartStream(rawIs, boundary);
ms.setEncoding(javaEncoding);
ReadStream is;
while ((is = ms.openRead()) != null) {
String attr = (String) ms.getAttribute("content-disposition");
if (attr == null || ! attr.startsWith("form-data")) {
// XXX: is this an error?
continue;
}
String name = getAttribute(attr, "name");
String filename = getAttribute(attr, "filename");
String contentType = getAttribute(attr, "content-type");
if (contentType == null)
contentType = ms.getAttribute("content-type");
if (name == null) {
// XXX: is this an error?
continue;
}
else if (filename != null) {
Path tempDir = CauchoSystem.getWorkPath().lookup("form");
try {
tempDir.mkdirs();
} catch (IOException e) {
}
Path tempFile = tempDir.createTempFile("form", ".tmp");
request.addCloseOnExit(tempFile);
WriteStream os = tempFile.openWrite();
TempBuffer tempBuffer = TempBuffer.allocate();
byte []buf = tempBuffer.getBuffer();
int totalLength = 0;
try {
int len;
while ((len = is.read(buf, 0, buf.length)) > 0) {
os.write(buf, 0, len);
totalLength += len;
}
} finally {
os.close();
TempBuffer.free(tempBuffer);
tempBuffer = null;
}
if (uploadMax > 0 && uploadMax < tempFile.getLength()) {
String msg = L.l("multipart form data '{0}' too large",
"" + tempFile.getLength());
request.setAttribute("caucho.multipart.form.error", msg);
request.setAttribute("caucho.multipart.form.error.size",
new Long(tempFile.getLength()));
tempFile.remove();
throw new IOException(msg);
} else if (fileUploadMax > 0 && fileUploadMax < tempFile.getLength()){
String msg = L.l("multipart form data part '{0}':'{1}' is greater then the accepted value of '{2}'",
name, "" + tempFile.getLength(), fileUploadMax);
tempFile.remove();
throw new IllegalStateException(msg);
}
else if (tempFile.getLength() != totalLength) {
String msg = L.l("multipart form upload failed (possibly due to full disk).");
request.setAttribute("caucho.multipart.form.error", msg);
request.setAttribute("caucho.multipart.form.error.size",
new Long(tempFile.getLength()));
tempFile.remove();
throw new IOException(msg);
}
// server/136u, server/136v, #2578
if (table.get(name + ".filename") == null) {
table.put(name, new String[] { tempFile.getNativePath() });
table.put(name + ".file", new String[] { tempFile.getNativePath() });
table.put(name + ".filename", new String[] { filename });
table.put(name + ".content-type", new String[] { contentType });
}
else {
addTable(table, name, tempFile.getNativePath());
addTable(table, name + ".file", tempFile.getNativePath());
addTable(table, name + ".filename", filename);
addTable(table, name + ".content-type", contentType);
}
if (log.isLoggable(Level.FINE))
log.fine("mp-file: " + name + "(filename:" + filename + ")");
} else {
CharBuffer value = new CharBuffer();
int ch;
long totalLength = 0;
for (ch = is.readChar(); ch >= 0; ch = is.readChar()) {
value.append((char) ch);
totalLength++;
if (lengthMax < totalLength) {
String msg = L.l("multipart form upload failed because field '{0}' exceeds max length {1}",
name, lengthMax);
request.setAttribute("caucho.multipart.form.error", msg);
request.setAttribute("caucho.multipart.form.error.size",
new Long(totalLength));
throw new IOException(msg);
}
}
if (log.isLoggable(Level.FINE))
log.fine("mp-form: " + name + "=" + value);
addTable(table, name, value.toString());
if (contentType != null)
addTable(table, name + ".content-type", contentType);
}
parts.add(request.createPart(name,
new HashMap<String, List<String>>(ms.getHeaders())));
}
if (! ms.isComplete()) {
throw new IOException("Incomplete form");
}
}