BasicHttpHeaders.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  * BasicHttpHeaders.h - in-place HTTP header parsing
8  *
9  * @author: 2019 - Mikee47 <mike@sillyhouse.net>
10  *
11  ****/
12 
13 #pragma once
14 
15 #include "HttpCommon.h"
16 #include "HttpHeaderFields.h"
17 
24 {
25 public:
26  // Uses an array...
27  static constexpr size_t maxValues = 16;
28 
29  // ...of these
30  struct Header {
31  const char* name;
32  const char* value;
33 
37  String toString() const
38  {
39  String s;
40  s += name;
41  s += ": ";
42  s += value;
43  s += "\r\n";
44  return s;
45  }
46 
47  operator String() const
48  {
49  return toString();
50  }
51  };
52 
54  {
55  clear();
56  }
57 
61  void clear();
62 
75  HttpError parse(char* data, size_t len, http_parser_type type = HTTP_BOTH);
76 
77  const Header& operator[](unsigned i) const
78  {
79  return headers[i];
80  }
81 
82  String toString(unsigned i) const
83  {
84  return operator[](i).toString();
85  }
86 
87  const char*& operator[](const char* name);
88 
92  unsigned count() const
93  {
94  return count_;
95  }
96 
102  const char* operator[](const char* name) const;
103 
104  const char* operator[](HttpHeaderFieldName name) const
105  {
106  return operator[](HttpHeaderFields::toString(name).c_str());
107  }
108 
109  bool contains(const char* name) const
110  {
111  return operator[](name) != nullptr;
112  }
113 
114  bool contains(HttpHeaderFieldName name) const
115  {
116  return operator[](name) != nullptr;
117  }
118 
123  http_parser_type type() const
124  {
125  return http_parser_type(parser.type);
126  }
127 
132  {
133  return HttpMethod(parser.method);
134  }
135 
137  {
138  parser.method = unsigned(method);
139  }
140 
145  {
146  return HttpStatus(parser.status_code);
147  }
148 
152  unsigned contentLength() const
153  {
154  return parser.content_length;
155  }
156 
157 private:
158  static int staticOnField(http_parser* parser, const char* at, size_t length);
159  static int staticOnValue(http_parser* parser, const char* at, size_t length);
160  int onField(const char* at, size_t length);
161  int onValue(const char* at, size_t length);
162 
163 private:
164  http_parser parser;
165  static const http_parser_settings parserSettings;
166  Header headers[maxValues];
167  unsigned count_ = 0;
168  const char* nullValue = nullptr;
169 };
HttpMethod
Strongly-typed enum which shadows http_method from http_parser library.
Definition: HttpCommon.h:42
HttpStatus
HTTP status code.
Definition: HttpCommon.h:55
HttpError
HTTP error codes.
Definition: HttpCommon.h:68
HttpHeaderFieldName
Definition: HttpHeaderFields.h:84
Parse array of name/value pairs as references to original data.
Definition: BasicHttpHeaders.h:24
bool contains(const char *name) const
Definition: BasicHttpHeaders.h:109
const Header & operator[](unsigned i) const
Definition: BasicHttpHeaders.h:77
static constexpr size_t maxValues
Definition: BasicHttpHeaders.h:27
bool contains(HttpHeaderFieldName name) const
Definition: BasicHttpHeaders.h:114
void clear()
Reset to default state.
unsigned count() const
Get number of parsed headers.
Definition: BasicHttpHeaders.h:92
const char * operator[](const char *name) const
Find a header by name.
HttpStatus status() const
Obtain response status.
Definition: BasicHttpHeaders.h:144
BasicHttpHeaders()
Definition: BasicHttpHeaders.h:53
HttpMethod method() const
Obtain request method.
Definition: BasicHttpHeaders.h:131
const char * operator[](HttpHeaderFieldName name) const
Definition: BasicHttpHeaders.h:104
String toString(unsigned i) const
Definition: BasicHttpHeaders.h:82
const char *& operator[](const char *name)
HttpError parse(char *data, size_t len, http_parser_type type=HTTP_BOTH)
Parse header data into name/value pairs.
void setMethod(HttpMethod method)
Definition: BasicHttpHeaders.h:136
unsigned contentLength() const
Obtain content length field value.
Definition: BasicHttpHeaders.h:152
http_parser_type type() const
Get the type of message parsed.
Definition: BasicHttpHeaders.h:123
Definition: HttpHeaderFields.h:99
String toString(HttpHeaderFieldName name) const
The String class.
Definition: WString.h:137
Definition: BasicHttpHeaders.h:30
const char * value
Definition: BasicHttpHeaders.h:32
String toString() const
Return a String in the form "name: value\r\n".
Definition: BasicHttpHeaders.h:37
const char * name
Definition: BasicHttpHeaders.h:31