probably a commit

master
Drake 2 years ago
parent 64c38adca5
commit 766b77f096

@ -16,7 +16,7 @@ all: main
main: main:
@${CC} -o bin/httpsrv src/main.c ${CC_FLAGS} @${CC} -o bin/httpsrv src/main.c ${CC_FLAGS}
debug: CC_FLAGS:=-g -O0 -v ${CC_FLAGS} debug: CC_FLAGS:=-g -O0 -v -fsanitize=undefined -fsanitize=address ${CC_FLAGS}
debug: all debug: all
clean: clean:

Binary file not shown.

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <errno.h>
#ifdef NO_GC #ifdef NO_GC
#include <gc.h> #include <gc.h>
#define malloc(n) GC_malloc(n) #define malloc(n) GC_malloc(n)
@ -19,13 +20,14 @@ ReqHeader readReqHeader(int client) {
char* buf = malloc(1024); char* buf = malloc(1024);
read(client, buf, 1024); read(client, buf, 1024);
ReqHeader header; ReqHeader header;
header.method = malloc(1024); if (buf == NULL || !strcmp(buf, "")) {
header.path = malloc(1024); errno = -1;
char* tBuf = malloc(1024); return header;
/*strcpy(header.method, strtok(buf, " ")); }
strcpy(header.path, strtok(NULL, " "));*/ //header.method = malloc(1024);
sprintf(header.method, "%s", strtok_r(buf, " ", &tBuf)); //header.path = malloc(1024);
sprintf(header.path, "%s", strtok_r(NULL, " ", &tBuf)); header.method = strtok(buf, " ");
header.path = strtok(NULL, " ");
if (header.path[1] == '\0') { if (header.path[1] == '\0') {
//strcpy(header.path, "/index.html"); //strcpy(header.path, "/index.html");
sprintf(header.path, "/index.html"); sprintf(header.path, "/index.html");

@ -21,22 +21,42 @@
#include "log.h" #include "log.h"
#include "mime.h" #include "mime.h"
int srvSocket;
void safeExit(int sig) { void safeExit(int sig) {
info("Exiting...\n"); info("Exiting...\n");
#ifdef NO_GC #ifdef NO_GC
GC_gcollect(); GC_gcollect();
#endif #endif
close(srvSocket);
_exit(0); _exit(0);
} }
void segfault(int sig) {
warn("may or may not have segfaulted\n");
#ifdef NO_GC
GC_gcollect();
#endif
}
void *handle(void* arg) { void *handle(void* arg) {
int client = *(int*)arg; int client = *(int*)arg;
ReqHeader request = readReqHeader(client); ReqHeader request = readReqHeader(client);
ResHeader rawHeader; ResHeader rawHeader;
rawHeader.protocol = request.protocol; if (errno == -1) {
rawHeader.status = 200; warn("somehow we got a NULL request????\n");
rawHeader.notice = "OK"; rawHeader.status = 500;
rawHeader.notice = "Internal Server Error";
char* tmpHeader = malloc(1024);
makeHeader(tmpHeader, &rawHeader);
send(client, tmpHeader, strlen(tmpHeader), 0);
close(client);
return 0;
} else {
rawHeader.status = 200;
rawHeader.notice = "OK";
}
int useBinary = getMime(&rawHeader.mime, request.path); int useBinary = getMime(&rawHeader.mime, request.path);
if (errno == -1) { if (errno == -1) {
@ -67,7 +87,6 @@ void *handle(void* arg) {
} }
int srv(char* ADDR, int PORT) { int srv(char* ADDR, int PORT) {
int srvSocket;
struct sockaddr_in srvAddr; struct sockaddr_in srvAddr;
srvSocket = socket(AF_INET, SOCK_STREAM, 0); srvSocket = socket(AF_INET, SOCK_STREAM, 0);
@ -91,6 +110,7 @@ int srv(char* ADDR, int PORT) {
} }
signal(SIGINT, safeExit); signal(SIGINT, safeExit);
//signal(SIGSEGV, segfault);
info("Server (probably) listening on http://%s:%d\n", ADDR, PORT); info("Server (probably) listening on http://%s:%d\n", ADDR, PORT);

Loading…
Cancel
Save