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  HttpHeaders() = default;
38 
39  HttpHeaders(const HttpHeaders& headers)
40  {
41  *this = headers;
42  }
43 
44  using HashMap::operator[];
45 
51  const String& operator[](const String& name) const;
52 
58  String& operator[](const String& name)
59  {
60  return operator[](findOrCreate(name));
61  }
62 
68  String operator[](unsigned index) const
69  {
70  return toString(keyAt(index), valueAt(index));
71  }
72 
73  using HashMap::contains;
74 
75  bool contains(const String& name) const
76  {
77  return contains(fromString(name));
78  }
79 
80  using HashMap::remove;
81 
88  bool append(const HttpHeaderFieldName& name, const String& value);
89 
90  void remove(const String& name)
91  {
92  remove(fromString(name));
93  }
94 
95  void setMultiple(const HttpHeaders& headers);
96 
98  {
99  clear();
100  setMultiple(headers);
101  return *this;
102  }
103 
104  void clear()
105  {
107  HashMap::clear();
108  }
109 
110  using HashMap::count;
111 
113  {
114  DateTime dt;
115  String strLM = operator[](HTTP_HEADER_LAST_MODIFIED);
116  return dt.fromHttpDate(strLM) ? dt : DateTime();
117  }
118 
120  {
121  DateTime dt;
122  String strSD = operator[](HTTP_HEADER_DATE);
123  return dt.fromHttpDate(strSD) ? dt : DateTime();
124  }
125 };
Date and time class.
Definition: DateTime.h:77
bool contains(const String &name) const
Definition: HttpHeaders.h:75
void clear()
Definition: HttpHeaders.h:104
HttpHeaderFieldName fromString(const String &name) const
Find the enumerated value for the given field name string.
HashMap class template.
Definition: WHashMap.h:39
void clear()
Definition: WHashMap.h:425
const HttpHeaderFieldName & keyAt(unsigned int idx) const
Definition: WHashMap.h:185
void remove(const K &key)
Definition: WHashMap.h:305
HttpHeaders(const HttpHeaders &headers)
Definition: HttpHeaders.h:39
String operator[](unsigned index) const
Return the HTTP header line for the value at the given index.
Definition: HttpHeaders.h:68
The String class.
Definition: WString.h:136
String & operator[](const String &name)
Fetch a reference to the header field value by name.
Definition: HttpHeaders.h:58
bool append(const HttpHeaderFieldName &name, const String &value)
Append value to multi-value field.
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:34
const String & valueAt(unsigned int idx) const
Definition: WHashMap.h:210
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
const String & operator[](const String &name) const
Fetch a reference to the header field value by name.
void setMultiple(const HttpHeaders &headers)
HttpHeaders & operator=(const HttpHeaders &headers)
Definition: HttpHeaders.h:97
HttpHeaders()=default
DateTime getServerDate() const
Definition: HttpHeaders.h:119
DateTime getLastModifiedDate() const
Definition: HttpHeaders.h:112
HttpHeaderFieldName
Definition: HttpHeaderFields.h:83
bool contains(const K &key) const
Definition: WHashMap.h:284
bool fromHttpDate(const String &httpDate)
Parse a HTTP full date and set time and date.
String toString(HttpHeaderFieldName name) const
unsigned int count() const
Definition: WHashMap.h:171