/*
* $Header: /usr/local/cvsroot/3rdParty_sources/commons-httpclient/org/apache/commons/httpclient/ConnectMethod.java,v 1.2 2016/04/04 12:28:48 marcin Exp $
* $Revision: 1.2 $
* $Date: 2016/04/04 12:28:48 $
*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* This implementation adds User-Agent, Host, * and Proxy-Authorization headers, when appropriate. *
* * @param state the client state * @param conn the {@link HttpConnection} the headers will eventually be * written to * @throws IOException when an error occurs writing the request * @throws HttpException when a HTTP protocol error occurs * * @see #writeRequestHeaders */ protected void addRequestHeaders(HttpState state, HttpConnection conn) throws IOException, HttpException { LOG.trace("enter ConnectMethod.addRequestHeaders(HttpState, " + "HttpConnection)"); addUserAgentRequestHeader(state, conn); addHostRequestHeader(state, conn); addProxyConnectionHeader(state, conn); } /** * Execute this method and create a tunneled HttpConnection. If the method * is successful (i.e. the status is a 2xx) tunnelCreated() will be called * on the connection. * * @param state the current http state * @param conn the connection to write to * @return the http status code from execution * @throws HttpException when an error occurs writing the headers * @throws IOException when an error occurs writing the headers * * @see HttpConnection#tunnelCreated() */ public int execute(HttpState state, HttpConnection conn) throws IOException, HttpException { LOG.trace("enter ConnectMethod.execute(HttpState, HttpConnection)"); int code = super.execute(state, conn); if (LOG.isDebugEnabled()) { LOG.debug("CONNECT status code " + code); } return code; } /** * Special Connect request. * * @param state the current http state * @param conn the connection to write to * @throws IOException when an error occurs writing the request * @throws HttpException when an error occurs writing the request */ protected void writeRequestLine(HttpState state, HttpConnection conn) throws IOException, HttpException { StringBuffer buffer = new StringBuffer(); buffer.append(getName()); buffer.append(' '); if (this.targethost != null) { buffer.append(getPath()); } else { int port = conn.getPort(); if (port == -1) { port = conn.getProtocol().getDefaultPort(); } buffer.append(conn.getHost()); buffer.append(':'); buffer.append(port); } buffer.append(" "); buffer.append(getEffectiveVersion()); String line = buffer.toString(); conn.printLine(line, getParams().getHttpElementCharset()); if (Wire.HEADER_WIRE.enabled()) { Wire.HEADER_WIRE.output(line); } } /** * Returnstrue
if the status code is anything other than
* SC_OK, false
otherwise.
*
* @see HttpMethodBase#shouldCloseConnection(HttpConnection)
* @see HttpStatus#SC_OK
*
* @return true
if the connection should be closed
*/
protected boolean shouldCloseConnection(HttpConnection conn) {
if (getStatusCode() == HttpStatus.SC_OK) {
Header connectionHeader = null;
if (!conn.isTransparent()) {
connectionHeader = getResponseHeader("proxy-connection");
}
if (connectionHeader == null) {
connectionHeader = getResponseHeader("connection");
}
if (connectionHeader != null) {
if (connectionHeader.getValue().equalsIgnoreCase("close")) {
if (LOG.isWarnEnabled()) {
LOG.warn("Invalid header encountered '" + connectionHeader.toExternalForm()
+ "' in response " + getStatusLine().toString());
}
}
}
return false;
} else {
return super.shouldCloseConnection(conn);
}
}
/** Log object for this class. */
private static final Log LOG = LogFactory.getLog(ConnectMethod.class);
}