-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide access to the default redirect handler
This is useful for building redirect handlers for clients that need to delegate to the default behavior. In Quarkus we will leverage this to get rid of the deprecated (and since removed) calls to redirectHandler
- Loading branch information
Showing
3 changed files
with
90 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
vertx-core/src/main/java/io/vertx/core/http/impl/DefaultRedirectHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.vertx.core.http.impl; | ||
|
||
import static io.vertx.core.http.HttpHeaders.CONTENT_LENGTH; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.http.HttpClientResponse; | ||
import io.vertx.core.http.HttpHeaders; | ||
import io.vertx.core.http.HttpMethod; | ||
import io.vertx.core.http.RequestOptions; | ||
import java.net.URI; | ||
import java.util.function.Function; | ||
|
||
|
||
public class DefaultRedirectHandler implements Function<HttpClientResponse, Future<RequestOptions>> { | ||
|
||
public DefaultRedirectHandler() { | ||
} | ||
|
||
@Override | ||
public Future<RequestOptions> apply(HttpClientResponse resp) { | ||
try { | ||
int statusCode = resp.statusCode(); | ||
String location = resp.getHeader(HttpHeaders.LOCATION); | ||
if (location != null && (statusCode == 301 || statusCode == 302 || statusCode == 303 || statusCode == 307 | ||
|| statusCode == 308)) { | ||
HttpMethod m = resp.request().getMethod(); | ||
if (statusCode == 303) { | ||
m = HttpMethod.GET; | ||
} else if (m != HttpMethod.GET && m != HttpMethod.HEAD) { | ||
return null; | ||
} | ||
URI uri = HttpUtils.resolveURIReference(resp.request().absoluteURI(), location); | ||
boolean ssl; | ||
int port = uri.getPort(); | ||
String protocol = uri.getScheme(); | ||
char chend = protocol.charAt(protocol.length() - 1); | ||
if (chend == 'p') { | ||
ssl = false; | ||
if (port == -1) { | ||
port = 80; | ||
} | ||
} else if (chend == 's') { | ||
ssl = true; | ||
if (port == -1) { | ||
port = 443; | ||
} | ||
} else { | ||
return null; | ||
} | ||
String requestURI = uri.getPath(); | ||
if (requestURI == null || requestURI.isEmpty()) { | ||
requestURI = "/"; | ||
} | ||
String query = uri.getQuery(); | ||
if (query != null) { | ||
requestURI += "?" + query; | ||
} | ||
RequestOptions options = new RequestOptions(); | ||
options.setMethod(m); | ||
options.setHost(uri.getHost()); | ||
options.setPort(port); | ||
options.setSsl(ssl); | ||
options.setURI(requestURI); | ||
options.setHeaders(resp.request().headers()); | ||
options.removeHeader(CONTENT_LENGTH); | ||
return Future.succeededFuture(options); | ||
} | ||
return null; | ||
} catch (Exception e) { | ||
return Future.failedFuture(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters