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

hazendaz / httpunit / 755

14 Feb 2026 07:14PM UTC coverage: 80.526%. Remained the same
755

push

github

hazendaz
[ci] Fix badge

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10124 relevant lines covered (81.44%)

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
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2000-2026 Russell Gold
6
 * Copyright 2021-2000 hazendaz
7
 */
8
package com.meterware.httpunit;
9

10
import com.meterware.httpunit.protocol.ParameterProcessor;
11
import com.meterware.httpunit.protocol.UploadFileSpec;
12
import com.meterware.httpunit.scripting.ScriptingHandler;
13

14
import java.io.File;
15
import java.io.IOException;
16
import java.io.InputStream;
17
import java.io.OutputStream;
18
import java.net.HttpURLConnection;
19
import java.net.MalformedURLException;
20
import java.net.URL;
21
import java.net.URLConnection;
22
import java.net.URLStreamHandler;
23
import java.util.Dictionary;
24
import java.util.HashSet;
25
import java.util.Hashtable;
26
import java.util.Locale;
27

28
import org.w3c.dom.Element;
29
import org.xml.sax.SAXException;
30

31
/**
32
 * A request sent to a web server.
33
 **/
34
public abstract class WebRequest {
35

36
    /** The Constant REFERER_HEADER_NAME. */
37
    static final String REFERER_HEADER_NAME = "Referer";
38

39
    /** The javascript stream handler. */
40
    private static URLStreamHandler JAVASCRIPT_STREAM_HANDLER = new JavascriptURLStreamHandler();
1✔
41

42
    /** The https stream handler. */
43
    private static URLStreamHandler HTTPS_STREAM_HANDLER = new HttpsURLStreamHandler();
1✔
44

45
    /** The parameter holder. */
46
    private final ParameterHolder _parameterHolder;
47

48
    /** The url base. */
49
    private URL _urlBase;
50

51
    /** The source frame. */
52
    private FrameSelector _sourceFrame;
53

54
    /** The request target. */
55
    private String _requestTarget;
56

57
    /** The url string. */
58
    private String _urlString;
59

60
    /** The headers. */
61
    private Hashtable _headers;
62

63
    /** The web request source. */
64
    private WebRequestSource _webRequestSource;
65

66
    /** The referring page. */
67
    private WebResponse _referringPage;
68

69
    /** The button. */
70
    private SubmitButton _button;
71

72
    /** The source element. */
73
    private Element _sourceElement;
74

75
    /** The character set. */
76
    private String _characterSet;
77

78
    /**
79
     * Sets the value of a header to be sent with this request. A header set here will override any matching header set
80
     * in the WebClient when the request is actually sent.
81
     *
82
     * @param headerName
83
     *            - the name of the header
84
     * @param headerValue
85
     *            - the value to be set
86
     */
87
    public void setHeaderField(String headerName, String headerValue) {
88
        getHeaderDictionary().put(headerName, headerValue);
1✔
89
    }
1✔
90

91
    /**
92
     * Returns a copy of the headers to be sent with this request.
93
     *
94
     * @return the dictionary of headers
95
     **/
96
    public Dictionary getHeaders() {
97
        return (Dictionary) getHeaderDictionary().clone();
1✔
98
    }
99

100
    /**
101
     * Returns the final URL associated with this web request.
102
     *
103
     * @return the Uniform Resource Locator for this Web request
104
     *
105
     * @throws MalformedURLException
106
     *             if the URL is not o.k.
107
     **/
108
    public URL getURL() throws MalformedURLException {
109
        if (getURLBase() == null || getURLBase().toString().indexOf("?") < 0) {
1✔
110
            return newURL(getURLBase(), getURLString());
1✔
111
        }
112
        final String urlBaseString = getURLBase().toString();
1✔
113
        URL newurlbase = new URL(urlBaseString.substring(0, urlBaseString.indexOf("?")));
1✔
114
        return newURL(newurlbase, getURLString());
1✔
115
    }
116

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

146
    /**
147
     * New combined URL.
148
     *
149
     * @param base
150
     *            the base
151
     * @param spec
152
     *            the spec
153
     *
154
     * @return the url
155
     *
156
     * @throws MalformedURLException
157
     *             the malformed URL exception
158
     */
159
    private URL newCombinedURL(final URL base, final String spec) throws MalformedURLException {
160
        if (base == null) {
1✔
161
            return new URL(getNormalizedURL(spec));
1✔
162
        }
163
        if (spec.startsWith("..")) {
1✔
164
            return new URL(getNormalizedURL(getURLDirectory(base) + spec));
1✔
165
        }
166
        return new URL(base, getNormalizedURL(spec));
1✔
167
    }
168

169
    /**
170
     * Gets the URL directory.
171
     *
172
     * @param base
173
     *            the base
174
     *
175
     * @return the URL directory
176
     */
177
    private String getURLDirectory(final URL base) {
178
        String url = base.toExternalForm();
1✔
179
        int i = url.lastIndexOf('/');
1✔
180
        return url.substring(0, i + 1);
1✔
181
    }
182

183
    /**
184
     * Gets the normalized URL.
185
     *
186
     * @param url
187
     *            the url
188
     *
189
     * @return the normalized URL
190
     */
191
    private String getNormalizedURL(String url) {
192
        int questionIndex = url.indexOf('?');
1✔
193
        if (questionIndex < 0) {
1✔
194
            return getNormalizedPath(url);
1✔
195
        }
196
        return getNormalizedPath(url.substring(0, questionIndex)) + url.substring(questionIndex);
1✔
197
    }
198

199
    /**
200
     * Gets the normalized path.
201
     *
202
     * @param path
203
     *            the path
204
     *
205
     * @return the normalized path
206
     */
207
    private String getNormalizedPath(String path) {
208
        if (path.lastIndexOf("//") > path.lastIndexOf("://") + 1) {
1✔
209
            return getNormalizedPath(stripDoubleSlashes(path));
1✔
210
        }
211
        if (path.indexOf("/../") > 0 || path.endsWith("/..")) {
1!
212
            return getNormalizedPath(stripUpNavigation(path));
1✔
213
        }
214
        if (path.indexOf("/./") > 0) {
1✔
215
            return getNormalizedPath(stripInPlaceNavigation(path));
1✔
216
        }
217
        return path;
1✔
218
    }
219

220
    /**
221
     * Strip in place navigation.
222
     *
223
     * @param url
224
     *            the url
225
     *
226
     * @return the string
227
     */
228
    private String stripInPlaceNavigation(String url) {
229
        int i = url.lastIndexOf("/./");
1✔
230
        return url.substring(0, i + 1) + url.substring(i + 2);
1✔
231
    }
232

233
    /**
234
     * Strip up navigation.
235
     *
236
     * @param url
237
     *            the url
238
     *
239
     * @return the string
240
     */
241
    private String stripUpNavigation(String url) {
242
        int i = url.indexOf("/..");
1✔
243
        int j = url.lastIndexOf("/", i - 1);
1✔
244
        return url.substring(0, j + 1) + url.substring(i + 3);
1✔
245
    }
246

247
    /**
248
     * Strip double slashes.
249
     *
250
     * @param url
251
     *            the url
252
     *
253
     * @return the string
254
     */
255
    private String stripDoubleSlashes(String url) {
256
        int i = url.lastIndexOf("//");
1✔
257
        return url.substring(0, i) + url.substring(i + 1);
1✔
258
    }
259

260
    /**
261
     * Returns the target for this web request.
262
     *
263
     * @return the target
264
     */
265
    public String getTarget() {
266
        return _requestTarget;
1✔
267
    }
268

269
    /**
270
     * Returns the frame from which this request originates.
271
     *
272
     * @return the source frame
273
     */
274
    FrameSelector getSourceFrame() {
275
        return _sourceFrame;
1✔
276
    }
277

278
    /**
279
     * the HTTP method defined for this request e.g. DELETE,OPTIONS,HEAD
280
     */
281
    protected String method;
282

283
    /**
284
     * Gets the method.
285
     *
286
     * @return the method
287
     */
288
    public String getMethod() {
289
        return method;
1✔
290
    }
291

292
    /**
293
     * Returns the query string defined for this request. The query string is sent to the HTTP server as part of the
294
     * request header. This default implementation returns an empty string.
295
     *
296
     * @return the query string
297
     */
298
    public String getQueryString() {
299
        return "";
1✔
300
    }
301

302
    // ------------------------------------- ParameterCollection methods ------------------------------------
303

304
    /**
305
     * Sets the value of a parameter in a web request.
306
     *
307
     * @param name
308
     *            the name
309
     * @param value
310
     *            the value
311
     */
312
    public void setParameter(String name, String value) {
313
        _parameterHolder.setParameter(name, value);
1✔
314
    }
1✔
315

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

328
    /**
329
     * Sets the multiple values of a file upload parameter in a web request.
330
     *
331
     * @param parameterName
332
     *            the parameter name
333
     * @param files
334
     *            the files
335
     */
336
    public void setParameter(String parameterName, UploadFileSpec[] files) {
337
        if (!maySelectFile(parameterName)) {
1✔
338
            throw new IllegalNonFileParameterException(parameterName);
1✔
339
        }
340
        if (!isMimeEncoded()) {
1!
341
            throw new MultipartFormRequiredException();
×
342
        }
343
        _parameterHolder.setParameter(parameterName, files);
1✔
344
    }
1✔
345

346
    /**
347
     * Specifies the click position for the submit button. When a user clioks on an image button, not only the name and
348
     * value of the button, but also the position of the mouse at the time of the click is submitted with the form. This
349
     * method allows the caller to override the position selected when this request was created.
350
     *
351
     * @param x
352
     *            the x
353
     * @param y
354
     *            the y
355
     *
356
     * @exception IllegalRequestParameterException
357
     *                thrown if the request was not created from a form with an image button.
358
     */
359
    public void setImageButtonClickPosition(int x, int y) throws IllegalRequestParameterException {
360
        if (_button == null) {
1✔
361
            throw new IllegalButtonPositionException();
1✔
362
        }
363
        _parameterHolder.selectImageButtonPosition(_button, x, y);
1✔
364
    }
1✔
365

366
    /**
367
     * Returns true if the specified parameter is a file field.
368
     *
369
     * @param name
370
     *            the name
371
     *
372
     * @return true, if is file parameter
373
     */
374
    public boolean isFileParameter(String name) {
375
        return _parameterHolder.isFileParameter(name);
1✔
376
    }
377

378
    /**
379
     * Sets the file for a parameter upload in a web request.
380
     *
381
     * @param parameterName
382
     *            the parameter name
383
     * @param file
384
     *            the file
385
     */
386
    public void selectFile(String parameterName, File file) {
387
        setParameter(parameterName, new UploadFileSpec[] { new UploadFileSpec(file) });
1✔
388
    }
1✔
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
     * @param contentType
398
     *            the content type
399
     */
400
    public void selectFile(String parameterName, File file, String contentType) {
401
        setParameter(parameterName, new UploadFileSpec[] { new UploadFileSpec(file, contentType) });
1✔
402
    }
1✔
403

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

420
    /**
421
     * Returns an array of all parameter names defined as part of this web request.
422
     *
423
     * @return the request parameter names
424
     */
425
    public String[] getRequestParameterNames() {
426
        final HashSet<String> names = new HashSet<>();
1✔
427
        ParameterProcessor pp = new ParameterProcessor() {
1✔
428
            @Override
429
            public void addParameter(String name, String value, String characterSet) throws IOException {
430
                names.add(name);
1✔
431
            }
1✔
432

433
            @Override
434
            public void addFile(String parameterName, UploadFileSpec fileSpec) throws IOException {
435
                names.add(parameterName);
×
436
            }
×
437
        };
438

439
        try {
440
            _parameterHolder.recordPredefinedParameters(pp);
1✔
441
            _parameterHolder.recordParameters(pp);
1✔
442
        } catch (IOException e) {
×
443
        }
1✔
444

445
        return names.toArray(new String[names.size()]);
1✔
446
    }
447

448
    /**
449
     * Returns the value of a parameter in this web request.
450
     *
451
     * @param name
452
     *            the name
453
     *
454
     * @return the value of the named parameter, or empty string if it is not set.
455
     */
456
    public String getParameter(String name) {
457
        String[] values = getParameterValues(name);
1✔
458
        return values.length == 0 ? "" : values[0];
1✔
459
    }
460

461
    /**
462
     * Returns the multiple default values of the named parameter.
463
     *
464
     * @param name
465
     *            the name
466
     *
467
     * @return the parameter values
468
     */
469
    public String[] getParameterValues(String name) {
470
        return _parameterHolder.getParameterValues(name);
1✔
471
    }
472

473
    /**
474
     * Removes a parameter from this web request.
475
     *
476
     * @param name
477
     *            the name
478
     */
479
    public void removeParameter(String name) {
480
        _parameterHolder.removeParameter(name);
1✔
481
    }
1✔
482

483
    // ------------------------------------- Object methods ------------------------------------
484

485
    @Override
486
    public String toString() {
487
        return getMethod() + " request for (" + getURLBase() + ") " + getURLString();
×
488
    }
489

490
    // ------------------------------------- protected members ------------------------------------
491

492
    /**
493
     * Constructs a web request using an absolute URL string.
494
     *
495
     * @param urlString
496
     *            the url string
497
     */
498
    protected WebRequest(String urlString) {
499
        this(null, urlString);
1✔
500
    }
1✔
501

502
    /**
503
     * Constructs a web request using a base URL and a relative URL string.
504
     *
505
     * @param urlBase
506
     *            the url base
507
     * @param urlString
508
     *            the url string
509
     */
510
    protected WebRequest(URL urlBase, String urlString) {
511
        this(urlBase, urlString, TOP_FRAME);
1✔
512
    }
1✔
513

514
    /**
515
     * Constructs a web request using a base request and a relative URL string.
516
     *
517
     * @param baseRequest
518
     *            the base request
519
     * @param urlString
520
     *            the url string
521
     * @param target
522
     *            the target
523
     *
524
     * @throws MalformedURLException
525
     *             the malformed URL exception
526
     */
527
    protected WebRequest(WebRequest baseRequest, String urlString, String target) throws MalformedURLException {
528
        this(baseRequest.getURL(), urlString, target);
×
529
    }
×
530

531
    /**
532
     * Constructs a web request using a base URL, a relative URL string, and a target.
533
     *
534
     * @param urlBase
535
     *            the url base
536
     * @param urlString
537
     *            the url string
538
     * @param target
539
     *            the target
540
     */
541
    protected WebRequest(URL urlBase, String urlString, String target) {
542
        this(urlBase, urlString, FrameSelector.TOP_FRAME, target);
1✔
543
    }
1✔
544

545
    /**
546
     * Constructs a web request using a base URL, a relative URL string, and a target.
547
     *
548
     * @param referer
549
     *            the referer
550
     * @param sourceElement
551
     *            the source element
552
     * @param urlBase
553
     *            the url base
554
     * @param urlString
555
     *            the url string
556
     * @param target
557
     *            the target
558
     */
559
    protected WebRequest(WebResponse referer, Element sourceElement, URL urlBase, String urlString, String target) {
560
        this(urlBase, urlString, referer.getFrame(), target != null ? target : referer.getBaseTarget());
1✔
561
        _sourceElement = sourceElement;
1✔
562
        _referringPage = referer;
1✔
563
        setHeaderField(REFERER_HEADER_NAME, referer.getURL().toExternalForm());
1✔
564
    }
1✔
565

566
    /**
567
     * Constructs a web request using a base URL, a relative URL string, and a target.
568
     *
569
     * @param urlBase
570
     *            the url base
571
     * @param urlString
572
     *            the url string
573
     * @param frame
574
     *            the frame
575
     * @param target
576
     *            the target
577
     */
578
    protected WebRequest(URL urlBase, String urlString, FrameSelector frame, String target) {
579
        this(urlBase, urlString, frame, target, new UncheckedParameterHolder());
1✔
580
    }
1✔
581

582
    /**
583
     * Constructs a web request from a form.
584
     *
585
     * @param sourceForm
586
     *            - the WebForm to startFrom
587
     * @param parameterHolder
588
     *            - the parameter holder
589
     * @param button
590
     *            - the submit button
591
     * @param x
592
     *            - x position
593
     * @param y
594
     *            - y position
595
     **/
596
    protected WebRequest(WebForm sourceForm, ParameterHolder parameterHolder, SubmitButton button, int x, int y) {
597
        this(sourceForm, parameterHolder);
1✔
598
        // [ 1443333 ] Allow unnamed Image input elements to submit x,y values
599
        if (button != null && button.isValidImageButton()) {
1!
600
            _button = button;
1✔
601
            _parameterHolder.selectImageButtonPosition(_button, x, y);
1✔
602
        }
603
    }
1✔
604

605
    /**
606
     * Instantiates a new web request.
607
     *
608
     * @param requestSource
609
     *            the request source
610
     * @param parameterHolder
611
     *            the parameter holder
612
     */
613
    protected WebRequest(WebRequestSource requestSource, ParameterHolder parameterHolder) {
614
        this(requestSource.getBaseURL(), requestSource.getRelativePage(), requestSource.getFrame(),
1✔
615
                requestSource.getTarget(), parameterHolder);
1✔
616
        _webRequestSource = requestSource;
1✔
617
        _sourceElement = requestSource.getElement();
1✔
618
    }
1✔
619

620
    /**
621
     * New parameter holder.
622
     *
623
     * @param requestSource
624
     *            the request source
625
     *
626
     * @return the parameter holder
627
     */
628
    static ParameterHolder newParameterHolder(WebRequestSource requestSource) {
629
        if (HttpUnitOptions.getParameterValuesValidated()) {
1✔
630
            return requestSource;
1✔
631
        }
632
        return new UncheckedParameterHolder(requestSource);
1✔
633
    }
634

635
    /**
636
     * Constructs a web request using a base URL, a relative URL string, and a target.
637
     *
638
     * @param urlBase
639
     *            the url base
640
     * @param urlString
641
     *            the url string
642
     * @param sourceFrame
643
     *            the source frame
644
     * @param requestTarget
645
     *            the request target
646
     * @param parameterHolder
647
     *            the parameter holder
648
     */
649
    private WebRequest(URL urlBase, String urlString, FrameSelector sourceFrame, String requestTarget,
650
            ParameterHolder parameterHolder) {
1✔
651
        _urlBase = urlBase;
1✔
652
        _sourceFrame = sourceFrame;
1✔
653
        _requestTarget = requestTarget;
1✔
654
        _urlString = urlString.toLowerCase(Locale.ENGLISH).startsWith("http") ? escape(urlString) : urlString;
1✔
655
        _parameterHolder = parameterHolder;
1✔
656
        _characterSet = parameterHolder.getCharacterSet();
1✔
657
    }
1✔
658

659
    /**
660
     * Escape.
661
     *
662
     * @param urlString
663
     *            the url string
664
     *
665
     * @return the string
666
     */
667
    private static String escape(String urlString) {
668
        if (urlString.indexOf(' ') < 0) {
1✔
669
            return urlString;
1✔
670
        }
671
        StringBuilder sb = new StringBuilder();
1✔
672

673
        int start = 0;
1✔
674
        do {
675
            int index = urlString.indexOf(' ', start);
1✔
676
            if (index < 0) {
1✔
677
                sb.append(urlString.substring(start));
1✔
678
                break;
1✔
679
            }
680
            sb.append(urlString.substring(start, index)).append("%20");
1✔
681
            start = index + 1;
1✔
682
        } while (true);
1✔
683
        return sb.toString();
1✔
684
    }
685

686
    /**
687
     * Returns true if selectFile may be called with this parameter.
688
     *
689
     * @param parameterName
690
     *            the parameter name
691
     *
692
     * @return true, if successful
693
     */
694
    protected boolean maySelectFile(String parameterName) {
695
        return isFileParameter(parameterName);
×
696
    }
697

698
    /**
699
     * Returns true if this request is to be MIME-encoded.
700
     *
701
     * @return true, if is mime encoded
702
     */
703
    protected boolean isMimeEncoded() {
704
        return false;
×
705
    }
706

707
    /**
708
     * Returns the content type of this request. If null, no content is specified.
709
     *
710
     * @return the content type
711
     */
712
    protected String getContentType() {
713
        return null;
1✔
714
    }
715

716
    /**
717
     * Returns the character set required for this request.
718
     *
719
     * @return the character set
720
     */
721
    protected final String getCharacterSet() {
722
        return _characterSet;
1✔
723
    }
724

725
    /**
726
     * Performs any additional processing necessary to complete the request.
727
     *
728
     * @param connection
729
     *            the connection
730
     *
731
     * @throws IOException
732
     *             Signals that an I/O exception has occurred.
733
     */
734
    protected void completeRequest(URLConnection connection) throws IOException {
735
        if (connection instanceof HttpURLConnection) {
1✔
736
            ((HttpURLConnection) connection).setRequestMethod(getMethod());
1✔
737
        }
738
    }
1✔
739

740
    /**
741
     * Writes the contents of the message body to the specified stream.
742
     *
743
     * @param stream
744
     *            the stream
745
     *
746
     * @throws IOException
747
     *             Signals that an I/O exception has occurred.
748
     */
749
    protected void writeMessageBody(OutputStream stream) throws IOException {
750
    }
1✔
751

752
    /**
753
     * Gets the URL base.
754
     *
755
     * @return the URL base
756
     */
757
    protected final URL getURLBase() {
758
        return _urlBase;
1✔
759
    }
760

761
    // ------------------------------------- protected members ---------------------------------------------
762

763
    /**
764
     * Gets the URL string.
765
     *
766
     * @return the URL string
767
     */
768
    protected String getURLString() {
769
        final String queryString = getQueryString();
1✔
770
        if (queryString.isEmpty()) {
1✔
771
            return _urlString;
1✔
772
        }
773
        return _urlString + "?" + queryString;
1✔
774
    }
775

776
    /**
777
     * Gets the parameter holder.
778
     *
779
     * @return the parameter holder
780
     */
781
    protected final ParameterHolder getParameterHolder() {
782
        return _parameterHolder;
1✔
783
    }
784

785
    // ---------------------------------- package members --------------------------------
786

787
    /** The target indicating the topmost frame of a window. **/
788
    static final String TOP_FRAME = "_top";
789

790
    /** The target indicating the parent of a frame. **/
791
    static final String PARENT_FRAME = "_parent";
792

793
    /** The target indicating a new, empty window. **/
794
    static final String NEW_WINDOW = "_blank";
795

796
    /** The target indicating the same frame. **/
797
    static final String SAME_FRAME = "_self";
798

799
    /**
800
     * Gets the header dictionary.
801
     *
802
     * @return the header dictionary
803
     */
804
    Hashtable getHeaderDictionary() {
805
        if (_headers == null) {
1✔
806
            _headers = new Hashtable<>();
1✔
807
            if (getContentType() != null) {
1✔
808
                _headers.put("Content-Type", getContentType());
1✔
809
            }
810
        }
811
        return _headers;
1✔
812
    }
813

814
    /**
815
     * Gets the referer.
816
     *
817
     * @return the referer
818
     */
819
    String getReferer() {
820
        return _headers == null ? null : (String) _headers.get(REFERER_HEADER_NAME);
1!
821
    }
822

823
    /**
824
     * Gets the source scripting handler.
825
     *
826
     * @return the source scripting handler
827
     */
828
    ScriptingHandler getSourceScriptingHandler() {
829
        WebRequestSource wrs = _webRequestSource;
1✔
830
        if (wrs != null) {
1!
831
            return wrs.getScriptingHandler();
×
832
        }
833
        if (_referringPage != null && _sourceElement != null) {
1!
834
            try {
835
                _referringPage.getReceivedPage().getElement(_sourceElement).getScriptingHandler();
1✔
836
            } catch (SAXException e) {
×
837
            }
1✔
838
        }
839
        return null;
1✔
840
    }
841

842
}
843

