-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathRequest.java
More file actions
306 lines (256 loc) · 7.25 KB
/
Request.java
File metadata and controls
306 lines (256 loc) · 7.25 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* @(#)Request.java 0.3-3 06/05/2001
*
* This file is part of the HTTPClient package
* Copyright (C) 1996-2001 Ronald Tschal�r
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA
*
* For questions, suggestions, bug-reports, enhancement-requests etc.
* I may be contacted at:
*
* ronald@innovation.ch
*
* The HTTPClient's home page is located at:
*
* http://www.innovation.ch/java/HTTPClient/
*
*/
package HTTPClient;
/**
* This class represents an http request. It's used by classes which
* implement the HTTPClientModule interface.
*
* @version 0.3-3 06/05/2001
* @author Ronald Tschal�r
*/
public final class Request implements RoRequest, Cloneable
{
/** null headers */
private static final NVPair[] empty = new NVPair[0];
/** the current HTTPConnection */
private HTTPConnection connection;
/** the request method to be used (e.g. GET, POST, etc) */
private String method;
/** the request-uri */
private String req_uri;
/** the headers to be used */
private NVPair[] headers;
/** the entity (if any) */
private byte[] data;
/** or an output stream on which the entity will be written */
private HttpOutputStream stream;
/** are modules allowed to popup windows or otherwise prompt user? */
private boolean allow_ui;
/** number of millisecs to wait for an error from the server before sending
the entity (used when retrying requests) */
long delay_entity = 0;
/** number of retries so far */
int num_retries = 0;
/** disable pipelining of following request */
boolean dont_pipeline = false;
/** was this request aborted by the user? */
boolean aborted = false;
/** is this an internally generated subrequest? */
boolean internal_subrequest = false;
// Constructors
/**
* Creates a new request structure.
*
* @param con the current HTTPConnection
* @param method the request method
* @param req_uri the request-uri
* @param headers the request headers
* @param data the entity as a byte[]
* @param stream the entity as a stream
* @param allow_ui allow user interaction
*/
public Request(HTTPConnection con, String method, String req_uri,
NVPair[] headers, byte[] data, HttpOutputStream stream,
boolean allow_ui)
{
this.connection = con;
this.method = method;
setRequestURI(req_uri);
setHeaders(headers);
this.data = data;
this.stream = stream;
this.allow_ui = allow_ui;
}
// Methods
/**
* @return the HTTPConnection this request is associated with
*/
public HTTPConnection getConnection()
{
return connection;
}
/**
* @param con the HTTPConnection this request is associated with
*/
public void setConnection(HTTPConnection con)
{
this.connection = con;
}
/**
* @return the request method
*/
public String getMethod()
{
return method;
}
/**
* @param method the request method (e.g. GET, POST, etc)
*/
public void setMethod(String method)
{
this.method = method;
}
/**
* @return the request-uri
*/
public String getRequestURI()
{
return req_uri;
}
/**
* @param req_uri the request-uri
*/
public void setRequestURI(String req_uri)
{
if (req_uri != null && req_uri.trim().length() > 0)
{
req_uri = req_uri.trim();
if (req_uri.charAt(0) != '/' && !req_uri.equals("*") &&
!method.equals("CONNECT") && !isAbsolute(req_uri))
req_uri = "/" + req_uri;
this.req_uri = req_uri;
}
else
this.req_uri = "/";
}
private static final boolean isAbsolute(String uri)
{
char ch = '\0';
int pos = 0, len = uri.length();
while (pos < len && (ch = uri.charAt(pos)) != ':' &&
ch != '/' && ch != '?' && ch != '#')
pos++;
return (ch == ':');
}
/**
* @return the headers making up this request
*/
public NVPair[] getHeaders()
{
return headers;
}
/**
* @param headers the headers for this request
*/
public void setHeaders(NVPair[] headers)
{
if (headers != null)
this.headers = headers;
else
this.headers = empty;
}
/**
* @return the body of this request
*/
public byte[] getData()
{
return data;
}
/**
* @param data the entity for this request
*/
public void setData(byte[] data)
{
this.data = data;
}
/**
* @return the output stream on which the body is written
*/
public HttpOutputStream getStream()
{
return stream;
}
/**
* @param stream an output stream on which the entity is written
*/
public void setStream(HttpOutputStream stream)
{
this.stream = stream;
}
/**
* @return true if the modules or handlers for this request may popup
* windows or otherwise interact with the user
*/
public boolean allowUI()
{
return allow_ui;
}
/**
* @param allow_ui are modules and handlers allowed to popup windows or
* otherwise interact with the user?
*/
public void setAllowUI(boolean allow_ui)
{
this.allow_ui = allow_ui;
}
/**
* @return a clone of this request object
*/
public Object clone()
{
Request cl;
try
{ cl = (Request) super.clone(); }
catch (CloneNotSupportedException cnse)
{ throw new InternalError(cnse.toString()); /* shouldn't happen */ }
cl.headers = new NVPair[headers.length];
System.arraycopy(headers, 0, cl.headers, 0, headers.length);
return cl;
}
/**
* Copy all the fields from <var>other</var> to this request.
*
* @param other the Request to copy from
*/
public void copyFrom(Request other)
{
this.connection = other.connection;
this.method = other.method;
this.req_uri = other.req_uri;
this.headers = other.headers;
this.data = other.data;
this.stream = other.stream;
this.allow_ui = other.allow_ui;
this.delay_entity = other.delay_entity;
this.num_retries = other.num_retries;
this.dont_pipeline = other.dont_pipeline;
this.aborted = other.aborted;
this.internal_subrequest = other.internal_subrequest;
}
/**
* @return a string containing the method and request-uri
*/
public String toString()
{
return getClass().getName() + ": " + method + " " + req_uri;
}
}