Made sockets nonblocking (I'm a fool who didn't do this and spent several hours trying to figure out why I couldn't handle a second request if firefox was on the page I'm so dumb I hate myself I hate coding I hate the internet computers were a mistake)

This commit is contained in:
Nate Choe
2022-01-30 12:43:10 -06:00
parent e4fe179480
commit e05896356f
8 changed files with 115 additions and 10 deletions

View File

@@ -202,7 +202,7 @@ static int processChar(Connection *conn, char c, Sitefile *site) {
return 0;
}
long diff(struct timespec *t1, struct timespec *t2) {
static long diff(struct timespec *t1, struct timespec *t2) {
return (t2->tv_sec - t1->tv_sec) * 1000 +
(t2->tv_nsec - t1->tv_nsec) / 1000000;
}
@@ -220,12 +220,11 @@ int updateConnection(Connection *conn, Sitefile *site) {
if (received < 0)
return errno != EAGAIN;
if (received == 0)
break;
return 0;
memcpy(&conn->lastdata, &currentTime, sizeof(struct timespec));
for (unsigned long i = 0; i < received; i++) {
if (processChar(conn, buff[i], site))
return 1;
}
}
return 0;
}

View File

@@ -44,7 +44,7 @@ Listener *createListener(SocketType type, uint16_t port, int backlog, ...);
//extra arguments depend on type (similar to fcntl):
//tcp: (void)
//tls: (char *keyfile, char *certfile, char *ocspfile)
Stream *acceptStream(Listener *listener);
Stream *acceptStream(Listener *listener, int flags);
//returns 1 on error, accepts fcntl flags
void freeListener(Listener *listener);

View File

@@ -34,7 +34,7 @@
int main(int argc, char **argv) {
char *logout = "/var/log/swebs.log";
char *sitefile = NULL;
int processes = 8;
int processes = sysconf(_SC_NPROCESSORS_ONLN) + 1;
uint16_t port = 443;
int backlog = 100;
for (;;) {
@@ -76,7 +76,7 @@ int main(int argc, char **argv) {
printf(
"Usage: swebs [options]\n"
" -o [out] Set the log file (default: /var/log/swebs.log)\n"
" -j [cores] Use that many cores (default: 8)\n"
" -j [cores] Use that many cores (default: $(nproc)+1)\n"
" -s [site file] Use that site file (required)\n"
" -p [port] Set the port (default: 443)\n"
" -b [backlog] Set the socket backlog (default: 100)\n"
@@ -145,7 +145,7 @@ int main(int argc, char **argv) {
createLog("swebs started");
for (;;) {
Stream *stream = acceptStream(listener);
Stream *stream = acceptStream(listener, O_NONBLOCK);
if (stream == NULL) {
createLog("Accepting a stream failed");
continue;

View File

@@ -142,6 +142,7 @@ int sendResponse(Connection *conn, Sitefile *site) {
readResponse(conn, site->content[i].arg);
goto end;
default:
sendErrorResponse(conn, ERROR_500);
return 1;
}
}

View File

@@ -91,7 +91,7 @@ error:
return NULL;
}
Stream *acceptStream(Listener *listener) {
Stream *acceptStream(Listener *listener, int flags) {
Stream *ret = malloc(sizeof(Stream));
if (ret == NULL)
return NULL;
@@ -104,6 +104,9 @@ Stream *acceptStream(Listener *listener) {
return NULL;
}
int oldflags = fcntl(ret->fd, F_GETFL);
fcntl(ret->fd, F_SETFL, oldflags | flags);
switch (listener->type) {
case TCP: default:
break;