Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/bugs/B6433018.java
38867 views
/*1* Copyright (c) 2006, 2013 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*/2829import com.sun.net.httpserver.*;3031import java.util.concurrent.*;32import java.io.*;33import java.net.*;3435public class B6433018 {3637static final String CRLF = "\r\n";3839/* invalid HTTP POST with extra CRLF at end */40/* This checks that the server is able to handle it41* and recognise the second request */4243static final String cmd =44"POST /test/item HTTP/1.1"+CRLF+45"Keep-Alive: 300"+CRLF+46"Proxy-Connection: keep-alive"+CRLF+47"Content-Type: text/xml"+CRLF+48"Content-Length: 22"+CRLF+49"Pragma: no-cache"+CRLF+50"Cache-Control: no-cache"+CRLF+ CRLF+51"<item desc=\"excuse\" />"+CRLF+52"GET /test/items HTTP/1.1"+CRLF+53"Host: araku:9999"+CRLF+54"Accept-Language: en-us,en;q=0.5"+CRLF+55"Accept-Encoding: gzip,deflate"+CRLF+56"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"+CRLF+57"Keep-Alive: 300"+CRLF+58"Proxy-Connection: keep-alive"+CRLF+59"Pragma: no-cache"+CRLF+60"Cache-Control: no-cache"+CRLF+CRLF;6162public static void main(String[] args) throws Exception {63CountDownLatch finished = new CountDownLatch(2);64Handler handler = new Handler(finished);65InetSocketAddress addr = new InetSocketAddress(0);66HttpServer server = HttpServer.create(addr, 0);67HttpContext ctx = server.createContext("/test", handler);6869server.start();70int port = server.getAddress().getPort();71try (Socket s = new Socket("localhost", port);72OutputStream os = s.getOutputStream()) {73os.write(cmd.getBytes());74finished.await(30, TimeUnit.SECONDS);75} finally {76server.stop(2);77}7879if (finished.getCount() != 0)80throw new RuntimeException("did not receive the 2 requests");8182System.out.println("OK");83}8485static class Handler implements HttpHandler {86private final CountDownLatch finished;8788Handler(CountDownLatch finished) {89this.finished = finished;90}9192@Override93public void handle(HttpExchange t) throws IOException {94try (InputStream is = t.getRequestBody()) {95Headers map = t.getRequestHeaders();96Headers rmap = t.getResponseHeaders();97while (is.read() != -1);98}99t.sendResponseHeaders(200, -1);100t.close();101finished.countDown();102}103}104}105106107108