HttpCommon.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  * HttpCommon.h
8  *
9  * @author: 2017 - Slavey Karadzhov <slav@attachix.com>
10  *
11  ****/
12 
13 #pragma once
14 
15 #define ENABLE_HTTP_REQUEST_AUTH 1
16 
17 #include "WString.h"
18 #include <Data/WebConstants.h>
19 #include "../Url.h"
21 #include "Data/ObjectMap.h"
22 
23 #ifndef HTTP_MAX_HEADER_SIZE
24 #define HTTP_MAX_HEADER_SIZE (8 * 1024)
25 #endif
26 
27 /* Number of maximum tcp connections to be kept in the pool */
28 #ifndef HTTP_REQUEST_POOL_SIZE
29 #define HTTP_REQUEST_POOL_SIZE 20
30 #endif
31 
32 #ifdef USE_LEGACY_HTTP_PARSER
33 
34 #include <http-parser/http_parser.h>
35 
36 enum class HttpError {
37 #define XX(n, s) n,
38  HTTP_ERRNO_MAP(XX)
39 #undef XX
40 };
41 
42 #define XX(n, s) constexpr HttpError HPE_##n = HttpError::n;
43 HTTP_ERRNO_MAP(XX)
44 #undef XX
45 
46 /* Macro defined using C++ type. Internal http_parser code has own definition */
47 #define HTTP_PARSER_ERRNO(p) HttpError((p)->http_errno)
48 
49 #else
50 
51 #include <llhttp.h>
52 
53 using http_parser_type = llhttp_type;
54 using http_parser = llhttp_t;
55 using http_parser_settings = llhttp_settings_t;
56 using http_method = llhttp_method;
57 
58 inline const char* http_method_str(llhttp_method method)
59 {
60  return llhttp_method_name(method);
61 }
62 
66 enum class HttpError {
67 #define XX(num, name, ...) name,
68  HTTP_ERRNO_MAP(XX)
69 #undef XX
70 };
71 
72 #define XX(num, name, ...) constexpr HttpError HPE_##name = HttpError::name;
73 HTTP_ERRNO_MAP(XX)
74 #undef XX
75 
76 /* Macro defined using C++ type. Internal http_parser code has own definition */
77 #define HTTP_PARSER_ERRNO(p) HttpError((p)->error)
78 
79 #endif
80 
89 enum class HttpMethod {
90 #define XX(num, name, string) name = num,
91  HTTP_METHOD_MAP(XX)
92 #undef XX
93 };
94 
95 #define XX(num, name, string) constexpr HttpMethod HTTP_##name = HttpMethod::name;
96 HTTP_METHOD_MAP(XX)
97 #undef XX
98 
102 enum class HttpStatus {
103 #define XX(num, name, string) name = num,
104  HTTP_STATUS_MAP(XX)
105 #undef XX
106 };
107 
108 #define XX(num, name, string) constexpr HttpStatus HTTP_STATUS_##name = HttpStatus::name;
109 HTTP_STATUS_MAP(XX)
110 #undef XX
111 
123 };
124 
126 
131 
137 
142 
146 inline String httpGetStatusText(unsigned code)
147 {
148  return toString(HttpStatus(code));
149 }
150 
154 inline String toString(HttpMethod method)
155 {
156  auto fstr = reinterpret_cast<flash_string_t>(http_method_str(http_method(method)));
157  return String(fstr);
158 }
159 
#define XX(num, name,...)
Definition: HttpCommon.h:108
llhttp_t http_parser
Definition: HttpCommon.h:54
HttpConnectionState
Identifies current state for an HTTP connection.
Definition: HttpCommon.h:115
@ eHCS_Sent
Definition: HttpCommon.h:121
@ eHCS_WaitResponse
Definition: HttpCommon.h:122
@ eHCS_SendingHeaders
Definition: HttpCommon.h:118
@ eHCS_StartBody
Definition: HttpCommon.h:119
@ eHCS_SendingBody
Definition: HttpCommon.h:120
@ eHCS_StartSending
Definition: HttpCommon.h:117
@ eHCS_Ready
Definition: HttpCommon.h:116
HttpMethod
Strongly-typed enum which shadows llhttp_method from llhttp library.
Definition: HttpCommon.h:89
XX(num, name, string)
String toString(HttpError err)
Return a descriptive string for the given error.
String httpGetErrorDescription(HttpError err)
Return a descriptive string for the given error.
llhttp_method http_method
Definition: HttpCommon.h:56
HttpStatus
HTTP status code.
Definition: HttpCommon.h:102
XX(num, name, string)
HttpError
HTTP error codes.
Definition: HttpCommon.h:66
XX(num, name,...)
llhttp_type http_parser_type
Definition: HttpCommon.h:53
llhttp_settings_t http_parser_settings
Definition: HttpCommon.h:55
String httpGetStatusText(unsigned code)
Return a descriptive string for an HTTP status code.
Definition: HttpCommon.h:146
const char * http_method_str(llhttp_method method)
Definition: HttpCommon.h:58
const __FlashStringHelper * flash_string_t
Provides a strongly-typed pointer to allow safe implicit operation using String class methods.
Definition: WString.h:92
The String class.
Definition: WString.h:133
#define SMING_DEPRECATED
Definition: sming_attr.h:37