some stuff

master
Drake 2 years ago
parent abe5205aa0
commit 3c7740330a

@ -3,4 +3,3 @@
- threading or async
- optional allowlist of available paths (throw something else on unavailable ones)
- denylist of known-bad url strings
- fix memory issues or use a GC (bdwgc probably)

Binary file not shown.

@ -2,6 +2,7 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#ifdef NO_GC
#include <gc.h>
#define malloc(n) GC_malloc(n)
@ -30,6 +31,7 @@ char* readFile(char* path) {
buf = (char*)malloc(sizeof(char) * (length + 1));
fread(buf, sizeof(char), length, fp);
buf[length] = '\0';
fclose(fp);
char* tmp = buf;
return tmp;

@ -1,22 +1,32 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#ifdef NO_GC
#include <gc.h>
#define malloc(n) GC_malloc(n)
#else
#include <stdlib.h>
#endif
typedef struct {
char method[16];
char path[256];
char *method;
char *path;
char protocol[16];
} ReqHeader;
ReqHeader readReqHeader(int client) {
char buf[1024];
char* buf = malloc(1024);
read(client, buf, 1024);
ReqHeader header;
strcpy(header.method, strtok(buf, " "));
strcpy(header.path, strtok(NULL, " "));
strcpy(header.protocol, strtok(NULL, " "));
header.method = malloc(1024);
header.path = malloc(1024);
/*strcpy(header.method, strtok(buf, " "));
strcpy(header.path, strtok(NULL, " "));*/
sprintf(header.method, "%s", strtok(buf, " "));
sprintf(header.path, "%s", strtok(NULL, " "));
if (header.path[1] == '\0') {
strcpy(header.path, "/index.html");
//strcpy(header.path, "/index.html");
sprintf(header.path, "/index.html");
}
return header;
}

@ -8,5 +8,5 @@ typedef struct {
} ResHeader;
void makeHeader(char* out, ResHeader* header) {
sprintf(out, "%s %d %s\r\nContent-Type: %s\r\n\n", /*header->protocol TODO:this breaks everything because newlines*/ "HTTP/1.1", header->status, header->notice, header->mime);
sprintf(out, "%s %d %s\r\nContent-Type: %s\r\nServer: httpsrv/1.0-indev\r\n\n", /*header->protocol TODO:this breaks everything because newlines*/ "HTTP/1.1", header->status, header->notice, header->mime);
}

@ -7,6 +7,7 @@
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#ifdef NO_GC
#include <gc.h>
#define malloc(n) GC_malloc(n)
@ -28,37 +29,10 @@ void safeExit(int sig) {
_exit(0);
}
int srv(char* ADDR, int PORT) {
int srvSocket;
struct sockaddr_in srvAddr;
srvSocket = socket(AF_INET, SOCK_STREAM, 0);
srvAddr.sin_family = AF_INET;
srvAddr.sin_port = htons(PORT);
srvAddr.sin_addr.s_addr = inet_addr(ADDR);
void *handle(void* arg) {
int client = *(int*)arg;
if (bind(
srvSocket,
(struct sockaddr*) &srvAddr,
sizeof(srvAddr)
) < 0) {
error("Failed to bind to %s:%d.\n", ADDR, PORT);
return 1;
}
if (listen(srvSocket, 10) < 0) {
error("Unable to listen on %s:%d.\n", ADDR, PORT);
return 1;
}
signal(SIGINT, safeExit);
info("Server (probably) listening on http://%s:%d\n", ADDR, PORT);
while(1) {
int hasTriedIndex;
int client = accept(srvSocket, NULL, NULL);
ReqHeader request = readReqHeader(client);
funy: {
char* dot = strrchr(request.path, '.');
@ -115,5 +89,39 @@ funy: {
send(client, resHeader, strlen(resHeader), 0);
close(client);
}
return 0;
}
int srv(char* ADDR, int PORT) {
int srvSocket;
struct sockaddr_in srvAddr;
srvSocket = socket(AF_INET, SOCK_STREAM, 0);
srvAddr.sin_family = AF_INET;
srvAddr.sin_port = htons(PORT);
srvAddr.sin_addr.s_addr = inet_addr(ADDR);
if (bind(
srvSocket,
(struct sockaddr*) &srvAddr,
sizeof(srvAddr)
) < 0) {
error("Failed to bind to %s:%d.\n", ADDR, PORT);
return 1;
}
if (listen(srvSocket, 10) < 0) {
error("Unable to listen on %s:%d.\n", ADDR, PORT);
return 1;
}
signal(SIGINT, safeExit);
info("Server (probably) listening on http://%s:%d\n", ADDR, PORT);
while(1) {
int client = accept(srvSocket, NULL, NULL);
handle(&client);
}
}

Loading…
Cancel
Save