Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6433018.java
41154 views
/*1* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 643301826* @summary HTTP server sometimes sends bad request for browsers javascript27* @run main B643301828* @run main/othervm -Djava.net.preferIPv6Addresses=true B643301829*/3031import com.sun.net.httpserver.*;3233import java.util.concurrent.*;34import java.io.*;35import java.net.*;3637public class B6433018 {3839static final String CRLF = "\r\n";4041/* invalid HTTP POST with extra CRLF at end */42/* This checks that the server is able to handle it43* and recognise the second request */4445static final String cmd =46"POST /test/item HTTP/1.1"+CRLF+47"Keep-Alive: 300"+CRLF+48"Proxy-Connection: keep-alive"+CRLF+49"Content-Type: text/xml"+CRLF+50"Content-Length: 22"+CRLF+51"Pragma: no-cache"+CRLF+52"Cache-Control: no-cache"+CRLF+ CRLF+53"<item desc=\"excuse\" />"+CRLF+54"GET /test/items HTTP/1.1"+CRLF+55"Host: araku:9999"+CRLF+56"Accept-Language: en-us,en;q=0.5"+CRLF+57"Accept-Encoding: gzip,deflate"+CRLF+58"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"+CRLF+59"Keep-Alive: 300"+CRLF+60"Proxy-Connection: keep-alive"+CRLF+61"Pragma: no-cache"+CRLF+62"Cache-Control: no-cache"+CRLF+CRLF;6364public static void main(String[] args) throws Exception {65CountDownLatch finished = new CountDownLatch(2);66Handler handler = new Handler(finished);67InetAddress loopback = InetAddress.getLoopbackAddress();68InetSocketAddress addr = new InetSocketAddress(loopback, 0);69HttpServer server = HttpServer.create(addr, 0);70HttpContext ctx = server.createContext("/test", handler);7172server.start();73int port = server.getAddress().getPort();74try (Socket s = new Socket(loopback, port);75OutputStream os = s.getOutputStream()) {76os.write(cmd.getBytes());77finished.await(30, TimeUnit.SECONDS);78} finally {79server.stop(2);80}8182if (finished.getCount() != 0)83throw new RuntimeException("did not receive the 2 requests");8485System.out.println("OK");86}8788static class Handler implements HttpHandler {89private final CountDownLatch finished;9091Handler(CountDownLatch finished) {92this.finished = finished;93}9495@Override96public void handle(HttpExchange t) throws IOException {97try (InputStream is = t.getRequestBody()) {98Headers map = t.getRequestHeaders();99Headers rmap = t.getResponseHeaders();100while (is.read() != -1);101}102t.sendResponseHeaders(200, -1);103t.close();104finished.countDown();105}106}107}108109110