1 31 32 package org.apache.commons.httpclient; 33 34 import java.io.IOException ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 49 public class ConnectMethod extends HttpMethodBase { 50 51 public static final String NAME = "CONNECT"; 52 53 59 public ConnectMethod(HttpMethod method) { 60 LOG.trace("enter ConnectMethod(HttpMethod)"); 61 this.method = method; 62 } 63 64 69 public String getName() { 70 return NAME; 71 } 72 73 74 87 protected void addAuthorizationRequestHeader(HttpState state, HttpConnection conn) 88 throws IOException , HttpException { 89 } 91 92 105 protected void addContentLengthRequestHeader(HttpState state, HttpConnection conn) 106 throws IOException , HttpException { 107 } 109 110 123 protected void addCookieRequestHeader(HttpState state, HttpConnection conn) 124 throws IOException , HttpException { 125 } 127 128 129 146 protected void addRequestHeaders(HttpState state, HttpConnection conn) 147 throws IOException , HttpException { 148 LOG.trace("enter ConnectMethod.addRequestHeaders(HttpState, " 149 + "HttpConnection)"); 150 addUserAgentRequestHeader(state, conn); 151 addHostRequestHeader(state, conn); 152 addProxyAuthorizationRequestHeader(state, conn); 153 addProxyConnectionHeader(state, conn); 154 } 155 156 165 public int execute(HttpState state, HttpConnection conn) 166 throws IOException , HttpException { 167 168 LOG.trace("enter ConnectMethod.execute(HttpState, HttpConnection)"); 169 int code = super.execute(state, conn); 170 LOG.debug("CONNECT status code " + code); 171 if ((code >= 200) && (code < 300)) { 172 conn.tunnelCreated(); 173 code = method.execute(state, conn); 174 } else { 175 187 LOG.debug("CONNECT failed, fake the response for the original method"); 188 if (method instanceof HttpMethodBase) { 189 ((HttpMethodBase) method).fakeResponse( 197 getStatusLine(), 198 getResponseHeaderGroup(), 199 getResponseStream() 200 ); 201 } else { 202 releaseConnection(); 203 } 204 } 205 return code; 206 } 207 208 216 protected void writeRequestLine(HttpState state, HttpConnection conn) 217 throws IOException , HttpException { 218 int port = conn.getPort(); 219 if (port == -1) { 220 port = conn.getProtocol().getDefaultPort(); 221 } 222 StringBuffer buffer = new StringBuffer (); 223 buffer.append(getName()); 224 buffer.append(' '); 225 buffer.append(conn.getHost()); 226 if (port > -1) { 227 buffer.append(':'); 228 buffer.append(port); 229 } 230 buffer.append(" HTTP/1.1"); 231 String line = buffer.toString(); 232 conn.printLine(line); 233 if (Wire.HEADER_WIRE.enabled()) { 234 Wire.HEADER_WIRE.output(line); 235 } 236 } 237 238 249 protected boolean shouldCloseConnection(HttpConnection conn) { 250 if (getStatusCode() == HttpStatus.SC_OK) { 251 Header connectionHeader = null; 252 if (!conn.isTransparent()) { 253 connectionHeader = getResponseHeader("proxy-connection"); 254 } 255 if (connectionHeader == null) { 256 connectionHeader = getResponseHeader("connection"); 257 } 258 if (connectionHeader != null) { 259 if (connectionHeader.getValue().equalsIgnoreCase("close")) { 260 if (LOG.isWarnEnabled()) { 261 LOG.warn("Invalid header encountered '" + connectionHeader.toExternalForm() 262 + "' in response " + getStatusLine().toString()); 263 } 264 } 265 } 266 return false; 267 } else { 268 return super.shouldCloseConnection(conn); 269 } 270 } 271 272 273 private static final Log LOG = LogFactory.getLog(ConnectMethod.class); 274 275 276 private HttpMethod method; 277 278 } 279 | Popular Tags |