forked from jenkinsci/http-request-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpRequest.java
615 lines (515 loc) · 19.9 KB
/
HttpRequest.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
package jenkins.plugins.http_request;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import edu.umd.cs.findbugs.annotations.NonNull;
import org.apache.http.HttpHeaders;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import com.cloudbees.plugins.credentials.common.AbstractIdCredentialsListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Item;
import hudson.model.Items;
import hudson.model.TaskListener;
import hudson.remoting.VirtualChannel;
import hudson.security.ACL;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.util.ListBoxModel.Option;
import jenkins.plugins.http_request.auth.BasicDigestAuthentication;
import jenkins.plugins.http_request.auth.FormAuthentication;
import jenkins.plugins.http_request.util.HttpClientUtil;
import jenkins.plugins.http_request.util.HttpRequestFormDataPart;
import jenkins.plugins.http_request.util.HttpRequestNameValuePair;
/**
* @author Janario Oliveira
*/
public class HttpRequest extends Builder {
private final @NonNull String url;
private Boolean ignoreSslErrors = DescriptorImpl.ignoreSslErrors;
private HttpMode httpMode = DescriptorImpl.httpMode;
private String httpProxy = DescriptorImpl.httpProxy;
private String proxyAuthentication = DescriptorImpl.proxyAuthentication;
private Boolean passBuildParameters = DescriptorImpl.passBuildParameters;
private String validResponseCodes = DescriptorImpl.validResponseCodes;
private String validResponseContent = DescriptorImpl.validResponseContent;
private MimeType acceptType = DescriptorImpl.acceptType;
private MimeType contentType = DescriptorImpl.contentType;
private String outputFile = DescriptorImpl.outputFile;
private Integer timeout = DescriptorImpl.timeout;
private Boolean consoleLogResponseBody = DescriptorImpl.consoleLogResponseBody;
private Boolean quiet = DescriptorImpl.quiet;
private String authentication = DescriptorImpl.authentication;
private String requestBody = DescriptorImpl.requestBody;
private String uploadFile = DescriptorImpl.uploadFile;
private String multipartName = DescriptorImpl.multipartName;
private Boolean wrapAsMultipart = DescriptorImpl.wrapAsMultipart;
private Boolean useSystemProperties = DescriptorImpl.useSystemProperties;
private boolean useNtlm = DescriptorImpl.useNtlm;
private List<HttpRequestNameValuePair> customHeaders = DescriptorImpl.customHeaders;
private List<HttpRequestFormDataPart> formData = DescriptorImpl.formData;
@DataBoundConstructor
public HttpRequest(@NonNull String url) {
this.url = url;
}
@NonNull
public String getUrl() {
return url;
}
public Boolean getIgnoreSslErrors() {
return ignoreSslErrors;
}
@DataBoundSetter
public void setIgnoreSslErrors(Boolean ignoreSslErrors) {
this.ignoreSslErrors = ignoreSslErrors;
}
public HttpMode getHttpMode() {
return httpMode;
}
@DataBoundSetter
public void setHttpMode(HttpMode httpMode) {
this.httpMode = httpMode;
}
public String getHttpProxy() {
return httpProxy;
}
@DataBoundSetter
public void setHttpProxy(String httpProxy) {
this.httpProxy = httpProxy;
}
public Boolean getPassBuildParameters() {
return passBuildParameters;
}
@DataBoundSetter
public void setPassBuildParameters(Boolean passBuildParameters) {
this.passBuildParameters = passBuildParameters;
}
@NonNull
public String getValidResponseCodes() {
return validResponseCodes;
}
@DataBoundSetter
public void setValidResponseCodes(String validResponseCodes) {
this.validResponseCodes = validResponseCodes;
}
public String getValidResponseContent() {
return validResponseContent;
}
@DataBoundSetter
public void setValidResponseContent(String validResponseContent) {
this.validResponseContent = validResponseContent;
}
public MimeType getAcceptType() {
return acceptType;
}
@DataBoundSetter
public void setAcceptType(MimeType acceptType) {
this.acceptType = acceptType;
}
public MimeType getContentType() {
return contentType;
}
@DataBoundSetter
public void setContentType(MimeType contentType) {
this.contentType = contentType;
}
public String getOutputFile() {
return outputFile;
}
@DataBoundSetter
public void setOutputFile(String outputFile) {
this.outputFile = outputFile;
}
public Integer getTimeout() {
return timeout;
}
@DataBoundSetter
public void setTimeout(Integer timeout) {
this.timeout = timeout;
}
public Boolean getConsoleLogResponseBody() {
return consoleLogResponseBody;
}
@DataBoundSetter
public void setConsoleLogResponseBody(Boolean consoleLogResponseBody) {
this.consoleLogResponseBody = consoleLogResponseBody;
}
public Boolean getQuiet() {
return quiet;
}
@DataBoundSetter
public void setQuiet(Boolean quiet) {
this.quiet = quiet;
}
public String getAuthentication() {
return authentication;
}
@DataBoundSetter
public void setAuthentication(String authentication) {
this.authentication = authentication;
}
public String getProxyAuthentication() {
return proxyAuthentication;
}
@DataBoundSetter
public void setProxyAuthentication(String proxyAuthentication) {
this.proxyAuthentication = proxyAuthentication;
}
public String getRequestBody() {
return requestBody;
}
@DataBoundSetter
public void setRequestBody(String requestBody) {
this.requestBody = requestBody;
}
public Boolean getUseSystemProperties() {
return useSystemProperties;
}
@DataBoundSetter
public void setUseSystemProperties(Boolean useSystemProperties) {
this.useSystemProperties = useSystemProperties;
}
public List<HttpRequestNameValuePair> getCustomHeaders() {
return customHeaders;
}
@DataBoundSetter
public void setCustomHeaders(List<HttpRequestNameValuePair> customHeaders) {
this.customHeaders = customHeaders;
}
public List<HttpRequestFormDataPart> getFormData() {
return formData;
}
@DataBoundSetter
public void setFormData(List<HttpRequestFormDataPart> formData) {
this.formData = Collections.unmodifiableList(formData);
}
public String getUploadFile() {
return uploadFile;
}
@DataBoundSetter
public void setUploadFile(String uploadFile) {
this.uploadFile = uploadFile;
}
public String getMultipartName() {
return multipartName;
}
@DataBoundSetter
public void setMultipartName(String multipartName) {
this.multipartName = multipartName;
}
public Boolean getWrapAsMultipart() {
return wrapAsMultipart;
}
@DataBoundSetter
public void setWrapAsMultipart(Boolean wrapAsMultipart) {
this.wrapAsMultipart = wrapAsMultipart;
}
@Initializer(before = InitMilestone.PLUGINS_STARTED)
public static void xStreamCompatibility() {
Items.XSTREAM2.aliasField("logResponseBody", HttpRequest.class, "consoleLogResponseBody");
Items.XSTREAM2.aliasField("consoleLogResponseBody", HttpRequest.class, "consoleLogResponseBody");
Items.XSTREAM2.alias("pair", HttpRequestNameValuePair.class);
}
protected Object readResolve() {
if (customHeaders == null) {
customHeaders = DescriptorImpl.customHeaders;
}
if (formData == null) {
formData = DescriptorImpl.formData;
}
if (validResponseCodes == null || validResponseCodes.trim().isEmpty()) {
validResponseCodes = DescriptorImpl.validResponseCodes;
}
if (ignoreSslErrors == null) {
//default for new job false(DescriptorImpl.ignoreSslErrors) for old ones true to keep same behavior
ignoreSslErrors = true;
}
if (quiet == null) {
quiet = DescriptorImpl.quiet;
}
if (useSystemProperties == null) {
// old jobs use it (for compatibility), new jobs doesn't (jelly was not reading the default)
useSystemProperties = !DescriptorImpl.useSystemProperties;
}
if(wrapAsMultipart == null) {
wrapAsMultipart = DescriptorImpl.wrapAsMultipart;
}
return this;
}
private List<HttpRequestNameValuePair> createParams(EnvVars envVars, AbstractBuild<?, ?> build, TaskListener listener) {
Map<String, String> buildVariables = build.getBuildVariables();
if (buildVariables.isEmpty()) {
return Collections.emptyList();
}
PrintStream logger = listener.getLogger();
logger.println("Parameters: ");
List<HttpRequestNameValuePair> l = new ArrayList<>();
for (Map.Entry<String, String> entry : buildVariables.entrySet()) {
String value = envVars.expand(entry.getValue());
logger.println(" " + entry.getKey() + " = " + value);
l.add(new HttpRequestNameValuePair(entry.getKey(), value));
}
return l;
}
String resolveUrl(EnvVars envVars,
AbstractBuild<?, ?> build, TaskListener listener) throws IOException {
String url = envVars.expand(getUrl());
if (Boolean.TRUE.equals(getPassBuildParameters()) && getHttpMode() == HttpMode.GET) {
List<HttpRequestNameValuePair> params = createParams(envVars, build, listener);
if (!params.isEmpty()) {
url = HttpClientUtil.appendParamsToUrl(url, params);
}
}
return url;
}
List<HttpRequestNameValuePair> resolveHeaders(EnvVars envVars) {
final List<HttpRequestNameValuePair> headers = new ArrayList<>();
if (contentType != null && contentType != MimeType.NOT_SET) {
headers.add(new HttpRequestNameValuePair(HttpHeaders.CONTENT_TYPE, contentType.getContentType().toString()));
}
if (acceptType != null && acceptType != MimeType.NOT_SET) {
headers.add(new HttpRequestNameValuePair(HttpHeaders.ACCEPT, acceptType.getValue()));
}
for (HttpRequestNameValuePair header : customHeaders) {
String headerName = envVars.expand(header.getName());
String headerValue = envVars.expand(header.getValue());
boolean maskValue = headerName.equalsIgnoreCase(HttpHeaders.AUTHORIZATION) ||
header.getMaskValue();
headers.add(new HttpRequestNameValuePair(headerName, headerValue, maskValue));
}
return headers;
}
String resolveBody(EnvVars envVars,
AbstractBuild<?, ?> build, TaskListener listener) throws IOException {
String body = envVars.expand(getRequestBody());
if ((body == null || body.isEmpty()) && Boolean.TRUE.equals(getPassBuildParameters())) {
List<HttpRequestNameValuePair> params = createParams(envVars, build, listener);
if (!params.isEmpty()) {
body = HttpClientUtil.paramsToString(params);
}
}
return body;
}
FilePath resolveOutputFile(EnvVars envVars, AbstractBuild<?,?> build) {
if (outputFile == null || outputFile.trim().isEmpty()) {
return null;
}
String filePath = envVars.expand(outputFile);
FilePath workspace = build.getWorkspace();
if (workspace == null) {
throw new IllegalStateException("Could not find workspace to save file outputFile: " + outputFile);
}
return workspace.child(filePath);
}
FilePath resolveUploadFile(EnvVars envVars, AbstractBuild<?, ?> build) {
return resolveUploadFileInternal(uploadFile, envVars, build);
}
private static FilePath resolveUploadFileInternal(String path, EnvVars envVars, AbstractBuild<?, ?> build) {
if (path == null || path.trim().isEmpty()) {
return null;
}
String filePath = envVars.expand(path);
try {
FilePath workspace = build.getWorkspace();
if (workspace == null) {
throw new IllegalStateException(
"Could not find workspace to check existence of upload file: " + path
+ ". You should use it inside a 'node' block");
}
FilePath uploadFilePath = workspace.child(filePath);
if (!uploadFilePath.exists()) {
throw new IllegalStateException("Could not find upload file: " + path);
}
return uploadFilePath;
} catch (IOException | InterruptedException e) {
throw new IllegalStateException(e);
}
}
List<HttpRequestFormDataPart> resolveFormDataParts(EnvVars envVars, AbstractBuild<?, ?> build) {
if (formData == null || formData.isEmpty()) {
return Collections.emptyList();
}
List<HttpRequestFormDataPart> resolved = new ArrayList<>(formData.size());
for (HttpRequestFormDataPart part : formData) {
String name = envVars.expand(part.getName());
String fileName = envVars.expand(part.getFileName());
FilePath resolvedUploadFile =
resolveUploadFileInternal(part.getUploadFile(), envVars, build);
String body = envVars.expand(part.getBody());
HttpRequestFormDataPart newPart = new HttpRequestFormDataPart(part.getUploadFile(),
name, fileName, part.getContentType(), body);
newPart.setResolvedUploadFile(resolvedUploadFile);
resolved.add(newPart);
}
return resolved;
}
@Override
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException
{
EnvVars envVars = build.getEnvironment(listener);
envVars.putAll(build.getBuildVariables());
HttpRequestExecution exec = HttpRequestExecution.from(this, envVars, build,
this.getQuiet() ? TaskListener.NULL : listener);
VirtualChannel channel = launcher.getChannel();
if (channel == null) {
throw new IllegalStateException("Launcher doesn't support remoting but it is required");
}
channel.call(exec);
return true;
}
public boolean isUseNtlm() {
return useNtlm;
}
public void setUseNtlm(boolean useNtlm) {
this.useNtlm = useNtlm;
}
@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
public static final boolean ignoreSslErrors = false;
public static final HttpMode httpMode = HttpMode.GET;
public static final String httpProxy = "";
public static final String proxyAuthentication = "";
public static final Boolean passBuildParameters = false;
public static final String validResponseCodes = "100:399";
public static final String validResponseContent = "";
public static final MimeType acceptType = MimeType.NOT_SET;
public static final MimeType contentType = MimeType.NOT_SET;
public static final String outputFile = "";
public static final int timeout = 0;
public static final Boolean consoleLogResponseBody = false;
public static final Boolean quiet = false;
public static final String authentication = "";
public static final String requestBody = "";
public static final String uploadFile = "";
public static final String multipartName = "";
public static final boolean wrapAsMultipart = true;
public static final Boolean useSystemProperties = false;
public static final boolean useNtlm = false;
public static final List<HttpRequestNameValuePair> customHeaders = Collections.emptyList();
public static final List<HttpRequestFormDataPart> formData = Collections.emptyList();
public DescriptorImpl() {
load();
}
@Override
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
return true;
}
@NonNull
@Override
public String getDisplayName() {
return "HTTP Request";
}
public ListBoxModel doFillHttpModeItems() {
return HttpMode.getFillItems();
}
public ListBoxModel doFillAcceptTypeItems() {
return MimeType.getContentTypeFillItems();
}
public ListBoxModel doFillContentTypeItems() {
return MimeType.getContentTypeFillItems();
}
public ListBoxModel doFillAuthenticationItems(@AncestorInPath Item project,
@QueryParameter String url) {
return fillAuthenticationItems(project, url);
}
public ListBoxModel doFillProxyAuthenticationItems(@AncestorInPath Item project,
@QueryParameter String url) {
if (project == null || !project.hasPermission(Item.CONFIGURE)) {
return new StandardListBoxModel();
} else {
return new StandardListBoxModel()
.includeEmptyValue()
.includeAs(ACL.SYSTEM,
project, StandardUsernamePasswordCredentials.class,
URIRequirementBuilder.fromUri(url).build());
}
}
public static ListBoxModel fillAuthenticationItems(Item project, String url) {
if (project == null || !project.hasPermission(Item.CONFIGURE)) {
return new StandardListBoxModel();
}
List<Option> options = new ArrayList<>();
for (BasicDigestAuthentication basic : HttpRequestGlobalConfig.get().getBasicDigestAuthentications()) {
options.add(new Option("(deprecated - use Jenkins Credentials) " +
basic.getKeyName(), basic.getKeyName()));
}
for (FormAuthentication formAuthentication : HttpRequestGlobalConfig.get().getFormAuthentications()) {
options.add(new Option(formAuthentication.getKeyName()));
}
AbstractIdCredentialsListBoxModel<StandardListBoxModel, StandardCredentials> items = new StandardListBoxModel()
.includeEmptyValue()
.includeAs(ACL.SYSTEM,
project, StandardUsernamePasswordCredentials.class,
URIRequirementBuilder.fromUri(url).build());
items.addMissing(options);
return items;
}
public static List<IntStream> parseToRange(String value) {
List<IntStream> validRanges = new ArrayList<>();
if (value == null || value.isEmpty()) {
value = HttpRequest.DescriptorImpl.validResponseCodes;
}
String[] codes = value.split(",");
for (String code : codes) {
String[] fromTo = code.trim().split(":");
if (fromTo.length > 2) {
throw new IllegalArgumentException(String.format("Code %s should be an interval from:to or a single value", code));
}
int from;
try {
from = Integer.parseInt(fromTo[0]);
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Invalid number "+fromTo[0]);
}
int to = from;
if (fromTo.length != 1) {
try {
to = Integer.parseInt(fromTo[1]);
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Invalid number "+fromTo[1]);
}
}
if (from > to) {
throw new IllegalArgumentException(String.format("Interval %s should be FROM less than TO", code));
}
validRanges.add(IntStream.rangeClosed(from, to));
}
return validRanges;
}
public FormValidation doCheckValidResponseCodes(@QueryParameter String value) {
return checkValidResponseCodes(value);
}
public static FormValidation checkValidResponseCodes(String value) {
if (value == null || value.trim().isEmpty()) {
return FormValidation.ok();
}
try {
parseToRange(value);
} catch (IllegalArgumentException iae) {
return FormValidation.error("Response codes expected is wrong. "+iae.getMessage());
}
return FormValidation.ok();
}
}
}