@@ -322,7 +322,10 @@ protected ServerSocket newServerSocket() throws IOException {
322
322
323
323
@ Override
324
324
public void run () {
325
+ int maxUnexpected = 10 ; // if we get there too often we may
326
+ // want to reassess the diagnosis
325
327
while (!closed ) {
328
+ boolean accepted = false ;
326
329
try (Socket s = ss .accept ()) {
327
330
out .print (name + ": got connection " );
328
331
InputStream is = s .getInputStream ();
@@ -333,26 +336,39 @@ public void run() {
333
336
readRequestHeaders (is );
334
337
335
338
String query = uriPath .getRawQuery ();
336
- assert query != null ;
339
+ if (query == null ) {
340
+ throw new IOException ("expected query not found in: " + uriPath );
341
+ }
337
342
String qv = query .split ("=" )[1 ];
338
343
int len ;
339
344
if (qv .equals ("all" )) {
340
345
len = responseBody ().getBytes (US_ASCII ).length ;
341
346
} else {
342
- len = Integer .parseInt (query . split ( "=" )[ 1 ] );
347
+ len = Integer .parseInt (qv );
343
348
}
344
349
350
+ // if we get an exception past this point
351
+ // we will rethrow it
352
+ accepted = true ;
353
+
345
354
OutputStream os = s .getOutputStream ();
346
355
os .write (responseHeaders ().getBytes (US_ASCII ));
347
- out .println (name + ": headers written, writing " + len + " body bytes" );
356
+ out .println (name + ": headers written, writing " + len + " body bytes" );
348
357
byte [] responseBytes = responseBody ().getBytes (US_ASCII );
349
- for (int i = 0 ; i < len ; i ++) {
358
+ for (int i = 0 ; i < len ; i ++) {
350
359
os .write (responseBytes [i ]);
351
360
os .flush ();
352
361
}
353
- } catch (IOException e ) {
354
- if (!closed )
355
- throw new UncheckedIOException ("Unexpected" , e );
362
+ } catch (IOException | RuntimeException e ) {
363
+ if (!closed ) {
364
+ if (--maxUnexpected <= 0 || accepted ) {
365
+ if (e instanceof IOException io )
366
+ throw new UncheckedIOException (io );
367
+ else throw (RuntimeException ) e ;
368
+ }
369
+ out .println ("ignoring unexpected exception: " + e );
370
+ continue ;
371
+ }
356
372
}
357
373
}
358
374
}
0 commit comments