Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6393710.java
41154 views
/*1* Copyright (c) 2006, 2020, 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 639371026* @library /test/lib27* @summary Non authenticated call followed by authenticated call never returns28* @run main B639371029* @run main/othervm -Djava.net.preferIPv6Addresses=true B639371030*/3132import com.sun.net.httpserver.*;3334import java.util.*;35import java.util.concurrent.*;36import java.io.*;37import java.net.*;3839import jdk.test.lib.Utils;4041/*42* Test checks for following bug(s) when a POST containing a request body43* needs to be authenticated44*45* 1) we were not reading the request body46*47* 2) we were not re-enabling the interestops for the socket channel48*/4950public class B6393710 {5152static String CRLF = "\r\n";5354/* Two post requests containing data. The second one55* has the expected authorization credentials56*/57static String cmd =58"POST /test/foo HTTP/1.1"+CRLF+59"Content-Length: 22"+CRLF+60"Pragma: no-cache"+CRLF+61"Cache-Control: no-cache"+CRLF+ CRLF+62"<item desc=\"excuse\" />"+63"POST /test/foo HTTP/1.1"+CRLF+64"Content-Length: 22"+CRLF+65"Pragma: no-cache"+CRLF+66"Authorization: Basic ZnJlZDpmcmVkcGFzc3dvcmQ="+CRLF+67"Cache-Control: no-cache"+CRLF+ CRLF+68"<item desc=\"excuse\" />";6970public static void main (String[] args) throws Exception {71Handler handler = new Handler();72InetAddress loopback = InetAddress.getLoopbackAddress();73InetSocketAddress addr = new InetSocketAddress (loopback, 0);74HttpServer server = HttpServer.create (addr, 0);75HttpContext ctx = server.createContext ("/test", handler);76ctx.setAuthenticator (new BasicAuthenticator ("test") {77public boolean checkCredentials (String user, String pass) {78return user.equals ("fred") && pass.equals("fredpassword");79}80});8182server.start ();8384Socket s = new Socket (loopback, server.getAddress().getPort());85s.setSoTimeout ((int) Utils.adjustTimeout(5000));8687OutputStream os = s.getOutputStream();88os.write (cmd.getBytes());89InputStream is = s.getInputStream ();90try {91ok = readAndCheck (is, "401 Unauthorized") &&92readAndCheck (is, "200 OK");93} catch (SocketTimeoutException e) {94System.out.println ("Did not received expected data");95ok = false;96} finally {97s.close();98server.stop(2);99}100101if (requests != 1) {102throw new RuntimeException ("server handler did not receive the request");103}104if (!ok) {105throw new RuntimeException ("did not get 200 OK");106}107System.out.println ("OK");108}109110/* check for expected string and return true if found in stream */111112static boolean readAndCheck (InputStream is, String expected) throws IOException {113int c;114int count = 0;115int expLen = expected.length();116expected = expected.toLowerCase();117118while ((c=is.read()) != -1) {119c = Character.toLowerCase (c);120if (c == expected.charAt (count)) {121count ++;122if (count == expLen) {123return true;124}125} else {126count = 0;127}128}129return false;130}131132public static volatile boolean ok = false;133static volatile int requests = 0;134135static class Handler implements HttpHandler {136int invocation = 1;137public void handle (HttpExchange t)138throws IOException139{140int count = 0;141InputStream is = t.getRequestBody();142Headers map = t.getRequestHeaders();143Headers rmap = t.getResponseHeaders();144while (is.read () != -1) {145count ++;146}147if (count != 22) {148System.out.println ("Handler expected 22. got " + count);149ok = false;150}151is.close();152t.sendResponseHeaders (200, -1);153t.close();154requests ++;155}156}157}158159160