Added error checking with sending responses

This commit is contained in:
Nate Choe
2022-01-30 21:37:47 -06:00
parent efd1ac1a0c
commit e1a66999b6
7 changed files with 23 additions and 8 deletions

View File

@@ -200,17 +200,19 @@ static int processChar(Connection *conn, char c, Sitefile *site) {
conn->receivedBody >= conn->bodylen)
getResponse(conn, site);
if (conn->progress == SEND_RESPONSE) {
for (;;) {
while (conn->len > 0) {
char buffer[1024];
size_t readLen = read(conn->fd, buffer, sizeof(buffer));
if (readLen == 0)
break;
if (readLen < 0)
return 1;
size_t writeLen = sendStream(conn->stream,
buffer, readLen);
if (writeLen < readLen)
return 1;
while (readLen > 0) {
size_t writeLen = sendStream(conn->stream,
buffer, readLen);
if (writeLen < 0)
return 1;
readLen -= writeLen;
}
conn->len -= readLen;
}
resetConnection(conn);
}

View File

@@ -55,7 +55,8 @@ typedef struct Connection {
size_t receivedBody;
int fd;
//the fd of the response
size_t len;
//the fd and length of the response left to send
char *currLine;
//persistent

View File

@@ -95,6 +95,7 @@ static int readResponse(Connection *conn, char *path) {
goto error;
lseek(fd, 0, SEEK_SET);
sendHeader(conn, CODE_200, len);
conn->len = len;
return fd;
error:
sendErrorResponse(conn, ERROR_500);