line = lis.readLine(100, 100, false);
}
boundary = "\r\n" + boundary;
RandomAccessBucket filedata = null;
String name = null;
String filename = null;
String contentType = null;
while(is.available() > 0) {
name = null;
filename = null;
contentType = null;
// chomp headers
while((line = lis.readLine(200, 200, true)) /* should be UTF-8 as we told the browser to send UTF-8 */ != null) {
if(line.length() == 0)
break;
String[] lineparts = line.split(":");
if(lineparts == null || lineparts.length == 0)
continue;
String hdrname = lineparts[0].trim();
if(hdrname.equalsIgnoreCase("Content-Disposition")) {
if(lineparts.length < 2)
continue;
String[] valueparts = lineparts[1].split(";");
for(int i = 0; i < valueparts.length; i++) {
String[] subparts = valueparts[i].split("=");
if(subparts.length != 2)
continue;
String fieldname = subparts[0].trim();
String value = subparts[1].trim();
if(value.startsWith("\"") && value.endsWith("\""))
value = value.substring(1, value.length() - 1);
if(fieldname.equalsIgnoreCase("name"))
name = value;
else if(fieldname.equalsIgnoreCase("filename"))
filename = value;
}
}
else if(hdrname.equalsIgnoreCase("Content-Type")) {
contentType = lineparts[1].trim();
if(logMINOR)
Logger.minor(this, "Parsed type: " + contentType);
}
else {
// Do nothing, irrelevant header
}
}
if(name == null)
continue;
// we should be at the data now. Start reading it in, checking for the
// boundary string
// we can only give an upper bound for the size of the bucket
filedata = this.bucketfactory.makeBucket(is.available());
bucketos = filedata.getOutputStream();
// buffer characters that match the boundary so far
// FIXME use whatever charset was used
byte[] bbound = boundary.getBytes("UTF-8"); // ISO-8859-1? boundary should be in US-ASCII
int offset = 0;
while((is.available() > 0) && (offset < bbound.length)) {
byte b = (byte) is.read();
if(b == bbound[offset])
offset++;
else if((b != bbound[offset]) && (offset > 0)) {
// offset bytes matched, but no more
// write the bytes that matched, then the non-matching byte
bucketos.write(bbound, 0, offset);
offset = 0;
if(b == bbound[0])
offset = 1;
else
bucketos.write(b);
}
else
bucketos.write(b);
}
bucketos.close();
bucketos = null;
parts.put(name, filedata);
if(logMINOR)
Logger.minor(this, "Name = " + name + " length = " + filedata.size() + " filename = " + filename);
if(filename != null)
uploadedFiles.put(name, new HTTPUploadedFileImpl(filename, contentType, filedata));
}
}
finally {