Skip to content

Commit 197a37b

Browse files
Merge pull request #272 from JosePineiro/refactor/asyncfileresponse-File
Fix: Refactor AsyncFileResponse (File overload)
2 parents 5668b9c + 149d7ca commit 197a37b

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

src/WebResponses.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -738,9 +738,12 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *con
738738
if (download) {
739739
// Extract filename from path and set as download attachment
740740
int filenameStart = path.lastIndexOf('/') + 1;
741-
char buf[26 + path.length() - filenameStart];
742-
char *filename = (char *)path.c_str() + filenameStart;
743-
snprintf(buf, sizeof(buf), T_attachment, filename);
741+
const char *filename = path.c_str() + filenameStart;
742+
String buf;
743+
buf.reserve(strlen(T_attachment) + strlen(filename) + 2);
744+
buf = T_attachment;
745+
buf += filename;
746+
buf += "\"";
744747
addHeader(T_Content_Disposition, buf, false);
745748
} else {
746749
// Serve file inline (display in browser)
@@ -764,22 +767,26 @@ AsyncFileResponse::AsyncFileResponse(File content, const String &path, const cha
764767
_content = content;
765768
_contentLength = _content.size();
766769

767-
if (strlen(contentType) == 0) {
770+
if (*contentType == '\0') {
768771
_setContentTypeFromPath(path);
769772
} else {
770773
_contentType = contentType;
771774
}
772775

773-
int filenameStart = path.lastIndexOf('/') + 1;
774-
char buf[26 + path.length() - filenameStart];
775-
char *filename = (char *)path.c_str() + filenameStart;
776-
777776
if (download) {
778-
snprintf_P(buf, sizeof(buf), PSTR("attachment; filename=\"%s\""), filename);
777+
// Extract filename from path and set as download attachment
778+
int filenameStart = path.lastIndexOf('/') + 1;
779+
const char *filename = path.c_str() + filenameStart;
780+
String buf;
781+
buf.reserve(strlen(T_attachment) + strlen(filename) + 2);
782+
buf = T_attachment;
783+
buf += filename;
784+
buf += "\"";
785+
addHeader(T_Content_Disposition, buf, false);
779786
} else {
780-
snprintf_P(buf, sizeof(buf), PSTR("inline"));
787+
// Serve file inline (display in browser)
788+
addHeader(T_Content_Disposition, T_inline, false);
781789
}
782-
addHeader(T_Content_Disposition, buf, false);
783790
}
784791

785792
size_t AsyncFileResponse::_fillBuffer(uint8_t *data, size_t len) {

src/literals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static constexpr const char *T_100_CONTINUE = "100-continue";
1212
static constexpr const char *T_13 = "13";
1313
static constexpr const char *T_ACCEPT = "Accept";
1414
static constexpr const char *T_Accept_Ranges = "Accept-Ranges";
15-
static constexpr const char *T_attachment = "attachment; filename=\"%s\"";
15+
static constexpr const char *T_attachment = "attachment; filename=\"";
1616
static constexpr const char *T_AUTH = "Authorization";
1717
static constexpr const char *T_auth_nonce = "\", qop=\"auth\", nonce=\"";
1818
static constexpr const char *T_BASIC = "Basic";

0 commit comments

Comments
 (0)