| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
package org.melati.servlet; |
| 50 | |
|
| 51 | |
import java.io.IOException; |
| 52 | |
import java.io.InputStream; |
| 53 | |
import java.util.Hashtable; |
| 54 | |
import org.melati.Melati; |
| 55 | |
import org.melati.util.DelimitedBufferedInputStream; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
public class MultipartFormDataDecoder { |
| 62 | |
|
| 63 | 2 | private static int MAX_SIZE = 2048; |
| 64 | |
private int maxSize; |
| 65 | 38 | private Melati melati = null; |
| 66 | |
DelimitedBufferedInputStream in; |
| 67 | |
String contentType; |
| 68 | |
FormDataAdaptorFactory factory; |
| 69 | 38 | Hashtable<String,MultipartFormField> fields = new Hashtable<String,MultipartFormField>(); |
| 70 | |
|
| 71 | |
private static final int FIELD_START = 0; |
| 72 | |
private static final int IN_FIELD_HEADER = 1; |
| 73 | |
private static final int IN_FIELD_DATA = 2; |
| 74 | |
private static final int STOP = 3; |
| 75 | |
|
| 76 | 38 | private int state = FIELD_START; |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public MultipartFormDataDecoder(Melati melati, |
| 88 | |
InputStream in, |
| 89 | |
String contentType, |
| 90 | |
FormDataAdaptorFactory factory) { |
| 91 | 38 | this(melati,in, contentType, factory, MAX_SIZE); |
| 92 | 38 | } |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public MultipartFormDataDecoder(Melati melati, |
| 105 | |
InputStream in, |
| 106 | |
String contentType, |
| 107 | |
FormDataAdaptorFactory factory, |
| 108 | 38 | int maxSize) { |
| 109 | 38 | this.melati = melati; |
| 110 | 38 | this.in = new DelimitedBufferedInputStream(in, maxSize); |
| 111 | 38 | this.contentType = contentType; |
| 112 | 38 | this.factory = factory; |
| 113 | 38 | this.maxSize = maxSize; |
| 114 | 38 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public Hashtable<String,MultipartFormField> parseData() throws IOException { |
| 124 | |
try { |
| 125 | 38 | return parseData(in, contentType, maxSize); |
| 126 | |
} |
| 127 | 0 | catch (IOException e) { |
| 128 | 0 | throw e; |
| 129 | |
} |
| 130 | |
finally { |
| 131 | 38 | in.close(); |
| 132 | 38 | in = null; |
| 133 | |
} |
| 134 | |
} |
| 135 | |
|
| 136 | |
private Hashtable<String,MultipartFormField> parseData(DelimitedBufferedInputStream inP, |
| 137 | |
String contentTypeP, |
| 138 | |
int maxSizeP) |
| 139 | |
throws IOException { |
| 140 | 38 | String boundary = getBoundary(contentTypeP); |
| 141 | |
String line; |
| 142 | 38 | String header = ""; |
| 143 | 38 | MultipartFormField field = null; |
| 144 | 38 | byte[] CRLF = {13,10}; |
| 145 | 38 | byte[] buff = new byte[maxSizeP]; |
| 146 | |
int count; |
| 147 | |
|
| 148 | 374 | while (state != STOP) { |
| 149 | |
|
| 150 | |
|
| 151 | 336 | if (state == FIELD_START) { |
| 152 | 166 | count = inP.readToDelimiter(buff, 0, buff.length, boundary.getBytes()); |
| 153 | 166 | if (count == buff.length) { |
| 154 | 0 | throw new IOException( |
| 155 | |
"Didn't find a boundary in the first " |
| 156 | |
+ buff.length + " bytes"); |
| 157 | |
} |
| 158 | 166 | count = inP.readToDelimiter(buff, 0, buff.length, CRLF); |
| 159 | 166 | line = new String(buff, 0, count); |
| 160 | |
|
| 161 | 166 | if (line.equals(boundary)) { |
| 162 | 128 | state = IN_FIELD_HEADER; |
| 163 | 128 | header = ""; |
| 164 | 128 | if (inP.read(buff, 0, 2) != 2) |
| 165 | 0 | throw new IOException( |
| 166 | |
"Boundary wasn't followed by 2 bytes (\\r\\n)"); |
| 167 | |
} |
| 168 | 38 | else if (line.equals(boundary+"--")) { |
| 169 | 38 | state = STOP; |
| 170 | |
} |
| 171 | |
else |
| 172 | 0 | throw new IOException( |
| 173 | |
"Didn't find the boundary I was expecting before a field"); |
| 174 | |
} |
| 175 | |
|
| 176 | |
|
| 177 | 336 | if (state == IN_FIELD_HEADER) { |
| 178 | 298 | count = inP.readToDelimiter(buff, 0, buff.length, CRLF); |
| 179 | 298 | if (count != 0) |
| 180 | 170 | header += new String(buff, 0, count) + "\r\n"; |
| 181 | |
else { |
| 182 | 128 | state = IN_FIELD_DATA; |
| 183 | 128 | field = new MultipartFormField(); |
| 184 | 128 | readField(field, header); |
| 185 | 128 | fields.put(field.getFieldName(), field); |
| 186 | |
} |
| 187 | 298 | if (inP.read(buff, 0, 2) != 2) |
| 188 | 0 | throw new IOException( |
| 189 | |
"Header line wasn't followed by 2 bytes (\\r\\n)"); |
| 190 | |
} |
| 191 | |
|
| 192 | |
|
| 193 | 336 | if (state == IN_FIELD_DATA) { |
| 194 | 128 | String dataBoundary = "\r\n" + boundary; |
| 195 | |
|
| 196 | |
|
| 197 | 128 | FormDataAdaptor adaptor = null; |
| 198 | |
|
| 199 | 128 | if (field != null && field.getUploadedFileName().equals("")) { |
| 200 | 102 | adaptor = new MemoryFormDataAdaptor(); |
| 201 | |
} |
| 202 | |
else { |
| 203 | 26 | adaptor = factory.get(melati, field); |
| 204 | |
} |
| 205 | 128 | adaptor.readData(field, inP, dataBoundary.getBytes()); |
| 206 | |
|
| 207 | 128 | if (field != null) field.setFormDataAdaptor(adaptor); |
| 208 | 128 | state = FIELD_START; |
| 209 | 128 | } |
| 210 | |
|
| 211 | |
} |
| 212 | 38 | return fields; |
| 213 | |
} |
| 214 | |
|
| 215 | |
private void readField(MultipartFormField field, String header) { |
| 216 | 128 | field.setContentDisposition(extractField(header, |
| 217 | |
"content-disposition:", ";")); |
| 218 | 128 | String fieldName = extractField(header, "name=",";"); |
| 219 | 128 | if (fieldName.length() != 0) { |
| 220 | 128 | if(fieldName.charAt(0) == '\"') |
| 221 | 128 | fieldName = fieldName.substring(1, fieldName.length() - 1); |
| 222 | 128 | field.setFieldName(fieldName); |
| 223 | |
} |
| 224 | 128 | String fileName = extractField(header, "filename=", ";"); |
| 225 | 128 | if(fileName.length() != 0) { |
| 226 | 42 | if(fileName.charAt(0) == '\"') |
| 227 | 42 | fileName = fileName.substring(1, fileName.length()-1); |
| 228 | 42 | field.setUploadedFilePath(fileName); |
| 229 | |
} |
| 230 | 128 | field.setContentType(extractField(header, "content-type:", ";")); |
| 231 | 128 | } |
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
protected String extractField(String header, String lBound, |
| 244 | |
String rBound) { |
| 245 | 512 | String lheader = header.toLowerCase(); |
| 246 | 512 | int begin = 0, end = 0; |
| 247 | 512 | begin = lheader.indexOf(lBound); |
| 248 | 512 | if(begin == -1) |
| 249 | 172 | return ""; |
| 250 | 340 | begin = begin + lBound.length(); |
| 251 | 340 | end = lheader.indexOf(rBound, begin); |
| 252 | 340 | if(end == -1) |
| 253 | 170 | end = lheader.indexOf("\r\n", begin); |
| 254 | 340 | if(end == -1) |
| 255 | 0 | end = lheader.length(); |
| 256 | 340 | return header.substring(begin, end).trim(); |
| 257 | |
} |
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
private String getBoundary( String header) |
| 265 | |
throws IOException { |
| 266 | 38 | String boundary=""; |
| 267 | |
int index; |
| 268 | |
|
| 269 | 38 | if ((index = header.lastIndexOf("boundary=")) != -1) { |
| 270 | 38 | boundary = header.substring(index + 9); |
| 271 | |
|
| 272 | |
|
| 273 | 38 | boundary = "--" + boundary; |
| 274 | |
} else { |
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | 0 | throw new IOException("Boundary not found in header"); |
| 296 | |
} |
| 297 | 38 | return boundary; |
| 298 | |
} |
| 299 | |
|
| 300 | |
} |
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|