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