Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/WebResponses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,9 +738,12 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *con
if (download) {
// Extract filename from path and set as download attachment
int filenameStart = path.lastIndexOf('/') + 1;
char buf[26 + path.length() - filenameStart];
char *filename = (char *)path.c_str() + filenameStart;
snprintf(buf, sizeof(buf), T_attachment, filename);
const char *filename = path.c_str() + filenameStart;
String buf;
buf.reserve(strlen(T_attachment) + strlen(filename) + 2);
buf = T_attachment;
buf += filename;
buf += "\"";
addHeader(T_Content_Disposition, buf, false);
} else {
// Serve file inline (display in browser)
Expand All @@ -764,22 +767,26 @@ AsyncFileResponse::AsyncFileResponse(File content, const String &path, const cha
_content = content;
_contentLength = _content.size();

if (strlen(contentType) == 0) {
if (*contentType == '\0') {
_setContentTypeFromPath(path);
} else {
_contentType = contentType;
}

int filenameStart = path.lastIndexOf('/') + 1;
char buf[26 + path.length() - filenameStart];
char *filename = (char *)path.c_str() + filenameStart;

if (download) {
snprintf_P(buf, sizeof(buf), PSTR("attachment; filename=\"%s\""), filename);
// Extract filename from path and set as download attachment
int filenameStart = path.lastIndexOf('/') + 1;
const char *filename = path.c_str() + filenameStart;
String buf;
buf.reserve(strlen(T_attachment) + strlen(filename) + 2);
buf = T_attachment;
buf += filename;
buf += "\"";
addHeader(T_Content_Disposition, buf, false);
} else {
snprintf_P(buf, sizeof(buf), PSTR("inline"));
// Serve file inline (display in browser)
addHeader(T_Content_Disposition, T_inline, false);
}
addHeader(T_Content_Disposition, buf, false);
}

size_t AsyncFileResponse::_fillBuffer(uint8_t *data, size_t len) {
Expand Down
2 changes: 1 addition & 1 deletion src/literals.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static constexpr const char *T_100_CONTINUE = "100-continue";
static constexpr const char *T_13 = "13";
static constexpr const char *T_ACCEPT = "Accept";
static constexpr const char *T_Accept_Ranges = "Accept-Ranges";
static constexpr const char *T_attachment = "attachment; filename=\"%s\"";
static constexpr const char *T_attachment = "attachment; filename=\"";
static constexpr const char *T_AUTH = "Authorization";
static constexpr const char *T_auth_nonce = "\", qop=\"auth\", nonce=\"";
static constexpr const char *T_BASIC = "Basic";
Expand Down
Loading