Commit dca912b4 authored by Matthew Burket's avatar Matthew Burket
Browse files

Move file serving to known method

parent 34e6f589
Loading
Loading
Loading
Loading
Loading
+17 −11
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ void getRequest(int clientfd, GrowthBuffer *request);

int checkFile(char *fileName);

void serveFile(int clientfd, const char *path);

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"

@@ -122,17 +124,7 @@ int handle_client(int clientfd) {
            appendBuffer(&response, basicHeader, strlen(basicHeader));
            write(clientfd, response.array, strlen(response.array));
            if (verb == HTTP_GET) {
                FILE *fp;
                size_t numBytesReadFile;
                fp = fopen(path, "r");
                char fileBuff[1024];
                if (fp) {
                    printf("\nGot a fp\n");
                    while ((numBytesReadFile = fread(fileBuff, 1023, 1, fp)) > 0) {
                        write(clientfd, fileBuff, strlen(fileBuff));
                    }
                }
                fclose(fp);
                serveFile(clientfd, path);
            }
        } else {
            char *basicHeader = "HTTP/1.1 404 Not Found\r\n\r\n";
@@ -150,6 +142,20 @@ int handle_client(int clientfd) {
#endif
}

void serveFile(int clientfd, const char *path) {
    FILE *fp;
    size_t numBytesReadFile;
    fp = fopen(path, "r");
    char fileBuff[1024];
    if (fp) {
        printf("\nGot a fp\n");
        while ((numBytesReadFile = fread(fileBuff, 1023, 1, fp)) > 0) {
            write(clientfd, fileBuff, strlen(fileBuff));
        }
    }
    fclose(fp);
}

void getRequest(int clientfd, GrowthBuffer *request) {
    struct pollfd pollfd = {
                .fd = clientfd,