Commit 1333a382 authored by Matthew Burket's avatar Matthew Burket
Browse files

Got basic serving

parent 6890940f
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ int appendBuffer(GrowthBuffer *arr, char *value, int length) {

        // realloc the array
        char *newArr = realloc(arr->array, arrNewSize * sizeof(char));
        for (int i = arr->total_size + 1; i < arrNewSize; i++) {
        for (int i = arr->total_size; i < arrNewSize; i++) {
            arr->array[i]= '\0';
        }
        // Set the new array
+5 −4
Original line number Diff line number Diff line
@@ -5,17 +5,18 @@
#include <stdio.h>
#include "http.h"
#include "utils.h"
#include "string.h"

/// Gets the HTTP verb from the
/// \param string
/// \return
HTTP_VERB get_http_verb(char *request) {
    if (startsWith(request, "HEAD")) {
        return HTTP_HEAD;
    } else if (startsWith(request, "GET")) {
    char verb[50];
    sscanf(request, "%49[^ ]", verb);
    if (strncmp(verb, "GET", 3) == 0) {
        return HTTP_GET;
    } else {
        return HTTP_UNKNOWN;

    }
}

+16 −4
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@
#include "http.h"


#define PORT 26884
#define PORT 26887
#define DEBUG 0
#define TRUE 1

@@ -116,17 +116,29 @@ int handle_client(int clientfd) {
        get_http_path(request.array, path);
        fprintf(stdout, "Verb:%d Path:%s\n", verb, path);
        GrowthBuffer response;
        initBuffer(&response, 500);
        initBuffer(&response, 5000);
        if (checkFile(path) != -1) {
            char *basicHeader = "HTTP/1.1 200 OK\r\n\r\n";
            appendBuffer(&response, basicHeader, strlen(basicHeader));
            write(clientfd, response.array, strlen(response.array));
            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) {
                    printf("%s", fileBuff);
                    write(clientfd, fileBuff, strlen(fileBuff));
                }
            }
            fclose(fp);
        } else {
            char *basicHeader = "HTTP/1.1 404 Not Found\r\n\r\n";
            appendBuffer(&response, basicHeader, strlen(basicHeader));
            write(clientfd, response.array, strlen(response.array));
        }

        fprintf(stdout, response.array);
        write(clientfd, response.array, strlen(response.array));
        shutdown(clientfd, SHUT_RDWR);
        free(path);
        freeBuffer(&request);