66
77using 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 */
708697AsyncFileResponse::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
761753AsyncFileResponse::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