• 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

82.81
/src/main/java/com/meterware/pseudoserver/HttpRequest.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.pseudoserver;
9

10
import com.meterware.httpunit.HttpUnitUtils;
11

12
import java.io.IOException;
13
import java.io.InputStream;
14
import java.util.Hashtable;
15
import java.util.StringTokenizer;
16

17
/**
18
 * Represents a single HTTP request, extracted from the input stream.
19
 */
20
public class HttpRequest extends ReceivedHttpMessage {
21

22
    /** The protocol. */
23
    private String _protocol;
24

25
    /** The command. */
26
    private String _command;
27

28
    /** The uri. */
29
    private String _uri;
30

31
    /** The parameters. */
32
    private Hashtable _parameters;
33

34
    /**
35
     * Instantiates a new http request.
36
     *
37
     * @param inputStream
38
     *            the input stream
39
     *
40
     * @throws IOException
41
     *             Signals that an I/O exception has occurred.
42
     */
43
    HttpRequest(InputStream inputStream) throws IOException {
44
        super(inputStream);
1✔
45
    }
1✔
46

47
    @Override
48
    void interpretMessageHeader(String messageHeader) {
49
        StringTokenizer st = new StringTokenizer(messageHeader);
1✔
50
        _command = st.nextToken();
1✔
51
        _uri = st.nextToken();
1✔
52
        _protocol = st.nextToken();
1✔
53
    }
1✔
54

55
    @Override
56
    void appendMessageHeader(StringBuilder sb) {
57
        sb.append(_command).append(' ').append(_uri).append(' ').append(_protocol);
1✔
58
    }
1✔
59

60
    /**
61
     * Returns the command associated with this request.
62
     *
63
     * @return the command
64
     */
65
    public String getCommand() {
66
        return _command;
1✔
67
    }
68

69
    /**
70
     * Returns the URI specified in the message header for this request.
71
     *
72
     * @return the uri
73
     */
74
    public String getURI() {
75
        return _uri;
1✔
76
    }
77

78
    /**
79
     * Returns the protocol string specified in the message header for this request.
80
     *
81
     * @return the protocol
82
     */
83
    public String getProtocol() {
84
        return _protocol;
1✔
85
    }
86

87
    /**
88
     * Returns the parameter with the specified name. If no such parameter exists, will return null.
89
     *
90
     * @param name
91
     *            the name
92
     *
93
     * @return the parameter
94
     */
95
    public String[] getParameter(String name) {
96
        if (_parameters == null) {
1!
97
            if (_command.equalsIgnoreCase("GET") || _command.equalsIgnoreCase("HEAD")) {
1!
98
                _parameters = readParameters(getParameterString(_uri));
1✔
99
            } else {
100
                _parameters = readParameters(new String(getBody()));
1✔
101
            }
102
        }
103
        return (String[]) _parameters.get(name);
1✔
104
    }
105

106
    /**
107
     * Gets the parameter string.
108
     *
109
     * @param uri
110
     *            the uri
111
     *
112
     * @return the parameter string
113
     */
114
    private String getParameterString(String uri) {
115
        return uri.indexOf('?') < 0 ? "" : uri.substring(uri.indexOf('?') + 1);
1!
116
    }
117

118
    /**
119
     * Wants keep alive.
120
     *
121
     * @return true, if successful
122
     */
123
    boolean wantsKeepAlive() {
124
        if ("Keep-alive".equalsIgnoreCase(getConnectionHeader())) {
1✔
125
            return true;
1✔
126
        }
127
        if (_protocol.equals("HTTP/1.1")) {
1✔
128
            return !"Close".equalsIgnoreCase(getConnectionHeader());
1✔
129
        }
130
        return false;
1✔
131
    }
132

133
    /**
134
     * Read parameters.
135
     *
136
     * @param content
137
     *            the content
138
     *
139
     * @return the hashtable
140
     */
141
    private Hashtable readParameters(String content) {
142
        Hashtable parameters = new Hashtable<>();
1✔
143
        if (content == null || content.trim().isEmpty()) {
1!
144
            return parameters;
×
145
        }
146

147
        for (String spec : content.split("&")) {
1✔
148
            String[] split = spec.split("=");
1✔
149
            addParameter(parameters, HttpUnitUtils.decode(split[0]),
1✔
150
                    split.length < 2 ? null : HttpUnitUtils.decode(split[1]));
1✔
151
        }
152
        return parameters;
1✔
153
    }
154

155
    /**
156
     * Adds the parameter.
157
     *
158
     * @param parameters
159
     *            the parameters
160
     * @param name
161
     *            the name
162
     * @param value
163
     *            the value
164
     */
165
    private void addParameter(Hashtable parameters, String name, String value) {
166
        String[] oldValues = (String[]) parameters.get(name);
1✔
167
        if (oldValues == null) {
1!
168
            parameters.put(name, new String[] { value });
1✔
169
        } else {
170
            String[] values = new String[oldValues.length + 1];
×
171
            System.arraycopy(oldValues, 0, values, 0, oldValues.length);
×
172
            values[oldValues.length] = value;
×
173
            parameters.put(name, values);
×
174
        }
175
    }
1✔
176

177
    /**
178
     * Gets the connection header.
179
     *
180
     * @return the connection header
181
     */
182
    private String getConnectionHeader() {
183
        return getHeader("Connection");
1✔
184
    }
185

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