Skip to content

Commit 5341eca

Browse files
authored
Resolves conflict
Resolves conflict. Optimizes _getEtag to use less memory.
1 parent 9c87c59 commit 5341eca

File tree

2 files changed

+29
-43
lines changed

2 files changed

+29
-43
lines changed

src/AsyncWebServerRequest.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,20 @@ void AsyncWebServerRequest::send(FS &fs, const String &path, const char *content
6969
bool AsyncWebServerRequest::_getEtag(File gzFile, char *etag) {
7070
static constexpr char hexChars[] = "0123456789ABCDEF";
7171

72-
// Compressed file not found or invalid
73-
if (!gzFile.seek(gzFile.size() - 8)) {
72+
if (!gzFile.seek(gzFile.size() - 8))
7473
return false;
75-
}
76-
77-
uint8_t crcInTrailer[4];
78-
gzFile.read(crcInTrailer, 4);
7974

80-
uint32_t data;
81-
memcpy(&data, crcInTrailer, 4);
75+
uint32_t crc;
76+
gzFile.read(reinterpret_cast<uint8_t*>(&crc), sizeof(crc));
8277

83-
etag[0] = hexChars[(data >> 4) & 0x0F];
84-
etag[1] = hexChars[data & 0x0F];
85-
etag[2] = hexChars[(data >> 12) & 0x0F];
86-
etag[3] = hexChars[(data >> 8) & 0x0F];
87-
etag[4] = hexChars[(data >> 20) & 0x0F];
88-
etag[5] = hexChars[(data >> 16) & 0x0F];
89-
etag[6] = hexChars[(data >> 28)];
90-
etag[7] = hexChars[(data >> 24) & 0x0F];
78+
etag[0] = hexChars[(crc >> 4) & 0x0F];
79+
etag[1] = hexChars[crc & 0x0F];
80+
etag[2] = hexChars[(crc >> 12) & 0x0F];
81+
etag[3] = hexChars[(crc >> 8) & 0x0F];
82+
etag[4] = hexChars[(crc >> 20) & 0x0F];
83+
etag[5] = hexChars[(crc >> 16) & 0x0F];
84+
etag[6] = hexChars[(crc >> 28)];
85+
etag[7] = hexChars[(crc >> 24) & 0x0F];
9186
etag[8] = '\0';
9287

9388
return true;

src/WebResponses.cpp

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@
66

77
using namespace asyncsrv;
88

9-
// Since ESP8266 does not link memchr by default, here's its implementation.
10-
void *memchr(void *ptr, int ch, size_t count) {
11-
unsigned char *p = static_cast<unsigned char *>(ptr);
12-
while (count--) {
13-
if (*p++ == static_cast<unsigned char>(ch)) {
14-
return --p;
15-
}
16-
}
17-
return nullptr;
18-
}
19-
209
/*
2110
* Abstract Response
2211
*
@@ -642,7 +631,7 @@ void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
642631
const char *dot = strrchr(cpath, '.');
643632

644633
if (!dot) {
645-
_contentType = T_text_plain;
634+
_contentType = T_application_octet_stream;
646635
return;
647636
}
648637

@@ -674,20 +663,20 @@ void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
674663
_contentType = T_font_woff;
675664
} else if (strcmp(dot, T__ttf) == 0) {
676665
_contentType = T_font_ttf;
677-
} else if (strcmp(dot, T__eot) == 0) {
678-
_contentType = T_font_eot;
679666
} else if (strcmp(dot, T__xml) == 0) {
680667
_contentType = T_text_xml;
681668
} else if (strcmp(dot, T__pdf) == 0) {
682669
_contentType = T_application_pdf;
683670
} else if (strcmp(dot, T__mp4) == 0) {
684671
_contentType = T_video_mp4;
685-
} else if (strcmp(dot, T__zip) == 0) {
686-
_contentType = T_application_zip;
687-
} else if (strcmp(dot, T__gz) == 0) {
688-
_contentType = T_application_x_gzip;
689-
} else {
672+
} else if (strcmp(dot, T__opus) == 0) {
673+
_contentType = T_audio_opus;
674+
} else if (strcmp(dot, T__webm) == 0) {
675+
_contentType = T_video_webm;
676+
} else if (strcmp(dot, T__txt) == 0) {
690677
_contentType = T_text_plain;
678+
} else {
679+
_contentType = T_application_octet_stream;
691680
}
692681
#endif
693682
}
@@ -707,14 +696,17 @@ void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
707696
*/
708697
AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *contentType, bool download, AwsTemplateProcessor callback)
709698
: AsyncAbstractResponse(callback) {
699+
710700
// Try to open the uncompressed version first
711701
_content = fs.open(path, fs::FileOpenMode::read);
712-
if (_content.available()) {
713-
_path = path;
714-
} else {
715-
// Try to open the compressed version (.gz)
716-
_path = path + asyncsrv::T__gz;
717-
_content = fs.open(_path, fs::FileOpenMode::read);
702+
if (!_content.available()) {
703+
// If not available try to open the compressed version (.gz)
704+
String gzPath;
705+
uint16_t pathLen = path.length();
706+
gzPath.reserve(pathLen + 3);
707+
gzPath.concat(path);
708+
gzPath.concat(asyncsrv::T__gz);
709+
_content = fs.open(gzPath, fs::FileOpenMode::read);
718710

719711
char serverETag[9];
720712
if (AsyncWebServerRequest::_getEtag(_content, serverETag)) {
@@ -761,9 +753,8 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *con
761753
AsyncFileResponse::AsyncFileResponse(File content, const String &path, const char *contentType, bool download, AwsTemplateProcessor callback)
762754
: AsyncAbstractResponse(callback) {
763755
_code = 200;
764-
_path = path;
765756

766-
if (!download && String(content.name()).endsWith(T__gz) && !path.endsWith(T__gz)) {
757+
if (String(content.name()).endsWith(T__gz) && !path.endsWith(T__gz)) {
767758
addHeader(T_Content_Encoding, T_gzip, false);
768759
_callback = nullptr; // Unable to process gzipped templates
769760
_sendContentLength = true;

0 commit comments

Comments
 (0)