Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/HeadTest.java
41155 views
/*1* Copyright (c) 2010, 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 688672326* @library /test/lib27* @run main HeadTest28* @run main/othervm -Djava.net.preferIPv6Addresses=true HeadTest29* @summary light weight http server doesn't return correct status code for HEAD requests30*/3132import java.net.InetAddress;33import java.net.InetSocketAddress;34import java.net.HttpURLConnection;35import java.net.Proxy;36import java.net.URL;37import java.io.IOException;38import java.util.concurrent.ExecutorService;39import java.util.concurrent.Executors;40import com.sun.net.httpserver.HttpContext;41import com.sun.net.httpserver.HttpExchange;42import com.sun.net.httpserver.HttpHandler;43import com.sun.net.httpserver.HttpServer;44import jdk.test.lib.net.URIBuilder;4546public class HeadTest {4748public static void main(String[] args) throws Exception {49server();50}5152static void server() throws Exception {53InetAddress loopback = InetAddress.getLoopbackAddress();54InetSocketAddress inetAddress = new InetSocketAddress(loopback, 0);55HttpServer server = HttpServer.create(inetAddress, 5);56try {57server.setExecutor(Executors.newFixedThreadPool(5));58HttpContext chunkedContext = server.createContext("/chunked");59chunkedContext.setHandler(new HttpHandler() {60@Override61public void handle(HttpExchange msg) {62try {63try {64if (msg.getRequestMethod().equals("HEAD")) {65msg.getRequestBody().close();66msg.getResponseHeaders().add("Transfer-encoding", "chunked");67msg.sendResponseHeaders(200, -1);68}69} catch(IOException ioe) {70ioe.printStackTrace();71}72} finally {73msg.close();74}75}76});77HttpContext clContext = server.createContext("/content");78clContext.setHandler(new HttpHandler() {79@Override80public void handle(HttpExchange msg) {81try {82try {83if (msg.getRequestMethod().equals("HEAD")) {84msg.getRequestBody().close();85msg.getResponseHeaders().add("Content-length", "1024");86msg.sendResponseHeaders(200, -1);87}88} catch(IOException ioe) {89ioe.printStackTrace();90}91} finally {92msg.close();93}94}95});96server.start();97String urlStr = URIBuilder.newBuilder()98.scheme("http")99.loopback()100.port(server.getAddress().getPort())101.path("/")102.build()103.toString();104System.out.println("Server is at " + urlStr);105106// Run the chunked client107for(int i=0; i < 10; i++) {108runClient(urlStr + "chunked/");109}110// Run the content length client111for(int i=0; i < 10; i++) {112runClient(urlStr + "content/");113}114} finally {115// Stop the server116((ExecutorService)server.getExecutor()).shutdown();117server.stop(0);118}119}120121static void runClient(String urlStr) throws Exception {122HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection(Proxy.NO_PROXY);123conn.setRequestMethod("HEAD");124int status = conn.getResponseCode();125if (status != 200) {126throw new RuntimeException("HEAD request doesn't return 200, but returns " + status);127}128}129}130131132