• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / httpunit / 656

06 Dec 2025 09:11PM UTC coverage: 80.452% (+0.02%) from 80.435%
656

push

github

hazendaz
[maven-release-plugin] prepare for next development iteration

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10137 relevant lines covered (81.34%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.66
/src/main/java/com/meterware/httpunit/WebRequest.java
1
/*
2
 * MIT License
3
 *
4
 * Copyright 2011-2025 Russell Gold
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
9
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
 * of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
15
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
 * DEALINGS IN THE SOFTWARE.
19
 */
20
package com.meterware.httpunit;
21

22
import com.meterware.httpunit.protocol.ParameterProcessor;
23
import com.meterware.httpunit.protocol.UploadFileSpec;
24
import com.meterware.httpunit.scripting.ScriptingHandler;
25

26
import java.io.File;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.OutputStream;
30
import java.net.HttpURLConnection;
31
import java.net.MalformedURLException;
32
import java.net.URL;
33
import java.net.URLConnection;
34
import java.net.URLStreamHandler;
35
import java.util.Dictionary;
36
import java.util.HashSet;
37
import java.util.Hashtable;
38
import java.util.Locale;
39

40
import org.w3c.dom.Element;
41
import org.xml.sax.SAXException;
42

43
/**
44
 * A request sent to a web server.
45
 **/
46
public abstract class WebRequest {
47

48
    /** The Constant REFERER_HEADER_NAME. */
49
    static final String REFERER_HEADER_NAME = "Referer";
50

51
    /** The javascript stream handler. */
52
    private static URLStreamHandler JAVASCRIPT_STREAM_HANDLER = new JavascriptURLStreamHandler();
1✔
53

54
    /** The https stream handler. */
55
    private static URLStreamHandler HTTPS_STREAM_HANDLER = new HttpsURLStreamHandler();
1✔
56

57
    /** The parameter holder. */
58
    private final ParameterHolder _parameterHolder;
59

60
    /** The url base. */
61
    private URL _urlBase;
62

63
    /** The source frame. */
64
    private FrameSelector _sourceFrame;
65

66
    /** The request target. */
67
    private String _requestTarget;
68

69
    /** The url string. */
70
    private String _urlString;
71

72
    /** The headers. */
73
    private Hashtable _headers;
74

75
    /** The web request source. */
76
    private WebRequestSource _webRequestSource;
77

78
    /** The referring page. */
79
    private WebResponse _referringPage;
80

81
    /** The button. */
82
    private SubmitButton _button;
83

84
    /** The source element. */
85
    private Element _sourceElement;
86

87
    /** The character set. */
88
    private String _characterSet;
89

90
    /**
91
     * Sets the value of a header to be sent with this request. A header set here will override any matching header set
92
     * in the WebClient when the request is actually sent.
93
     *
94
     * @param headerName
95
     *            - the name of the header
96
     * @param headerValue
97
     *            - the value to be set
98
     */
99
    public void setHeaderField(String headerName, String headerValue) {
100
        getHeaderDictionary().put(headerName, headerValue);
1✔
101
    }
1✔
102

103
    /**
104
     * Returns a copy of the headers to be sent with this request.
105
     *
106
     * @return the dictionary of headers
107
     **/
108
    public Dictionary getHeaders() {
109
        return (Dictionary) getHeaderDictionary().clone();
1✔
110
    }
111

112
    /**
113
     * Returns the final URL associated with this web request.
114
     *
115
     * @return the Uniform Resource Locator for this Web request
116
     *
117
     * @throws MalformedURLException
118
     *             if the URL is not o.k.
119
     **/
120
    public URL getURL() throws MalformedURLException {
121
        if (getURLBase() == null || getURLBase().toString().indexOf("?") < 0) {
1✔
122
            return newURL(getURLBase(), getURLString());
1✔
123
        }
124
        final String urlBaseString = getURLBase().toString();
1✔
125
        URL newurlbase = new URL(urlBaseString.substring(0, urlBaseString.indexOf("?")));
1✔
126
        return newURL(newurlbase, getURLString());
1✔
127
    }
128

129
    /**
130
     * Creates a new URL, handling the case where the relative URL begins with a '?'.
131
     *
132
     * @param base
133
     *            - the URL to start from
134
     * @param spec
135
     *            - additional specification string
136
     *
137
     * @return the URL
138
     *
139
     * @throws MalformedURLException
140
     *             the malformed URL exception
141
     */
142
    private URL newURL(final URL base, final String spec) throws MalformedURLException {
143
        if (spec.toLowerCase(Locale.ENGLISH).startsWith("javascript:")) {
1✔
144
            return new URL("javascript", null, -1, spec.substring("javascript:".length()), JAVASCRIPT_STREAM_HANDLER);
1✔
145
        }
146
        if (spec.toLowerCase(Locale.ENGLISH).startsWith("https:") && !HttpsProtocolSupport.hasHttpsSupport()) {
1!
147
            return new URL("https", null, -1, spec.substring("https:".length()), HTTPS_STREAM_HANDLER);
×
148
        }
149
        if (getURLBase() == null || getURLString().indexOf(':') > 0) {
1✔
150
            if (getURLString().indexOf(':') <= 0) {
1!
151
                throw new RuntimeException("No protocol specified in URL '" + getURLString() + "'");
×
152
            }
153
            HttpsProtocolSupport.verifyProtocolSupport(getURLString().substring(0, getURLString().indexOf(':')));
1✔
154
        }
155
        return spec.startsWith("?") ? new URL(base + spec) : newCombinedURL(base, spec);
1✔
156
    }
157

158
    /**
159
     * New combined URL.
160
     *
161
     * @param base
162
     *            the base
163
     * @param spec
164
     *            the spec
165
     *
166
     * @return the url
167
     *
168
     * @throws MalformedURLException
169
     *             the malformed URL exception
170
     */
171
    private URL newCombinedURL(final URL base, final String spec) throws MalformedURLException {
172
        if (base == null) {
1✔
173
            return new URL(getNormalizedURL(spec));
1✔
174
        }
175
        if (spec.startsWith("..")) {
1✔
176
            return new URL(getNormalizedURL(getURLDirectory(base) + spec));
1✔
177
        }
178
        return new URL(base, getNormalizedURL(spec));
1✔
179
    }
180

181
    /**
182
     * Gets the URL directory.
183
     *
184
     * @param base
185
     *            the base
186
     *
187
     * @return the URL directory
188
     */
189
    private String getURLDirectory(final URL base) {
190
        String url = base.toExternalForm();
1✔
191
        int i = url.lastIndexOf('/');
1✔
192
        return url.substring(0, i + 1);
1✔
193
    }
194

195
    /**
196
     * Gets the normalized URL.
197
     *
198
     * @param url
199
     *            the url
200
     *
201
     * @return the normalized URL
202
     */
203
    private String getNormalizedURL(String url) {
204
        int questionIndex = url.indexOf('?');
1✔
205
        if (questionIndex < 0) {
1✔
206
            return getNormalizedPath(url);
1✔
207
        }
208
        return getNormalizedPath(url.substring(0, questionIndex)) + url.substring(questionIndex);
1✔
209
    }
210

211
    /**
212
     * Gets the normalized path.
213
     *
214
     * @param path
215
     *            the path
216
     *
217
     * @return the normalized path
218
     */
219
    private String getNormalizedPath(String path) {
220
        if (path.lastIndexOf("//") > path.lastIndexOf("://") + 1) {
1✔
221
            return getNormalizedPath(stripDoubleSlashes(path));
1✔
222
        }
223
        if (path.indexOf("/../") > 0 || path.endsWith("/..")) {
1!
224
            return getNormalizedPath(stripUpNavigation(path));
1✔
225
        }
226
        if (path.indexOf("/./") > 0) {
1✔
227
            return getNormalizedPath(stripInPlaceNavigation(path));
1✔
228
        }
229
        return path;
1✔
230
    }
231

232
    /**
233
     * Strip in place navigation.
234
     *
235
     * @param url
236
     *            the url
237
     *
238
     * @return the string
239
     */
240
    private String stripInPlaceNavigation(String url) {
241
        int i = url.lastIndexOf("/./");
1✔
242
        return url.substring(0, i + 1) + url.substring(i + 2);
1✔
243
    }
244

245
    /**
246
     * Strip up navigation.
247
     *
248
     * @param url
249
     *            the url
250
     *
251
     * @return the string
252
     */
253
    private String stripUpNavigation(String url) {
254
        int i = url.indexOf("/..");
1✔
255
        int j = url.lastIndexOf("/", i - 1);
1✔
256
        return url.substring(0, j + 1) + url.substring(i + 3);
1✔
257
    }
258

259
    /**
260
     * Strip double slashes.
261
     *
262
     * @param url
263
     *            the url
264
     *
265
     * @return the string
266
     */
267
    private String stripDoubleSlashes(String url) {
268
        int i = url.lastIndexOf("//");
1✔
269
        return url.substring(0, i) + url.substring(i + 1);
1✔
270
    }
271

272
    /**
273
     * Returns the target for this web request.
274
     *
275
     * @return the target
276
     */
277
    public String getTarget() {
278
        return _requestTarget;
1✔
279
    }
280

281
    /**
282
     * Returns the frame from which this request originates.
283
     *
284
     * @return the source frame
285
     */
286
    FrameSelector getSourceFrame() {
287
        return _sourceFrame;
1✔
288
    }
289

290
    /**
291
     * the HTTP method defined for this request e.g. DELETE,OPTIONS,HEAD
292
     */
293
    protected String method;
294

295
    /**
296
     * Gets the method.
297
     *
298
     * @return the method
299
     */
300
    public String getMethod() {
301
        return method;
1✔
302
    }
303

304
    /**
305
     * Returns the query string defined for this request. The query string is sent to the HTTP server as part of the
306
     * request header. This default implementation returns an empty string.
307
     *
308
     * @return the query string
309
     */
310
    public String getQueryString() {
311
        return "";
1✔
312
    }
313

314
    // ------------------------------------- ParameterCollection methods ------------------------------------
315

316
    /**
317
     * Sets the value of a parameter in a web request.
318
     *
319
     * @param name
320
     *            the name
321
     * @param value
322
     *            the value
323
     */
324
    public void setParameter(String name, String value) {
325
        _parameterHolder.setParameter(name, value);
1✔
326
    }
1✔
327

328
    /**
329
     * Sets the multiple values of a parameter in a web request.
330
     *
331
     * @param name
332
     *            the name
333
     * @param values
334
     *            the values
335
     */
336
    public void setParameter(String name, String[] values) {
337
        _parameterHolder.setParameter(name, values);
1✔
338
    }
1✔
339

340
    /**
341
     * Sets the multiple values of a file upload parameter in a web request.
342
     *
343
     * @param parameterName
344
     *            the parameter name
345
     * @param files
346
     *            the files
347
     */
348
    public void setParameter(String parameterName, UploadFileSpec[] files) {
349
        if (!maySelectFile(parameterName)) {
1✔
350
            throw new IllegalNonFileParameterException(parameterName);
1✔
351
        }
352
        if (!isMimeEncoded()) {
1!
353
            throw new MultipartFormRequiredException();
×
354
        }
355
        _parameterHolder.setParameter(parameterName, files);
1✔
356
    }
1✔
357

358
    /**
359
     * Specifies the click position for the submit button. When a user clioks on an image button, not only the name and
360
     * value of the button, but also the position of the mouse at the time of the click is submitted with the form. This
361
     * method allows the caller to override the position selected when this request was created.
362
     *
363
     * @param x
364
     *            the x
365
     * @param y
366
     *            the y
367
     *
368
     * @exception IllegalRequestParameterException
369
     *                thrown if the request was not created from a form with an image button.
370
     */
371
    public void setImageButtonClickPosition(int x, int y) throws IllegalRequestParameterException {
372
        if (_button == null) {
1✔
373
            throw new IllegalButtonPositionException();
1✔
374
        }
375
        _parameterHolder.selectImageButtonPosition(_button, x, y);
1✔
376
    }
1✔
377

378
    /**
379
     * Returns true if the specified parameter is a file field.
380
     *
381
     * @param name
382
     *            the name
383
     *
384
     * @return true, if is file parameter
385
     */
386
    public boolean isFileParameter(String name) {
387
        return _parameterHolder.isFileParameter(name);
1✔
388
    }
389

390
    /**
391
     * Sets the file for a parameter upload in a web request.
392
     *
393
     * @param parameterName
394
     *            the parameter name
395
     * @param file
396
     *            the file
397
     */
398
    public void selectFile(String parameterName, File file) {
399
        setParameter(parameterName, new UploadFileSpec[] { new UploadFileSpec(file) });
1✔
400
    }
1✔
401

402
    /**
403
     * Sets the file for a parameter upload in a web request.
404
     *
405
     * @param parameterName
406
     *            the parameter name
407
     * @param file
408
     *            the file
409
     * @param contentType
410
     *            the content type
411
     */
412
    public void selectFile(String parameterName, File file, String contentType) {
413
        setParameter(parameterName, new UploadFileSpec[] { new UploadFileSpec(file, contentType) });
1✔
414
    }
1✔
415

416
    /**
417
     * Sets the file for a parameter upload in a web request.
418
     *
419
     * @param parameterName
420
     *            the parameter name
421
     * @param fileName
422
     *            the file name
423
     * @param inputStream
424
     *            the input stream
425
     * @param contentType
426
     *            the content type
427
     */
428
    public void selectFile(String parameterName, String fileName, InputStream inputStream, String contentType) {
429
        setParameter(parameterName, new UploadFileSpec[] { new UploadFileSpec(fileName, inputStream, contentType) });
1✔
430
    }
1✔
431

432
    /**
433
     * Returns an array of all parameter names defined as part of this web request.
434
     *
435
     * @return the request parameter names
436
     */
437
    public String[] getRequestParameterNames() {
438
        final HashSet<String> names = new HashSet<>();
1✔
439
        ParameterProcessor pp = new ParameterProcessor() {
1✔
440
            @Override
441
            public void addParameter(String name, String value, String characterSet) throws IOException {
442
                names.add(name);
1✔
443
            }
1✔
444

445
            @Override
446
            public void addFile(String parameterName, UploadFileSpec fileSpec) throws IOException {
447
                names.add(parameterName);
×
448
            }
×
449
        };
450

451
        try {
452
            _parameterHolder.recordPredefinedParameters(pp);
1✔
453
            _parameterHolder.recordParameters(pp);
1✔
454
        } catch (IOException e) {
×
455
        }
1✔
456

457
        return names.toArray(new String[names.size()]);
1✔
458
    }
459

460
    /**
461
     * Returns the value of a parameter in this web request.
462
     *
463
     * @param name
464
     *            the name
465
     *
466
     * @return the value of the named parameter, or empty string if it is not set.
467
     */
468
    public String getParameter(String name) {
469
        String[] values = getParameterValues(name);
1✔
470
        return values.length == 0 ? "" : values[0];
1✔
471
    }
472

473
    /**
474
     * Returns the multiple default values of the named parameter.
475
     *
476
     * @param name
477
     *            the name
478
     *
479
     * @return the parameter values
480
     */
481
    public String[] getParameterValues(String name) {
482
        return _parameterHolder.getParameterValues(name);
1✔
483
    }
484

485
    /**
486
     * Removes a parameter from this web request.
487
     *
488
     * @param name
489
     *            the name
490
     */
491
    public void removeParameter(String name) {
492
        _parameterHolder.removeParameter(name);
1✔
493
    }
1✔
494

495
    // ------------------------------------- Object methods ------------------------------------
496

497
    @Override
498
    public String toString() {
499
        return getMethod() + " request for (" + getURLBase() + ") " + getURLString();
×
500
    }
501

502
    // ------------------------------------- protected members ------------------------------------
503

504
    /**
505
     * Constructs a web request using an absolute URL string.
506
     *
507
     * @param urlString
508
     *            the url string
509
     */
510
    protected WebRequest(String urlString) {
511
        this(null, urlString);
1✔
512
    }
1✔
513

514
    /**
515
     * Constructs a web request using a base URL and a relative URL string.
516
     *
517
     * @param urlBase
518
     *            the url base
519
     * @param urlString
520
     *            the url string
521
     */
522
    protected WebRequest(URL urlBase, String urlString) {
523
        this(urlBase, urlString, TOP_FRAME);
1✔
524
    }
1✔
525

526
    /**
527
     * Constructs a web request using a base request and a relative URL string.
528
     *
529
     * @param baseRequest
530
     *            the base request
531
     * @param urlString
532
     *            the url string
533
     * @param target
534
     *            the target
535
     *
536
     * @throws MalformedURLException
537
     *             the malformed URL exception
538
     */
539
    protected WebRequest(WebRequest baseRequest, String urlString, String target) throws MalformedURLException {
540
        this(baseRequest.getURL(), urlString, target);
×
541
    }
×
542

543
    /**
544
     * Constructs a web request using a base URL, a relative URL string, and a target.
545
     *
546
     * @param urlBase
547
     *            the url base
548
     * @param urlString
549
     *            the url string
550
     * @param target
551
     *            the target
552
     */
553
    protected WebRequest(URL urlBase, String urlString, String target) {
554
        this(urlBase, urlString, FrameSelector.TOP_FRAME, target);
1✔
555
    }
1✔
556

557
    /**
558
     * Constructs a web request using a base URL, a relative URL string, and a target.
559
     *
560
     * @param referer
561
     *            the referer
562
     * @param sourceElement
563
     *            the source element
564
     * @param urlBase
565
     *            the url base
566
     * @param urlString
567
     *            the url string
568
     * @param target
569
     *            the target
570
     */
571
    protected WebRequest(WebResponse referer, Element sourceElement, URL urlBase, String urlString, String target) {
572
        this(urlBase, urlString, referer.getFrame(), target != null ? target : referer.getBaseTarget());
1✔
573
        _sourceElement = sourceElement;
1✔
574
        _referringPage = referer;
1✔
575
        setHeaderField(REFERER_HEADER_NAME, referer.getURL().toExternalForm());
1✔
576
    }
1✔
577

578
    /**
579
     * Constructs a web request using a base URL, a relative URL string, and a target.
580
     *
581
     * @param urlBase
582
     *            the url base
583
     * @param urlString
584
     *            the url string
585
     * @param frame
586
     *            the frame
587
     * @param target
588
     *            the target
589
     */
590
    protected WebRequest(URL urlBase, String urlString, FrameSelector frame, String target) {
591
        this(urlBase, urlString, frame, target, new UncheckedParameterHolder());
1✔
592
    }
1✔
593

594
    /**
595
     * Constructs a web request from a form.
596
     *
597
     * @param sourceForm
598
     *            - the WebForm to startFrom
599
     * @param parameterHolder
600
     *            - the parameter holder
601
     * @param button
602
     *            - the submit button
603
     * @param x
604
     *            - x position
605
     * @param y
606
     *            - y position
607
     **/
608
    protected WebRequest(WebForm sourceForm, ParameterHolder parameterHolder, SubmitButton button, int x, int y) {
609
        this(sourceForm, parameterHolder);
1✔
610
        // [ 1443333 ] Allow unnamed Image input elements to submit x,y values
611
        if (button != null && button.isValidImageButton()) {
1!
612
            _button = button;
1✔
613
            _parameterHolder.selectImageButtonPosition(_button, x, y);
1✔
614
        }
615
    }
1✔
616

617
    /**
618
     * Instantiates a new web request.
619
     *
620
     * @param requestSource
621
     *            the request source
622
     * @param parameterHolder
623
     *            the parameter holder
624
     */
625
    protected WebRequest(WebRequestSource requestSource, ParameterHolder parameterHolder) {
626
        this(requestSource.getBaseURL(), requestSource.getRelativePage(), requestSource.getFrame(),
1✔
627
                requestSource.getTarget(), parameterHolder);
1✔
628
        _webRequestSource = requestSource;
1✔
629
        _sourceElement = requestSource.getElement();
1✔
630
    }
1✔
631

632
    /**
633
     * New parameter holder.
634
     *
635
     * @param requestSource
636
     *            the request source
637
     *
638
     * @return the parameter holder
639
     */
640
    static ParameterHolder newParameterHolder(WebRequestSource requestSource) {
641
        if (HttpUnitOptions.getParameterValuesValidated()) {
1✔
642
            return requestSource;
1✔
643
        }
644
        return new UncheckedParameterHolder(requestSource);
1✔
645
    }
646

647
    /**
648
     * Constructs a web request using a base URL, a relative URL string, and a target.
649
     *
650
     * @param urlBase
651
     *            the url base
652
     * @param urlString
653
     *            the url string
654
     * @param sourceFrame
655
     *            the source frame
656
     * @param requestTarget
657
     *            the request target
658
     * @param parameterHolder
659
     *            the parameter holder
660
     */
661
    private WebRequest(URL urlBase, String urlString, FrameSelector sourceFrame, String requestTarget,
662
            ParameterHolder parameterHolder) {
1✔
663
        _urlBase = urlBase;
1✔
664
        _sourceFrame = sourceFrame;
1✔
665
        _requestTarget = requestTarget;
1✔
666
        _urlString = urlString.toLowerCase(Locale.ENGLISH).startsWith("http") ? escape(urlString) : urlString;
1✔
667
        _parameterHolder = parameterHolder;
1✔
668
        _characterSet = parameterHolder.getCharacterSet();
1✔
669
    }
1✔
670

671
    /**
672
     * Escape.
673
     *
674
     * @param urlString
675
     *            the url string
676
     *
677
     * @return the string
678
     */
679
    private static String escape(String urlString) {
680
        if (urlString.indexOf(' ') < 0) {
1✔
681
            return urlString;
1✔
682
        }
683
        StringBuilder sb = new StringBuilder();
1✔
684

685
        int start = 0;
1✔
686
        do {
687
            int index = urlString.indexOf(' ', start);
1✔
688
            if (index < 0) {
1✔
689
                sb.append(urlString.substring(start));
1✔
690
                break;
1✔
691
            }
692
            sb.append(urlString.substring(start, index)).append("%20");
1✔
693
            start = index + 1;
1✔
694
        } while (true);
1✔
695
        return sb.toString();
1✔
696
    }
697

698
    /**
699
     * Returns true if selectFile may be called with this parameter.
700
     *
701
     * @param parameterName
702
     *            the parameter name
703
     *
704
     * @return true, if successful
705
     */
706
    protected boolean maySelectFile(String parameterName) {
707
        return isFileParameter(parameterName);
×
708
    }
709

710
    /**
711
     * Returns true if this request is to be MIME-encoded.
712
     *
713
     * @return true, if is mime encoded
714
     */
715
    protected boolean isMimeEncoded() {
716
        return false;
×
717
    }
718

719
    /**
720
     * Returns the content type of this request. If null, no content is specified.
721
     *
722
     * @return the content type
723
     */
724
    protected String getContentType() {
725
        return null;
1✔
726
    }
727

728
    /**
729
     * Returns the character set required for this request.
730
     *
731
     * @return the character set
732
     */
733
    protected final String getCharacterSet() {
734
        return _characterSet;
1✔
735
    }
736

737
    /**
738
     * Performs any additional processing necessary to complete the request.
739
     *
740
     * @param connection
741
     *            the connection
742
     *
743
     * @throws IOException
744
     *             Signals that an I/O exception has occurred.
745
     */
746
    protected void completeRequest(URLConnection connection) throws IOException {
747
        if (connection instanceof HttpURLConnection) {
1✔
748
            ((HttpURLConnection) connection).setRequestMethod(getMethod());
1✔
749
        }
750
    }
1✔
751

752
    /**
753
     * Writes the contents of the message body to the specified stream.
754
     *
755
     * @param stream
756
     *            the stream
757
     *
758
     * @throws IOException
759
     *             Signals that an I/O exception has occurred.
760
     */
761
    protected void writeMessageBody(OutputStream stream) throws IOException {
762
    }
1✔
763

764
    /**
765
     * Gets the URL base.
766
     *
767
     * @return the URL base
768
     */
769
    protected final URL getURLBase() {
770
        return _urlBase;
1✔
771
    }
772

773
    // ------------------------------------- protected members ---------------------------------------------
774

775
    /**
776
     * Gets the URL string.
777
     *
778
     * @return the URL string
779
     */
780
    protected String getURLString() {
781
        final String queryString = getQueryString();
1✔
782
        if (queryString.isEmpty()) {
1✔
783
            return _urlString;
1✔
784
        }
785
        return _urlString + "?" + queryString;
1✔
786
    }
787

788
    /**
789
     * Gets the parameter holder.
790
     *
791
     * @return the parameter holder
792
     */
793
    protected final ParameterHolder getParameterHolder() {
794
        return _parameterHolder;
1✔
795
    }
796

797
    // ---------------------------------- package members --------------------------------
798

799
    /** The target indicating the topmost frame of a window. **/
800
    static final String TOP_FRAME = "_top";
801

802
    /** The target indicating the parent of a frame. **/
803
    static final String PARENT_FRAME = "_parent";
804

805
    /** The target indicating a new, empty window. **/
806
    static final String NEW_WINDOW = "_blank";
807

808
    /** The target indicating the same frame. **/
809
    static final String SAME_FRAME = "_self";
810

811
    /**
812
     * Gets the header dictionary.
813
     *
814
     * @return the header dictionary
815
     */
816
    Hashtable getHeaderDictionary() {
817
        if (_headers == null) {
1✔
818
            _headers = new Hashtable<>();
1✔
819
            if (getContentType() != null) {
1✔
820
                _headers.put("Content-Type", getContentType());
1✔
821
            }
822
        }
823
        return _headers;
1✔
824
    }
825

826
    /**
827
     * Gets the referer.
828
     *
829
     * @return the referer
830
     */
831
    String getReferer() {
832
        return _headers == null ? null : (String) _headers.get(REFERER_HEADER_NAME);
1!
833
    }
834

835
    /**
836
     * Gets the source scripting handler.
837
     *
838
     * @return the source scripting handler
839
     */
840
    ScriptingHandler getSourceScriptingHandler() {
841
        WebRequestSource wrs = _webRequestSource;
1✔
842
        if (wrs != null) {
1!
843
            return wrs.getScriptingHandler();
×
844
        }
845
        if (_referringPage != null && _sourceElement != null) {
1!
846
            try {
847
                _referringPage.getReceivedPage().getElement(_sourceElement).getScriptingHandler();
1✔
848
            } catch (SAXException e) {
×
849
            }
1✔
850
        }
851
        return null;
1✔
852
    }
853

854
}
855

856
// ======================================== class JavaScriptURLStreamHandler
857
// ============================================
858

859
class JavascriptURLStreamHandler extends URLStreamHandler {
1✔
860

861
    @Override
862
    protected URLConnection openConnection(URL u) throws IOException {
863
        return null;
×
864
    }
865
}
866

867
// ======================================== class HttpsURLStreamHandler ============================================
868

869
class HttpsURLStreamHandler extends URLStreamHandler {
1✔
870

871
    @Override
872
    protected URLConnection openConnection(URL u) throws IOException {
873
        throw new RuntimeException(
×
874
                "https support requires the Java Secure Sockets Extension. See http://java.sun.com/products/jsse");
875
    }
876
}
877

878
// ============================= exception class IllegalNonFileParameterException ======================================
879

880
/**
881
 * This exception is thrown on an attempt to set a non-file parameter to a file value.
882
 **/
883
class IllegalNonFileParameterException extends IllegalRequestParameterException {
884

885
    private static final long serialVersionUID = 1L;
886

887
    IllegalNonFileParameterException(String parameterName) {
1✔
888
        _parameterName = parameterName;
1✔
889
    }
1✔
890

891
    @Override
892
    public String getMessage() {
893
        return "Parameter '" + _parameterName + "' is not a file parameter and may not be set to a file value.";
×
894
    }
895

896
    private String _parameterName;
897

898
}
899

900
// ============================= exception class MultipartFormRequiredException ======================================
901

902
/**
903
 * This exception is thrown on an attempt to set a file parameter in a form that does not specify MIME encoding.
904
 **/
905
class MultipartFormRequiredException extends IllegalRequestParameterException {
906

907
    private static final long serialVersionUID = 1L;
908

909
    MultipartFormRequiredException() {
×
910
    }
×
911

912
    @Override
913
    public String getMessage() {
914
        return "The request does not use multipart/form-data encoding, and cannot be used to upload files ";
×
915
    }
916

917
}
918

919
// ============================= exception class IllegalButtonPositionException ======================================
920

921
/**
922
 * This exception is thrown on an attempt to set a file parameter in a form that does not specify MIME encoding.
923
 **/
924
class IllegalButtonPositionException extends IllegalRequestParameterException {
925

926
    private static final long serialVersionUID = 1L;
927

928
    IllegalButtonPositionException() {
1✔
929
    }
1✔
930

931
    @Override
932
    public String getMessage() {
933
        return "The request was not created with an image button, and cannot accept an image button click position";
×
934
    }
935

936
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc