aboutsummaryrefslogtreecommitdiff
path: root/test/sun/net/www/B8185898.java
blob: 67f3998e6bd2c218b51091944900af51690d3082 (plain)
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
/*
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @bug 8185898
 * @library /lib/testlibrary
 * @run main/othervm B8185898
 * @summary setRequestProperty(key, null) results in HTTP header without colon in request
 */

import java.io.*;
import java.net.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.Collections;

import jdk.testlibrary.net.URIBuilder;
import sun.net.www.MessageHeader;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.UTF_8;

/*
 * Test checks that MessageHeader with key != null and value == null is set correctly
 * and printed according to HTTP standard in the format <key>: <value>
 * */
public class B8185898 {

    static HttpServer server;
    static final String RESPONSE_BODY = "Test response body";
    static final String H1 = "X-header1";
    static final String H2 = "X-header2";
    static final String VALUE = "This test value should appear";
    static final List<String> oneList = Arrays.asList(VALUE);
    static final List<String> zeroList = Arrays.asList("");
    static int port;
    static URL url;
    static volatile Map<String, List<String>> headers;

    static class Handler implements HttpHandler {

        public void handle(HttpExchange t) throws IOException {
            InputStream is = t.getRequestBody();
            InetSocketAddress rem = t.getRemoteAddress();
            headers = t.getRequestHeaders();    // Get request headers on the server side
            while(is.read() != -1){}
            is.close();

            OutputStream os = t.getResponseBody();
            t.sendResponseHeaders(200, RESPONSE_BODY.length());
            os.write(RESPONSE_BODY.getBytes(UTF_8));
            t.close();
        }
    }

    public static void main(String[] args) throws Exception {
        ExecutorService exec = Executors.newCachedThreadPool();
        InetAddress loopback = InetAddress.getLoopbackAddress();

        try {
            InetSocketAddress addr = new InetSocketAddress(loopback, 0);
            server = HttpServer.create(addr, 100);
            HttpHandler handler = new Handler();
            HttpContext context = server.createContext("/", handler);
            server.setExecutor(exec);
            server.start();

            port = server.getAddress().getPort();
            System.out.println("Server on port: " + port);
            url = URIBuilder.newBuilder()
                    .scheme("http")
                    .loopback()
                    .port(port)
                    .path("/foo")
                    .toURLUnchecked();
            System.out.println("URL: " + url);
            testMessageHeader();
            testMessageHeaderMethods();
            testURLConnectionMethods();
        } finally {
            server.stop(0);
            System.out.println("After server shutdown");
            exec.shutdown();
        }
    }

    // Test message header with malformed message header and fake request line
    static void testMessageHeader() {
        final String badHeader = "This is not a request line for HTTP/1.1";
        final String fakeRequestLine = "This /is/a/fake/status/line HTTP/2.0";
        final String expectedHeaders = fakeRequestLine + "\r\n"
                + H1 + ": " + VALUE + "\r\n"
                + H2 + ": " + VALUE + "\r\n"
                + badHeader + ":\r\n\r\n";

        MessageHeader header = new MessageHeader();
        header.add(H1, VALUE);
        header.add(H2, VALUE);
        header.add(badHeader, null);
        header.prepend(fakeRequestLine, null);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        header.print(new PrintStream(out));

        if (!out.toString().equals(expectedHeaders)) {
            throw new AssertionError("FAILED: expected: "
                    + expectedHeaders + "\nReceived: " + out.toString());
        } else {
            System.out.println("PASSED: ::print returned correct "
                    + "status line and headers:\n" + out.toString());
        }
    }

