Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/bugs/B6393710.java
38867 views
/*1* Copyright (c) 2006, 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* @summary Non authenticated call followed by authenticated call never returns27*/2829import com.sun.net.httpserver.*;3031import java.util.*;32import java.util.concurrent.*;33import java.io.*;34import java.net.*;3536/*37* Test checks for following bug(s) when a POST containing a request body38* needs to be authenticated39*40* 1) we were not reading the request body41*42* 2) we were not re-enabling the interestops for the socket channel43*/4445public class B6393710 {4647static String CRLF = "\r\n";4849/* Two post requests containing data. The second one50* has the expected authorization credentials51*/52static String cmd =53"POST /test/foo HTTP/1.1"+CRLF+54"Content-Length: 22"+CRLF+55"Pragma: no-cache"+CRLF+56"Cache-Control: no-cache"+CRLF+ CRLF+57"<item desc=\"excuse\" />"+58"POST /test/foo HTTP/1.1"+CRLF+59"Content-Length: 22"+CRLF+60"Pragma: no-cache"+CRLF+61"Authorization: Basic ZnJlZDpmcmVkcGFzc3dvcmQ="+CRLF+62"Cache-Control: no-cache"+CRLF+ CRLF+63"<item desc=\"excuse\" />";6465public static void main (String[] args) throws Exception {66Handler handler = new Handler();67InetSocketAddress addr = new InetSocketAddress (0);68HttpServer server = HttpServer.create (addr, 0);69HttpContext ctx = server.createContext ("/test", handler);70ctx.setAuthenticator (new BasicAuthenticator ("test") {71public boolean checkCredentials (String user, String pass) {72return user.equals ("fred") && pass.equals("fredpassword");73}74});7576server.start ();7778Socket s = new Socket ("localhost", server.getAddress().getPort());79s.setSoTimeout (5000);8081OutputStream os = s.getOutputStream();82os.write (cmd.getBytes());83InputStream is = s.getInputStream ();84try {85ok = readAndCheck (is, "401 Unauthorized") &&86readAndCheck (is, "200 OK");87} catch (SocketTimeoutException e) {88System.out.println ("Did not received expected data");89ok = false;90} finally {91s.close();92server.stop(2);93}9495if (requests != 1) {96throw new RuntimeException ("server handler did not receive the request");97}98if (!ok) {99throw new RuntimeException ("did not get 200 OK");100}101System.out.println ("OK");102}103104/* check for expected string and return true if found in stream */105106static boolean readAndCheck (InputStream is, String expected) throws IOException {107int c;108int count = 0;109int expLen = expected.length();110expected = expected.toLowerCase();111112while ((c=is.read()) != -1) {113c = Character.toLowerCase (c);114if (c == expected.charAt (count)) {115count ++;116if (count == expLen) {117return true;118}119} else {120count = 0;121}122}123return false;124}125126public static boolean ok = false;127static int requests = 0;128129static class Handler implements HttpHandler {130int invocation = 1;131public void handle (HttpExchange t)132throws IOException133{134int count = 0;135InputStream is = t.getRequestBody();136Headers map = t.getRequestHeaders();137Headers rmap = t.getResponseHeaders();138while (is.read () != -1) {139count ++;140}141if (count != 22) {142System.out.println ("Handler expected 22. got " + count);143ok = false;144}145is.close();146t.sendResponseHeaders (200, -1);147t.close();148requests ++;149}150}151}152153154