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 | 1 | private static int MAX_SIZE = 2048; |
64 | |
private int maxSize; |
65 | 19 | private Melati melati = null; |
66 | |
DelimitedBufferedInputStream in; |
67 | |
String contentType; |
68 | |
FormDataAdaptorFactory factory; |
69 | 19 | 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 | 19 | 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 | 19 | this(melati,in, contentType, factory, MAX_SIZE); |
92 | 19 | } |
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 | 19 | int maxSize) { |
109 | 19 | this.melati = melati; |
110 | 19 | this.in = new DelimitedBufferedInputStream(in, maxSize); |
111 | 19 | this.contentType = contentType; |
112 | 19 | this.factory = factory; |
113 | 19 | this.maxSize = maxSize; |
114 | 19 | } |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public Hashtable<String,MultipartFormField> parseData() throws IOException { |
124 | |
try { |
125 | 19 | return parseData(in, contentType, maxSize); |
126 | |
} |
127 | 0 | catch (IOException e) { |
128 | 0 | throw e; |
129 | |
} |
130 | |
finally { |
131 | 19 | in.close(); |
132 | 19 | in = null; |
133 | |
} |
134 | |
} |
135 | |
|
136 | |
private Hashtable<String,MultipartFormField> parseData(DelimitedBufferedInputStream inP, |
137 | |
String contentTypeP, |
138 | |
int maxSizeP) |
139 | |
throws IOException { |
140 | 19 | String boundary = getBoundary(contentTypeP); |
141 | |
String line; |
142 | 19 | String header = ""; |
143 | 19 | MultipartFormField field = null; |
144 | 19 | byte[] CRLF = {13,10}; |
145 | 19 | byte[] buff = new byte[maxSizeP]; |
146 | |
int count; |
147 | |
|
148 | 187 | while (state != STOP) { |
149 | |
|
150 | |
|
151 | 168 | if (state == FIELD_START) { |
152 | 83 | count = inP.readToDelimiter(buff, 0, buff.length, boundary.getBytes()); |
153 | 83 | if (count == buff.length) { |
154 | 0 | throw new IOException( |
155 | |
"Didn't find a boundary in the first " |
156 | |
+ buff.length + " bytes"); |
157 | |
} |
158 | 83 | count = inP.readToDelimiter(buff, 0, buff.length, CRLF); |
159 | 83 | line = new String(buff, 0, count); |
160 | |
|
161 | 83 | if (line.equals(boundary)) { |
162 | 64 | state = IN_FIELD_HEADER; |
163 | 64 | header = ""; |
164 | 64 | if (inP.read(buff, 0, 2) != 2) |
165 | 0 | throw new IOException( |
166 | |
"Boundary wasn't followed by 2 bytes (\\r\\n)"); |
167 | |
} |
168 | 19 | else if (line.equals(boundary+"--")) { |
169 | 19 | 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 | 168 | if (state == IN_FIELD_HEADER) { |
178 | 149 | count = inP.readToDelimiter(buff, 0, buff.length, CRLF); |
179 | 149 | if (count != 0) |
180 | 85 | header += new String(buff, 0, count) + "\r\n"; |
181 | |
else { |
182 | 64 | state = IN_FIELD_DATA; |
183 | 64 | field = new MultipartFormField(); |
184 | 64 | readField(field, header); |
185 | 64 | fields.put(field.getFieldName(), field); |
186 | |
} |
187 | 149 | 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 | 168 | if (state == IN_FIELD_DATA) { |
194 | 64 | String dataBoundary = "\r\n" + boundary; |
195 | |
|
196 | |
|
197 | 64 | FormDataAdaptor adaptor = null; |
198 | |
|
199 | 64 | if (field != null && field.getUploadedFileName().equals("")) { |
200 | 51 | adaptor = new MemoryFormDataAdaptor(); |
201 | |
} |
202 | |
else { |
203 | 13 | adaptor = factory.get(melati, field); |
204 | |
} |
205 | 64 | adaptor.readData(field, inP, dataBoundary.getBytes()); |
206 | |
|
207 | 64 | if (field != null) field.setFormDataAdaptor(adaptor); |
208 | 64 | state = FIELD_START; |
209 | 64 | } |
210 | |
|
211 | |
} |
212 | 19 | return fields; |
213 | |
} |
214 | |
|
215 | |
private void readField(MultipartFormField field, String header) { |
216 | 64 | field.setContentDisposition(extractField(header, |
217 | |
"content-disposition:", ";")); |
218 | 64 | String fieldName = extractField(header, "name=",";"); |
219 | 64 | if (fieldName.length() != 0) { |
220 | 64 | if(fieldName.charAt(0) == '\"') |
221 | 64 | fieldName = fieldName.substring(1, fieldName.length() - 1); |
222 | 64 | field.setFieldName(fieldName); |
223 | |
} |
224 | 64 | String fileName = extractField(header, "filename=", ";"); |
225 | 64 | if(fileName.length() != 0) { |
226 | 21 | if(fileName.charAt(0) == '\"') |
227 | 21 | fileName = fileName.substring(1, fileName.length()-1); |
228 | 21 | field.setUploadedFilePath(fileName); |
229 | |
} |
230 | 64 | field.setContentType(extractField(header, "content-type:", ";")); |
231 | 64 | } |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
protected String extractField(String header, String lBound, |
244 | |
String rBound) { |
245 | 256 | String lheader = header.toLowerCase(); |
246 | 256 | int begin = 0, end = 0; |
247 | 256 | begin = lheader.indexOf(lBound); |
248 | 256 | if(begin == -1) |
249 | 86 | return ""; |
250 | 170 | begin = begin + lBound.length(); |
251 | 170 | end = lheader.indexOf(rBound, begin); |
252 | 170 | if(end == -1) |
253 | 85 | end = lheader.indexOf("\r\n", begin); |
254 | 170 | if(end == -1) |
255 | 0 | end = lheader.length(); |
256 | 170 | return header.substring(begin, end).trim(); |
257 | |
} |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
private String getBoundary( String header) |
265 | |
throws IOException { |
266 | 19 | String boundary=""; |
267 | |
int index; |
268 | |
|
269 | 19 | if ((index = header.lastIndexOf("boundary=")) != -1) { |
270 | 19 | boundary = header.substring(index + 9); |
271 | |
|
272 | |
|
273 | 19 | 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 | 19 | return boundary; |
298 | |
} |
299 | |
|
300 | |
} |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|