allow serving gz files from lfs with gz header

This commit is contained in:
Tester23
2025-07-09 08:15:30 +02:00
parent 3c85604378
commit 7f0b7b75d3
3 changed files with 59 additions and 20 deletions

View File

@ -295,6 +295,17 @@ void http_setup(http_request_t* request, const char* type) {
poststr(request, "\r\n"); // end headers with double CRLF
poststr(request, "\r\n");
}
void http_setup_gz(http_request_t* request, const char* type) {
hprintf255(request, httpHeader, request->responseCode, type);
poststr(request, "\r\n"); // next header
poststr(request, httpCorsHeaders);
poststr(request, "\r\n");
poststr(request, "Content-Encoding: gzip");
poststr(request, "\r\n");
poststr(request, "Connection: close");
poststr(request, "\r\n"); // end headers with double CRLF
poststr(request, "\r\n");
}
void http_html_start(http_request_t* request, const char* pagename) {
poststr(request, htmlDoctype);

View File

@ -64,6 +64,7 @@ typedef struct http_request_tag {
int HTTP_ProcessPacket(http_request_t* request);
void http_setup(http_request_t* request, const char* type);
void http_setup_gz(http_request_t* request, const char* type);
void http_html_start(http_request_t* request, const char* pagename);
void http_html_end(http_request_t* request);
int poststr(http_request_t* request, const char* str);

View File

@ -546,6 +546,7 @@ static int http_rest_run_lfs_file(http_request_t* request) {
free(fpath);
return 0;
}
static int http_rest_get_lfs_file(http_request_t* request) {
char* fpath;
char* buff;
@ -554,6 +555,7 @@ static int http_rest_get_lfs_file(http_request_t* request) {
int total = 0;
lfs_file_t* file;
char *args;
bool isGzip;
// don't start LFS just because we're trying to read a file -
// it won't exist anyway
@ -578,6 +580,8 @@ static int http_rest_get_lfs_file(http_request_t* request) {
*args = 0;
}
isGzip = EndsWith(fpath, "gz");
ADDLOG_DEBUG(LOG_FEATURE_API, "LFS read of %s", fpath);
lfsres = lfs_file_open(&lfs, file, fpath, LFS_O_RDONLY);
@ -636,34 +640,58 @@ static int http_rest_get_lfs_file(http_request_t* request) {
ADDLOG_DEBUG(LOG_FEATURE_API, "LFS open [%s] gives %d", fpath, lfsres);
if (lfsres >= 0) {
const char* mimetype = httpMimeTypeBinary;
do {
if (EndsWith(fpath, ".ico")) {
mimetype = "image/x-icon";
break;
char* ext = fpath;
if (isGzip) {
// find original extension (e.g., .js from .js.gz)
char* dot = strrchr(fpath, '.');
if (dot) {
*dot = '\0'; // temporarily strip .gz
if (EndsWith(fpath, ".js")) {
mimetype = httpMimeTypeJavascript;
}
else if (EndsWith(fpath, ".html")) {
mimetype = httpMimeTypeHTML;
}
else if (EndsWith(fpath, ".css")) {
mimetype = httpMimeTypeCSS;
}
else if (EndsWith(fpath, ".json")) {
mimetype = httpMimeTypeJson;
}
else if (EndsWith(fpath, ".ico")) {
mimetype = "image/x-icon";
}
*dot = '.'; // restore .gz
}
}
else {
if (EndsWith(fpath, ".js") || EndsWith(fpath, ".vue")) {
mimetype = httpMimeTypeJavascript;
break;
}
if (EndsWith(fpath, ".json")) {
else if (EndsWith(fpath, ".json")) {
mimetype = httpMimeTypeJson;
break;
}
if (EndsWith(fpath, ".html")) {
else if (EndsWith(fpath, ".html")) {
mimetype = httpMimeTypeHTML;
break;
}
if (EndsWith(fpath, ".css")) {
else if (EndsWith(fpath, ".css")) {
mimetype = httpMimeTypeCSS;
break;
}
break;
} while (0);
else if (EndsWith(fpath, ".ico")) {
mimetype = "image/x-icon";
}
}
http_setup(request, mimetype);
//#if ENABLE_OBK_BERRY
// http_runBerryFile(request, fpath);
//#else
if (isGzip) {
http_setup_gz(request, mimetype);
}
else {
http_setup(request, mimetype);
}
//#if ENABLE_OBK_BERRY
// http_runBerryFile(request, fpath);
//#else
do {
len = lfs_file_read(&lfs, file, buff, 1024);
total += len;
@ -671,8 +699,8 @@ static int http_rest_get_lfs_file(http_request_t* request) {
//ADDLOG_DEBUG(LOG_FEATURE_API, "%d bytes read", len);
postany(request, buff, len);
}
} while (len > 0);
//#endif
} while (len > 0);
//#endif
lfs_file_close(&lfs, file);
ADDLOG_DEBUG(LOG_FEATURE_API, "%d total bytes read", total);
}
@ -689,7 +717,6 @@ static int http_rest_get_lfs_file(http_request_t* request) {
if (buff) os_free(buff);
return 0;
}
static int http_rest_get_lfs_delete(http_request_t* request) {
char* fpath;
int lfsres;