From 1888bb9518ee97871b2b4c00a8c104020dbb03fa Mon Sep 17 00:00:00 2001 From: Nate Choe Date: Sun, 30 Jan 2022 17:57:20 -0600 Subject: [PATCH] Actually used the SEND_RESPONSE phase --- src/connections.c | 15 ++++++++++++- src/include/connections.h | 3 +++ src/include/responses.h | 3 ++- src/include/responseutil.h | 4 +++- src/responses.c | 45 +++++++++++++++++--------------------- src/responseutil.c | 9 ++++++++ 6 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/connections.c b/src/connections.c index 87681a4..65aabaf 100644 --- a/src/connections.c +++ b/src/connections.c @@ -198,7 +198,20 @@ static int processChar(Connection *conn, char c, Sitefile *site) { } if (conn->progress == RECEIVE_BODY && conn->receivedBody >= conn->bodylen) - sendResponse(conn, site); + getResponse(conn, site); + if (conn->progress == SEND_RESPONSE) { + for (;;) { + char buffer[1024]; + size_t readLen = read(conn->fd, buffer, sizeof(buffer)); + if (readLen == 0) + break; + size_t writeLen = sendStream(conn->stream, + buffer, readLen); + if (writeLen < readLen) + return 1; + } + resetConnection(conn); + } return 0; } diff --git a/src/include/connections.h b/src/include/connections.h index 6c76a29..1d445d5 100644 --- a/src/include/connections.h +++ b/src/include/connections.h @@ -54,6 +54,9 @@ typedef struct Connection { size_t bodylen; size_t receivedBody; + int fd; + //the fd of the response + char *currLine; //persistent size_t currLineAlloc; diff --git a/src/include/responses.h b/src/include/responses.h index 1b1219b..eabd9e8 100644 --- a/src/include/responses.h +++ b/src/include/responses.h @@ -20,5 +20,6 @@ #include #include -int sendResponse(Connection *conn, Sitefile *site); +int getResponse(Connection *conn, Sitefile *site); +//returns 1 on error, sets conn->progress to SEND_RESPONSE and sets conn->fd #endif diff --git a/src/include/responseutil.h b/src/include/responseutil.h index 2c7755a..c898315 100644 --- a/src/include/responseutil.h +++ b/src/include/responseutil.h @@ -19,6 +19,7 @@ #define _HAVE_RESPONSE_UTIL #include +#define CODE_200 "200 OK" #define ERROR_400 "400 Bad Request" #define ERROR_403 "403 Forbidden" #define ERROR_404 "404 Not Found" @@ -27,5 +28,6 @@ int sendStringResponse(Connection *conn, char *status, char *str); int sendBinaryResponse(Connection *conn, char *status, void *data, size_t len); int sendErrorResponse(Connection *conn, char *error); -//sendErrorResponse(conn, "404 Not found"); +//sendErrorResponse(conn, ERROR_404); +int sendHeader(Connection *conn, char *status, size_t len); #endif diff --git a/src/responses.c b/src/responses.c index 2e9c8bb..6e5d80d 100644 --- a/src/responses.c +++ b/src/responses.c @@ -28,12 +28,12 @@ #include #include -static void readResponse(Connection *conn, char *path) { +static int readResponse(Connection *conn, char *path) { FILE *file; struct stat statbuf; if (stat(path, &statbuf)) { sendErrorResponse(conn, ERROR_404); - return; + return -1; } if (S_ISDIR(statbuf.st_mode)) { long reqPathLen = strlen(conn->path); @@ -49,7 +49,7 @@ static void readResponse(Connection *conn, char *path) { if (errno == ENOENT) { free(assembledPath); sendErrorResponse(conn, ERROR_404); - return; + return -1; } free(assembledPath); goto error; @@ -74,12 +74,12 @@ static void readResponse(Connection *conn, char *path) { if (stat(requestPath, &requestbuf)) { free(assembledPath); sendErrorResponse(conn, ERROR_404); - return; + return -1; } if (S_ISDIR(requestbuf.st_mode)) { free(assembledPath); sendErrorResponse(conn, ERROR_400); - return; + return -1; } file = fopen(requestPath, "r"); @@ -91,25 +91,15 @@ static void readResponse(Connection *conn, char *path) { goto forbidden; fseek(file, 0, SEEK_END); long len = ftell(file); - char *data = malloc(len); - if (data == NULL) - return; fseek(file, 0, SEEK_SET); - if (fread(data, 1, len, file) < len) { - fclose(file); - goto error; - } - fclose(file); - if (sendBinaryResponse(conn, "200 OK", data, len) < len) - goto error; - free(data); - return; + sendHeader(conn, CODE_200, len); + return fileno(file); error: sendErrorResponse(conn, ERROR_500); - return; + return -1; forbidden: sendErrorResponse(conn, ERROR_403); - return; + return -1; } static int fullmatch(regex_t *regex, char *str) { @@ -119,7 +109,7 @@ static int fullmatch(regex_t *regex, char *str) { return match.rm_so != 0 || match.rm_eo != strlen(str); } -int sendResponse(Connection *conn, Sitefile *site) { +int getResponse(Connection *conn, Sitefile *site) { char *host = NULL; for (int i = 0; i < conn->fieldCount; i++) { if (strcmp(conn->fields[i].field, "Host") == 0) { @@ -137,18 +127,23 @@ int sendResponse(Connection *conn, Sitefile *site) { if (fullmatch(&site->content[i].host, host)) continue; if (fullmatch(&site->content[i].path, conn->path) == 0) { + int fd = -1; switch (site->content[i].command) { case READ: - readResponse(conn, site->content[i].arg); - goto end; + return + fd = readResponse(conn, + site->content[i].arg); default: sendErrorResponse(conn, ERROR_500); return 1; } + if (fd == -1) + return 1; + conn->fd = fd; + conn->progress = SEND_RESPONSE; + return 0; } } sendErrorResponse(conn, ERROR_404); -end: - resetConnection(conn); - return 0; + return -1; } diff --git a/src/responseutil.c b/src/responseutil.c index 74ca9ae..4efb38b 100644 --- a/src/responseutil.c +++ b/src/responseutil.c @@ -85,3 +85,12 @@ int sendBinaryResponse(Connection *conn, char *status, return 1; return sendStream(conn->stream, data, len) < len; } + +int sendHeader(Connection *conn, char *status, size_t len) { + return (sendConnection(conn, + "HTTP/1.1 %s\r\n" + CONST_FIELDS + "Content-Length: %lu\r\n" + "\r\n" + , status, len)); +}