Skip to content

Commit b2538cb

Browse files
committed
Add gzip support and use uint8_t content buffers
Switch content pointers and static content buffer from char to uint8_t to correctly handle binary/gzipped assets. Introduce a gzip_ flag on HttpDeamonHandleRequest and propagate a bool& gzip through GetFileContent so responses include a Content-Encoding header when appropriate. Update GetFileContent signature/return type, adjust callers and header formatting, and fix related casts when sending data. Also include minor formatting and style cleanups in get_file_content.cpp.
1 parent eafa964 commit b2538cb

3 files changed

Lines changed: 48 additions & 61 deletions

File tree

lib-remoteconfig/include/httpd/httpdhandlerequest.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,13 @@ class HttpDeamonHandleRequest
8585
char* file_data_{nullptr};
8686
char* firmwarefile_name_{nullptr};
8787
char* receive_buffer_{nullptr};
88-
const char* content_{nullptr};
88+
const uint8_t* content_{nullptr};
8989
char upload_filename_[16];
9090

9191
http::Status status_{http::Status::kUnknownError};
9292
http::RequestMethod request_method_{http::RequestMethod::kUnknown};
9393
http::ContentTypes request_content_type_{http::ContentTypes::kNotDefined};
94+
bool gzip_{false};
9495

9596
char dynamic_content_[httpd::kBufsize];
9697
};

lib-remoteconfig/src/httpd/get_file_content.cpp

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,26 @@
3636

3737
#include "httpd/httpd.h"
3838

