|
|
@ -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);
|
|
|
|
|
|
|
|
|
|
|
|