1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2019 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

18
19 package io.undertow.servlet.handlers;
20
21 import io.undertow.server.HttpHandler;
22 import io.undertow.server.HttpServerExchange;
23 import io.undertow.util.Headers;
24 import io.undertow.util.Methods;
25 import io.undertow.util.RedirectBuilder;
26 import io.undertow.util.StatusCodes;
27
28 /**
29  * Handler that redirects the directory requests without trailing slash to the one append trailing slash.
30  *
31  * @author Lin Gao
32  */

33 public class RedirectDirHandler implements HttpHandler {
34
35     private static final String HTTP2_UPGRADE_PREFIX = "h2";
36
37     private final HttpHandler next;
38     private final ServletPathMatches paths;
39
40     public RedirectDirHandler(HttpHandler next, ServletPathMatches paths) {
41         this.next = next;
42         this.paths = paths;
43     }
44
45     @Override
46     public void handleRequest(HttpServerExchange exchange) throws Exception {
47         final String path = exchange.getRelativePath();
48         final ServletPathMatch info = paths.getServletHandlerByPath(path);
49         // https://issues.jboss.org/browse/WFLY-3439
50         // if the request is an upgrade request then we don't want to redirect
51         // as there is a good chance the web socket client won't understand the redirect
52         // we make an exception for HTTP2 upgrade requests, as this would have already be handled at
53         // the connector level if it was going to be handled.
54         String upgradeString = exchange.getRequestHeaders().getFirst(Headers.UPGRADE);
55         boolean isUpgradeRequest = upgradeString != null && !upgradeString.startsWith(HTTP2_UPGRADE_PREFIX);
56         if (info.getType() == ServletPathMatch.Type.REDIRECT && !isUpgradeRequest) {
57             // UNDERTOW-89
58             // we redirect on GET requests to the root context to add an / to the end
59             if (exchange.getRequestMethod().equals(Methods.GET) || exchange.getRequestMethod().equals(Methods.HEAD)) {
60                 exchange.setStatusCode(StatusCodes.FOUND);
61             } else {
62                 exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT);
63             }
64             exchange.getResponseHeaders().put(Headers.LOCATION,
65                     RedirectBuilder.redirect(exchange, exchange.getRelativePath() + "/"true));
66             return;
67         }
68         next.handleRequest(exchange);
69     }
70
71 }
72