diff options
Diffstat (limited to 'backend/server.cpp')
-rw-r--r-- | backend/server.cpp | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/backend/server.cpp b/backend/server.cpp index a58eed1..ac8f9fb 100644 --- a/backend/server.cpp +++ b/backend/server.cpp @@ -143,17 +143,6 @@ struct HeaderParseResult { HttpRequestError error = HttpRequestError_None; }; -int fixNewLines(char* str, int strSize) { - int removed = 0; - for(int i = 0; i < strSize; i++) { - if(str[i] == '\n') { - str[i] = '\r\n'; - removed++; - } - } - - return removed; -} struct HeaderParser { char buffer[4096]; @@ -275,15 +264,15 @@ void sendErrorMessage(SOCKET socket, HttpStatusCode status, const char* errorHtm #define MAXBUFLEN 1000000 -int readFileToMemory(char* filepath, char source[MAXBUFLEN + 1]) { +int readFileToMemory(char* filepath, char source[MAXBUFLEN + 1], bool isBinary) { FILE* file; - fopen_s(&file, filepath, "r+"); + fopen_s(&file, filepath, isBinary ? "rb" : "r+"); if(file == NULL) { printf("Failed to read the file\n"); return -1; - } + size_t newLen = fread(source, sizeof(char), MAXBUFLEN, file); if ( ferror( file ) != 0 ) { fputs("Error reading file", stderr); @@ -295,19 +284,35 @@ int readFileToMemory(char* filepath, char source[MAXBUFLEN + 1]) { } +int fixNewLines(char* str, int strSize) { + int removed = 0; + for(int i = 0; i < strSize; i++) { + if(str[i] == '\n') { + str[i] = '\r\n'; + removed++; + } + } + + return removed; +} + + bool trySendFile(SOCKET clientSocket, char* filename) { - FILE* file; char filePath[128]; sprintf(filePath, "../../frontend%s", filename); + bool isBinary = false; + if (endsWith(filename, ".ico")) { + isBinary = true; + } + char source[MAXBUFLEN + 1]; - int fileSizeBytes = readFileToMemory(filePath, source); + int fileSizeBytes = readFileToMemory(filePath, source, isBinary); if (fileSizeBytes <= 0) { return false; } - const char* contentType; if (endsWith(filename, ".html")) { contentType = "text/html"; |