Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/MissingTrailingSpace.java
38855 views
/*1* Copyright (c) 2015, 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* @author [email protected]28*/2930import java.net.InetSocketAddress;31import java.io.InputStreamReader;32import java.io.IOException;33import java.io.BufferedReader;34import java.io.OutputStreamWriter;35import java.io.PrintWriter;36import java.net.Socket;37import java.util.concurrent.ExecutorService;38import java.util.concurrent.Executors;39import com.sun.net.httpserver.HttpExchange;40import com.sun.net.httpserver.HttpHandler;41import com.sun.net.httpserver.HttpServer;4243public class MissingTrailingSpace {4445private static final int noMsgCode = 207;46private static final String someContext = "/context";4748public static void main(String[] args) throws Exception {49HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);50try {51server.setExecutor(Executors.newFixedThreadPool(1));52server.createContext(someContext, new HttpHandler() {53@Override54public void handle(HttpExchange msg) {55try {56try {57msg.sendResponseHeaders(noMsgCode, -1);58} catch(IOException ioe) {59ioe.printStackTrace();60}61} finally {62msg.close();63}64}65});66server.start();67System.out.println("Server started at port "68+ server.getAddress().getPort());6970runRawSocketHttpClient("localhost", server.getAddress().getPort());71} finally {72((ExecutorService)server.getExecutor()).shutdown();73server.stop(0);74}75System.out.println("Server finished.");76}7778static void runRawSocketHttpClient(String hostname, int port)79throws Exception80{81Socket socket = null;82PrintWriter writer = null;83BufferedReader reader = null;84final String CRLF = "\r\n";85try {86socket = new Socket(hostname, port);87writer = new PrintWriter(new OutputStreamWriter(88socket.getOutputStream()));89System.out.println("Client connected by socket: " + socket);9091writer.print("GET " + someContext + "/ HTTP/1.1" + CRLF);92writer.print("User-Agent: Java/"93+ System.getProperty("java.version")94+ CRLF);95writer.print("Host: " + hostname + CRLF);96writer.print("Accept: */*" + CRLF);97writer.print("Connection: keep-alive" + CRLF);98writer.print(CRLF); // Important, else the server will expect that99// there's more into the request.100writer.flush();101System.out.println("Client wrote rquest to socket: " + socket);102103reader = new BufferedReader(new InputStreamReader(104socket.getInputStream()));105System.out.println("Client start reading from server:" );106String line = reader.readLine();107if ( !line.endsWith(" ") ) {108throw new RuntimeException("respond to unknown code "109+ noMsgCode110+ " doesn't return space at the end of the first header.\n"111+ "Should be: " + "\"" + line + " \""112+ ", but returns: " + "\"" + line + "\".");113}114for (; line != null; line = reader.readLine()) {115if (line.isEmpty()) {116break;117}118System.out.println("\"" + line + "\"");119}120System.out.println("Client finished reading from server" );121} finally {122if (reader != null)123try {124reader.close();125} catch (IOException logOrIgnore) {126logOrIgnore.printStackTrace();127}128if (writer != null) {129writer.close();130}131if (socket != null) {132try {133socket.close();134} catch (IOException logOrIgnore) {135logOrIgnore.printStackTrace();136}137}138}139System.out.println("Client finished." );140}141}142143144145