HttpHeaderFields.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  * HttpHeaderFields.h - Standard HTTP header field definitions
8  *
9  * @author: 2018 - Mikee47 <mike@sillyhouse.net>
10  *
11  ****/
12 
13 #pragma once
14 
15 #include "Data/CStringArray.h"
16 #include "WString.h"
17 
18 /*
19  * Common HTTP header field names. Enumerating these simplifies matching
20  * and generating headers. The strings themselves are stored in flash to
21  * save RAM.
22  *
23  * According to RFC 2616: 4.2, field names are case-insensitive.
24  *
25  * A brief description of each header field is given for information purposes.
26  * For details see https://www.iana.org/assignments/message-headers/message-headers.xhtml
27  *
28  */
29 #define HTTP_HEADER_FIELDNAME_MAP(XX) \
30  XX(ACCEPT, "Accept", "Limit acceptable response types") \
31  XX(ACCESS_CONTROL_ALLOW_ORIGIN, "Access-Control-Allow-Origin", "") \
32  XX(AUTHORIZATION, "Authorization", "Basic user agent authentication") \
33  XX(CC, "Cc", "email field") \
34  XX(CONNECTION, "Connection", "Indicates sender's desired control options") \
35  XX(CONTENT_DISPOSITION, "Content-Disposition", "Additional information about how to process response payload") \
36  XX(CONTENT_ENCODING, "Content-Encoding", "Applied encodings in addition to content type") \
37  XX(CONTENT_LENGTH, "Content-Length", "Anticipated size for payload when not using transfer encoding") \
38  XX(CONTENT_TYPE, "Content-Type", \
39  "Payload media type indicating both data format and intended manner of processing by recipient") \
40  XX(CONTENT_TRANSFER_ENCODING, "Content-Transfer-Encoding", "Coding method used in a MIME message body part") \
41  XX(CACHE_CONTROL, "Cache-Control", "Directives for caches along the request/response chain") \
42  XX(DATE, "Date", "Message originating date/time") \
43  XX(EXPECT, "Expect", "Behaviours to be supported by the server in order to properly handle this request.") \
44  XX(ETAG, "ETag", \
45  "Validates resource, such as a file, so recipient can confirm whether it has changed - generally more " \
46  "reliable than Date") \
47  XX(FROM, "From", "email address of human user who controls the requesting user agent") \
48  XX(HOST, "Host", \
49  "Request host and port information for target URI; allows server to service requests for multiple hosts on a " \
50  "single IP address") \
51  XX(IF_MATCH, "If-Match", \
52  "Precondition check using ETag to avoid accidental overwrites when servicing multiple user requests. Ensures " \
53  "resource entity tag matches before proceeding.") \
54  XX(IF_MODIFIED_SINCE, "If-Modified-Since", "Precondition check using Date") \
55  XX(LAST_MODIFIED, "Last-Modified", "Server timestamp indicating date and time resource was last modified") \
56  XX(LOCATION, "Location", "Used in redirect responses, amongst other places") \
57  XX(SEC_WEBSOCKET_ACCEPT, "Sec-WebSocket-Accept", "Server response to opening Websocket handshake") \
58  XX(SEC_WEBSOCKET_VERSION, "Sec-WebSocket-Version", \
59  "Websocket opening request indicates acceptable protocol version. Can appear more than once.") \
60  XX(SEC_WEBSOCKET_KEY, "Sec-WebSocket-Key", "Websocket opening request validation key") \
61  XX(SEC_WEBSOCKET_PROTOCOL, "Sec-WebSocket-Protocol", \
62  "Websocket opening request indicates supported protocol(s), response contains negotiated protocol(s)") \
63  XX(SERVER, "Server", "Identifies software handling requests") \
64  XX(SET_COOKIE, "Set-Cookie", "Server may pass name/value pairs and associated metadata to user agent (client)") \
65  XX(SUBJECT, "Subject", "email subject line") \
66  XX(TO, "To", "email intended recipient address") \
67  XX(TRANSFER_ENCODING, "Transfer-Encoding", "e.g. Chunked, compress, deflate, gzip") \
68  XX(UPGRADE, "Upgrade", \
69  "Used to transition from HTTP to some other protocol on the same connection. e.g. Websocket") \
70  XX(USER_AGENT, "User-Agent", "Information about the user agent originating the request") \
71  XX(WWW_AUTHENTICATE, "WWW-Authenticate", "Indicates HTTP authentication scheme(s) and applicable parameters")
72 
73 enum class HttpHeaderFieldName {
74  UNKNOWN = 0,
75 #define XX(tag, str, comment) tag,
77 #undef XX
78  CUSTOM // First custom header tag value
79 };
80 
81 #define XX(tag, str, comment) constexpr HttpHeaderFieldName HTTP_HEADER_##tag = HttpHeaderFieldName::tag;
82 XX(UNKNOWN, "", "")
84 XX(CUSTOM, "", "")
85 #undef XX
86 
88 {
89 public:
91 
97  static String toString(const String& name, const String& value);
98 
99  String toString(HttpHeaderFieldName name, const String& value) const
100  {
101  return toString(toString(name), value);
102  }
103 
109  HttpHeaderFieldName fromString(const String& name) const;
110 
117  {
118  auto field = fromString(name);
119  if(field == HTTP_HEADER_UNKNOWN) {
120  field = static_cast<HttpHeaderFieldName>(unsigned(HTTP_HEADER_CUSTOM) + customFieldNames.count());
121  customFieldNames.add(name);
122  }
123  return field;
124  }
125 
126  void clear()
127  {
128  customFieldNames.clear();
129  }
130 
131 private:
136  HttpHeaderFieldName findCustomFieldName(const String& name) const;
137 
138  CStringArray customFieldNames;
139 };
String toString(uint8_t value)
MimeType fromString(const char *str)
Get enumerated value for a MIME type string.
The String class.
Definition: WString.h:136
String toString(HttpHeaderFieldName name, const String &value) const
Definition: HttpHeaderFields.h:99
#define HTTP_HEADER_FIELDNAME_MAP(XX)
Definition: HttpHeaderFields.h:29
HttpHeaderFieldName findOrCreate(const String &name)
Find the enumerated value for the given field name string, create a custom entry if not found...
Definition: HttpHeaderFields.h:116
Definition: HttpHeaderFields.h:87
void clear()
Definition: HttpHeaderFields.h:126
XX(tag, str, comment)
Class to manage a double null-terminated list of strings, such as "one\0two\0three\0".
Definition: CStringArray.h:21
HttpHeaderFieldName
Definition: HttpHeaderFields.h:73