1 31 32 package org.apache.commons.httpclient.methods; 33 34 import java.io.IOException ; 35 36 import org.apache.commons.httpclient.HttpConnection; 37 import org.apache.commons.httpclient.HttpException; 38 import org.apache.commons.httpclient.HttpMethodBase; 39 import org.apache.commons.httpclient.HttpState; 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 43 68 public class HeadMethod extends HttpMethodBase { 69 71 72 private static final Log LOG = LogFactory.getLog(HeadMethod.class); 73 74 private int bodyCheckTimeout = -1; 75 76 78 83 public HeadMethod() { 84 setFollowRedirects(true); 85 } 86 87 94 public HeadMethod(String uri) { 95 super(uri); 96 setFollowRedirects(true); 97 } 98 99 101 108 public String getName() { 109 return "HEAD"; 110 } 111 112 125 public void recycle() { 126 super.recycle(); 127 setFollowRedirects(true); 128 } 129 130 150 protected void readResponseBody(HttpState state, HttpConnection conn) 151 throws IOException { 152 LOG.trace( 153 "enter HeadMethod.readResponseBody(HttpState, HttpConnection)"); 154 155 if (this.bodyCheckTimeout < 0) { 156 responseBodyConsumed(); 157 } else { 158 if (LOG.isDebugEnabled()) { 159 LOG.debug("Check for non-compliant response body. Timeout in " 160 + this.bodyCheckTimeout + " ms"); 161 } 162 boolean responseAvailable = false; 163 try { 164 responseAvailable = conn.isResponseAvailable(this.bodyCheckTimeout); 165 } catch (IOException e) { 166 LOG.debug("An IOException occurred while testing if a response was available," 167 + " we will assume one is not.", 168 e); 169 responseAvailable = false; 170 } 171 if (responseAvailable) { 172 if (isStrictMode()) { 173 throw new HttpException( 174 "Body content may not be sent in response to HTTP HEAD request"); 175 } else { 176 LOG.warn("Body content returned in response to HTTP HEAD"); 177 } 178 super.readResponseBody(state, conn); 179 } 180 } 181 182 } 183 184 191 public int getBodyCheckTimeout() { 192 return this.bodyCheckTimeout; 193 } 194 195 202 public void setBodyCheckTimeout(int timeout) { 203 this.bodyCheckTimeout = timeout; 204 } 205 206 } 207 | Popular Tags |