Path: blob/master/test/jdk/com/sun/net/httpserver/MissingTrailingSpace.java
41152 views
/*1* Copyright (c) 2015, 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 806879526* @summary HttpServer missing tailing space for some response codes27* @run main MissingTrailingSpace28* @run main/othervm -Djava.net.preferIPv6Addresses=true MissingTrailingSpace29* @author [email protected]30*/3132import java.net.InetAddress;33import java.net.InetSocketAddress;34import java.io.InputStreamReader;35import java.io.IOException;36import java.io.BufferedReader;37import java.io.OutputStreamWriter;38import java.io.PrintWriter;39import java.net.Socket;40import java.util.concurrent.ExecutorService;41import java.util.concurrent.Executors;42import com.sun.net.httpserver.HttpExchange;43import com.sun.net.httpserver.HttpHandler;44import com.sun.net.httpserver.HttpServer;4546public class MissingTrailingSpace {4748private static final int noMsgCode = 207;49private static final String someContext = "/context";5051public static void main(String[] args) throws Exception {52InetAddress loopback = InetAddress.getLoopbackAddress();53HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 0);54try {55server.setExecutor(Executors.newFixedThreadPool(1));56server.createContext(someContext, new HttpHandler() {57@Override58public void handle(HttpExchange msg) {59try {60try {61msg.sendResponseHeaders(noMsgCode, -1);62} catch(IOException ioe) {63ioe.printStackTrace();64}65} finally {66msg.close();67}68}69});70server.start();71System.out.println("Server started at port "72+ server.getAddress().getPort());7374runRawSocketHttpClient(loopback, server.getAddress().getPort());75} finally {76((ExecutorService)server.getExecutor()).shutdown();77server.stop(0);78}79System.out.println("Server finished.");80}8182static void runRawSocketHttpClient(InetAddress address, int port)83throws Exception84{85Socket socket = null;86PrintWriter writer = null;87BufferedReader reader = null;88final String CRLF = "\r\n";89try {90socket = new Socket(address, port);91writer = new PrintWriter(new OutputStreamWriter(92socket.getOutputStream()));93System.out.println("Client connected by socket: " + socket);9495writer.print("GET " + someContext + "/ HTTP/1.1" + CRLF);96writer.print("User-Agent: Java/"97+ System.getProperty("java.version")98+ CRLF);99writer.print("Host: " + address.getHostName() + CRLF);100writer.print("Accept: */*" + CRLF);101writer.print("Connection: keep-alive" + CRLF);102writer.print(CRLF); // Important, else the server will expect that103// there's more into the request.104writer.flush();105System.out.println("Client wrote rquest to socket: " + socket);106107reader = new BufferedReader(new InputStreamReader(108socket.getInputStream()));109System.out.println("Client start reading from server:" );110String line = reader.readLine();111if ( !line.endsWith(" ") ) {112throw new RuntimeException("respond to unknown code "113+ noMsgCode114+ " doesn't return space at the end of the first header.\n"115+ "Should be: " + "\"" + line + " \""116+ ", but returns: " + "\"" + line + "\".");117}118for (; line != null; line = reader.readLine()) {119if (line.isEmpty()) {120break;121}122System.out.println("\"" + line + "\"");123}124System.out.println("Client finished reading from server" );125} finally {126if (reader != null)127try {128reader.close();129} catch (IOException logOrIgnore) {130logOrIgnore.printStackTrace();131}132if (writer != null) {133writer.close();134}135if (socket != null) {136try {137socket.close();138} catch (IOException logOrIgnore) {139logOrIgnore.printStackTrace();140}141}142}143System.out.println("Client finished." );144}145}146147148