-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormInput.java
More file actions
212 lines (162 loc) · 7.01 KB
/
FormInput.java
File metadata and controls
212 lines (162 loc) · 7.01 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package javaxt.http.servlet;
import java.io.IOException;
//******************************************************************************
//** FormInput
//******************************************************************************
/**
* Used to represent a form input found in the body of an http request.
*
******************************************************************************/
public class FormInput {
private String name;
private ServletInputStream is;
private boolean readFully = false;
private FormValue value;
private String boundary;
private java.util.HashMap<String, String> metadata;
private String contentDisposition;
private String fileName;
//**************************************************************************
//** Constructor
//**************************************************************************
/** Used to create a new instance of this class using
*
*/
protected FormInput(ServletInputStream is, FormInput prevInput, String boundary) throws IOException {
this.is = is;
this.boundary = boundary;
//Read all remaining bytes from the previous input as needed.
if (prevInput!=null){
if (!prevInput.readFully) prevInput.getValue().readFully();
prevInput.readFully = true;
}
if (boundary.equals("&")){ //Content-Type: application/x-www-form-urlencoded
//TODO: Check first byte. See if it start with a "&".
//byte a = (byte) is.read();
//if (a=='&') a = (byte) is.read();
java.io.ByteArrayOutputStream bas = new java.io.ByteArrayOutputStream();
while (true){
byte a = (byte) is.read();
if (a=='=' || a==-1) break;
bas.write(a);
}
if (bas.size()<1) throw new IOException();
name = bas.toString();
value = new FormValue(this);
}
else{ //Content-Type: multipart/form-data
if (prevInput==null) is.readLine();
//Extract form metadata (e.g. "Content-Type", "Content-Disposition", etc.)
metadata = new java.util.HashMap<String, String>();
while (true){
String str = new String(is.readLine(), "UTF-8").trim();
if (str.length()==0) break;
String key = str.substring(0, str.indexOf(":")).trim();
String val = str.substring(str.indexOf(":")+1).trim();
metadata.put(key, val);
}
//Extract input name from the "Content-Disposition"
java.util.Iterator<String> it = metadata.keySet().iterator();
while (it.hasNext()){
String key = it.next();
if (key.equalsIgnoreCase("Content-Disposition")){
contentDisposition = metadata.get(key);
if (contentDisposition!=null){
for (String str : contentDisposition.split(";")){
str = str.trim();
if (str.contains("=")){
String val = str.substring(str.indexOf("\"")+1, str.lastIndexOf("\"")).trim();
if (str.toLowerCase().startsWith("name=")){
name = val;
}
else if (str.toLowerCase().startsWith("filename=")){
if (val.length()>0) fileName = val;
}
}
}
}
break;
}
}
//Throw an exception if no form name is found. This is our key to
//stop parsing form data.
if (name==null) throw new IOException();
//If the input is a file and no filename is supplied, don't generate
//a value.
if (this.isFile()){
if (fileName==null) return;
}
//If we're still here, get the form value.
value = new FormValue(this);
}
}
//**************************************************************************
//** getContentDisposition
//**************************************************************************
/** Returns the "Content-Disposition" value associated with this form input.
* This attribute is unique to "multipart/form-data".
*/
public String getContentDisposition(){
return contentDisposition;
}
//**************************************************************************
//** getFileName
//**************************************************************************
/** Returns the "filename" attribute found in the "Content-Disposition"
* value associated with this form input. This attribute is unique to
* "multipart/form-data".
*/
public String getFileName(){
return fileName;
}
//**************************************************************************
//** isFile
//**************************************************************************
/** Convenience method used to determine whether the input is associated
* with a file upload. Returns true if a "filename" attribute is found in
* the "Content-Disposition" metadata. This attribute is unique to
* "multipart/form-data".
*/
public boolean isFile(){
return fileName!=null;
}
//**************************************************************************
//** getMetadata
//**************************************************************************
/** Used to return metadata associated with this form input. This attribute
* is unique to "multipart/form-data". Each input may include information
* such as the "Content-Disposition", "Content-Type", and
* "Content-Transfer-Encoding".
*/
public java.util.HashMap<String, String> getMetadata(){
return metadata;
}
//**************************************************************************
//** getName
//**************************************************************************
/** Used to return the name associated with this form input. */
public String getName(){
return name;
}
//**************************************************************************
//** getValue
//**************************************************************************
/** Used to return the value associated with this form input. */
public FormValue getValue(){
return value;
}
protected String getBoundary(){
return boundary;
}
protected ServletInputStream getInputStream(){
return is;
}
/** Used to set flag used to indicate whether the entire input has been read. */
protected void setReadFully(){
this.readFully = true;
}
/** Returns boolean used to indicate whether the entire input has been read.*/
protected boolean isFullyRead(){
return readFully;
}
}