Only issue ContentType header once; correctly.

1.2-legacy
Charles Crossan 2020-11-13 22:03:21 -05:00
rodzic 576526576a
commit 49a19e26d5
1 zmienionych plików z 9 dodań i 3 usunięć

Wyświetl plik

@ -72,7 +72,8 @@ uint32_t timeSpeedUp = 0;
// right content type and are displayed correctly in the browser // right content type and are displayed correctly in the browser
char contentTypes[][2][32] = {{".txt", "text/plain"}, {".html", "text/html"}, {".js", "text/javascript"}, char contentTypes[][2][32] = {{".txt", "text/plain"}, {".html", "text/html"}, {".js", "text/javascript"},
{".png", "image/png"}, {".jpg", "image/jpg"}, {".gz", "application/gzip"}, {".png", "image/png"}, {".jpg", "image/jpg"}, {".gz", "application/gzip"},
{".gif", "image/gif"}, {".json", "application/json"}, {"", ""}}; {".gif", "image/gif"}, {".json", "application/json"}, {".css", "text/css"},
{"", ""}};
void handleWebResponse() void handleWebResponse()
{ {
@ -511,8 +512,6 @@ void handleStatic(HTTPRequest *req, HTTPResponse *res)
// Get access to the parameters // Get access to the parameters
ResourceParameters *params = req->getParams(); ResourceParameters *params = req->getParams();
// Set a default content type
res->setHeader("Content-Type", "application/octet-stream");
std::string parameter1; std::string parameter1;
// Print the first parameter value // Print the first parameter value
@ -549,16 +548,23 @@ void handleStatic(HTTPRequest *req, HTTPResponse *res)
res->setHeader("Content-Length", httpsserver::intToString(file.size())); res->setHeader("Content-Length", httpsserver::intToString(file.size()));
bool has_set_content_type = false;
// Content-Type is guessed using the definition of the contentTypes-table defined above // Content-Type is guessed using the definition of the contentTypes-table defined above
int cTypeIdx = 0; int cTypeIdx = 0;
do { do {
if (filename.rfind(contentTypes[cTypeIdx][0]) != std::string::npos) { if (filename.rfind(contentTypes[cTypeIdx][0]) != std::string::npos) {
res->setHeader("Content-Type", contentTypes[cTypeIdx][1]); res->setHeader("Content-Type", contentTypes[cTypeIdx][1]);
has_set_content_type = true;
break; break;
} }
cTypeIdx += 1; cTypeIdx += 1;
} while (strlen(contentTypes[cTypeIdx][0]) > 0); } while (strlen(contentTypes[cTypeIdx][0]) > 0);
if(!has_set_content_type) {
// Set a default content type
res->setHeader("Content-Type", "application/octet-stream");
}
// Read the file from SPIFFS and write it to the HTTP response body // Read the file from SPIFFS and write it to the HTTP response body
size_t length = 0; size_t length = 0;
do { do {