File tree 3 files changed +38
-3
lines changed
3 files changed +38
-3
lines changed Original file line number Diff line number Diff line change 1
1
#include " HTTPHeader.hpp"
2
2
3
+ #include < locale>
4
+ #include < ostream>
5
+ #include < sstream>
6
+
3
7
namespace httpsserver {
4
8
5
9
HTTPHeader::HTTPHeader (const std::string &name, const std::string &value):
6
- _name (name),
10
+ _name (normalizeHeaderName( name) ),
7
11
_value (value) {
8
12
9
13
}
@@ -16,4 +20,24 @@ std::string HTTPHeader::print() {
16
20
return _name + " : " + _value;
17
21
}
18
22
23
+ std::string normalizeHeaderName (std::string const &name) {
24
+ std::locale loc;
25
+ std::stringbuf buf;
26
+ std::ostream oBuf (&buf);
27
+ bool upper = true ;
28
+ std::string::size_type len = name.length ();
29
+ for (std::string::size_type i = 0 ; i < len; ++i) {
30
+ if (upper) {
31
+ oBuf << std::toupper (name[i], loc);
32
+ upper = false ;
33
+ } else {
34
+ oBuf << std::tolower (name[i], loc);
35
+ if (!std::isalnum (name[i], loc)) {
36
+ upper=true ;
37
+ }
38
+ }
39
+ }
40
+ return buf.str ();
41
+ }
42
+
19
43
} /* namespace httpsserver */
Original file line number Diff line number Diff line change @@ -18,6 +18,15 @@ class HTTPHeader {
18
18
std::string print ();
19
19
};
20
20
21
+ /* *
22
+ * \brief Normalizes case in header names
23
+ *
24
+ * It converts the first letter and every letter after a non-alnum character
25
+ * to uppercase. For example, "content-length" becomes "Content-Length" and
26
+ * "HOST" becomes "Host".
27
+ */
28
+ std::string normalizeHeaderName (std::string const &name);
29
+
21
30
} /* namespace httpsserver */
22
31
23
32
#endif /* SRC_HTTPHEADER_HPP_ */
Original file line number Diff line number Diff line change @@ -13,17 +13,19 @@ HTTPHeaders::~HTTPHeaders() {
13
13
}
14
14
15
15
HTTPHeader * HTTPHeaders::get (std::string const &name) {
16
+ std::string normalizedName = normalizeHeaderName (name);
16
17
for (std::vector<HTTPHeader*>::iterator header = _headers->begin (); header != _headers->end (); ++header) {
17
- if ((*header)->_name .compare (name )==0 ) {
18
+ if ((*header)->_name .compare (normalizedName )==0 ) {
18
19
return (*header);
19
20
}
20
21
}
21
22
return NULL ;
22
23
}
23
24
24
25
std::string HTTPHeaders::getValue (std::string const &name) {
26
+ std::string normalizedName = normalizeHeaderName (name);
25
27
for (std::vector<HTTPHeader*>::iterator header = _headers->begin (); header != _headers->end (); ++header) {
26
- if ((*header)->_name .compare (name )==0 ) {
28
+ if ((*header)->_name .compare (normalizedName )==0 ) {
27
29
return ((*header)->_value );
28
30
}
29
31
}
You can’t perform that action at this time.
0 commit comments