WebConstants.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * WebConstants.h
8  *
9  ****/
10 
18 #pragma once
19 
20 #include <WString.h>
21 
26 #define MIME_TYPE_MAP(XX) \
27  /* Type, extension start, Mime type */ \
28  \
29  /* Texts */ \
30  XX(HTML, "html", "text/html") \
31  XX(TEXT, "txt", "text/plain") \
32  XX(JS, "js", "text/javascript") \
33  XX(CSS, "css", "text/css") \
34  XX(XML, "xml", "text/xml") \
35  XX(JSON, "json", "application/json") \
36  \
37  /* Images */ \
38  XX(JPEG, "jpg", "image/jpeg") \
39  XX(GIF, "gif", "image/gif") \
40  XX(PNG, "png", "image/png") \
41  XX(SVG, "svg", "image/svg+xml") \
42  XX(ICO, "ico", "image/x-icon") \
43  \
44  /* Archives */ \
45  XX(GZIP, "gzip", "application/x-gzip") \
46  XX(ZIP, "zip", "application/zip") \
47  \
48  /* Binary and Form */ \
49  XX(BINARY, "", "application/octet-stream") \
50  XX(FORM_URL_ENCODED, "", "application/x-www-form-urlencoded") \
51  XX(FORM_MULTIPART, "", "multipart/form-data")
52 
53 enum class MimeType : uint8_t {
54 #define XX(name, extensionStart, mime) name,
56 #undef XX
57  UNKNOWN
58 };
59 
60 #define XX(name, extensionStart, mime) constexpr MimeType MIME_##name = MimeType::name;
62 XX(UNKNOWN, "", "")
63 #undef XX
64 
70 
71 namespace ContentType
72 {
78 MimeType fromFileExtension(const char* extension, MimeType unknown);
79 
84 String fromFileExtension(const char* extension);
85 
90 inline String fromFileExtension(const String& extension)
91 {
92  return fromFileExtension(extension.c_str());
93 }
94 
99 MimeType fromString(const char* str);
100 
106 {
107  return fromString(str.c_str());
108 }
109 
120 MimeType fromFullFileName(const char* fileName, MimeType unknown);
121 
125 inline MimeType fromFullFileName(const String& fileName, MimeType unknown)
126 {
127  return fromFullFileName(fileName.c_str(), unknown);
128 }
129 
139 String fromFullFileName(const char* fileName);
140 
144 inline String fromFullFileName(const String& fileName)
145 {
146  return fromFullFileName(fileName.c_str());
147 }
148 
151 }; // namespace ContentType
152 
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
#define MIME_TYPE_MAP(XX)
Basic MIME types and file extensions.
Definition: WebConstants.h:26
MimeType
Definition: WebConstants.h:53
#define XX(name, extensionStart, mime)
Definition: WebConstants.h:60
String toString(MimeType m)
Get textual representation for a MIME type.
XX(name, extensionStart, mime)
Definition: WebConstants.h:72
MimeType fromFileExtension(const char *extension, MimeType unknown)
Obtain MIME type value from file extension.
MimeType fromString(const char *str)
Get enumerated value for a MIME type string.
MimeType fromFullFileName(const char *fileName, MimeType unknown)
#define str(s)
Definition: testrunner.h:124