probably a commit

master
Drake 2 years ago
parent 64c38adca5
commit 766b77f096

@ -16,7 +16,7 @@ all: main
main:
@${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
clean:

Binary file not shown.

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

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

Loading…
Cancel
Save