some stuff

master
Drake 2 years ago
parent abe5205aa0
commit 3c7740330a

@ -3,4 +3,3 @@
- threading or async - threading or async
- optional allowlist of available paths (throw something else on unavailable ones) - optional allowlist of available paths (throw something else on unavailable ones)
- denylist of known-bad url strings - 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 <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <fcntl.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)
@ -30,6 +31,7 @@ char* readFile(char* path) {
buf = (char*)malloc(sizeof(char) * (length + 1)); buf = (char*)malloc(sizeof(char) * (length + 1));
fread(buf, sizeof(char), length, fp); fread(buf, sizeof(char), length, fp);
buf[length] = '\0'; buf[length] = '\0';
fclose(fp);
char* tmp = buf; char* tmp = buf;
return tmp; return tmp;

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

@ -8,5 +8,5 @@ typedef struct {
} ResHeader; } ResHeader;
void makeHeader(char* out, ResHeader* header) { 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 <string.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <pthread.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)
@ -28,37 +29,10 @@ void safeExit(int sig) {
_exit(0); _exit(0);
} }
int srv(char* ADDR, int PORT) { void *handle(void* arg) {
int srvSocket; int client = *(int*)arg;
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 hasTriedIndex; int hasTriedIndex;
int client = accept(srvSocket, NULL, NULL);
ReqHeader request = readReqHeader(client); ReqHeader request = readReqHeader(client);
funy: { funy: {
char* dot = strrchr(request.path, '.'); char* dot = strrchr(request.path, '.');
@ -115,5 +89,39 @@ funy: {
send(client, resHeader, strlen(resHeader), 0); send(client, resHeader, strlen(resHeader), 0);
close(client); 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