factor out mimetypes, probably something else

master
Drake 2 years ago
parent cda88d2f76
commit 4d323d55cd

Binary file not shown.

@ -1,3 +1,4 @@
#pragma once
/*
* This is free and unencumbered software released into the public domain.
*

@ -1,3 +1,4 @@
#pragma once
#include <unistd.h>
#include <stdio.h>
#include <string.h>

@ -1,3 +1,4 @@
#pragma once
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

@ -0,0 +1,53 @@
#pragma once
#include <string.h>
#ifdef NO_GC
#include <gc.h>
#define malloc(n) GC_malloc(n)
#else
#include <stdlib.h>
#endif
#include "log.h"
int hasTriedIndex;
int getMime(char** mime, char* filename) {
char* dot = strrchr(filename, '.');
if (!dot || dot == filename) {
if (!hasTriedIndex) {
hasTriedIndex++;
strcat(filename, "/index.html");
info("trying to upgrade directory to index.html");
return getMime(mime, filename);
} else {
warn("file extension does not exist or is the same as request's path.\n");
hasTriedIndex = 1;
*mime = "text/plain";
return 0;
}
} else {
if (!strcmp(dot, ".html")) {
*mime = "text/html";
} else if (!strcmp(dot, ".css")) {
*mime = "text/css";
} else if (!strcmp(dot, ".js")) {
*mime = "text/javascript";
} else if (!strcmp(dot, ".woff2")) {
*mime = "font/woff2";
return 1;
} else if (!strcmp(dot, ".ico")) {
*mime = "image/vnd.microsoft.icon";
return 1;
} else {
warn("mimetype for %s not found\n", filename);
*mime = "text/plain";
}
if (hasTriedIndex == 1) {
hasTriedIndex = 2;
} else {
hasTriedIndex = 0;
}
}
return 0;
}

@ -1,3 +1,4 @@
#pragma once
#include <stdio.h>
#include <unistd.h>
#include <string.h>

@ -1,3 +1,4 @@
#pragma once
#include <stdio.h>
typedef struct {

@ -19,7 +19,7 @@
#include "reqHeader.h"
#include "file.h"
#include "log.h"
#include "mime.h"
void safeExit(int sig) {
info("Exiting...\n");
@ -32,51 +32,13 @@ void safeExit(int sig) {
void *handle(void* arg) {
int client = *(int*)arg;
int useBinary = 0;
int hasTriedIndex;
ReqHeader request = readReqHeader(client);
funy: {
char* dot = strrchr(request.path, '.');
ResHeader rawHeader;
rawHeader.protocol = request.protocol;
rawHeader.status = 200;
rawHeader.notice = "OK";
if (!dot || dot == request.path) {
if (!hasTriedIndex) {
strcat(request.path, "/index.html");
info("trying to upgrade directory to index.html\n");
hasTriedIndex = 1;
goto funy;
} else {
warn("file extension does not exist or is the same as request path\n");
hasTriedIndex = 1;
rawHeader.mime = "text/plain";
}
} else {
if (!strcmp(dot, ".html")) {
rawHeader.mime = "text/html";
} else if (!strcmp(dot, ".css")) {
rawHeader.mime = "text/css";
} else if (!strcmp(dot, ".ico")) {
rawHeader.mime = "image/vnd.microsoft.icon";
useBinary = 1;
} else if (!strcmp(dot, ".js")) {
rawHeader.mime = "text/javascript";
} else if (!strcmp(dot, ".woff2")) {
rawHeader.mime = "font/woff2";
useBinary = 1;
} else {
warn("mimetype for %s not found\n", request.path);
rawHeader.mime = "text/plain";
}
if (hasTriedIndex == 1) {
hasTriedIndex = 2;
} else {
hasTriedIndex = 0;
}
}
int useBinary = getMime(&rawHeader.mime, request.path);
if (errno == -1) {
rawHeader.status = 404;
rawHeader.notice = "Not Found";
@ -104,7 +66,6 @@ funy: {
//sprintf(resHeader, "%s%s", (char*)tmpHeader, file);
//send(client, resHeader, strlen(resHeader), 0);
close(client);
}
return 0;
}

Loading…
Cancel
Save