le binary file

master
Drake 2 years ago
parent 3c7740330a
commit cda88d2f76

Binary file not shown.

@ -3,6 +3,7 @@
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef NO_GC
#include <gc.h>
#define malloc(n) GC_malloc(n)
@ -23,16 +24,44 @@ char* readFile(char* path) {
errno = -1;
return "";
}
char* buf = NULL;
char* buf;
fseek(fp, 0, SEEK_END);
int length = ftell(fp);
rewind(fp);
buf = (char*)malloc(sizeof(char) * (length + 1));
fread(buf, sizeof(char), length, fp);
buf[length] = '\0';
fclose(fp);
char* tmp = buf;
return tmp;
}
void* readFileBin(int* len, char* path) {
path++;
if (strlen(path) <= 1) {
printf("/ converted to /index.html\n");
path = (char*)malloc(11);
path = "index.html";
}
FILE* fp = fopen(path, "rb");
if (!(access(path, F_OK) == 0)) {
errno = -1;
return "";
}
void* buf;
struct stat st;
stat(path, &st);
int length = st.st_size;
*len = length;
printf("%d\n", length);
buf = (void*)malloc(sizeof(int) * (length + 1));
fread(buf, sizeof(int), length, fp);
fclose(fp);
void* tmp = buf;
return tmp;
}

@ -32,12 +32,13 @@ void safeExit(int sig) {
void *handle(void* arg) {
int client = *(int*)arg;
int useBinary = 0;
int hasTriedIndex;
ReqHeader request = readReqHeader(client);
funy: {
char* dot = strrchr(request.path, '.');
char *file = readFile(request.path);
ResHeader rawHeader;
rawHeader.protocol = request.protocol;
rawHeader.status = 200;
@ -60,8 +61,12 @@ funy: {
rawHeader.mime = "text/css";
} else if (!strcmp(dot, ".ico")) {
rawHeader.mime = "image/vnd.microsoft.icon";
useBinary = 1;
} else if (!strcmp(dot, ".js")) {
rawHeader.mime = "text/javascript";
} else if (!strcmp(dot, ".woff2")) {
rawHeader.mime = "font/woff2";
useBinary = 1;
} else {
warn("mimetype for %s not found\n", request.path);
rawHeader.mime = "text/plain";
@ -84,9 +89,20 @@ funy: {
reqlog("%d %s\n", rawHeader.status, request.path);
char tmpHeader[1024];
makeHeader(tmpHeader, &rawHeader);
char* resHeader = malloc(strlen(tmpHeader) + strlen(file) + 1); //= "HTTP/1.1 200 OK\r\n\nHello, world!";
sprintf(resHeader, "%s%s", (char*)tmpHeader, file);
send(client, resHeader, strlen(resHeader), 0);
send(client, tmpHeader, strlen(tmpHeader), 0);
void* file;
if (!useBinary) {
file = readFile(request.path);
send(client, file, strlen(file), 0);
} else {
int len;
file = readFileBin(&len, request.path);
send(client, file, len, 0);
}
//char* resHeader = malloc(strlen(tmpHeader) + strlen(file) + 1); //= "HTTP/1.1 200 OK\r\n\nHello, world!";
//sprintf(resHeader, "%s%s", (char*)tmpHeader, file);
//send(client, resHeader, strlen(resHeader), 0);
close(client);
}
return 0;

Loading…
Cancel
Save