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 package HTTPClient;
34
35
36
37
38
39
40
41
42
43 public final class Request implements RoRequest, Cloneable
44 {
45
46 private static final NVPair[] empty = new NVPair[0];
47
48
49 private HTTPConnection connection;
50
51
52 private String method;
53
54
55 private String req_uri;
56
57
58 private NVPair[] headers;
59
60
61 private byte[] data;
62
63
64 private HttpOutputStream stream;
65
66
67 private boolean allow_ui;
68
69
70
71 long delay_entity = 0;
72
73
74 int num_retries = 0;
75
76
77 boolean dont_pipeline = false;
78
79
80 boolean aborted = false;
81
82
83 boolean internal_subrequest = false;
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 public Request(HTTPConnection con, String method, String req_uri,
100 NVPair[] headers, byte[] data, HttpOutputStream stream,
101 boolean allow_ui)
102 {
103 this.connection = con;
104 this.method = method;
105 setRequestURI(req_uri);
106 setHeaders(headers);
107 this.data = data;
108 this.stream = stream;
109 this.allow_ui = allow_ui;
110 }
111
112
113
114
115
116
117
118 public HTTPConnection getConnection()
119 {
120 return connection;
121 }
122
123
124
125
126 public void setConnection(HTTPConnection con)
127 {
128 this.connection = con;
129 }
130
131
132
133
134
135 public String getMethod()
136 {
137 return method;
138 }
139
140
141
142
143 public void setMethod(String method)
144 {
145 this.method = method;
146 }
147
148
149
150
151
152 public String getRequestURI()
153 {
154 return req_uri;
155 }
156
157
158
159
160 public void setRequestURI(String req_uri)
161 {
162 if (req_uri != null && req_uri.trim().length() > 0)
163 {
164 req_uri = req_uri.trim();
165 if (req_uri.charAt(0) != '/' && !req_uri.equals("*") &&
166 !method.equals("CONNECT") && !isAbsolute(req_uri))
167 req_uri = "/" + req_uri;
168 this.req_uri = req_uri;
169 }
170 else
171 this.req_uri = "/";
172 }
173
174 private static final boolean isAbsolute(String uri)
175 {
176 char ch = '\0';
177 int pos = 0, len = uri.length();
178 while (pos < len && (ch = uri.charAt(pos)) != ':' &&
179 ch != '/' && ch != '?' && ch != '#')
180 pos++;
181
182 return (ch == ':');
183 }
184
185
186
187
188
189 public NVPair[] getHeaders()
190 {
191 return headers;
192 }
193
194
195
196
197 public void setHeaders(NVPair[] headers)
198 {
199 if (headers != null)
200 this.headers = headers;
201 else
202 this.headers = empty;
203 }
204
205
206
207
208
209 public byte[] getData()
210 {
211 return data;
212 }
213
214
215
216
217 public void setData(byte[] data)
218 {
219 this.data = data;
220 }
221
222
223
224
225
226 public HttpOutputStream getStream()
227 {
228 return stream;
229 }
230
231
232
233
234 public void setStream(HttpOutputStream stream)
235 {
236 this.stream = stream;
237 }
238
239
240
241
242
243
244 public boolean allowUI()
245 {
246 return allow_ui;
247 }
248
249
250
251
252
253 public void setAllowUI(boolean allow_ui)
254 {
255 this.allow_ui = allow_ui;
256 }
257
258
259
260
261
262 public Object clone()
263 {
264 Request cl;
265 try
266 { cl = (Request) super.clone(); }
267 catch (CloneNotSupportedException cnse)
268 { throw new InternalError(cnse.toString());
269
270 cl.headers = new NVPair[headers.length];
271 System.arraycopy(headers, 0, cl.headers, 0, headers.length);
272
273 return cl;
274 }
275
276
277
278
279
280
281
282 public void copyFrom(Request other)
283 {
284 this.connection = other.connection;
285 this.method = other.method;
286 this.req_uri = other.req_uri;
287 this.headers = other.headers;
288 this.data = other.data;
289 this.stream = other.stream;
290 this.allow_ui = other.allow_ui;
291 this.delay_entity = other.delay_entity;
292 this.num_retries = other.num_retries;
293 this.dont_pipeline = other.dont_pipeline;
294 this.aborted = other.aborted;
295 this.internal_subrequest = other.internal_subrequest;
296 }
297
298
299
300
301
302 public String toString()
303 {
304 return getClass().getName() + ": " + method + " " + req_uri;
305 }
306 }