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