1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/ExpectContinueMethod.java,v 1.13 2004/05/08 10:12:08 olegk Exp $
  3. * $Revision: 1.13 $
  4. * $Date: 2004/05/08 10:12:08 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 2003-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.methods;
  30. import java.io.IOException;
  31. import org.apache.commons.httpclient.HttpConnection;
  32. import org.apache.commons.httpclient.HttpException;
  33. import org.apache.commons.httpclient.HttpMethodBase;
  34. import org.apache.commons.httpclient.HttpState;
  35. import org.apache.commons.httpclient.HttpVersion;
  36. import org.apache.commons.httpclient.params.HttpMethodParams;
  37. import org.apache.commons.logging.Log;
  38. import org.apache.commons.logging.LogFactory;
  39. /**
  40. * <p>
  41. * This abstract class serves as a foundation for all HTTP methods
  42. * that support 'Expect: 100-continue' handshake.
  43. * </p>
  44. *
  45. * <p>
  46. * The purpose of the 100 (Continue) status (refer to section 10.1.1
  47. * of the RFC 2616 for more details) is to allow a client that is
  48. * sending a request message with a request body to determine if the
  49. * origin server is willing to accept the request (based on the request
  50. * headers) before the client sends the request body. In some cases,
  51. * it might either be inappropriate or highly inefficient for the
  52. * client to send the body if the server will reject the message
  53. * without looking at the body.
  54. * </p>
  55. *
  56. * <p>
  57. * 'Expect: 100-continue' handshake should be used with caution,
  58. * as it may cause problems with HTTP servers and proxies that
  59. * do not support HTTP/1.1 protocol.
  60. * </p>
  61. *
  62. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  63. *
  64. * @since 2.0beta1
  65. */
  66. public abstract class ExpectContinueMethod extends HttpMethodBase {
  67. /** LOG object for this class. */
  68. private static final Log LOG = LogFactory.getLog(ExpectContinueMethod.class);
  69. /**
  70. * No-arg constructor.
  71. *
  72. * @since 2.0
  73. */
  74. public ExpectContinueMethod() {
  75. super();
  76. }
  77. /**
  78. * Constructor specifying a URI.
  79. *
  80. * @param uri either an absolute or relative URI
  81. *
  82. * @since 2.0
  83. */
  84. public ExpectContinueMethod(String uri) {
  85. super(uri);
  86. }
  87. /**
  88. * <p>
  89. * Returns <tt>true</tt> if the 'Expect: 100-Continue' handshake
  90. * is activated. The purpose of the 'Expect: 100-Continue'
  91. * handshake to allow a client that is sending a request message
  92. * with a request body to determine if the origin server is
  93. * willing to accept the request (based on the request headers)
  94. * before the client sends the request body.
  95. * </p>
  96. *
  97. * @return <tt>true</tt> if 'Expect: 100-Continue' handshake is to
  98. * be used, <tt>false</tt> otherwise.
  99. *
  100. * @since 2.0beta1
  101. *
  102. * @deprecated Use {@link HttpMethodParams}
  103. *
  104. * @see #getParams()
  105. * @see HttpMethodParams
  106. * @see HttpMethodParams#USE_EXPECT_CONTINUE
  107. */
  108. public boolean getUseExpectHeader() {
  109. return getParams().getBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false);
  110. }
  111. /**
  112. * <p>
  113. * Activates 'Expect: 100-Continue' handshake. The purpose of
  114. * the 'Expect: 100-Continue' handshake to allow a client that is
  115. * sending a request message with a request body to determine if
  116. * the origin server is willing to accept the request (based on
  117. * the request headers) before the client sends the request body.
  118. * </p>
  119. *
  120. * <p>
  121. * The use of the 'Expect: 100-continue' handshake can result in
  122. * noticable peformance improvement for entity enclosing requests
  123. * (such as POST and PUT) that require the target server's
  124. * authentication.
  125. * </p>
  126. *
  127. * <p>
  128. * 'Expect: 100-continue' handshake should be used with
  129. * caution, as it may cause problems with HTTP servers and
  130. * proxies that do not support HTTP/1.1 protocol.
  131. * </p>
  132. *
  133. * @param value boolean value
  134. *
  135. * @since 2.0beta1
  136. *
  137. * @deprecated Use {@link HttpMethodParams}
  138. *
  139. * @see #getParams()
  140. * @see HttpMethodParams
  141. * @see HttpMethodParams#USE_EXPECT_CONTINUE
  142. */
  143. public void setUseExpectHeader(boolean value) {
  144. getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, value);
  145. }
  146. /**
  147. * Returns <tt>true</tt> if there is a request body to be sent.
  148. * 'Expect: 100-continue' handshake may not be used if request
  149. * body is not present
  150. *
  151. * @return boolean
  152. *
  153. * @since 2.0beta1
  154. */
  155. protected abstract boolean hasRequestContent();
  156. /**
  157. * Sets the <tt>Expect</tt> header if it has not already been set,
  158. * in addition to the "standard" set of headers.
  159. *
  160. * @param state the {@link HttpState state} information associated with this method
  161. * @param conn the {@link HttpConnection connection} used to execute
  162. * this HTTP method
  163. *
  164. * @throws IOException if an I/O (transport) error occurs. Some transport exceptions
  165. * can be recovered from.
  166. * @throws HttpException if a protocol exception occurs. Usually protocol exceptions
  167. * cannot be recovered from.
  168. */
  169. protected void addRequestHeaders(HttpState state, HttpConnection conn)
  170. throws IOException, HttpException {
  171. LOG.trace("enter ExpectContinueMethod.addRequestHeaders(HttpState, HttpConnection)");
  172. super.addRequestHeaders(state, conn);
  173. // If the request is being retried, the header may already be present
  174. boolean headerPresent = (getRequestHeader("Expect") != null);
  175. // See if the expect header should be sent
  176. // = HTTP/1.1 or higher
  177. // = request body present
  178. if (getParams().isParameterTrue(HttpMethodParams.USE_EXPECT_CONTINUE)
  179. && getEffectiveVersion().greaterEquals(HttpVersion.HTTP_1_1)
  180. && hasRequestContent())
  181. {
  182. if (!headerPresent) {
  183. setRequestHeader("Expect", "100-continue");
  184. }
  185. } else {
  186. if (headerPresent) {
  187. removeRequestHeader("Expect");
  188. }
  189. }
  190. }
  191. }