10000 Make getContentType available for 3rd party usage by vdeconinck · Pull Request #7254 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

Make getContentType available for 3rd party usage #7254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Refactored to make getContentType public for 3rd party use.
  • Loading branch information
vdeconinck committed Apr 29, 2020
commit 80627fb9263b17d065dbdc1c4444a30e4ffcda4b
18 changes: 4 additions & 14 deletions libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class StaticRequestHandler : public RequestHandler<ServerType> {
}
DEBUGV("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile);

String contentType = getContentType(path);
String contentType = mime::getContentType(path);

// look for gz file, only if the original specified path is not a gz. So part only works to send gzip via content encoding when a non compressed is asked for
// if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc...
Expand All @@ -146,19 +146,9 @@ class StaticRequestHandler : public RequestHandler<ServerType> {
return true;
}

static String getContentType(const String& path) {
char buff[sizeof(mimeTable[0].mimeType)];
// Check all entries but last one for match, return if found
for (size_t i=0; i < sizeof(mimeTable)/sizeof(mimeTable[0])-1; i++) {
strcpy_P(buff, mimeTable[i].endsWith);
if (path.endsWith(buff)) {
strcpy_P(buff, mimeTable[i].mimeType);
return String(buff);
}
}
// Fall-through and just return default type
strcpy_P(buff, mimeTable[sizeof(mimeTable)/sizeof(mimeTable[0])-1].mimeType);
return String(buff);
/* Deprecated version. Please use mime::getContentType instead */
static String getContentType(const String& path) __attribute__((deprecated)) {
return mime::getContentType(path);
}

protected:
Expand Down
16 changes: 16 additions & 0 deletions libraries/ESP8266WebServer/src/detail/mimetable.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "mimetable.h"
#include "pgmspace.h"
#include "WString.h"

namespace mime
{
Expand Down Expand Up @@ -32,4 +33,19 @@ const Entry mimeTable[maxType] PROGMEM =
{ "", "application/octet-stream" }
};

String getContentType(const String& path) {
char buff[sizeof(mimeTable[0].mimeType)];
// Check all entries but last one for match, return if found
for (size_t i=0; i < sizeof(mimeTable)/sizeof(mimeTable[0])-1; i++) {
strcpy_P(buff, mimeTable[i].endsWith);
if (path.endsWith(buff)) {
strcpy_P(buff, mimeTable[i].mimeType);
return String(buff);
}
}
// Fall-through and just return default type
strcpy_P(buff, mimeTable[sizeof(mimeTable)/sizeof(mimeTable[0])-1].mimeType);
return String(buff);
}

}
4 changes: 3 additions & 1 deletion libraries/ESP8266WebServer/src/detail/mimetable.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __MIMETABLE_H__
#define __MIMETABLE_H__

#include "WString.h"

namespace mime
{
Expand Down Expand Up @@ -41,7 +42,8 @@ struct Entry


extern const Entry mimeTable[maxType];
}

String getContentType(const String& path);
}

#endif
0