    // Test MessageHeader::print, ::toString, implicitly testing that
    // MessageHeader::mergeHeader formats headers correctly for responses
    static void testMessageHeaderMethods() throws IOException {
        // {{inputString1, expectedToString1, expectedPrint1}, {...}}
        String[][] strings = {
                {"HTTP/1.1 200 OK\r\n"
                        + "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"
                        + "Connection: keep-alive\r\n"
                        + "Host: 127.0.0.1:12345\r\n"
                        + "User-agent: Java/12\r\n\r\nfoooo",
                "pairs: {null: HTTP/1.1 200 OK}"
                        + "{Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}"
                        + "{Connection: keep-alive}"
                        + "{Host: 127.0.0.1:12345}"
                        + "{User-agent: Java/12}",
                "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"
                        + "Connection: keep-alive\r\n"
                        + "Host: 127.0.0.1:12345\r\n"
                        + "User-agent: Java/12\r\n\r\n"},
                {"HTTP/1.1 200 OK\r\n"
                        + "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"
                        + "Connection: keep-alive\r\n"
                        + "Host: 127.0.0.1:12345\r\n"
                        + "User-agent: Java/12\r\n"
                        + "X-Header:\r\n\r\n",
                "pairs: {null: HTTP/1.1 200 OK}"
                        + "{Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}"
                        + "{Connection: keep-alive}"
                        + "{Host: 127.0.0.1:12345}"
                        + "{User-agent: Java/12}"
                        + "{X-Header: }",
                "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"
                        + "Connection: keep-alive\r\n"
                        + "Host: 127.0.0.1:12345\r\n"
                        + "User-agent: Java/12\r\n"
                        + "X-Header: \r\n\r\n"},
        };

        System.out.println("Test custom message headers");
        for (String[] s : strings) {
            // Test MessageHeader::toString
            MessageHeader header = new MessageHeader(
                    new ByteArrayInputStream(s[0].getBytes(ISO_8859_1)));
            if (!header.toString().endsWith(s[1])) {
                throw new AssertionError("FAILED: expected: "
                        + s[1] + "\nReceived: " + header);
            } else {
                System.out.println("PASSED: ::toString returned correct "
                        + "status line and headers:\n" + header);
            }

            // Test MessageHeader::print
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            header.print(new PrintStream(out));
            if (!out.toString().equals(s[2])) {
                throw new AssertionError("FAILED: expected: "
                        + s[2] + "\nReceived: " + out.toString());
            } else {
                System.out.println("PASSED: ::print returned correct "
                        + "status line and headers:\n" + out.toString());
            }
        }
    }

    // Test methods URLConnection::getRequestProperties,
    // ::getHeaderField, ::getHeaderFieldKey
    static void testURLConnectionMethods() throws IOException {
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
        urlConn.setRequestProperty(H1, "");
        urlConn.setRequestProperty(H1, VALUE);
        urlConn.setRequestProperty(H2, null);    // Expected to contain ':' between key and value
        Map<String, List<String>> props = urlConn.getRequestProperties();
        Map<String, List<String>> expectedMap = new HashMap<String, List<String>>();
        expectedMap.put(H1, oneList);
        expectedMap.put(H2, Arrays.asList((String)null));

        // Test request properties
        System.out.println("Client request properties");
        StringBuilder sb = new StringBuilder();
        props.forEach((k, v) -> sb.append(k + ": "
                + v.stream().collect(Collectors.joining()) + "\n"));
        System.out.println(sb);

        if (!props.equals(expectedMap)) {
            throw new AssertionError("Unexpected properties returned: "
                    + props);
        } else {
            System.out.println("Properties returned as expected");
        }

        // Test header fields
        String headerField = urlConn.getHeaderField(0);
        if (!headerField.contains("200 OK")) {
            throw new AssertionError("Expected headerField[0]: status line. "
                    + "Received: " + headerField);
        } else {
            System.out.println("PASSED: headerField[0] contains status line: "
                    + headerField);
        }

        String headerFieldKey = urlConn.getHeaderFieldKey(0);
        if (headerFieldKey != null) {
            throw new AssertionError("Expected headerFieldKey[0]: null. "
                    + "Received: " + headerFieldKey);
        } else {
            System.out.println("PASSED: headerFieldKey[0] is null");
        }

        // Check that test request headers are included with correct format
        try (
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(urlConn.getInputStream()))
        ) {
            if (!headers.keySet().contains(H1)) {
                throw new AssertionError("Expected key not found: "
                        + H1 + ": " + VALUE);
            } else if (!headers.get(H1).equals(oneList)) {
                throw new AssertionError("Unexpected key-value pair: "
                        + H1 + ": " + headers.get(H1));
            } else {
                System.out.println("PASSED: " + H1 + " included in request headers");
            }

            if (!headers.keySet().contains(H2)) {
                throw new AssertionError("Expected key not found: "
                        + H2 + ": ");
                // Check that empty list is returned
            } else if (!headers.get(H2).equals(zeroList)) {
                throw new AssertionError("Unexpected key-value pair: "
                        + H2 + ": " + headers.get(H2));
            } else {
                System.out.println("PASSED: " + H2 + " included in request headers");
            }

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
        }
    }
}