fix bugs that end connection randomly

This commit is contained in:
Daniella / Tove 2022-07-06 19:56:18 +02:00
parent 487d1660e2
commit 9959d33290
3 changed files with 23 additions and 5 deletions

View file

@ -31,7 +31,7 @@ public class HTTPRequestReader {
i = stream.read(); i = stream.read();
} catch (SocketException e) { } catch (SocketException e) {
if(e.getMessage().equals("Connection reset")) if(e.getMessage().equals("Connection reset"))
throw new Stop(); throw new Stop("Connection closed by client");
throw e; throw e;
} }
if(i == -1) { if(i == -1) {
@ -46,11 +46,21 @@ public class HTTPRequestReader {
i = stream.read(bytes); i = stream.read(bytes);
} catch (SocketException e) { } catch (SocketException e) {
if(e.getMessage().equals("Connection reset")) if(e.getMessage().equals("Connection reset"))
throw new Stop(); throw new Stop("Connection closed by client");
throw e; throw e;
} }
if(i != bytes.length) { while(i != bytes.length) {
throw new Stop(); try {
int n = stream.read(bytes, i, bytes.length - i);
if(n == 0) {
throw new Stop("0 bytes read");
}
i += n;
} catch (SocketException e) {
if(e.getMessage().equals("Connection reset"))
throw new Stop("Connection closed by client");
throw e;
}
} }
return i; return i;
} }

View file

@ -56,7 +56,7 @@ public class Server {
throw stop; throw stop;
} catch(Throwable e) { } catch(Throwable e) {
if(e instanceof Nothing) if(e instanceof Nothing)
return; throw new Stop("Nothing to do");
else { else {
e.printStackTrace(); e.printStackTrace();
break; break;
@ -93,6 +93,12 @@ public class Server {
break; break;
} }
} catch (Stop stop) { } catch (Stop stop) {
if(stop.getMessage() != null) {
System.out.println("Connection stopped: " + stop.getMessage());
}
else {
System.out.println("Connection stopped");
}
} }
try { try {
socket.close(); socket.close();

View file

@ -1,4 +1,6 @@
package de.tudbut.tryumph.server.http; package de.tudbut.tryumph.server.http;
public class Stop extends RuntimeException { public class Stop extends RuntimeException {
public Stop() {}
public Stop(String reason) { super(reason); }
} }