Index: java/org/apache/commons/httpclient/HttpConnection.java
===================================================================
retrieving revision 1.85
diff -u -r1.85 HttpConnection.java
--- java/org/apache/commons/httpclient/HttpConnection.java 22 Feb 2004 18:08:45 -0000 1.85
+++ java/org/apache/commons/httpclient/HttpConnection.java 12 Apr 2004 02:35:19 -0000
@@ -199,6 +199,15 @@
}
// ------------------------------------------ Attribute Setters and Getters
+
+ /**
+ * Returns the connection socket.
+ *
+ * @return the socket.
+ */
+ protected Socket getSocket() {
+ return this.socket;
+ }
/**
* Returns the host.
Index: src/examples/ProxyTunnelDemo.java
===================================================================
RCS file: src/examples/ProxyTunnelDemo.java
diff -N src/examples/ProxyTunnelDemo.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/examples/ProxyTunnelDemo.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,96 @@
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.net.Socket;
+
+import org.apache.commons.httpclient.ProxyClient;
+import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.commons.httpclient.auth.HttpAuthRealm;
+
+/*
+ * $Header$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ *
+ * Copyright 2002-2004 The Apache Software Foundation
+ *
+ * Licensed 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
+ *
+ * Even though HTTP CONNECT proxying is generally used for HTTPS tunneling, the returned + * socket will not have been wrapped in an SSL socket. + *
+ * + *+ * Both the proxy and destination hosts must be set via the + * {@link #getHostConfiguration() host configuration} prior to calling this method. + *
+ * + * @return the connect response + * + * @throws IOException + * @throws HttpException + * + * @see #getHostConfiguration() + */ + public ConnectResponse connect() throws IOException, HttpException { + + if (!getHostConfiguration().isProxySet()) { + throw new IllegalStateException("proxy host must be configured"); + } + if (!getHostConfiguration().isHostSet()) { + throw new IllegalStateException("destination host must be configured"); + } + + ConnectMethod method = new ConnectMethod(); + method.getParams().setDefaults(getParams()); + + DummyConnectionManager connectionManager = new DummyConnectionManager(); + connectionManager.setConnectionParams(getParams()); + + HttpMethodDirector director = new HttpMethodDirector( + connectionManager, + getHostConfiguration(), + getParams(), + getState() + ); + + director.executeMethod(method); + + ConnectResponse response = new ConnectResponse(); + response.setConnectMethod(method); + + // only set the socket if the connect was successful + if (method.getStatusCode() == HttpStatus.SC_OK) { + response.setSocket(connectionManager.getConnection().getSocket()); + } else { + connectionManager.getConnection().close(); + } + + return response; + } + + /** + * Contains the method used to execute the connect along with the created socket. + */ + public static class ConnectResponse { + + private ConnectMethod connectMethod; + + private Socket socket; + + private ConnectResponse() {} + + /** + * Gets the method that was used to execute the connect. This method is useful for + * analyzing the proxy's response when a connect fails. + * + * @return the connectMethod. + */ + public ConnectMethod getConnectMethod() { + return connectMethod; + } + /** + * @param connectMethod The connectMethod to set. + */ + private void setConnectMethod(ConnectMethod connectMethod) { + this.connectMethod = connectMethod; + } + /** + * Gets the socket connected and authenticated (if appropriate) to the configured + * HTTP proxy, ornull if a connection could not be made. It is the
+ * responsibility of the user to close this socket when it is no longer needed.
+ *
+ * @return the socket.
+ */
+ public Socket getSocket() {
+ return socket;
+ }
+ /**
+ * @param socket The socket to set.
+ */
+ private void setSocket(Socket socket) {
+ this.socket = socket;
+ }
+ }
+
+ /**
+ * A connection manager that creates a single connection. Meant to be used only once.
+ */
+ class DummyConnectionManager implements HttpConnectionManager {
+
+ private HttpConnection httpConnection;
+
+ private HttpParams connectionParams;
+
+ public HttpConnection getConnection() {
+ return httpConnection;
+ }
+
+ public void setConnectionParams(HttpParams httpParams) {
+ this.connectionParams = httpParams;
+ }
+
+ public HttpConnection getConnectionWithTimeout(
+ HostConfiguration hostConfiguration, long timeout) {
+
+ httpConnection = new HttpConnection(hostConfiguration);
+ httpConnection.setHttpConnectionManager(this);
+ httpConnection.getParams().setDefaults(connectionParams);
+ return httpConnection;
+ }
+
+ public HttpConnection getConnection(HostConfiguration hostConfiguration, long timeout)
+ throws HttpException {
+ return getConnectionWithTimeout(hostConfiguration, timeout);
+ }
+
+ public HttpConnection getConnection(HostConfiguration hostConfiguration) {
+ return getConnectionWithTimeout(hostConfiguration, -1);
+ }
+
+ public void releaseConnection(HttpConnection conn) {
+ }
+
+ public HttpConnectionManagerParams getParams() {
+ return null;
+ }
+
+ public void setParams(HttpConnectionManagerParams params) {
+ }
+ }
+}