src/java.net.http/share/classes/jdk/internal/net/http/RedirectFilter.java
changeset 58758 2b13d126a2d8
parent 50681 4254bed3c09d
equal deleted inserted replaced
58756:b7aa58d7f5aa 58758:2b13d126a2d8
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    87                 // unexpected but return orig
    87                 // unexpected but return orig
    88                 return orig;
    88                 return orig;
    89         }
    89         }
    90     }
    90     }
    91 
    91 
       
    92     private static boolean isRedirecting(int statusCode) {
       
    93         // < 300: not a redirect codes
       
    94         if (statusCode < 300) return false;
       
    95         // 309-399 Unassigned => don't follow
       
    96         // > 399: not a redirect code
       
    97         if (statusCode > 308) return false;
       
    98         switch (statusCode) {
       
    99             // 300: MultipleChoice => don't follow
       
   100             case 300:
       
   101                 return false;
       
   102             // 304: Not Modified => don't follow
       
   103             case 304:
       
   104                 return false;
       
   105             // 305: Proxy Redirect => don't follow.
       
   106             case 305:
       
   107                 return false;
       
   108             // 306: Unused => don't follow
       
   109             case 306:
       
   110                 return false;
       
   111             // 301, 302, 303, 307, 308: OK to follow.
       
   112             default:
       
   113                 return true;
       
   114         }
       
   115     }
       
   116 
    92     /**
   117     /**
    93      * Checks to see if a new request is needed and returns it.
   118      * Checks to see if a new request is needed and returns it.
    94      * Null means response is ok to return to user.
   119      * Null means response is ok to return to user.
    95      */
   120      */
    96     private HttpRequestImpl handleResponse(Response r) {
   121     private HttpRequestImpl handleResponse(Response r) {
   100         }
   125         }
   101 
   126 
   102         if (rcode == HTTP_NOT_MODIFIED)
   127         if (rcode == HTTP_NOT_MODIFIED)
   103             return null;
   128             return null;
   104 
   129 
   105         if (rcode >= 300 && rcode <= 399) {
   130         if (isRedirecting(rcode)) {
   106             URI redir = getRedirectedURI(r.headers());
   131             URI redir = getRedirectedURI(r.headers());
   107             String newMethod = redirectedMethod(rcode, method);
   132             String newMethod = redirectedMethod(rcode, method);
   108             Log.logTrace("response code: {0}, redirected URI: {1}", rcode, redir);
   133             Log.logTrace("response code: {0}, redirected URI: {1}", rcode, redir);
   109             if (canRedirect(redir) && ++exchange.numberOfRedirects < max_redirects) {
   134             if (canRedirect(redir) && ++exchange.numberOfRedirects < max_redirects) {
   110                 Log.logTrace("redirect to: {0} with method: {1}", redir, newMethod);
   135                 Log.logTrace("redirect to: {0} with method: {1}", redir, newMethod);
   111                 return HttpRequestImpl.newInstanceForRedirection(redir, newMethod, request);
   136                 return HttpRequestImpl.newInstanceForRedirection(redir, newMethod, request, rcode != 303);
   112             } else {
   137             } else {
   113                 Log.logTrace("not redirecting");
   138                 Log.logTrace("not redirecting");
   114                 return null;
   139                 return null;
   115             }
   140             }
   116         }
   141         }