39-
static constexpr char kSupportedExtensions[static_cast<int>(http::ContentTypes::kNotDefined)][8] =
40-
{
39+
static constexpr char kSupportedExtensions[static_cast<int>(http::ContentTypes::kNotDefined)][8] = {
4140
"html",
4241
"css",
4342
"js",
4443
"json",
4544
"bin"
4645
};
4746

48-
static char s_static_content[4096];
47+
static uint8_t s_static_content[4096];
4948

50-
static http::ContentTypes GetContentType(const char* file_name)
51-
{
49+
static http::ContentTypes GetContentType(const char* file_name) {
5250
DEBUG_ENTRY();
5351

54-
for (int i = 0; i < static_cast<int>(http::ContentTypes::kNotDefined); i++)
55-
{
52+
for (int i = 0; i < static_cast<int>(http::ContentTypes::kNotDefined); i++) {
5653
const auto kL = strlen(file_name);
5754
const auto kE = strlen(kSupportedExtensions[i]);
5855

59-
if (kL > (kE + 2))
60-
{
61-
if (file_name[kL - kE - 1] == '.')
62-
{
63-
if (strcmp(&file_name[kL - kE], kSupportedExtensions[i]) == 0)
64-
{
56+
if (kL > (kE + 2)) {
57+
if (file_name[kL - kE - 1] == '.') {
58+
if (strcmp(&file_name[kL - kE], kSupportedExtensions[i]) == 0) {
6559
DEBUG_EXIT();
6660
return static_cast<http::ContentTypes>(i);
6761
}
@@ -73,22 +67,19 @@ static http::ContentTypes GetContentType(const char* file_name)
7367
return http::ContentTypes::kNotDefined;
7468
}
7569

76-
uint32_t GetFileContentFromFile(const char* file_name, char* dst, http::ContentTypes& content_type)
77-
{
70+
uint32_t GetFileContentFromFile(const char* file_name, char* dst, http::ContentTypes& content_type) {
7871
DEBUG_PUTS(file_name);
7972

8073
auto* file = fopen(file_name, "r");
8174

82-
if (file == nullptr)
83-
{
75+
if (file == nullptr) {
8476
DEBUG_EXIT();
8577
return 0;
8678
}
8779

8880
content_type = GetContentType(file_name);
8981

90-
if (content_type == http::ContentTypes::kNotDefined)
91-
{
82+
if (content_type == http::ContentTypes::kNotDefined) {
9283
DEBUG_EXIT();
9384
fclose(file);
9485
return 0;
@@ -98,29 +89,20 @@ uint32_t GetFileContentFromFile(const char* file_name, char* dst, http::ContentT
9889
auto* p = dst;
9990
int c;
10091

101-
while ((c = fgetc(file)) != EOF)
102-
{
103-
if (do_remove_white_spaces)
104-
{
105-
if (c <= ' ')
106-
{
92+
while ((c = fgetc(file)) != EOF) {
93+
if (do_remove_white_spaces) {
94+
if (c <= ' ') {
10795
continue;
108-
}
109-
else
110-
{
96+
} else {
11197
do_remove_white_spaces = false;
11298
}
113-
}
114-
else
115-
{
116-
if (c == '\n')
117-
{
99+
} else {
100+
if (c == '\n') {
118101
do_remove_white_spaces = true;
119102
}
120103
}
121104
*p++ = c;
122-
if ((p - dst) == sizeof(s_static_content))
123-
{
105+
if ((p - dst) == sizeof(s_static_content)) {
124106
DEBUG_PUTS("File too long");
125107
break;
126108
}
@@ -132,15 +114,14 @@ uint32_t GetFileContentFromFile(const char* file_name, char* dst, http::ContentT
132114
return static_cast<uint32_t>(p - dst);
133115
}
134116

135-
const char* GetFileContent(const char* file_name, uint32_t& size, http::ContentTypes& content_type)
136-
{
117+
const uint8_t* GetFileContent(const char* file_name, uint32_t& size, http::ContentTypes& content_type, , bool& gzip) {
137118
DEBUG_ENTRY();
138119
DEBUG_PUTS(file_name);
139120

140121
size = GetFileContentFromFile(file_name, s_static_content, content_type);
122+
gzip = false;
141123

142-
if (size != 0)
143-
{
124+
if (size != 0) {
144125
return s_static_content;
145126
}
146127

@@ -151,21 +132,19 @@ const char* GetFileContent(const char* file_name, uint32_t& size, http::ContentT
151132
#include "../http/content/content.h"
152133
#include "common/utils/utils_hash.h"
153134

154-
const char* GetFileContent(const char* file_name, uint32_t& size, http::ContentTypes& content_type)
155-
{
135+
const uint8_t* GetFileContent(const char* file_name, uint32_t& size, http::ContentTypes& content_type, bool& gzip) {
156136
DEBUG_ENTRY();
157-
158-
const auto kHash = Fnv1a32Runtime(file_name, strlen(file_name));
159-
160-
DEBUG_PRINTF("%s:%u", file_name, kHash);
161-
162-
for (auto& content : kHttpContent)
163-
{
164-
if (kHash == content.hash)
165-
{
137+
138+
const auto kHash = Fnv1a32Runtime(file_name, strlen(file_name));
139+
140+
DEBUG_PRINTF("%s:%u", file_name, kHash);
141+
142+
for (auto& content : kHttpContent) {
143+
if (kHash == content.hash) {
166144
size = content.content_length;
167145
content_type = content.content_type;
168-
DEBUG_PUTS(content.file_name);
146+
gzip = content.gzip;
147+
DEBUG_PUTS(content.file_name);
169148
return content.content;
170149
}
171150
}

lib-remoteconfig/src/httpd/httpdhandlerequest.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void Reboot();
6262
#define _TIME_STAMP_ 0
6363
#endif
6464

65-
const char* GetFileContent(const char* file_name, uint32_t& size, http::ContentTypes& content_type);
65+
const uint8_t* GetFileContent(const char* file_name, uint32_t& size, http::ContentTypes& content_type, bool& gzip);
6666

6767
void HttpDeamonHandleRequest::HandleRequest(uint32_t bytes_received, char* receive_buffer) {
6868
DEBUG_ENTRY();
@@ -154,7 +154,7 @@ void HttpDeamonHandleRequest::HandleRequest(uint32_t bytes_received, char* recei
154154
}
155155

156156
request_content_type_ = http::ContentTypes::kTextHtml;
157-
content_ = dynamic_content_;
157+
content_ = reinterpret_cast<uint8_t*>(dynamic_content_);
158158
content_size_ = static_cast<uint32_t>(snprintf(dynamic_content_, sizeof(dynamic_content_), "%u %s\n", static_cast<unsigned>(status_), status_msg));
159159

160160
const auto kHeaderLength =
@@ -172,22 +172,28 @@ void HttpDeamonHandleRequest::HandleRequest(uint32_t bytes_received, char* recei
172172
const auto kHeaderLength = static_cast<uint32_t>(snprintf(receive_buffer_, network::tcp::kTcpDataMss,
173173
"HTTP/1.1 %u %s\r\n"
174174
"Server: %s\r\n"
175+
"Content-Encoding: %s\r\n"
175176
"Content-Type: %s\r\n"
176177
"Content-Length: %u\r\n"
177178
"Cache-Control: %s\r\n"
178179
"ETag: \"%u\"\r\n"
179180
"Connection: close\r\n"
180181
"\r\n",
181-
static_cast<unsigned int>(status_), status_msg, network::iface::HostName(), http::kContentType[static_cast<uint32_t>(request_content_type_)],
182-
static_cast<unsigned int>(content_size_), (content_ == dynamic_content_) ? "no-cache" : "max-age=3600", (content_ == dynamic_content_) ? timing::Millis() : _TIME_STAMP_));
182+
static_cast<unsigned int>(status_),
183+
status_msg, network::iface::HostName(),
184+
gzip_ ? "gzip" : "identity",
185+
http::kContentType[static_cast<uint32_t>(request_content_type_)],
186+
static_cast<unsigned int>(content_size_), (content_ == reinterpret_cast<uint8_t*>(dynamic_content_)) ? "no-cache" : "max-age=3600",
187+
(content_ == reinterpret_cast<uint8_t*>(dynamic_content_)) ? timing::Millis() : _TIME_STAMP_)
188+
);
183189

184190
network::tcp::Send(connection_handle_, reinterpret_cast<const uint8_t*>(receive_buffer_), kHeaderLength);
185191

186192
DEBUG_PRINTF("content_size_=%u, %s", content_size_, (content_ == dynamic_content_) ? "Dynamic" : "Static");
187193
}
188194

189195
if (content_size_ != 0U) {
190-
network::tcp::Send(connection_handle_, reinterpret_cast<const uint8_t*>(content_), content_size_);
196+
network::tcp::Send(connection_handle_, content_, content_size_);
191197
}
192198

193199
// Reset request state after reply is sent.
@@ -414,8 +420,9 @@ uint32_t RdmTod(char*, uint32_t, uint32_t);
414420
http::Status HttpDeamonHandleRequest::HandleGet() {
415421
DEBUG_ENTRY();
416422

423+
gzip_ = false;
417424
uint32_t length = 0;
418-
content_ = dynamic_content_;
425+
content_ = reinterpret_cast<uint8_t*>(dynamic_content_);
419426
DEBUG_PUTS(uri_);
420427

421428
if (memcmp(uri_, "/json/", 6) == 0) {
@@ -456,7 +463,7 @@ http::Status HttpDeamonHandleRequest::HandleGet() {
456463
length = (*(handler.get))(dynamic_content_, static_cast<uint32_t>(sizeof(dynamic_content_)));
457464
}
458465
} else {
459-
content_ = GetFileContent(&uri_[6], length, request_content_type_);
466+
content_ = GetFileContent(&uri_[6], length, request_content_type_, gzip_);
460467
}
461468
}
462469
} else {
@@ -465,9 +472,9 @@ http::Status HttpDeamonHandleRequest::HandleGet() {
465472

466473
if (kIndex >= 0) {
467474
const auto& handler = html::kHtmlInfos[kIndex];
468-
content_ = GetFileContent(handler.label, length, request_content_type_);
475+
content_ = GetFileContent(handler.label, length, request_content_type_, gzip_);
469476
} else {
470-
content_ = GetFileContent(&uri_[1], length, request_content_type_);
477+
content_ = GetFileContent(&uri_[1], length, request_content_type_, gzip_);
471478
}
472479
}
473480

0 commit comments

Comments
 (0)