Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the option to turn off automatic redirect handling #124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/jenkins/plugins/http_request/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class HttpRequest extends Builder {
private boolean useNtlm = DescriptorImpl.useNtlm;
private List<HttpRequestNameValuePair> customHeaders = DescriptorImpl.customHeaders;
private List<HttpRequestFormDataPart> formData = DescriptorImpl.formData;
private boolean followRedirects = DescriptorImpl.followRedirects;

@DataBoundConstructor
public HttpRequest(@NonNull String url) {
Expand Down Expand Up @@ -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<Builder> {
public static final boolean ignoreSslErrors = false;
Expand All @@ -483,6 +493,7 @@ public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
public static final boolean useNtlm = false;
public static final List<HttpRequestNameValuePair> customHeaders = Collections.emptyList();
public static final List<HttpRequestFormDataPart> formData = Collections.emptyList();
public static final boolean followRedirects = true;

public DescriptorImpl() {
load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public class HttpRequestExecution extends MasterToSlaveCallable<ResponseContentS
private final OutputStream remoteLogger;
private transient PrintStream localLogger;

private final boolean followRedirects;

static HttpRequestExecution from(HttpRequest http,
EnvVars envVars, AbstractBuild<?, ?> build, TaskListener taskListener) {
try {
Expand All @@ -133,7 +135,7 @@ static HttpRequestExecution from(HttpRequest http,

http.getValidResponseCodes(), http.getValidResponseContent(),
http.getConsoleLogResponseBody(), outputFile,
ResponseHandle.NONE,
ResponseHandle.NONE, http.isFollowRedirects(),

project,
run,
Expand Down Expand Up @@ -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());
}

Expand All @@ -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);
Expand Down Expand Up @@ -308,6 +311,10 @@ private ResponseContentSupplier authAndRequest()
try {
HttpClientBuilder clientBuilder = HttpClientBuilder.create();

if (!this.followRedirects) {
clientBuilder.disableRedirectHandling();
}

if (useSystemProperties) {
clientBuilder.useSystemProperties();
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/jenkins/plugins/http_request/HttpRequestStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public final class HttpRequestStep extends Step {
private List<HttpRequestFormDataPart> formData = DescriptorImpl.formData;
private String outputFile = DescriptorImpl.outputFile;
private ResponseHandle responseHandle = DescriptorImpl.responseHandle;
private boolean followRedirects = DescriptorImpl.followRedirects;

@DataBoundConstructor
public HttpRequestStep(@NonNull String url) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -325,6 +335,7 @@ public static final class DescriptorImpl extends StepDescriptor {
public static final List <HttpRequestFormDataPart> 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<? extends Class<?>> getRequiredContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
<f:entry field="quiet" title="Quiet all output?" help="/plugin/http_request/help-quiet.html">
<f:booleanRadio />
</f:entry>
<f:entry field="followRedirects" title="Enable redirect handling?" help="/plugin/http_request/help-followRedirects.html">
<f:booleanRadio />
</f:entry>
</f:section>
</f:advanced>
</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
<f:entry field="useSystemProperties" title="Use system properties" help="/plugin/http_request/use-system-properties.html">
<f:booleanRadio />
</f:entry>
<f:entry field="followRedirects" title="Enable redirect handling?" help="/plugin/http_request/help-followRedirects.html">
<f:booleanRadio />
</f:entry>
<f:entry title="Custom headers">
<f:repeatableProperty field="customHeaders" />
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
This allows to turn off automatic redirect handling
</div>
3 changes: 3 additions & 0 deletions src/main/webapp/help-followRedirects.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
This allows to turn off automatic redirect handling
</div>
37 changes: 37 additions & 0 deletions src/test/java/jenkins/plugins/http_request/HttpRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
12 changes: 12 additions & 0 deletions src/test/java/jenkins/plugins/http_request/Registers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down