844
// ======================================== class JavaScriptURLStreamHandler
845
// ============================================
846

847
class JavascriptURLStreamHandler extends URLStreamHandler {
1✔
848

849
    @Override
850
    protected URLConnection openConnection(URL u) throws IOException {
851
        return null;
×
852
    }
853
}
854

855
// ======================================== class HttpsURLStreamHandler ============================================
856

857
class HttpsURLStreamHandler extends URLStreamHandler {
1✔
858

859
    @Override
860
    protected URLConnection openConnection(URL u) throws IOException {
861
        throw new RuntimeException(
×
862
                "https support requires the Java Secure Sockets Extension. See http://java.sun.com/products/jsse");
863
    }
864
}
865

866
// ============================= exception class IllegalNonFileParameterException ======================================
867

868
/**
869
 * This exception is thrown on an attempt to set a non-file parameter to a file value.
870
 **/
871
class IllegalNonFileParameterException extends IllegalRequestParameterException {
872

873
    private static final long serialVersionUID = 1L;
874

875
    IllegalNonFileParameterException(String parameterName) {
1✔
876
        _parameterName = parameterName;
1✔
877
    }
1✔
878

879
    @Override
880
    public String getMessage() {
881
        return "Parameter '" + _parameterName + "' is not a file parameter and may not be set to a file value.";
×
882
    }
883

884
    private String _parameterName;
885

886
}
887

888
// ============================= exception class MultipartFormRequiredException ======================================
889

890
/**
891
 * This exception is thrown on an attempt to set a file parameter in a form that does not specify MIME encoding.
892
 **/
893
class MultipartFormRequiredException extends IllegalRequestParameterException {
894

895
    private static final long serialVersionUID = 1L;
896

897
    MultipartFormRequiredException() {
×
898
    }
×
899

900
    @Override
901
    public String getMessage() {
902
        return "The request does not use multipart/form-data encoding, and cannot be used to upload files ";
×
903
    }
904

905
}
906

907
// ============================= exception class IllegalButtonPositionException ======================================
908

909
/**
910
 * This exception is thrown on an attempt to set a file parameter in a form that does not specify MIME encoding.
911
 **/
912
class IllegalButtonPositionException extends IllegalRequestParameterException {
913

914
    private static final long serialVersionUID = 1L;
915

916
    IllegalButtonPositionException() {
1✔
917
    }
1✔
918

919
    @Override
920
    public String getMessage() {
921
        return "The request was not created with an image button, and cannot accept an image button click position";
×
922
    }
923

924
}
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