more relaibility under crappy connections

This commit is contained in:
Adam D. Ruppe 2017-04-26 22:20:32 -04:00
parent fcb59c4c55
commit c8c61150d5
1 changed files with 55 additions and 43 deletions

16
cgi.d
View File

@ -2649,8 +2649,12 @@ mixin template CustomCgiMainImpl(CustomCgi, alias fun, long maxContentLength = d
i = addr.sizeof;
int s = accept(sock, &addr, &i);
try {
if(s == -1)
throw new Exception("accept");
scope(failure) close(s);
//ubyte[__traits(classInstanceSize, BufferedInputRange)] bufferedRangeContainer;
auto ir = new BufferedInputRange(s);
//auto ir = emplace!BufferedInputRange(bufferedRangeContainer, s, backingBuffer);
@ -2699,6 +2703,10 @@ mixin template CustomCgiMainImpl(CustomCgi, alias fun, long maxContentLength = d
}
ir.source.close();
} catch(Throwable t) {
debug writeln(t);
// most likely cause is a timeout
}
}
} else {
processCount++;
@ -3138,6 +3146,9 @@ class BufferedInputRange {
}
this(Socket source, ubyte[] buffer = null) {
// if they connect but never send stuff to us, we don't want it wasting the process
// so setting a time out
source.setOption(SocketOptionLevel.SOCKET, SocketOption.RCVTIMEO, dur!"seconds"(3));
this.source = source;
if(buffer is null) {
underlyingBuffer = new ubyte[4096];
@ -3194,10 +3205,11 @@ class BufferedInputRange {
if(ret == Socket.ERROR) {
version(Posix) {
import core.stdc.errno;
if(errno == EINTR)
if(errno == EINTR) {
goto try_again;
}
throw new Exception("uh oh " ~ lastSocketError); // FIXME
}
throw new Exception(lastSocketError); // FIXME
}
if(ret == 0) {
sourceClosed = true;