11/*
2- * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
@@ -82,9 +82,9 @@ public void expect100ContinueHitCountTest() throws Exception {
8282 conn .setRequestProperty ("Expect" , "100-continue" );
8383 sendRequest (conn );
8484 getHeaderField (conn );
85- assertEquals (1 , server .getServerHitCount ());
8685 // Server rejects the expect 100-continue request with 417 response
8786 assertEquals (417 , conn .getResponseCode ());
87+ assertEquals (1 , server .getServerHitCount ());
8888 }
8989
9090 @ Test
@@ -99,12 +99,12 @@ public void defaultRequestHitCountTest() throws Exception {
9999 conn .setRequestMethod ("PUT" );
100100 sendRequest (conn );
101101 getHeaderField (conn );
102- assertEquals (1 , server .getServerHitCount ());
103102 assertEquals (200 , conn .getResponseCode ());
104103 try ( InputStream in = conn .getInputStream ()) {
105104 byte [] data = in .readAllBytes ();
106105 assertEquals (RESPONSE .length (), data .length );
107106 }
107+ assertEquals (1 , server .getServerHitCount ());
108108 }
109109
110110 private void sendRequest (final HttpURLConnection conn ) throws Exception {
@@ -116,6 +116,7 @@ private void sendRequest(final HttpURLConnection conn) throws Exception {
116116 os .flush ();
117117 } catch (IOException e ) {
118118 // intentional, server will reject the expect 100
119+ System .err .println ("Got expected exception: " + e );
119120 }
120121 }
121122
@@ -179,7 +180,7 @@ void close() {
179180 @ Override
180181 public void run () {
181182 Socket client ;
182- try {
183+ try ( ss ) {
183184 while (isRunning ) {
184185 client = ss .accept ();
185186 System .out .println (client .getRemoteSocketAddress ().toString ());
@@ -191,29 +192,16 @@ public void run() {
191192 if (isRunning ) {
192193 throw new RuntimeException (ex );
193194 }
194- } finally {
195- if (ss != null && !ss .isClosed ()) {
196- try {
197- ss .close ();
198- } catch (IOException ex ) {
199- //ignore
200- }
201- }
202195 }
203196 }
204197
205198 private void handleConnection (Socket client ) throws IOException {
206- try ( BufferedReader in = new BufferedReader (
199+ try (client ; BufferedReader in = new BufferedReader (
207200 new InputStreamReader (client .getInputStream ()));
208201 PrintStream out = new PrintStream (client .getOutputStream ())) {
209202 handle_connection (in , out );
210203 } catch (InterruptedException e ) {
211204 Thread .currentThread ().interrupt ();
212- } finally {
213- try {
214- client .close ();
215- } catch (IOException e ) {
216- }
217205 }
218206 }
219207
@@ -230,6 +218,10 @@ private void handle_connection(BufferedReader in, PrintStream out)
230218 } else {
231219 defaultResponse (out );
232220 }
221+ // wait until the client closes the socket
222+ while (line != null ) {
223+ line = in .readLine ();
224+ }
233225 }
234226
235227 private void rejectExpect100Continue (PrintStream out ) {
0 commit comments