HttpHeaders.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  * HttpHeaders.h
8  *
9  * @author: 2018 - Mikee47 <mike@sillyhouse.net>
10  *
11  * Encapsulate encoding and decoding of HTTP header fields
12  * Used for HTTP connections and SMTP mail
13  *
14  * The HttpHeaders class was an empty HashMap class living in 'Structures.h'.
15  * It has been expanded here to simplify code, and to provide enumerated keys
16  * for common field names.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include "HttpHeaderFields.h"
23 #include "WHashMap.h"
24 #include "DateTime.h"
25 
34 class HttpHeaders : public HttpHeaderFields, private HashMap<HttpHeaderFieldName, String>
35 {
36 public:
37  class HeaderConst : public BaseElement<true>
38  {
39  public:
40  HeaderConst(const HttpHeaderFields& fields, const HttpHeaderFieldName& key, const String& value)
41  : BaseElement(key, value), fields(fields)
42  {
43  }
44 
46  operator String() const;
47  size_t printTo(Print& p) const;
48 
49  private:
50  const HttpHeaderFields& fields;
51  };
52 
53  class Iterator : public HashMap::Iterator<true>
54  {
55  public:
56  Iterator(const HttpHeaders& headers, unsigned index)
57  : HashMap::Iterator<true>::Iterator(headers, index), fields(headers)
58  {
59  }
60 
62  {
63  return HeaderConst(fields, map.keyAt(index), map.valueAt(index));
64  }
65 
67  {
68  return HeaderConst(fields, map.keyAt(index), map.valueAt(index));
69  }
70 
71  private:
72  const HttpHeaderFields& fields;
73  };
74 
75  HttpHeaders() = default;
76 
77  HttpHeaders(const HttpHeaders& headers)
78  {
79  *this = headers;
80  }
81 
82  Iterator begin() const
83  {
84  return Iterator(*this, 0);
85  }
86 
87  Iterator end() const
88  {
89  return Iterator(*this, count());
90  }
91 
92  using HashMap::operator[];
93 
99  const String& operator[](const String& name) const;
100 
106  String& operator[](const String& name)
107  {
108  return operator[](findOrCreate(name));
109  }
110 
116  String operator[](unsigned index) const
117  {
118  return toString(keyAt(index), valueAt(index));
119  }
120 
121  template <bool is_const> String operator[](const BaseElement<is_const>& elem) const
122  {
123  return toString(elem.key(), elem.value());
124  }
125 
126  using HashMap::contains;
127 
131  bool contains(const String& name) const
132  {
133  return contains(fromString(name));
134  }
135 
136  using HashMap::remove;
137 
144  bool append(const HttpHeaderFieldName& name, const String& value);
145 
146  void remove(const String& name)
147  {
148  remove(fromString(name));
149  }
150 
151  void setMultiple(const HttpHeaders& headers);
152 
154  {
155  clear();
156  setMultiple(headers);
157  return *this;
158  }
159 
160  void clear()
161  {
163  HashMap::clear();
164  }
165 
166  using HashMap::count;
167 
169  {
170  DateTime dt;
171  String strLM = operator[](HTTP_HEADER_LAST_MODIFIED);
172  return dt.fromHttpDate(strLM) ? dt : DateTime();
173  }
174 
176  {
177  DateTime dt;
178  String strSD = operator[](HTTP_HEADER_DATE);
179  return dt.fromHttpDate(strSD) ? dt : DateTime();
180  }
181 };
HttpHeaderFieldName
Definition: HttpHeaderFields.h:84
void size_t const void * key
Definition: blake2s.h:33
Date and time class.
Definition: DateTime.h:136
bool fromHttpDate(const String &httpDate)
Parse a HTTP full date and set time and date.
Definition: WHashMap.h:111
unsigned index
Definition: WHashMap.h:170
Map & map
Definition: WHashMap.h:169
HashMap class template.
Definition: WHashMap.h:42
const String & valueAt(unsigned int idx) const
Definition: WHashMap.h:249
bool contains(const K &key) const
Definition: WHashMap.h:343
const HttpHeaderFieldName & keyAt(unsigned int idx) const
Definition: WHashMap.h:224
unsigned int count() const
Definition: WHashMap.h:210
void remove(const K &key)
Definition: WHashMap.h:374
void clear()
Definition: WHashMap.h:382
Definition: HttpHeaderFields.h:99
void clear()
Definition: HttpHeaderFields.h:148
HttpHeaderFieldName fromString(const String &name) const
Find the enumerated value for the given field name string.
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:138
String toString(HttpHeaderFieldName name) const
Definition: HttpHeaders.h:38
size_t printTo(Print &p) const
String getFieldName() const
HeaderConst(const HttpHeaderFields &fields, const HttpHeaderFieldName &key, const String &value)
Definition: HttpHeaders.h:40
Definition: HttpHeaders.h:54
Iterator(const HttpHeaders &headers, unsigned index)
Definition: HttpHeaders.h:56
HeaderConst operator*() const
Definition: HttpHeaders.h:66
HeaderConst operator*()
Definition: HttpHeaders.h:61
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:35
HttpHeaders()=default
bool contains(const K &key) const
Definition: WHashMap.h:343
DateTime getLastModifiedDate() const
Definition: HttpHeaders.h:168
bool append(const HttpHeaderFieldName &name, const String &value)
Append value to multi-value field.
bool contains(const String &name) const
Determine if given header field is present.
Definition: HttpHeaders.h:131
Iterator end() const
Definition: HttpHeaders.h:87
String operator[](const BaseElement< is_const > &elem) const
Definition: HttpHeaders.h:121
unsigned int count() const
Definition: WHashMap.h:210
void remove(const String &name)
Definition: HttpHeaders.h:146
void clear()
Definition: HttpHeaders.h:160
HttpHeaders & operator=(const HttpHeaders &headers)
Definition: HttpHeaders.h:153
void remove(const K &key)
Definition: WHashMap.h:374
const String & operator[](const String &name) const
Fetch a reference to the header field value by name.
HttpHeaders(const HttpHeaders &headers)
Definition: HttpHeaders.h:77
String operator[](unsigned index) const
Return the HTTP header line for the value at the given index.
Definition: HttpHeaders.h:116
String & operator[](const String &name)
Fetch a reference to the header field value by name.
Definition: HttpHeaders.h:106
DateTime getServerDate() const
Definition: HttpHeaders.h:175
Iterator begin() const
Definition: HttpHeaders.h:82
void setMultiple(const HttpHeaders &headers)
Provides formatted output to stream.
Definition: Print.h:37
The String class.
Definition: WString.h:137