src/java.net.http/share/classes/jdk/internal/net/http/RedirectFilter.java
author chegar
Tue, 13 Mar 2018 14:16:52 +0000
branchhttp-client-branch
changeset 56289 904b7c299931
parent 56281 7fdd89dabab2
child 56451 9585061fdb04
permissions -rw-r--r--
http-client-branch: expand RedirectMethodChange test to cover HTTP2 and HTTPS
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     1
/*
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents: 56010
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     4
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    15
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    20
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    23
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    26
package jdk.internal.net.http;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    27
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    28
import java.io.IOException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    29
import java.io.UncheckedIOException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    30
import java.net.URI;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56079
diff changeset
    31
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56079
diff changeset
    32
import java.net.http.HttpHeaders;
56186
261b5b524dd3 http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
chegar
parents: 56092
diff changeset
    33
import jdk.internal.net.http.common.Log;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    34
import jdk.internal.net.http.common.Utils;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    35
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    36
class RedirectFilter implements HeaderFilter {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    37
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    38
    HttpRequestImpl request;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    39
    HttpClientImpl client;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
    HttpClient.Redirect policy;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    41
    String method;
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    42
    MultiExchange<?> exchange;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    43
    static final int DEFAULT_MAX_REDIRECTS = 5;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    44
    URI uri;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    45
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    46
    static final int max_redirects = Utils.getIntegerNetProperty(
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    47
            "jdk.httpclient.redirects.retrylimit", DEFAULT_MAX_REDIRECTS
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    48
    );
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    49
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    50
    // A public no-arg constructor is required by FilterFactory
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    51
    public RedirectFilter() {}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    52
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    53
    @Override
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    54
    public synchronized void request(HttpRequestImpl r, MultiExchange<?> e) throws IOException {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    55
        this.request = r;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    56
        this.client = e.client();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    57
        this.policy = client.followRedirects();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    58
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    59
        this.method = r.method();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    60
        this.uri = r.uri();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    61
        this.exchange = e;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    62
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    63
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    64
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    65
    public synchronized HttpRequestImpl response(Response r) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    66
        return handleResponse(r);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    67
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    68
56289
904b7c299931 http-client-branch: expand RedirectMethodChange test to cover HTTP2 and HTTPS
chegar
parents: 56281
diff changeset
    69
    private static String redirectedMethod(int statusCode, String orig) {
56281
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    70
        switch (statusCode) {
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    71
            case 301:
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    72
            case 302:
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    73
                return orig.equals("POST") ? "GET" : orig;
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    74
            case 303:
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    75
                return "GET";
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    76
            case 307:
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    77
            case 308:
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    78
                return orig;
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    79
            default:
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    80
                // unexpected but return orig
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    81
                return orig;
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    82
        }
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    83
    }
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    84
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    85
    /**
56186
261b5b524dd3 http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
chegar
parents: 56092
diff changeset
    86
     * Checks to see if a new request is needed and returns it.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    87
     * Null means response is ok to return to user.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    88
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    89
    private HttpRequestImpl handleResponse(Response r) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    90
        int rcode = r.statusCode();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    91
        if (rcode == 200 || policy == HttpClient.Redirect.NEVER) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    92
            return null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    93
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    94
        if (rcode >= 300 && rcode <= 399) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    95
            URI redir = getRedirectedURI(r.headers());
56281
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    96
            String newMethod = redirectedMethod(rcode, method);
56186
261b5b524dd3 http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
chegar
parents: 56092
diff changeset
    97
            Log.logTrace("response code: {0}, redirected URI: {1}", rcode, redir);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    98
            if (canRedirect(redir) && ++exchange.numberOfRedirects < max_redirects) {
56281
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
    99
                Log.logTrace("redirect to: {0} with method: {1}", redir, newMethod);
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
   100
                return HttpRequestImpl.newInstanceForRedirection(redir, newMethod, request);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   101
            } else {
56186
261b5b524dd3 http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
chegar
parents: 56092
diff changeset
   102
                Log.logTrace("not redirecting");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   103
                return null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   104
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   105
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   106
        return null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   107
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   108
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   109
    private URI getRedirectedURI(HttpHeaders headers) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   110
        URI redirectedURI;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   111
        redirectedURI = headers.firstValue("Location")
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   112
                .map(URI::create)
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   113
                .orElseThrow(() -> new UncheckedIOException(
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   114
                        new IOException("Invalid redirection")));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   115
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   116
        // redirect could be relative to original URL, but if not
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   117
        // then redirect is used.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   118
        redirectedURI = uri.resolve(redirectedURI);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   119
        return redirectedURI;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   120
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   121
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   122
    private boolean canRedirect(URI redir) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   123
        String newScheme = redir.getScheme();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   124
        String oldScheme = uri.getScheme();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   125
        switch (policy) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   126
            case ALWAYS:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   127
                return true;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   128
            case NEVER:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   129
                return false;
56281
7fdd89dabab2 http-client-branch: changed Redirect enums and implemented RFC 7231 auto redirect POST to GETs
michaelm
parents: 56186
diff changeset
   130
            case NORMAL:
56186
261b5b524dd3 http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
chegar
parents: 56092
diff changeset
   131
                return newScheme.equalsIgnoreCase(oldScheme)
261b5b524dd3 http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
chegar
parents: 56092
diff changeset
   132
                        || newScheme.equalsIgnoreCase("https");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   133
            default:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   134
                throw new InternalError();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   135
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   136
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   137
}