1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -82,9 +82,9 @@ public void expect100ContinueHitCountTest() throws Exception {
82
82
conn .setRequestProperty ("Expect" , "100-continue" );
83
83
sendRequest (conn );
84
84
getHeaderField (conn );
85
- assertEquals (1 , server .getServerHitCount ());
86
85
// Server rejects the expect 100-continue request with 417 response
87
86
assertEquals (417 , conn .getResponseCode ());
87
+ assertEquals (1 , server .getServerHitCount ());
88
88
}
89
89
90
90
@ Test
@@ -99,12 +99,12 @@ public void defaultRequestHitCountTest() throws Exception {
99
99
conn .setRequestMethod ("PUT" );
100
100
sendRequest (conn );
101
101
getHeaderField (conn );
102
- assertEquals (1 , server .getServerHitCount ());
103
102
assertEquals (200 , conn .getResponseCode ());
104
103
try ( InputStream in = conn .getInputStream ()) {
105
104
byte [] data = in .readAllBytes ();
106
105
assertEquals (RESPONSE .length (), data .length );
107
106
}
107
+ assertEquals (1 , server .getServerHitCount ());
108
108
}
109
109
110
110
private void sendRequest (final HttpURLConnection conn ) throws Exception {
@@ -116,6 +116,7 @@ private void sendRequest(final HttpURLConnection conn) throws Exception {
116
116
os .flush ();
117
117
} catch (IOException e ) {
118
118
// intentional, server will reject the expect 100
119
+ System .err .println ("Got expected exception: " + e );
119
120
}
120
121
}
121
122
@@ -179,7 +180,7 @@ void close() {
179
180
@ Override
180
181
public void run () {
181
182
Socket client ;
182
- try {
183
+ try ( ss ) {
183
184
while (isRunning ) {
184
185
client = ss .accept ();
185
186
System .out .println (client .getRemoteSocketAddress ().toString ());
@@ -191,29 +192,16 @@ public void run() {
191
192
if (isRunning ) {
192
193
throw new RuntimeException (ex );
193
194
}
194
- } finally {
195
- if (ss != null && !ss .isClosed ()) {
196
- try {
197
- ss .close ();
198
- } catch (IOException ex ) {
199
- //ignore
200
- }
201
- }
202
195
}
203
196
}
204
197
205
198
private void handleConnection (Socket client ) throws IOException {
206
- try ( BufferedReader in = new BufferedReader (
199
+ try (client ; BufferedReader in = new BufferedReader (
207
200
new InputStreamReader (client .getInputStream ()));
208
201
PrintStream out = new PrintStream (client .getOutputStream ())) {
209
202
handle_connection (in , out );
210
203
} catch (InterruptedException e ) {
211
204
Thread .currentThread ().interrupt ();
212
- } finally {
213
- try {
214
- client .close ();
215
- } catch (IOException e ) {
216
- }
217
205
}
218
206
}
219
207
@@ -230,6 +218,10 @@ private void handle_connection(BufferedReader in, PrintStream out)
230
218
} else {
231
219
defaultResponse (out );
232
220
}
221
+ // wait until the client closes the socket
222
+ while (line != null ) {
223
+ line = in .readLine ();
224
+ }
233
225
}
234
226
235
227
private void rejectExpect100Continue (PrintStream out ) {
0 commit comments