You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2007/05/13 21:58:23 UTC

svn commit: r537650 - in /jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client: DefaultClientRequestDirector.java TunnelRefusedException.java

Author: olegk
Date: Sun May 13 12:58:22 2007
New Revision: 537650

URL: http://svn.apache.org/viewvc?view=rev&rev=537650
Log:
Correctly propagate HTTP response back to the caller in case request tunneling has been refused by the proxy

Added:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/TunnelRefusedException.java   (with props)
Modified:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java?view=diff&rev=537650&r1=537649&r2=537650
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java Sun May 13 12:58:22 2007
@@ -65,6 +65,7 @@
 import org.apache.http.conn.ManagedClientConnection;
 import org.apache.http.conn.RouteDirector;
 import org.apache.http.conn.Scheme;
+import org.apache.http.entity.BufferedHttpEntity;
 import org.apache.http.message.BasicHttpRequest;
 import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.params.HttpParams;
@@ -262,7 +263,15 @@
                 if (managedConn == null || !managedConn.isOpen()) {
                     managedConn = allocateConnection(route);
                 }
-                establishRoute(route, context);
+                try {
+                    establishRoute(route, context);
+                } catch (TunnelRefusedException ex) {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug(ex.getMessage());
+                    }
+                    response = ex.getResponse();
+                    break;
+                }
 
                 context.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST,
                         roureq.getRoute().getTargetHost());
@@ -451,17 +460,24 @@
         HttpRequest connect = createConnectRequest(route, context);
         //@@@ authenticate here, in method above, or in request interceptor?
 
-        HttpResponse connected =
+        HttpResponse response =
             requestExec.execute(connect, managedConn, context);
         managedConn.markReusable();
-        int status = connected.getStatusLine().getStatusCode();
-        //@@@ log something about the response?
+        int status = response.getStatusLine().getStatusCode();
 
-        //@@@ check for proxy authentication challenge, repeat with auth
+        if (status < 200) {
+            throw new HttpException("Unexpected response to CONNECT request: " +
+                    response.getStatusLine());
+        }
+        
+        // Buffer response
+        if (response.getEntity() != null) {
+            response.setEntity(new BufferedHttpEntity(response.getEntity()));
+        }
 
-        if ((status < 200) || (status > 299)) {
-            throw new HttpException("CONNECT refused by proxy: " +
-                                    connected.getStatusLine());
+        if (status > 299) {
+            throw new TunnelRefusedException("CONNECT refused by proxy: " +
+                    response.getStatusLine(), response);
         }
 
         // How to decide on security of the tunnelled connection?

Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/TunnelRefusedException.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/TunnelRefusedException.java?view=auto&rev=537650
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/TunnelRefusedException.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/TunnelRefusedException.java Sun May 13 12:58:22 2007
@@ -0,0 +1,52 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * 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
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.client;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpResponse;
+
+public class TunnelRefusedException extends HttpException {
+
+    private static final long serialVersionUID = -8646722842745617323L;
+
+    private final HttpResponse response;
+    
+    public TunnelRefusedException(final String message, final HttpResponse response) {
+        super(message);
+        this.response = response;
+    }
+
+    public HttpResponse getResponse() {
+        return this.response;
+    }
+
+}

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/TunnelRefusedException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/TunnelRefusedException.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/TunnelRefusedException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain