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  {
53  auto field = fromString(name);
54  if(field == HTTP_HEADER_UNKNOWN) {
55  return nil;
56  }
57  return operator[](field);
58  }
59 
65  String& operator[](const String& name)
66  {
67  return operator[](findOrCreate(name));
68  }
69 
75  String operator[](unsigned index) const
76  {
77  return toString(keyAt(index), valueAt(index));
78  }
79 
80  using HashMap::contains;
81 
82  bool contains(const String& name) const
83  {
84  return contains(fromString(name));
85  }
86 
87  using HashMap::remove;
88 
89  void remove(const String& name)
90  {
91  remove(fromString(name));
92  }
93 
94  void setMultiple(const HttpHeaders& headers)
95  {
96  for(unsigned i = 0; i < headers.count(); i++) {
97  HttpHeaderFieldName fieldName = headers.keyAt(i);
98  auto fieldNameString = headers.toString(fieldName);
99  operator[](fieldNameString) = headers.valueAt(i);
100  }
101  }
102 
104  {
105  clear();
106  setMultiple(headers);
107  return *this;
108  }
109 
110  void clear()
111  {
113  HashMap::clear();
114  }
115 
116  using HashMap::count;
117 
119  {
120  DateTime dt;
121  String strLM = operator[](HTTP_HEADER_LAST_MODIFIED);
122  return dt.fromHttpDate(strLM) ? dt : DateTime();
123  }
124 
126  {
127  DateTime dt;
128  String strSD = operator[](HTTP_HEADER_DATE);
129  return dt.fromHttpDate(strSD) ? dt : DateTime();
130  }
131 };
Date and time class.
Definition: DateTime.h:77
bool contains(const String &name) const
Definition: HttpHeaders.h:82
void clear()
Definition: HttpHeaders.h:110
HttpHeaderFieldName fromString(const String &name) const
Find the enumerated value for the given field name string.
HashMap class template.
Definition: WHashMap.h:38
void clear()
Definition: WHashMap.h:424
const HttpHeaderFieldName & keyAt(unsigned int idx) const
Definition: WHashMap.h:184
void remove(const K &key)
Definition: WHashMap.h:304
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:75
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:65
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:34
const String & valueAt(unsigned int idx) const
Definition: WHashMap.h:209
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:116
Definition: HttpHeaderFields.h:87
void clear()
Definition: HttpHeaderFields.h:126
const String & operator[](const String &name) const
Fetch a reference to the header field value by name.
Definition: HttpHeaders.h:51
void setMultiple(const HttpHeaders &headers)
Definition: HttpHeaders.h:94
HttpHeaders & operator=(const HttpHeaders &headers)
Definition: HttpHeaders.h:103
String nil
Definition: WHashMap.h:344
HttpHeaders()=default
DateTime getServerDate() const
Definition: HttpHeaders.h:125
DateTime getLastModifiedDate() const
Definition: HttpHeaders.h:118
HttpHeaderFieldName
Definition: HttpHeaderFields.h:73
bool contains(const K &key) const
Definition: WHashMap.h:283
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:170