Commit dca912b4 authored by Matthew Burket's avatar Matthew Burket

Move file serving to known method

parent 34e6f589
Pipeline #340 passed with stage
in 14 seconds
......@@ -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,
......
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