GZip compression for static files

This commit is contained in:
Rene Zeldenthuis
2022-09-20 11:20:19 +02:00
parent b1480a93d9
commit cbdea51a12
8 changed files with 41 additions and 25 deletions

View File

@@ -2,3 +2,4 @@
. python3 -m pip install htmlmin
. python3 ./html_to_cpp.py ./html ./include/html_data.h
. python3 ./html_to_cpp_gzip.py ./html_gzip ./include/html_data_gzip.h

View File

@@ -2,4 +2,5 @@
python3 -m pip install --upgrade pip setuptools wheel
python3 -m pip install htmlmin
python3 ./html_to_cpp.py ./html ./include/html_data.h
python3 ./html_to_cpp.py ./html ./include/html_data.h
python3 ./html_to_cpp_gzip.py ./html_gzip ./include/html_data_gzip.h

View File

@@ -16,11 +16,11 @@ file_names = filter(lambda x: x[0] != '.' and os.path.isfile(os.path.join(input_
file_names = sorted(file_names)
output_file = open(file_h, 'w')
output_file.write('//*******************************************************************************\n')
output_file.write('// HTML import\n')
output_file.write('// Machine generated file\n')
output_file.write('// ******************************************************************************\n')
output_file.write('\n')
output_file.write('//*******************************************************************************\n'
'// HTML import\n'
'// Machine generated file\n'
'// ******************************************************************************\n'
'\n\n')
for file_name in file_names:
print(f'Processing: {file_name}... ')
@@ -33,13 +33,9 @@ for file_name in file_names:
file.close()
html_mimified = htmlmin.minify(html, remove_empty_space=True)
output_file.write('\n')
output_file.write('constexpr char ' + file_data_name + '[] = "')
# escape "
html_mimified_escaped = html_mimified.replace('"', '\\"')
output_file.write(html_mimified_escaped)
output_file.write('";\n')
output_file.write(f'constexpr char {file_data_name }[] = "{html_mimified_escaped}";\n')
output_file.close()

View File

@@ -37,9 +37,9 @@ for file_name in file_names:
html_mimified = htmlmin.minify(html, remove_empty_space=True)
html_mimified_gzip = gzip.compress(bytes(html_mimified, 'utf-8'))
html_mimified_gzip_values = ','.join(f'0x{i:02x}' for x in html_mimified_gzip)
html_mimified_gzip_values = ','.join(f'0x{i:02x}' for i in html_mimified_gzip)
output_file.write(f'constexpr unsigned char {file_data_name}[] = {{{html_mimified_gzip_values}}};\n\n')
output_file.write(f'constexpr unsigned char {file_data_name}[] = {{\n{html_mimified_gzip_values}\n}};\n\n')
output_file.close()

File diff suppressed because one or more lines are too long

10
include/html_data_gzip.h Normal file

File diff suppressed because one or more lines are too long

View File

@@ -11,6 +11,7 @@
#include <format_number.h>
#include <template_render.h>
#include <html_data.h>
#include <html_data_gzip.h>
#include <settings.h>
char camera_config_val[sizeof(camera_config_entry)];
@@ -41,11 +42,19 @@ bool config_changed = false;
// Camera initialization result
esp_err_t camera_init_result;
void stream_text_file(const char *contents, const char *mime_type)
void stream_text_file(const char *content, const char *mime_type)
{
// Cache for 86400 seconds (one day)
web_server.sendHeader("Cache-Control", "max-age=86400");
web_server.send(200, mime_type, contents);
web_server.send(200, mime_type, content);
}
void stream_text_file_gzip(const unsigned char *content, size_t length, const char *mime_type)
{
// Cache for 86400 seconds (one day)
web_server.sendHeader("Cache-Control", "max-age=86400");
web_server.sendContent(mime_type);
web_server.sendContent(reinterpret_cast<const char *>(content), length);
}
void handle_root()
@@ -133,20 +142,20 @@ void handle_snapshot()
log_v("handle_jpg");
if (camera_init_result != ESP_OK)
{
web_server.send(404, "text/plain", "Camera is not initialized");
return;
web_server.send(404, "text/plain", "Camera is not initialized");
return;
}
web_server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
web_server.sendContent("HTTP/1.1 200 OK\r\n"
"Content-Disposition: inline; filename=snapshot.jpg\r\n"
"Content-Type: image/jpeg\r\n\r\n");
// Make a copy in memory to prevent interaction with RTSP
// Make a copy in memory to prevent interaction with RTSP
auto fb_len = cam.getSize();
cam.run();
auto fb = (uint8_t*)memcpy(new uint8_t[cam.getSize()], cam.getfb(), fb_len);
auto fb = (uint8_t *)memcpy(new uint8_t[cam.getSize()], cam.getfb(), fb_len);
web_server.sendContent(reinterpret_cast<const char *>(fb), fb_len);
delete []fb;
delete[] fb;
}
void on_config_saved()
@@ -237,7 +246,9 @@ void setup()
// bootstrap
web_server.on("/bootstrap.min.css", HTTP_GET, []()
{ stream_text_file(file_data_bootstrap_min_css, "text/css"); });
{
web_server.sendHeader("Content-encoding", "gzip");
stream_text_file_gzip(file_data_bootstrap_min_css, sizeof(file_data_bootstrap_min_css), "text/css"); });
web_server.onNotFound([]()
{ iotWebConf.handleNotFound(); });