attempt to send binary data (for images, etc)

master
Drake 2 years ago
parent a6428d1b1b
commit 3ff8f9a0c6

Binary file not shown.

Binary file not shown.

@ -4,7 +4,7 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
char* readFile(char* path) { void* readFile(char* path) {
path++; path++;
if (strlen(path) <= 1) { if (strlen(path) <= 1) {
printf("/ converted to /index.html\n"); printf("/ converted to /index.html\n");
@ -17,15 +17,13 @@ char* readFile(char* path) {
errno = -1; errno = -1;
return ""; return "";
} }
char* buf = NULL; void* buf = NULL;
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
int length = ftell(fp); int length = ftell(fp);
rewind(fp); rewind(fp);
buf = (char*)malloc(sizeof(char) * (length + 1)); buf = malloc(sizeof(char) * (length + 1));
fread(buf, sizeof(char), length, fp); fread(buf, sizeof(char), length, fp);
buf[length] = '\0';
char* tmp = buf; return buf;
return tmp;
} }

@ -45,7 +45,7 @@ int srv(char* ADDR, int PORT) {
char* tPath = request.path; char* tPath = request.path;
char* dot = strrchr(tPath, '.'); char* dot = strrchr(tPath, '.');
char *file = readFile(request.path); void *file = readFile(request.path);
ResHeader rawHeader; ResHeader rawHeader;
rawHeader.protocol = request.protocol; rawHeader.protocol = request.protocol;
rawHeader.status = 200; rawHeader.status = 200;
@ -75,8 +75,9 @@ int srv(char* ADDR, int PORT) {
char tmpHeader[1024]; char tmpHeader[1024];
makeHeader(tmpHeader, &rawHeader); makeHeader(tmpHeader, &rawHeader);
char resHeader[1024]; //= "HTTP/1.1 200 OK\r\n\nHello, world!"; char resHeader[1024]; //= "HTTP/1.1 200 OK\r\n\nHello, world!";
sprintf(resHeader, "%s%s", (char*)tmpHeader, file); sprintf(resHeader, "%s", (char*)tmpHeader);
send(client, resHeader, strlen(resHeader), 0); send(client, resHeader, strlen(resHeader), 0);
send(client, (void*)(&file), sizeof(file), 0);
close(client); close(client);
} }
} }

Loading…
Cancel
Save