mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/protocol/RequestProxyAuthentication.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /*
     2  * ====================================================================
     3  * Licensed to the Apache Software Foundation (ASF) under one
     4  * or more contributor license agreements.  See the NOTICE file
     5  * distributed with this work for additional information
     6  * regarding copyright ownership.  The ASF licenses this file
     7  * to you under the Apache License, Version 2.0 (the
     8  * "License"); you may not use this file except in compliance
     9  * with the License.  You may obtain a copy of the License at
    10  *
    11  *   http://www.apache.org/licenses/LICENSE-2.0
    12  *
    13  * Unless required by applicable law or agreed to in writing,
    14  * software distributed under the License is distributed on an
    15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    16  * KIND, either express or implied.  See the License for the
    17  * specific language governing permissions and limitations
    18  * under the License.
    19  * ====================================================================
    20  *
    21  * This software consists of voluntary contributions made by many
    22  * individuals on behalf of the Apache Software Foundation.  For more
    23  * information on the Apache Software Foundation, please see
    24  * <http://www.apache.org/>.
    25  *
    26  */
    28 package ch.boye.httpclientandroidlib.client.protocol;
    30 import java.io.IOException;
    32 import ch.boye.httpclientandroidlib.annotation.Immutable;
    34 import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog;
    35 /* LogFactory removed by HttpClient for Android script. */
    36 import ch.boye.httpclientandroidlib.Header;
    37 import ch.boye.httpclientandroidlib.HttpException;
    38 import ch.boye.httpclientandroidlib.HttpRequest;
    39 import ch.boye.httpclientandroidlib.HttpRequestInterceptor;
    40 import ch.boye.httpclientandroidlib.auth.AUTH;
    41 import ch.boye.httpclientandroidlib.auth.AuthScheme;
    42 import ch.boye.httpclientandroidlib.auth.AuthState;
    43 import ch.boye.httpclientandroidlib.auth.AuthenticationException;
    44 import ch.boye.httpclientandroidlib.auth.ContextAwareAuthScheme;
    45 import ch.boye.httpclientandroidlib.auth.Credentials;
    46 import ch.boye.httpclientandroidlib.conn.HttpRoutedConnection;
    47 import ch.boye.httpclientandroidlib.conn.routing.HttpRoute;
    48 import ch.boye.httpclientandroidlib.protocol.ExecutionContext;
    49 import ch.boye.httpclientandroidlib.protocol.HttpContext;
    51 /**
    52  * Generates authentication header for the proxy host, if required,
    53  * based on the actual state of the HTTP authentication context.
    54  *
    55  * @since 4.0
    56  */
    57 @Immutable
    58 public class RequestProxyAuthentication implements HttpRequestInterceptor {
    60     public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass());
    62     public RequestProxyAuthentication() {
    63         super();
    64     }
    66     @SuppressWarnings("deprecation")
    67     public void process(final HttpRequest request, final HttpContext context)
    68             throws HttpException, IOException {
    69         if (request == null) {
    70             throw new IllegalArgumentException("HTTP request may not be null");
    71         }
    72         if (context == null) {
    73             throw new IllegalArgumentException("HTTP context may not be null");
    74         }
    76         if (request.containsHeader(AUTH.PROXY_AUTH_RESP)) {
    77             return;
    78         }
    80         HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
    81                 ExecutionContext.HTTP_CONNECTION);
    82         if (conn == null) {
    83             this.log.debug("HTTP connection not set in the context");
    84             return;
    85         }
    86         HttpRoute route = conn.getRoute();
    87         if (route.isTunnelled()) {
    88             return;
    89         }
    91         // Obtain authentication state
    92         AuthState authState = (AuthState) context.getAttribute(
    93                 ClientContext.PROXY_AUTH_STATE);
    94         if (authState == null) {
    95             this.log.debug("Proxy auth state not set in the context");
    96             return;
    97         }
    99         AuthScheme authScheme = authState.getAuthScheme();
   100         if (authScheme == null) {
   101             return;
   102         }
   104         Credentials creds = authState.getCredentials();
   105         if (creds == null) {
   106             this.log.debug("User credentials not available");
   107             return;
   108         }
   109         if (authState.getAuthScope() != null || !authScheme.isConnectionBased()) {
   110             try {
   111                 Header header;
   112                 if (authScheme instanceof ContextAwareAuthScheme) {
   113                     header = ((ContextAwareAuthScheme) authScheme).authenticate(
   114                             creds, request, context);
   115                 } else {
   116                     header = authScheme.authenticate(creds, request);
   117                 }
   118                 request.addHeader(header);
   119             } catch (AuthenticationException ex) {
   120                 if (this.log.isErrorEnabled()) {
   121                     this.log.error("Proxy authentication error: " + ex.getMessage());
   122                 }
   123             }
   124         }
   125     }
   127 }

mercurial