@@ -346,7 +346,10 @@ protected ServerSocket newServerSocket() throws IOException {
346
346
347
347
@ Override
348
348
public void run () {
349
+ int maxUnexpected = 10 ; // if we get there too often we may
350
+ // want to reassess the diagnosis
349
351
while (!closed ) {
352
+ boolean accepted = false ;
350
353
try (Socket s = ss .accept ()) {
351
354
out .print (name + ": got connection " );
352
355
InputStream is = s .getInputStream ();
@@ -357,26 +360,39 @@ public void run() {
357
360
readRequestHeaders (is );
358
361
359
362
String query = uriPath .getRawQuery ();
360
- assert query != null ;
363
+ if (query == null ) {
364
+ throw new IOException ("expected query not found in: " + uriPath );
365
+ }
361
366
String qv = query .split ("=" )[1 ];
362
367
int len ;
363
368
if (qv .equals ("all" )) {
364
369
len = responseBody ().getBytes (US_ASCII ).length ;
365
370
} else {
366
- len = Integer .parseInt (query . split ( "=" )[ 1 ] );
371
+ len = Integer .parseInt (qv );
367
372
}
368
373
374
+ // if we get an exception past this point
375
+ // we will rethrow it
376
+ accepted = true ;
377
+
369
378
OutputStream os = s .getOutputStream ();
370
379
os .write (responseHeaders ().getBytes (US_ASCII ));
371
- out .println (name + ": headers written, writing " + len + " body bytes" );
380
+ out .println (name + ": headers written, writing " + len + " body bytes" );
372
381
byte [] responseBytes = responseBody ().getBytes (US_ASCII );
373
- for (int i = 0 ; i < len ; i ++) {
382
+ for (int i = 0 ; i < len ; i ++) {
374
383
os .write (responseBytes [i ]);
375
384
os .flush ();
376
385
}
377
- } catch (IOException e ) {
378
- if (!closed )
379
- throw new UncheckedIOException ("Unexpected" , e );
386
+ } catch (IOException | RuntimeException e ) {
387
+ if (!closed ) {
388
+ if (--maxUnexpected <= 0 || accepted ) {
389
+ if (e instanceof IOException io )
390
+ throw new UncheckedIOException (io );
391
+ else throw (RuntimeException ) e ;
392
+ }
393
+ out .println ("ignoring unexpected exception: " + e );
394
+ continue ;
395
+ }
380
396
}
381
397
}
382
398
}
0 commit comments