From 266fefa6af03f4b78eefdb0493cb6ad251a21c89 Mon Sep 17 00:00:00 2001 From: Valerii Bondarenko Date: Sun, 20 Nov 2022 12:45:29 +0100 Subject: [PATCH] Add the option to turn off automatic redirect handling --- .../plugins/http_request/HttpRequest.java | 11 ++++++ .../http_request/HttpRequestExecution.java | 13 +++++-- .../plugins/http_request/HttpRequestStep.java | 11 ++++++ .../http_request/HttpRequest/config.jelly | 3 ++ .../http_request/HttpRequestStep/config.jelly | 3 ++ .../HttpRequestStep/help-followRedirects.html | 3 ++ src/main/webapp/help-followRedirects.html | 3 ++ .../plugins/http_request/HttpRequestTest.java | 37 +++++++++++++++++++ .../plugins/http_request/Registers.java | 12 ++++++ 9 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/jenkins/plugins/http_request/HttpRequestStep/help-followRedirects.html create mode 100644 src/main/webapp/help-followRedirects.html diff --git a/src/main/java/jenkins/plugins/http_request/HttpRequest.java b/src/main/java/jenkins/plugins/http_request/HttpRequest.java index b9e95760..37904f7d 100644 --- a/src/main/java/jenkins/plugins/http_request/HttpRequest.java +++ b/src/main/java/jenkins/plugins/http_request/HttpRequest.java @@ -75,6 +75,7 @@ public class HttpRequest extends Builder { private boolean useNtlm = DescriptorImpl.useNtlm; private List customHeaders = DescriptorImpl.customHeaders; private List formData = DescriptorImpl.formData; + private boolean followRedirects = DescriptorImpl.followRedirects; @DataBoundConstructor public HttpRequest(@NonNull String url) { @@ -459,6 +460,15 @@ public void setUseNtlm(boolean useNtlm) { this.useNtlm = useNtlm; } + public boolean isFollowRedirects() { + return this.followRedirects; + } + + @DataBoundSetter + public void setFollowRedirects(boolean followRedirects) { + this.followRedirects = followRedirects; + } + @Extension public static final class DescriptorImpl extends BuildStepDescriptor { public static final boolean ignoreSslErrors = false; @@ -483,6 +493,7 @@ public static final class DescriptorImpl extends BuildStepDescriptor { public static final boolean useNtlm = false; public static final List customHeaders = Collections.emptyList(); public static final List formData = Collections.emptyList(); + public static final boolean followRedirects = true; public DescriptorImpl() { load(); diff --git a/src/main/java/jenkins/plugins/http_request/HttpRequestExecution.java b/src/main/java/jenkins/plugins/http_request/HttpRequestExecution.java index 026c71e1..ec3a7371 100644 --- a/src/main/java/jenkins/plugins/http_request/HttpRequestExecution.java +++ b/src/main/java/jenkins/plugins/http_request/HttpRequestExecution.java @@ -109,6 +109,8 @@ public class HttpRequestExecution extends MasterToSlaveCallable build, TaskListener taskListener) { try { @@ -133,7 +135,7 @@ static HttpRequestExecution from(HttpRequest http, http.getValidResponseCodes(), http.getValidResponseContent(), http.getConsoleLogResponseBody(), outputFile, - ResponseHandle.NONE, + ResponseHandle.NONE, http.isFollowRedirects(), project, run, @@ -164,7 +166,7 @@ static HttpRequestExecution from(HttpRequestStep step, TaskListener taskListener step.getValidResponseCodes(), step.getValidResponseContent(), step.getConsoleLogResponseBody(), outputFile, - step.getResponseHandle(), + step.getResponseHandle(), step.isFollowRedirects(), project, run, taskListener.getLogger()); } @@ -178,13 +180,14 @@ private HttpRequestExecution( String validResponseCodes, String validResponseContent, Boolean consoleLogResponseBody, FilePath outputFile, - ResponseHandle responseHandle, + ResponseHandle responseHandle, boolean followRedirects, Item project, Run run, PrintStream logger ) { this.url = url; this.httpMode = httpMode; this.ignoreSslErrors = ignoreSslErrors; + this.followRedirects = followRedirects; if (StringUtils.isNotBlank(httpProxy)) { this.httpProxy = HttpHost.create(httpProxy); @@ -308,6 +311,10 @@ private ResponseContentSupplier authAndRequest() try { HttpClientBuilder clientBuilder = HttpClientBuilder.create(); + if (!this.followRedirects) { + clientBuilder.disableRedirectHandling(); + } + if (useSystemProperties) { clientBuilder.useSystemProperties(); } diff --git a/src/main/java/jenkins/plugins/http_request/HttpRequestStep.java b/src/main/java/jenkins/plugins/http_request/HttpRequestStep.java index eddec451..8d12da1e 100644 --- a/src/main/java/jenkins/plugins/http_request/HttpRequestStep.java +++ b/src/main/java/jenkins/plugins/http_request/HttpRequestStep.java @@ -62,6 +62,7 @@ public final class HttpRequestStep extends Step { private List formData = DescriptorImpl.formData; private String outputFile = DescriptorImpl.outputFile; private ResponseHandle responseHandle = DescriptorImpl.responseHandle; + private boolean followRedirects = DescriptorImpl.followRedirects; @DataBoundConstructor public HttpRequestStep(@NonNull String url) { @@ -272,6 +273,15 @@ public boolean isUseNtlm() { return useNtlm; } + @DataBoundSetter + public void setFollowRedirects(boolean followRedirects) { + this.followRedirects = followRedirects; + } + + public boolean isFollowRedirects() { + return this.followRedirects; + } + @Override public StepExecution start(StepContext context) { return new Execution(context, this); @@ -325,6 +335,7 @@ public static final class DescriptorImpl extends StepDescriptor { public static final List formData = Collections.emptyList(); public static final String outputFile = ""; public static final ResponseHandle responseHandle = ResponseHandle.STRING; + public static final boolean followRedirects = HttpRequest.DescriptorImpl.followRedirects; @Override public Set> getRequiredContext() { diff --git a/src/main/resources/jenkins/plugins/http_request/HttpRequest/config.jelly b/src/main/resources/jenkins/plugins/http_request/HttpRequest/config.jelly index 934411e6..6ca0d93f 100644 --- a/src/main/resources/jenkins/plugins/http_request/HttpRequest/config.jelly +++ b/src/main/resources/jenkins/plugins/http_request/HttpRequest/config.jelly @@ -76,6 +76,9 @@ + + + diff --git a/src/main/resources/jenkins/plugins/http_request/HttpRequestStep/config.jelly b/src/main/resources/jenkins/plugins/http_request/HttpRequestStep/config.jelly index f6a13a42..3875cb53 100644 --- a/src/main/resources/jenkins/plugins/http_request/HttpRequestStep/config.jelly +++ b/src/main/resources/jenkins/plugins/http_request/HttpRequestStep/config.jelly @@ -65,6 +65,9 @@ + + + diff --git a/src/main/resources/jenkins/plugins/http_request/HttpRequestStep/help-followRedirects.html b/src/main/resources/jenkins/plugins/http_request/HttpRequestStep/help-followRedirects.html new file mode 100644 index 00000000..725aaf74 --- /dev/null +++ b/src/main/resources/jenkins/plugins/http_request/HttpRequestStep/help-followRedirects.html @@ -0,0 +1,3 @@ +
+ This allows to turn off automatic redirect handling +
diff --git a/src/main/webapp/help-followRedirects.html b/src/main/webapp/help-followRedirects.html new file mode 100644 index 00000000..725aaf74 --- /dev/null +++ b/src/main/webapp/help-followRedirects.html @@ -0,0 +1,3 @@ +
+ This allows to turn off automatic redirect handling +
diff --git a/src/test/java/jenkins/plugins/http_request/HttpRequestTest.java b/src/test/java/jenkins/plugins/http_request/HttpRequestTest.java index b677fe9f..d3c75add 100644 --- a/src/test/java/jenkins/plugins/http_request/HttpRequestTest.java +++ b/src/test/java/jenkins/plugins/http_request/HttpRequestTest.java @@ -12,6 +12,7 @@ import static jenkins.plugins.http_request.Registers.registerFormAuth; import static jenkins.plugins.http_request.Registers.registerFormAuthBad; import static jenkins.plugins.http_request.Registers.registerInvalidStatusCode; +import static jenkins.plugins.http_request.Registers.registerRedirects; import static jenkins.plugins.http_request.Registers.registerReqAction; import static jenkins.plugins.http_request.Registers.registerRequestChecker; import static jenkins.plugins.http_request.Registers.registerTimeout; @@ -1054,4 +1055,40 @@ public void nonExistentProxyAuthFailsTheBuild() throws Exception { this.j.assertLogContains("Proxy authentication 'non-existent-key' doesn't exist anymore or is not a username/password credential type", build); } + @Test + public void testFollowRedirects() throws Exception { + registerRequestChecker(HttpMode.HEAD); + registerRedirects(); + + // Prepare HttpRequest + HttpRequest httpRequest = new HttpRequest(baseURL() + "/redirects"); + httpRequest.setHttpMode(HttpMode.HEAD); + + // Run build + FreeStyleProject project = this.j.createFreeStyleProject(); + project.getBuildersList().add(httpRequest); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + this.j.assertBuildStatusSuccess(build); + this.j.assertLogContains("Success: Status code 200 is in the accepted range: 100:399", build); + } + + @Test + public void testNotFollowRedirects() throws Exception { + registerRedirects(); + + // Prepare HttpRequest + HttpRequest httpRequest = new HttpRequest(baseURL() + "/redirects"); + httpRequest.setHttpMode(HttpMode.HEAD); + httpRequest.setFollowRedirects(false); + + // Run build + FreeStyleProject project = this.j.createFreeStyleProject(); + project.getBuildersList().add(httpRequest); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + this.j.assertBuildStatusSuccess(build); + this.j.assertLogContains("Success: Status code 302 is in the accepted range: 100:399", build); + } + } diff --git a/src/test/java/jenkins/plugins/http_request/Registers.java b/src/test/java/jenkins/plugins/http_request/Registers.java index 5fe54ed1..4bcf863d 100644 --- a/src/test/java/jenkins/plugins/http_request/Registers.java +++ b/src/test/java/jenkins/plugins/http_request/Registers.java @@ -394,6 +394,18 @@ void doHandle(String target, Request baseRequest, HttpServletRequest request, Ht }); } + static void registerRedirects() { + registerHandler("/redirects", HttpMode.HEAD, new SimpleHandler() { + @Override + void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { + assertEquals("HEAD", request.getMethod()); + + response.sendRedirect(request.getScheme() + "://" + + request.getServerName() + ":" + request.getServerPort() + "/doHEAD"); + } + }); + } + private static void registerHandler(String target, HttpMode method, SimpleHandler handler) { HttpRequestTestBase.registerHandler(target, method, handler); }