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

Got basic serving

parent 6890940f
Pipeline #338 passed with stage
in 13 seconds
......@@ -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,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;
}
}
......
......@@ -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);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment