1 28 29 package com.caucho.server.connection; 30 31 import com.caucho.log.Log; 32 import com.caucho.server.util.CauchoSystem; 33 import com.caucho.util.CharBuffer; 34 import com.caucho.util.HashMapImpl; 35 import com.caucho.util.L10N; 36 import com.caucho.vfs.MultipartStream; 37 import com.caucho.vfs.Path; 38 import com.caucho.vfs.ReadStream; 39 import com.caucho.vfs.TempBuffer; 40 import com.caucho.vfs.WriteStream; 41 42 import java.io.IOException ; 43 import java.util.logging.Level ; 44 import java.util.logging.Logger ; 45 46 49 class MultipartForm { 50 static final Logger log = Log.open(MultipartForm.class); 51 static final L10N L = new L10N(MultipartForm.class); 52 53 static void parsePostData(HashMapImpl<String ,String []> table, 54 ReadStream rawIs, String boundary, 55 AbstractHttpRequest request, 56 String javaEncoding, 57 long uploadMax) 58 throws IOException 59 { 60 MultipartStream ms = new MultipartStream(rawIs, boundary); 61 ms.setEncoding(javaEncoding); 62 ReadStream is; 63 64 while ((is = ms.openRead()) != null) { 65 String attr = (String ) ms.getAttribute("content-disposition"); 66 67 if (attr == null || ! attr.startsWith("form-data")) { 68 continue; 70 } 71 72 String name = getAttribute(attr, "name"); 73 String filename = getAttribute(attr, "filename"); 74 75 if (name == null) { 76 continue; 78 } 79 else if (filename != null) { 80 String contentType = (String ) ms.getAttribute("content-type"); 81 82 Path tempDir = CauchoSystem.getWorkPath().lookup("form"); 83 try { 84 tempDir.mkdirs(); 85 } catch (IOException e) { 86 } 87 Path tempFile = tempDir.createTempFile("form", ".tmp"); 88 request.addCloseOnExit(tempFile); 89 90 WriteStream os = tempFile.openWrite(); 91 92 TempBuffer tempBuffer = TempBuffer.allocate(); 93 byte []buf = tempBuffer.getBuffer(); 94 95 int totalLength = 0; 96 97 try { 98 int len; 99 100 while ((len = is.read(buf, 0, buf.length)) > 0) { 101 os.write(buf, 0, len); 102 totalLength += len; 103 } 104 } finally { 105 os.close(); 106 107 TempBuffer.free(tempBuffer); 108 } 109 110 if (uploadMax > 0 && uploadMax < tempFile.getLength()) { 111 String msg = L.l("multipart form data `{0}' too large", 112 "" + tempFile.getLength()); 113 request.setAttribute("caucho.multipart.form.error", msg); 114 request.setAttribute("caucho.multipart.form.error.size", 115 new Long (tempFile.getLength())); 116 117 tempFile.remove(); 118 119 throw new IOException (msg); 120 } 121 else if (tempFile.getLength() != totalLength) { 122 String msg = L.l("multipart form upload failed (possibly due to full disk)."); 123 124 request.setAttribute("caucho.multipart.form.error", msg); 125 request.setAttribute("caucho.multipart.form.error.size", 126 new Long (tempFile.getLength())); 127 128 tempFile.remove(); 129 130 throw new IOException (msg); 131 } 132 133 addTable(table, name, tempFile.getNativePath()); 134 addTable(table, name + ".file", tempFile.getNativePath()); 135 addTable(table, name + ".filename", filename); 136 addTable(table, name + ".content-type", contentType); 137 138 if (log.isLoggable(Level.FINE)) 139 log.fine("mp-file: " + name + "(filename:" + filename + ")"); 140 } else { 141 CharBuffer value = new CharBuffer(); 142 int ch; 143 144 for (ch = is.readChar(); ch >= 0; ch = is.readChar()) 145 value.append((char) ch); 146 147 if (log.isLoggable(Level.FINE)) 148 log.fine("mp-form: " + name + "=" + value); 149 150 addTable(table, name, value.toString()); 151 } 152 } 153 154 if (! ms.isComplete()) { 155 throw new IOException ("Incomplete form"); 156 } 157 } 158 159 private static void addTable(HashMapImpl<String ,String []> table, String name, String value) 160 { 161 String []oldArray = table.get(name); 162 163 if (oldArray != null) { 164 String []newArray = new String [oldArray.length + 1]; 165 System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); 166 newArray[oldArray.length] = value; 167 table.put(name, newArray); 168 } 169 else 170 table.put(name, new String [] {value}); 171 } 172 173 private static String getAttribute(String attr, String name) 174 { 175 if (attr == null) 176 return null; 177 178 int length = attr.length(); 179 int i = attr.indexOf(name); 180 if (i < 0) 181 return null; 182 183 for (i += name.length(); i < length && attr.charAt(i) != '='; i++) { 184 } 185 186 for (i++; i < length && attr.charAt(i) == ' '; i++) { 187 } 188 189 CharBuffer value = CharBuffer.allocate(); 190 if (i < length && attr.charAt(i) == '\'') { 191 for (i++; i < length && attr.charAt(i) != '\''; i++) 192 value.append(attr.charAt(i)); 193 } 194 else if (i < length && attr.charAt(i) == '"') { 195 for (i++; i < length && attr.charAt(i) != '"'; i++) 196 value.append(attr.charAt(i)); 197 } 198 else if (i < length) { 199 char ch; 200 for (; i < length && (ch = attr.charAt(i)) != ' ' && ch != ';'; i++) 201 value.append(ch); 202 } 203 204 return value.close(); 205 } 206 } 207 | Popular Tags |