fix bugs that end connection randomly
This commit is contained in:
parent
487d1660e2
commit
9959d33290
3 changed files with 23 additions and 5 deletions
|
@ -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;
|
||||||
|
}
|
||||||
|
while(i != bytes.length) {
|
||||||
|
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;
|
throw e;
|
||||||
}
|
}
|
||||||
if(i != bytes.length) {
|
|
||||||
throw new Stop();
|
|
||||||
}
|
}
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
|
|
@ -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); }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue