1 // Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "libcef/common/net/http_header_utils.h"
6
7 #include <algorithm>
8
9 #include "base/strings/string_util.h"
10 #include "net/http/http_response_headers.h"
11 #include "net/http/http_util.h"
12
13 using net::HttpResponseHeaders;
14
15 namespace HttpHeaderUtils {
16
GenerateHeaders(const HeaderMap & map)17 std::string GenerateHeaders(const HeaderMap& map) {
18 std::string headers;
19
20 for (HeaderMap::const_iterator header = map.begin(); header != map.end();
21 ++header) {
22 const CefString& key = header->first;
23 const CefString& value = header->second;
24
25 if (!key.empty()) {
26 // Delimit with "\r\n".
27 if (!headers.empty())
28 headers += "\r\n";
29
30 headers += std::string(key) + ": " + std::string(value);
31 }
32 }
33
34 return headers;
35 }
36
ParseHeaders(const std::string & header_str,HeaderMap & map)37 void ParseHeaders(const std::string& header_str, HeaderMap& map) {
38 // Parse the request header values
39 for (net::HttpUtil::HeadersIterator i(header_str.begin(), header_str.end(),
40 "\n\r");
41 i.GetNext();) {
42 map.insert(std::make_pair(i.name(), i.values()));
43 }
44 }
45
MakeASCIILower(std::string * str)46 void MakeASCIILower(std::string* str) {
47 std::transform(str->begin(), str->end(), str->begin(), ::tolower);
48 }
49
FindHeaderInMap(const std::string & nameLower,HeaderMap & map)50 HeaderMap::iterator FindHeaderInMap(const std::string& nameLower,
51 HeaderMap& map) {
52 for (auto it = map.begin(); it != map.end(); ++it) {
53 if (base::EqualsCaseInsensitiveASCII(it->first.ToString(), nameLower))
54 return it;
55 }
56
57 return map.end();
58 }
59
60 } // namespace HttpHeaderUtils
61