-
Notifications
You must be signed in to change notification settings - Fork 974
/
SinglePortConfidentialityHandler.java
73 lines (65 loc) · 2.63 KB
/
SinglePortConfidentialityHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.undertow.security.handlers;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import java.net.URI;
import java.net.URISyntaxException;
/**
* An extension to {@link AbstractConfidentialityHandler} that uses the Host header from the incoming message and specifies the
* confidential address by just switching the port.
*
* @author <a href="mailto:darran.lofthouse@jboss.com">Darran Lofthouse</a>
*/
public class SinglePortConfidentialityHandler extends AbstractConfidentialityHandler {
private final int redirectPort;
public SinglePortConfidentialityHandler(final HttpHandler next, final int redirectPort) {
super(next);
this.redirectPort = redirectPort == 443 ? -1 : redirectPort;
}
@Override
protected URI getRedirectURI(HttpServerExchange exchange) throws URISyntaxException {
return getRedirectURI(exchange, redirectPort);
}
protected URI getRedirectURI(final HttpServerExchange exchange, final int port) throws URISyntaxException {
final StringBuilder uriBuilder = new StringBuilder();
uriBuilder.append("https://").append(exchange.getHostName());
if (port > 0) {
uriBuilder.append(":").append(port);
}
String uri = exchange.getRequestURI();
if(exchange.isHostIncludedInRequestURI()) {
int slashCount = 0;
for(int i = 0; i < uri.length(); ++i) {
if(uri.charAt(i) == '/') {
slashCount++;
if(slashCount == 3) {
uri = uri.substring(i);
break;
}
}
}
}
uriBuilder.append(uri);
final String queryString = exchange.getQueryString();
if (queryString != null && !queryString.isEmpty()) {
uriBuilder.append("?").append(queryString);
}
return new URI(uriBuilder.toString());
}
}