From 09f1380e3486dd648ac1afd1748c5689c08a06c8 Mon Sep 17 00:00:00 2001 From: miha-q <> Date: Mon, 20 Jan 2025 15:17:58 -0500 Subject: [PATCH] Mon Jan 20 03:17:58 PM EST 2025 --- src/http.c | 19 +++++++++++++++++++ src/main.c | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/http.c b/src/http.c index 1ba45a7..6316f77 100644 --- a/src/http.c +++ b/src/http.c @@ -349,6 +349,25 @@ int httpStringOut(int session_fd, char* str) httpRespond(session_fd, "\n"); } +int httpStringOutByType(int session_fd, char* str, const char* type) +{ + //Good header + httpRespond(session_fd, "HTTP/1.1 200 OK\n"); + httpRespond(session_fd, HTTP_SERVER); + + int strSize = strlen(str); + //Spit out file content length part of the HTTP message + const char *contentLengthTemplate = "Access-Control-Allow-Origin: *\nContent-Type: %s\nContent-Length: %i\n\n"; + int contentLengthLen = snprintf(NULL, 0, contentLengthTemplate, type, strSize); + char* contentLength = malloc(contentLengthLen + 1); + sprintf(contentLength, contentLengthTemplate, strSize); + httpRespond(session_fd, contentLength); + free(contentLength); + + httpRespond(session_fd, str); + httpRespond(session_fd, "\n"); +} + void httpRequest(int session_fd, char** str, int* strlen) { int flags = fcntl(session_fd, F_GETFL, 0); diff --git a/src/main.c b/src/main.c index d62ab2b..8f686ff 100644 --- a/src/main.c +++ b/src/main.c @@ -379,7 +379,7 @@ void *handleRequest(void *vargp) uint8_t* frontend = malloc(sizeof(FAVICON) + 1); memcpy(frontend, FAVICON, sizeof(FAVICON)); frontend[sizeof(FAVICON)] = 0; - httpStringOut(session_fd, frontend); + httpStringOutByType(session_fd, frontend, "image/x-icon"); free(frontend); } else -- 2.39.5