1 29 30 package com.caucho.quercus.env; 31 32 import com.caucho.quercus.lib.string.StringModule; 33 import com.caucho.vfs.*; 34 35 import javax.servlet.*; 36 import javax.servlet.http.HttpServletRequest ; 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 40 43 public class Post { 44 static void fillPost(Env env, 45 ArrayValue post, ArrayValue files, 46 HttpServletRequest request, 47 boolean addSlashesToValues) 48 { 49 InputStream is = null; 50 51 try { 52 if (! request.getMethod().equals("POST")) 53 return; 54 55 String contentType = request.getHeader("Content-Type"); 56 57 if (contentType == null) 58 return; 59 else if (contentType.startsWith("application/x-www-form-urlencoded")) { 60 is = request.getInputStream(); 61 62 if (is == null) 63 return; 64 65 StringBuilder value = new StringBuilder (); 66 int ch; 67 68 while ((ch = is.read()) >= 0) { 69 value.append((char) ch); 70 } 71 72 StringModule.parse_str(env, value.toString(), post); 73 74 return; 75 } 76 else if (! contentType.startsWith("multipart/form-data")) { 77 return; 78 } 79 80 String boundary = getBoundary(contentType); 81 82 is = request.getInputStream(); 83 84 ReadStream rs = new ReadStream(new VfsStream(is, null)); 85 MultipartStream ms = new MultipartStream(rs, boundary); 86 88 readMultipartStream(env, ms, post, files, addSlashesToValues); 89 90 rs.close(); 91 } catch (IOException e) { 92 e.printStackTrace(); 93 } finally { 94 try { 95 if (is != null) 96 is.close(); 97 } catch (IOException e) { 98 } 99 } 100 } 101 102 private static void readMultipartStream(Env env, 103 MultipartStream ms, 104 ArrayValue post, 105 ArrayValue files, 106 boolean addSlashesToValues) 107 throws IOException 108 { 109 ReadStream is; 110 111 while ((is = ms.openRead()) != null) { 112 String attr = (String ) ms.getAttribute("content-disposition"); 113 114 if (attr == null || ! attr.startsWith("form-data")) { 115 continue; 117 } 118 119 String name = getAttribute(attr, "name"); 120 String filename = getAttribute(attr, "filename"); 121 122 if (filename == null) { 123 StringBuilder value = new StringBuilder (); 124 int ch; 125 126 while ((ch = is.read()) >= 0) { 127 value.append((char) ch); 128 } 129 130 addFormValue(post, name, new StringValueImpl(value.toString()), null, addSlashesToValues); 131 } 132 else { 133 Path tmpName = env.getUploadDirectory().createTempFile("php", "tmp"); 134 135 env.addRemovePath(tmpName); 136 137 WriteStream os = tmpName.openWrite(); 138 try { 139 os.writeStream(is); 140 } finally { 141 os.close(); 142 } 143 144 int p = name.indexOf('['); 145 String index = ""; 146 if (p >= 0) { 147 index = name.substring(p); 148 name = name.substring(0, p); 149 } 150 151 StringValue nameValue = new StringValueImpl(name); 152 Value v = files.get(nameValue).toValue(); 153 ArrayValue entry = null; 154 if (v instanceof ArrayValue) 155 entry = (ArrayValue) v; 156 157 if (entry == null) { 158 entry = new ArrayValueImpl(); 159 files.put(nameValue, entry); 160 } 161 162 addFormValue(entry, "name" + index, new StringValueImpl(filename), 163 null, addSlashesToValues); 164 165 String mimeType = getAttribute(attr, "mime-type"); 166 if (mimeType != null) { 167 addFormValue(entry, "type" + index, new StringValueImpl(mimeType), 168 null, addSlashesToValues); 169 entry.put("type", mimeType); 170 } 171 172 addFormValue(entry, "size" + index, new LongValue(tmpName.getLength()), 173 null, addSlashesToValues); 174 175 addFormValue(entry, "tmp_name" + index, 176 new StringValueImpl(tmpName.getFullPath()), 177 null, addSlashesToValues); 178 179 181 addFormValue(files, name, entry, null, addSlashesToValues); 182 } 183 } 184 } 185 186 public static void addFormValue(ArrayValue array, 187 String key, 188 String []formValueList, 189 boolean addSlashesToValues) 190 { 191 Value value = new StringValueImpl(formValueList[formValueList.length - 1]); 193 194 addFormValue(array, key, 195 value, 196 formValueList, 197 addSlashesToValues); 198 } 199 200 public static void addFormValue(ArrayValue array, 201 String key, 202 Value formValue, 203 String []formValueList, 204 boolean addSlashesToValues) 205 { 206 int p = key.indexOf('['); 207 int q = key.indexOf(']', p); 208 209 if (p >= 0 && p < q) { 210 String index = key; 211 212 Value keyValue; 213 Value existingValue; 214 215 if (p > 0) { 216 key = key.substring(0, p); 217 218 keyValue = new StringValueImpl(key); 219 existingValue = array.get(keyValue); 220 221 if (existingValue == null || ! existingValue.isset()) { 222 existingValue = new ArrayValueImpl(); 223 array.put(keyValue, existingValue); 224 } 225 else if (! existingValue.isArray()) { 226 existingValue = new ArrayValueImpl().put(existingValue); 227 array.put(keyValue, existingValue); 228 } 229 230 array = (ArrayValue) existingValue; 231 } 232 233 int p1; 234 while ((p1 = index.indexOf('[', q)) > 0) { 235 key = index.substring(p + 1, q); 236 237 if (key.equals("")) { 238 existingValue = new ArrayValueImpl(); 239 array.put(existingValue); 240 } 241 else { 242 keyValue = new StringValueImpl(key); 243 existingValue = array.get(keyValue); 244 245 if (existingValue == null || ! existingValue.isset()) { 246 existingValue = new ArrayValueImpl(); 247 array.put(keyValue, existingValue); 248 } 249 else if (! existingValue.isArray()) { 250 existingValue = new ArrayValueImpl().put(existingValue); 251 array.put(keyValue, existingValue); 252 } 253 } 254 255 array = (ArrayValue) existingValue; 256 257 p = p1; 258 q = index.indexOf(']', p); 259 } 260 261 if (q > 0) 262 index = index.substring(p + 1, q); 263 else 264 index = index.substring(p + 1); 265 266 if (index.equals("")) { 267 if (formValueList != null) { 268 for (int i = 0; i < formValueList.length; i++) { 269 put(array, null, new StringValueImpl(formValueList[i]), addSlashesToValues); 270 } 271 } 272 else 273 array.put(formValue); 274 } 275 else if ('0' <= index.charAt(0) && index.charAt(0) <= '9') 276 put(array, new LongValue(StringValue.toLong(index)), formValue, addSlashesToValues); 277 else 278 put(array, new StringValueImpl(index), formValue, addSlashesToValues); 279 } 280 else { 281 put(array, new StringValueImpl(key), formValue, addSlashesToValues); 282 } 283 } 284 285 private static void put(ArrayValue array, Value key, Value value, boolean addSlashes) 286 { 287 if (addSlashes && (value instanceof StringValue)) { 288 value = StringModule.addslashes(value.toStringValue()); 289 } 290 291 if (key == null) 292 array.put(value); 293 else 294 array.put(key, value); 295 } 296 297 private static String getBoundary(String contentType) 298 { 299 int i = contentType.indexOf("boundary="); 300 if (i < 0) 301 return null; 302 303 i += "boundary=".length(); 304 305 int length = contentType.length(); 306 307 char ch; 308 309 if (length <= i) 310 return null; 311 else if ((ch = contentType.charAt(i)) == '\'') { 312 StringBuilder sb = new StringBuilder (); 313 314 for (i++; i < length && (ch = contentType.charAt(i)) != '\''; i++) { 315 sb.append(ch); 316 } 317 318 return sb.toString(); 319 } 320 else if (ch == '"') { 321 StringBuilder sb = new StringBuilder (); 322 323 for (i++; i < length && (ch = contentType.charAt(i)) != '"'; i++) { 324 sb.append(ch); 325 } 326 327 return sb.toString(); 328 } 329 else { 330 StringBuilder sb = new StringBuilder (); 331 332 for (; i < length && (ch = contentType.charAt(i)) != ' ' && ch != ';'; i++) { 333 sb.append(ch); 334 } 335 336 return sb.toString(); 337 } 338 } 339 340 private static String getAttribute(String attr, String name) 341 { 342 if (attr == null) 343 return null; 344 345 int length = attr.length(); 346 int i = attr.indexOf(name); 347 if (i < 0) 348 return null; 349 350 for (i += name.length(); i < length && attr.charAt(i) != '='; i++) { 351 } 352 353 for (i++; i < length && attr.charAt(i) == ' '; i++) { 354 } 355 356 StringBuilder value = new StringBuilder (); 357 358 if (i < length && attr.charAt(i) == '\'') { 359 for (i++; i < length && attr.charAt(i) != '\''; i++) 360 value.append(attr.charAt(i)); 361 } 362 else if (i < length && attr.charAt(i) == '"') { 363 for (i++; i < length && attr.charAt(i) != '"'; i++) 364 value.append(attr.charAt(i)); 365 } 366 else if (i < length) { 367 char ch; 368 for (; i < length && (ch = attr.charAt(i)) != ' ' && ch != ';'; i++) 369 value.append(ch); 370 } 371 372 return value.toString(); 373 } 374 } 375 376 | Popular Tags |