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 
78  bool contains(const String& name) const
79  {
80  return contains(fromString(name));
81  }
82 
83  using HashMap::remove;
84 
91  bool append(const HttpHeaderFieldName& name, const String& value);
92 
93  void remove(const String& name)
94  {
96  }
97 
98  void setMultiple(const HttpHeaders& headers);
99 
100  HttpHeaders& operator=(const HttpHeaders& headers)
101  {
102  clear();
103  setMultiple(headers);
104  return *this;
105  }
106 
107  void clear()
108  {
111  }
112 
113  using HashMap::count;
114 
116  {
118  String strLM = operator[](HTTP_HEADER_LAST_MODIFIED);
119  return dt.fromHttpDate(strLM) ? dt : DateTime();
120  }
121 
122  DateTime getServerDate() const
123  {
125  String strSD = operator[](HTTP_HEADER_DATE);
126  return dt.fromHttpDate(strSD) ? dt : DateTime();
127  }
128 };
DateTime getLastModifiedDate() const
Definition: HttpHeaders.h:132
HashMap class template.
Definition: WHashMap.h:41
void remove(const K &key)
Definition: WHashMap.h:374
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
void clear()
Definition: HttpHeaders.h:124
bool append(const HttpHeaderFieldName &name, const String &value)
Append value to multi-value field.
The String class.
Definition: WString.h:136
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:34
void clear()
Definition: HttpHeaderFields.h:148
String toString(HttpHeaderFieldName name) const
HttpHeaderFieldName
Definition: HttpHeaderFields.h:84
Date and time class.
Definition: DateTime.h:79
const String & operator[](const String &name) const
Fetch a reference to the header field value by name.
void setMultiple(const HttpHeaders &headers)
unsigned int count() const
Definition: WHashMap.h:210
bool contains(const K &key) const
Definition: WHashMap.h:343
Definition: HttpHeaderFields.h:98
const HttpHeaderFieldName & keyAt(unsigned int idx) const
Definition: WHashMap.h:224
HttpHeaderFieldName fromString(const String &name) const
Find the enumerated value for the given field name string.
HttpHeaders & operator=(const HttpHeaders &headers)
Definition: HttpHeaders.h:117
HttpHeaders()=default
void remove(const String &name)
Definition: HttpHeaders.h:110
DateTime getServerDate() const
Definition: HttpHeaders.h:139
const String & valueAt(unsigned int idx) const
Definition: WHashMap.h:249
void clear()
Definition: WHashMap.h:382
bool contains(const String &name) const
Determine if given header field is present.
Definition: HttpHeaders.h:95
bool fromHttpDate(const String &httpDate)
Parse a HTTP full date and set time and date.