KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > connection > MultipartForm


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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 JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * Multipart form handling.
48  */

49 class MultipartForm {
50   static final Logger JavaDoc log = Log.open(MultipartForm.class);
51   static final L10N L = new L10N(MultipartForm.class);
52   
53   static void parsePostData(HashMapImpl<String JavaDoc,String JavaDoc[]> table,
54                             ReadStream rawIs, String JavaDoc boundary,
55                             AbstractHttpRequest request,
56                             String JavaDoc javaEncoding,
57                             long uploadMax)
58     throws IOException JavaDoc
59   {
60     MultipartStream ms = new MultipartStream(rawIs, boundary);
61     ms.setEncoding(javaEncoding);
62     ReadStream is;
63
64     while ((is = ms.openRead()) != null) {
65       String JavaDoc attr = (String JavaDoc) ms.getAttribute("content-disposition");
66
67       if (attr == null || ! attr.startsWith("form-data")) {
68         // XXX: is this an error?
69
continue;
70       }
71
72       String JavaDoc name = getAttribute(attr, "name");
73       String JavaDoc filename = getAttribute(attr, "filename");
74
75       if (name == null) {
76         // XXX: is this an error?
77
continue;
78       }
79       else if (filename != null) {
80         String JavaDoc contentType = (String JavaDoc) ms.getAttribute("content-type");
81
82         Path tempDir = CauchoSystem.getWorkPath().lookup("form");
83         try {
84           tempDir.mkdirs();
85         } catch (IOException JavaDoc 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 JavaDoc 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 JavaDoc(tempFile.getLength()));
116           
117           tempFile.remove();
118           
119           throw new IOException JavaDoc(msg);
120         }
121     else if (tempFile.getLength() != totalLength) {
122           String JavaDoc 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 JavaDoc(tempFile.getLength()));
127           
128           tempFile.remove();
129           
130           throw new IOException JavaDoc(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 JavaDoc("Incomplete form");
156     }
157   }
158
159   private static void addTable(HashMapImpl<String JavaDoc,String JavaDoc[]> table, String JavaDoc name, String JavaDoc value)
160   {
161     String JavaDoc []oldArray = table.get(name);
162
163     if (oldArray != null) {
164       String JavaDoc []newArray = new String JavaDoc[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 JavaDoc[] {value});
171   }
172
173   private static String JavaDoc getAttribute(String JavaDoc attr, String JavaDoc 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