1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/HeadMethod.java,v 1.19 2003/05/12 01:17:49 mbecke Exp $
3 * $Revision: 1.19 $
4 * $Date: 2003/05/12 01:17:49 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * [Additional notices, if required by prior licensing conditions]
61 *
62 */
63
64 package org.apache.commons.httpclient.methods;
65
66 import java.io.IOException;
67
68 import org.apache.commons.httpclient.HttpConnection;
69 import org.apache.commons.httpclient.HttpException;
70 import org.apache.commons.httpclient.HttpMethodBase;
71 import org.apache.commons.httpclient.HttpState;
72 import org.apache.commons.logging.Log;
73 import org.apache.commons.logging.LogFactory;
74
75 /***
76 * Implements the HTTP HEAD specification.
77 * <p>
78 * The HTTP HEAD method is defined in section 8.2 of
79 * <a href="http://www.ietf.org/rfc/rfc1945.txt">RFC1945</a>:
80 * <blockquote>
81 * The HEAD method is identical to GET except that the server MUST NOT
82 * return a message-body in the response. The metainformation contained
83 * in the HTTP headers in response to a HEAD request SHOULD be identical
84 * to the information sent in response to a GET request. This method can
85 * be used for obtaining metainformation about the entity implied by the
86 * request without transferring the entity-body itself. This method is
87 * often used for testing hypertext links for validity, accessibility,
88 * and recent modification.
89 * </blockquote>
90 * </p>
91 *
92 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
93 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
94 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
95 * @author <a href="mailto:oleg@ural.ru">oleg Kalnichevski</a>
96 *
97 * @version $Revision: 1.19 $
98 * @since 1.0
99 */
100 public class HeadMethod extends HttpMethodBase {
101 //~ Static variables/initializers ������������������������������������������
102
103 /*** Log object for this class. */
104 private static final Log LOG = LogFactory.getLog(HeadMethod.class);
105
106 private int bodyCheckTimeout = -1; /* Disabled per default */
107
108 //~ Constructors �����������������������������������������������������������
109
110 /***
111 * No-arg constructor.
112 *
113 * @since 1.0
114 */
115 public HeadMethod() {
116 setFollowRedirects(true);
117 }
118
119 /***
120 * Constructor specifying a URI.
121 *
122 * @param uri either an absolute or relative URI
123 *
124 * @since 1.0
125 */
126 public HeadMethod(String uri) {
127 super(uri);
128 setFollowRedirects(true);
129 }
130
131 //~ Methods ����������������������������������������������������������������
132
133 /***
134 * Returns <tt>"HEAD"</tt>.
135 *
136 * @return <tt>"HEAD"</tt>
137 *
138 * @since 2.0
139 */
140 public String getName() {
141 return "HEAD";
142 }
143
144 /***
145 * Override recycle to reset redirects default.
146 *
147 * @since 1.0
148 */
149 public void recycle() {
150 super.recycle();
151 setFollowRedirects(true);
152 }
153
154 /***
155 * Overrides {@link HttpMethodBase} method to <i>not</i> read a response
156 * body, despite the presence of a <tt>Content-Length</tt> or
157 * <tt>Transfer-Encoding</tt> header.
158 *
159 * @param state shared http state
160 * @param conn the http connection to read from
161 *
162 * @throws IOException when there's an error reading data
163 *
164 * @since 2.0
165 */
166 protected void readResponseBody(HttpState state, HttpConnection conn)
167 throws IOException {
168 LOG.trace(
169 "enter HeadMethod.readResponseBody(HttpState, HttpConnection)");
170
171 if (this.bodyCheckTimeout < 0) {
172 responseBodyConsumed();
173 } else {
174 if (LOG.isDebugEnabled()) {
175 LOG.debug("Check for non-compliant response body. Timeout in "
176 + this.bodyCheckTimeout + " ms");
177 }
178 if (conn.isResponseAvailable(this.bodyCheckTimeout)) {
179 if (isStrictMode()) {
180 throw new HttpException(
181 "Body content may not be sent in response to HTTP HEAD request");
182 } else {
183 LOG.warn("Body content returned in response to HTTP HEAD");
184 }
185 super.readResponseBody(state, conn);
186 }
187 }
188
189 }
190
191 /***
192 * Return non-compliant response body check timeout.
193 *
194 * @return The period of time in milliseconds to wait for a response
195 * body from a non-compliant server. <tt>-1</tt> returned when
196 * non-compliant response body check is disabled
197 */
198 public int getBodyCheckTimeout() {
199 return this.bodyCheckTimeout;
200 }
201
202 /***
203 * Set non-compliant response body check timeout.
204 *
205 * @param timeout The period of time in milliseconds to wait for a response
206 * body from a non-compliant server. <tt>-1</tt> can be used to
207 * disable non-compliant response body check
208 */
209 public void setBodyCheckTimeout(int timeout) {
210 this.bodyCheckTimeout = timeout;
211 }
212
213 }
This page was automatically generated by Maven