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 package org.apache.commons.httpclient.params;
31
32 import org.apache.commons.httpclient.HttpMethodRetryHandler;
33 import org.apache.commons.httpclient.HttpVersion;
34 import org.apache.commons.httpclient.cookie.CookiePolicy;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /***
39 * This class represents a collection of HTTP protocol parameters applicable to
40 * {@link org.apache.commons.httpclient.HttpMethod HTTP methods}. Protocol
41 * parameters may be linked together to form a hierarchy. If a particular
42 * parameter value has not been explicitly defined in the collection itself,
43 * its value will be drawn from the parent collection of parameters.
44 *
45 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
46 * @author Christian Kohlschuetter
47 *
48 * @version $Revision: 1.15 $
49 *
50 * @since 3.0
51 */
52 public class HttpMethodParams extends DefaultHttpParams {
53
54 /*** Log object for this class. */
55 private static final Log LOG = LogFactory.getLog(HttpMethodParams.class);
56
57 /***
58 * Defines the content of the <tt>User-Agent</tt> header used by
59 * {@link org.apache.commons.httpclient.HttpMethod HTTP methods}.
60 * <p>
61 * This parameter expects a value of type {@link String}.
62 * </p>
63 */
64 public static final String USER_AGENT = "http.useragent";
65
66 /***
67 * Defines the {@link HttpVersion HTTP protocol version} used by
68 * {@link org.apache.commons.httpclient.HttpMethod HTTP methods} per
69 * default.
70 * <p>
71 * This parameter expects a value of type {@link HttpVersion}.
72 * </p>
73 */
74 public static final String PROTOCOL_VERSION = "http.protocol.version";
75
76 /***
77 * Defines whether {@link org.apache.commons.httpclient.HttpMethod HTTP methods} should
78 * reject ambiguous {@link org.apache.commons.httpclient.StatusLine HTTP status line}.
79 * <p>
80 * This parameter expects a value of type {@link Boolean}.
81 * </p>
82 */
83 public static final String UNAMBIGUOUS_STATUS_LINE = "http.protocol.unambiguous-statusline";
84
85 /***
86 * Defines whether {@link org.apache.commons.httpclient.Cookie cookies} should be put on
87 * a single {@link org.apache.commons.httpclient.Header response header}.
88 * <p>
89 * This parameter expects a value of type {@link Boolean}.
90 * </p>
91 */
92 public static final String SINGLE_COOKIE_HEADER = "http.protocol.single-cookie-header";
93
94 /***
95 * Defines whether responses with an invalid <tt>Transfer-Encoding</tt> header should be
96 * rejected.
97 * <p>
98 * This parameter expects a value of type {@link Boolean}.
99 * </p>
100 */
101 public static final String STRICT_TRANSFER_ENCODING = "http.protocol.strict-transfer-encoding";
102
103 /***
104 * Defines whether the content body sent in response to
105 * {@link org.apache.commons.httpclient.methods.HeadMethod} should be rejected.
106 * <p>
107 * This parameter expects a value of type {@link Boolean}.
108 * </p>
109 */
110 public static final String REJECT_HEAD_BODY = "http.protocol.reject-head-body";
111
112 /***
113 * Sets period of time in milliseconds to wait for a content body sent in response to
114 * {@link org.apache.commons.httpclient.methods.HeadMethod HEAD method} from a
115 * non-compliant server. If the parameter is not set or set to <tt>-1</tt> non-compliant
116 * response body check is disabled.
117 * <p>
118 * This parameter expects a value of type {@link Integer}.
119 * </p>
120 */
121 public static final String HEAD_BODY_CHECK_TIMEOUT = "http.protocol.head-body-timeout";
122
123 /***
124 * <p>
125 * Activates 'Expect: 100-Continue' handshake for the
126 * {@link org.apache.commons.httpclient.methods.ExpectContinueMethod
127 * entity enclosing methods}. The purpose of the 'Expect: 100-Continue'
128 * handshake to allow a client that is sending a request message with
129 * a request body to determine if the origin server is willing to
130 * accept the request (based on the request headers) before the client
131 * sends the request body.
132 * </p>
133 *
134 * <p>
135 * The use of the 'Expect: 100-continue' handshake can result in
136 * noticable peformance improvement for entity enclosing requests
137 * (such as POST and PUT) that require the target server's
138 * authentication.
139 * </p>
140 *
141 * <p>
142 * 'Expect: 100-continue' handshake should be used with
143 * caution, as it may cause problems with HTTP servers and
144 * proxies that do not support HTTP/1.1 protocol.
145 * </p>
146 *
147 * This parameter expects a value of type {@link Boolean}.
148 */
149 public static final String USE_EXPECT_CONTINUE = "http.protocol.expect-continue";
150
151 /***
152 * Defines the charset to be used when encoding
153 * {@link org.apache.commons.httpclient.Credentials}. If not defined then the
154 * {@link #HTTP_ELEMENT_CHARSET} should be used.
155 * <p>
156 * This parameter expects a value of type {@link String}.
157 * </p>
158 */
159 public static final String CREDENTIAL_CHARSET = "http.protocol.credential-charset";
160
161 /***
162 * Defines the charset to be used for encoding HTTP protocol elements.
163 * <p>
164 * This parameter expects a value of type {@link String}.
165 * </p>
166 */
167 public static final String HTTP_ELEMENT_CHARSET = "http.protocol.element-charset";
168
169 /***
170 * Defines the charset to be used for encoding content body.
171 * <p>
172 * This parameter expects a value of type {@link String}.
173 * </p>
174 */
175 public static final String HTTP_CONTENT_CHARSET = "http.protocol.content-charset";
176
177 /***
178 * Defines {@link CookiePolicy cookie policy} to be used for cookie management.
179 * <p>
180 * This parameter expects a value of type {@link String}.
181 * </p>
182 */
183 public static final String COOKIE_POLICY = "http.protocol.cookie-policy";
184
185 /***
186 * Defines HttpClient's behavior when a response provides more bytes than
187 * expected (specified with Content-Length, for example).
188 * <p>
189 * Such surplus data makes the HTTP connection unreliable for keep-alive
190 * requests, as malicious response data (faked headers etc.) can lead to undesired
191 * results on the next request using that connection.
192 * </p>
193 * <p>
194 * If this parameter is set to <code>true</code>, any detection of extra
195 * input data will generate a warning in the log.
196 * </p>
197 * <p>
198 * This parameter expects a value of type {@link Boolean}.
199 * </p>
200 */
201 public static final String WARN_EXTRA_INPUT = "http.protocol.warn-extra-input";
202
203 /***
204 * Defines the maximum number of ignorable lines before we expect
205 * a HTTP response's status code.
206 * <p>
207 * With HTTP/1.1 persistent connections, the problem arises that
208 * broken scripts could return a wrong Content-Length
209 * (there are more bytes sent than specified).<br />
210 * Unfortunately, in some cases, this is not possible after the bad response,
211 * but only before the next one. <br />
212 * So, HttpClient must be able to skip those surplus lines this way.
213 * </p>
214 * <p>
215 * Set this to 0 to disallow any garbage/empty lines before the status line.<br />
216 * To specify no limit, use {@link java.lang.Integer#MAX_VALUE} (default in lenient mode).
217 * </p>
218 *
219 * This parameter expects a value of type {@link Integer}.
220 */
221 public static final String STATUS_LINE_GARBAGE_LIMIT = "http.protocol.status-line-garbage-limit";
222
223 /***
224 * Sets the socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds to be used when executing the method.
225 * A timeout value of zero is interpreted as an infinite timeout.
226 * <p>
227 * This parameter expects a value of type {@link Integer}.
228 * </p>
229 * @see java.net.SocketOptions#SO_TIMEOUT
230 */
231 public static final String SO_TIMEOUT = "http.socket.timeout";
232
233 /***
234 * The key used to look up the date patterns used for parsing. The String patterns are stored
235 * in a {@link java.util.Collection} and must be compatible with
236 * {@link java.text.SimpleDateFormat}.
237 * <p>
238 * This parameter expects a value of type {@link java.util.Collection}.
239 * </p>
240 */
241 public static final String DATE_PATTERNS = "http.dateparser.patterns";
242
243 /***
244 * Sets the method retry handler parameter.
245 * <p>
246 * This parameter expects a value of type {@link HttpMethodRetryHandler}.
247 * </p>
248 */
249 public static final String RETRY_HANDLER = "http.method.retry-handler";
250
251 /***
252 * Sets the maximum buffered response size (in bytes) that triggers no warning. Buffered
253 * responses exceeding this size will trigger a warning in the log.
254 * <p>
255 * This parameter expects a value if type {@link Integer}.
256 * </p>
257 */
258 public static final String BUFFER_WARN_TRIGGER_LIMIT = "http.method.response.buffer.warnlimit";
259
260 /***
261 * Creates a new collection of parameters with the collection returned
262 * by {@link #getDefaultParams()} as a parent. The collection will defer
263 * to its parent for a default value if a particular parameter is not
264 * explicitly set in the collection itself.
265 *
266 * @see #getDefaultParams()
267 */
268 public HttpMethodParams() {
269 super(getDefaultParams());
270 }
271
272 /***
273 * Creates a new collection of parameters with the given parent.
274 * The collection will defer to its parent for a default value
275 * if a particular parameter is not explicitly set in the collection
276 * itself.
277 *
278 * @param defaults the parent collection to defer to, if a parameter
279 * is not explictly set in the collection itself.
280 *
281 * @see #getDefaultParams()
282 */
283 public HttpMethodParams(HttpParams defaults) {
284 super(defaults);
285 }
286
287 /***
288 * Returns the charset to be used for writing HTTP headers.
289 * @return The charset
290 */
291 public String getHttpElementCharset() {
292 String charset = (String) getParameter(HTTP_ELEMENT_CHARSET);
293 if (charset == null) {
294 LOG.warn("HTTP element charset not configured, using US-ASCII");
295 charset = "US-ASCII";
296 }
297 return charset;
298 }
299
300 /***
301 * Sets the charset to be used for writing HTTP headers.
302 * @param charset The charset
303 */
304 public void setHttpElementCharset(String charset) {
305 setParameter(HTTP_ELEMENT_CHARSET, charset);
306 }
307
308 /***
309 * Returns the default charset to be used for writing content body,
310 * when no charset explicitly specified.
311 * @return The charset
312 */
313 public String getContentCharset() {
314 String charset = (String) getParameter(HTTP_CONTENT_CHARSET);
315 if (charset == null) {
316 LOG.warn("Default content charset not configured, using ISO-8859-1");
317 charset = "ISO-8859-1";
318 }
319 return charset;
320 }
321
322 /***
323 * Sets the default charset to be used for writing content body,
324 * when no charset explicitly specified.
325 * @param charset The charset
326 */
327 public void setContentCharset(String charset) {
328 setParameter(HTTP_CONTENT_CHARSET, charset);
329 }
330
331 /***
332 * Returns the charset to be used for {@link org.apache.commons.httpclient.Credentials}. If
333 * not configured the {@link #HTTP_ELEMENT_CHARSET HTTP element charset} is used.
334 * @return The charset
335 */
336 public String getCredentialCharset() {
337 String charset = (String) getParameter(CREDENTIAL_CHARSET);
338 if (charset == null) {
339 LOG.debug("Credential charset not configured, using HTTP element charset");
340 charset = getHttpElementCharset();
341 }
342 return charset;
343 }
344
345 /***
346 * Sets the charset to be used for writing HTTP headers.
347 * @param charset The charset
348 */
349 public void setCredentialCharset(String charset) {
350 setParameter(CREDENTIAL_CHARSET, charset);
351 }
352
353 /***
354 * Returns {@link HttpVersion HTTP protocol version} to be used by the
355 * {@link org.apache.commons.httpclient.HttpMethod HTTP methods} that
356 * this collection of parameters applies to.
357 *
358 * @return {@link HttpVersion HTTP protocol version}
359 */
360 public HttpVersion getVersion() {
361 Object param = getParameter(PROTOCOL_VERSION);
362 if (param == null) {
363 return HttpVersion.HTTP_1_1;
364 }
365 return (HttpVersion)param;
366 }
367
368 /***
369 * Assigns the {@link HttpVersion HTTP protocol version} to be used by the
370 * {@link org.apache.commons.httpclient.HttpMethod HTTP methods} that
371 * this collection of parameters applies to.
372 *
373 * @param version the {@link HttpVersion HTTP protocol version}
374 */
375 public void setVersion(HttpVersion version) {
376 setParameter(PROTOCOL_VERSION, version);
377 }
378
379
380 /***
381 * Returns {@link CookiePolicy cookie policy} to be used by the
382 * {@link org.apache.commons.httpclient.HttpMethod HTTP methods}
383 * this collection of parameters applies to.
384 *
385 * @return {@link CookiePolicy cookie policy}
386 */
387 public String getCookiePolicy() {
388 Object param = getParameter(COOKIE_POLICY);
389 if (param == null) {
390 return CookiePolicy.DEFAULT;
391 }
392 return (String)param;
393 }
394
395 /***
396 * Assigns the {@link CookiePolicy cookie policy} to be used by the
397 * {@link org.apache.commons.httpclient.HttpMethod HTTP methods}
398 * this collection of parameters applies to.
399 *
400 * @param policy the {@link CookiePolicy cookie policy}
401 */
402 public void setCookiePolicy(String policy) {
403 setParameter(COOKIE_POLICY, policy);
404 }
405
406 /***
407 * Returns the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the
408 * timeout for waiting for data. A timeout value of zero is interpreted as an infinite
409 * timeout.
410 *
411 * @return timeout in milliseconds
412 */
413 public int getSoTimeout() {
414 return getIntParameter(SO_TIMEOUT, 0);
415 }
416
417 /***
418 * Sets the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the
419 * timeout for waiting for data. A timeout value of zero is interpreted as an infinite
420 * timeout.
421 *
422 * @param timeout Timeout in milliseconds
423 */
424 public void setSoTimeout(int timeout) {
425 setIntParameter(SO_TIMEOUT, timeout);
426 }
427
428 private static final String[] PROTOCOL_STRICTNESS_PARAMETERS = {
429 UNAMBIGUOUS_STATUS_LINE,
430 SINGLE_COOKIE_HEADER,
431 STRICT_TRANSFER_ENCODING,
432 REJECT_HEAD_BODY,
433 WARN_EXTRA_INPUT
434 };
435
436 /***
437 * Makes the {@link org.apache.commons.httpclient.HttpMethod HTTP methods}
438 * strictly follow the HTTP protocol specification (RFC 2616 and other relevant RFCs).
439 * It must be noted that popular HTTP agents have different degree of HTTP protocol
440 * compliance and some HTTP serves are programmed to expect the behaviour that does not
441 * strictly adhere to the HTTP specification.
442 */
443 public void makeStrict() {
444 setParameters(PROTOCOL_STRICTNESS_PARAMETERS, new Boolean(true));
445 setIntParameter(STATUS_LINE_GARBAGE_LIMIT, 0);
446 }
447
448 /***
449 * Makes the {@link org.apache.commons.httpclient.HttpMethod HTTP methods}
450 * attempt to mimic the exact behaviour of commonly used HTTP agents,
451 * which many HTTP servers expect, even though such behaviour may violate
452 * the HTTP protocol specification (RFC 2616 and other relevant RFCs).
453 */
454 public void makeLenient() {
455 setParameters(PROTOCOL_STRICTNESS_PARAMETERS, new Boolean(false));
456 setIntParameter(STATUS_LINE_GARBAGE_LIMIT, Integer.MAX_VALUE);
457 }
458
459 }