src/java.net.http/share/classes/java/net/http/internal/RedirectFilter.java
branchhttp-client-branch
changeset 56089 42208b2f224e
parent 56079 d23b02f37fce
equal deleted inserted replaced
56088:38fac6d0521d 56089:42208b2f224e
       
     1 /*
       
     2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package java.net.http.internal;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.io.UncheckedIOException;
       
    30 import java.net.URI;
       
    31 import java.net.http.HttpClient;
       
    32 import java.net.http.HttpHeaders;
       
    33 import java.net.http.internal.common.Utils;
       
    34 
       
    35 class RedirectFilter implements HeaderFilter {
       
    36 
       
    37     HttpRequestImpl request;
       
    38     HttpClientImpl client;
       
    39     HttpClient.Redirect policy;
       
    40     String method;
       
    41     MultiExchange<?> exchange;
       
    42     static final int DEFAULT_MAX_REDIRECTS = 5;
       
    43     URI uri;
       
    44 
       
    45     static final int max_redirects = Utils.getIntegerNetProperty(
       
    46             "jdk.httpclient.redirects.retrylimit", DEFAULT_MAX_REDIRECTS
       
    47     );
       
    48 
       
    49     // A public no-arg constructor is required by FilterFactory
       
    50     public RedirectFilter() {}
       
    51 
       
    52     @Override
       
    53     public synchronized void request(HttpRequestImpl r, MultiExchange<?> e) throws IOException {
       
    54         this.request = r;
       
    55         this.client = e.client();
       
    56         this.policy = client.followRedirects();
       
    57 
       
    58         this.method = r.method();
       
    59         this.uri = r.uri();
       
    60         this.exchange = e;
       
    61     }
       
    62 
       
    63     @Override
       
    64     public synchronized HttpRequestImpl response(Response r) throws IOException {
       
    65         return handleResponse(r);
       
    66     }
       
    67 
       
    68     /**
       
    69      * checks to see if new request needed and returns it.
       
    70      * Null means response is ok to return to user.
       
    71      */
       
    72     private HttpRequestImpl handleResponse(Response r) {
       
    73         int rcode = r.statusCode();
       
    74         if (rcode == 200 || policy == HttpClient.Redirect.NEVER) {
       
    75             return null;
       
    76         }
       
    77         if (rcode >= 300 && rcode <= 399) {
       
    78             URI redir = getRedirectedURI(r.headers());
       
    79             if (canRedirect(redir) && ++exchange.numberOfRedirects < max_redirects) {
       
    80                 //System.out.println("Redirecting to: " + redir);
       
    81                 return new HttpRequestImpl(redir, method, request);
       
    82             } else {
       
    83                 //System.out.println("Redirect: giving up");
       
    84                 return null;
       
    85             }
       
    86         }
       
    87         return null;
       
    88     }
       
    89 
       
    90     private URI getRedirectedURI(HttpHeaders headers) {
       
    91         URI redirectedURI;
       
    92         redirectedURI = headers.firstValue("Location")
       
    93                 .map(URI::create)
       
    94                 .orElseThrow(() -> new UncheckedIOException(
       
    95                         new IOException("Invalid redirection")));
       
    96 
       
    97         // redirect could be relative to original URL, but if not
       
    98         // then redirect is used.
       
    99         redirectedURI = uri.resolve(redirectedURI);
       
   100         return redirectedURI;
       
   101     }
       
   102 
       
   103     private boolean canRedirect(URI redir) {
       
   104         String newScheme = redir.getScheme();
       
   105         String oldScheme = uri.getScheme();
       
   106         switch (policy) {
       
   107             case ALWAYS:
       
   108                 return true;
       
   109             case NEVER:
       
   110                 return false;
       
   111             case SECURE:
       
   112                 return newScheme.equalsIgnoreCase("https");
       
   113             case SAME_PROTOCOL:
       
   114                 return newScheme.equalsIgnoreCase(oldScheme);
       
   115             default:
       
   116                 throw new InternalError();
       
   117         }
       
   118     }
       
   119 }