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 {
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 
105 inline MimeType fromString(const String& str)
106 {
107  return fromString(str.c_str());
108 }
109 
115 MimeType fromFullFileName(const char* fileName, MimeType unknown);
116 
117 inline MimeType fromFullFileName(const String& fileName, MimeType unknown)
118 {
119  return fromFullFileName(fileName.c_str(), unknown);
120 }
121 
127 String fromFullFileName(const char* fileName);
128 
131 inline String fromFullFileName(const String& fileName)
132 {
133  return fromFullFileName(fileName.c_str());
134 }
135 
138 }; // namespace ContentType
139 
String toString(MimeType m)
Get textual representation for a MIME type.
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:641
#define str(s)
Definition: testrunner.h:124
String fromFileExtension(const String &extension)
Obtain content type string from file extension.
Definition: WebConstants.h:90
The String class.
Definition: WString.h:136
bool fromString(const char *name, IFS::AttributeTag &tag)
String fromFullFileName(const String &fileName)
Obtain content type string from file name or path, with extension.
Definition: WebConstants.h:131
XX(name, extensionStart, mime)
Definition: WebConstants.h:71
MimeType
Definition: WebConstants.h:53
#define MIME_TYPE_MAP(XX)
Basic MIME types and file extensions.
Definition: WebConstants.h:26