|
| 1 | +package robaho.net.httpserver; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.io.OutputStream; |
| 7 | +import java.io.OutputStreamWriter; |
| 8 | +import java.io.PrintWriter; |
| 9 | +import java.net.InetAddress; |
| 10 | +import java.net.InetSocketAddress; |
| 11 | +import java.net.Socket; |
| 12 | +import java.util.concurrent.ExecutorService; |
| 13 | +import java.util.concurrent.Executors; |
| 14 | +import java.util.concurrent.ThreadFactory; |
| 15 | +import java.util.concurrent.atomic.AtomicLong; |
| 16 | +import java.util.logging.Level; |
| 17 | +import java.util.logging.Logger; |
| 18 | + |
| 19 | +import com.sun.net.httpserver.HttpExchange; |
| 20 | +import com.sun.net.httpserver.HttpHandler; |
| 21 | +import com.sun.net.httpserver.HttpServer; |
| 22 | + |
| 23 | +import org.testng.annotations.Test; |
| 24 | + |
| 25 | +import static java.nio.charset.StandardCharsets.*; |
| 26 | + |
| 27 | +/** |
| 28 | + * see issue #19 |
| 29 | + * |
| 30 | + * the server attempts to optimize flushing the response stream if there is |
| 31 | + * another request in the pipeline, but the bug caused the server to assume the |
| 32 | + * data remaining to be read was part of the next request, causing the server to |
| 33 | + * hang. Reading even a single character from the request body would have |
| 34 | + * prevented the issue since the buffer would have been filled. |
| 35 | + * |
| 36 | + * The solution is to read the remaining request data, then check if there are |
| 37 | + * any characters waiting to be read. |
| 38 | + */ |
| 39 | +public class PipeliningStallTest { |
| 40 | + |
| 41 | + private static final int msgCode = 200; |
| 42 | + private static final String someContext = "/context"; |
| 43 | + |
| 44 | + static class ServerThreadFactory implements ThreadFactory { |
| 45 | + |
| 46 | + static final AtomicLong tokens = new AtomicLong(); |
| 47 | + |
| 48 | + @Override |
| 49 | + public Thread newThread(Runnable r) { |
| 50 | + var thread = new Thread(r, "Server-" + tokens.incrementAndGet()); |
| 51 | + thread.setDaemon(true); |
| 52 | + return thread; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + static { |
| 57 | + Logger.getLogger("").setLevel(Level.ALL); |
| 58 | + Logger.getLogger("").getHandlers()[0].setLevel(Level.ALL); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testSendResponse() throws Exception { |
| 63 | + System.out.println("testSendResponse()"); |
| 64 | + InetAddress loopback = InetAddress.getLoopbackAddress(); |
| 65 | + HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 0); |
| 66 | + ExecutorService executor = Executors.newCachedThreadPool(new ServerThreadFactory()); |
| 67 | + server.setExecutor(executor); |
| 68 | + try { |
| 69 | + server.createContext(someContext, new HttpHandler() { |
| 70 | + @Override |
| 71 | + public void handle(HttpExchange exchange) throws IOException { |
| 72 | + var length = exchange.getRequestHeaders().getFirst("Content-Length"); |
| 73 | + |
| 74 | + var msg = "hi"; |
| 75 | + var status = 200; |
| 76 | + if (Integer.valueOf(length) > 4) { |
| 77 | + msg = "oversized"; |
| 78 | + status = 413; |
| 79 | + } |
| 80 | + |
| 81 | + var bytes = msg.getBytes(); |
| 82 | + |
| 83 | + // -1 means no content, 0 means unknown content length |
| 84 | + var contentLength = bytes.length == 0 ? -1 : bytes.length; |
| 85 | + |
| 86 | + try (OutputStream os = exchange.getResponseBody()) { |
| 87 | + exchange.sendResponseHeaders(status, contentLength); |
| 88 | + os.write(bytes); |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + server.start(); |
| 93 | + System.out.println("Server started at port " |
| 94 | + + server.getAddress().getPort()); |
| 95 | + |
| 96 | + runRawSocketHttpClient(loopback, server.getAddress().getPort(), -1); |
| 97 | + } finally { |
| 98 | + System.out.println("shutting server down"); |
| 99 | + executor.shutdown(); |
| 100 | + server.stop(0); |
| 101 | + } |
| 102 | + System.out.println("Server finished."); |
| 103 | + } |
| 104 | + |
| 105 | + static void runRawSocketHttpClient(InetAddress address, int port, int contentLength) |
| 106 | + throws Exception { |
| 107 | + Socket socket = null; |
| 108 | + PrintWriter writer = null; |
| 109 | + BufferedReader reader = null; |
| 110 | + final String CRLF = "\r\n"; |
| 111 | + try { |
| 112 | + socket = new Socket(address, port); |
| 113 | + writer = new PrintWriter(new OutputStreamWriter( |
| 114 | + socket.getOutputStream())); |
| 115 | + System.out.println("Client connected by socket: " + socket); |
| 116 | + String body = "I will send all the data."; |
| 117 | + if (contentLength <= 0) { |
| 118 | + contentLength = body.getBytes(UTF_8).length; |
| 119 | + } |
| 120 | + |
| 121 | + writer.print("GET " + someContext + "/ HTTP/1.1" + CRLF); |
| 122 | + writer.print("User-Agent: Java/" |
| 123 | + + System.getProperty("java.version") |
| 124 | + + CRLF); |
| 125 | + writer.print("Host: " + address.getHostName() + CRLF); |
| 126 | + writer.print("Accept: */*" + CRLF); |
| 127 | + writer.print("Content-Length: " + contentLength + CRLF); |
| 128 | + writer.print("Connection: keep-alive" + CRLF); |
| 129 | + writer.print(CRLF); // Important, else the server will expect that |
| 130 | + // there's more into the request. |
| 131 | + writer.flush(); |
| 132 | + System.out.println("Client wrote request to socket: " + socket); |
| 133 | + writer.print(body); |
| 134 | + writer.flush(); |
| 135 | + |
| 136 | + reader = new BufferedReader(new InputStreamReader( |
| 137 | + socket.getInputStream())); |
| 138 | + System.out.println("Client start reading from server:"); |
| 139 | + String line = reader.readLine(); |
| 140 | + for (; line != null; line = reader.readLine()) { |
| 141 | + if (line.isEmpty()) { |
| 142 | + break; |
| 143 | + } |
| 144 | + System.out.println("\"" + line + "\""); |
| 145 | + } |
| 146 | + System.out.println("Client finished reading from server"); |
| 147 | + } finally { |
| 148 | + // give time to the server to try & drain its input stream |
| 149 | + Thread.sleep(500); |
| 150 | + // closes the client outputstream while the server is draining |
| 151 | + // it |
| 152 | + if (writer != null) { |
| 153 | + writer.close(); |
| 154 | + } |
| 155 | + // give time to the server to trigger its assertion |
| 156 | + // error before closing the connection |
| 157 | + Thread.sleep(500); |
| 158 | + if (reader != null) |
| 159 | + try { |
| 160 | + reader.close(); |
| 161 | + } catch (IOException logOrIgnore) { |
| 162 | + logOrIgnore.printStackTrace(); |
| 163 | + } |
| 164 | + if (socket != null) { |
| 165 | + try { |
| 166 | + socket.close(); |
| 167 | + } catch (IOException logOrIgnore) { |
| 168 | + logOrIgnore.printStackTrace(); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + System.out.println("Client finished."); |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments