More boostisms, add string tokenizer
[shibboleth/cpp-xmltooling.git] / xmltooling / io / HTTPRequest.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * HTTPRequest.cpp
23  * 
24  * Interface to HTTP requests.
25  */
26
27 #include "internal.h"
28 #include "HTTPRequest.h"
29
30 #include <boost/algorithm/string.hpp>
31 #include <boost/bind.hpp>
32 #include <boost/tokenizer.hpp>
33
34 using namespace xmltooling;
35 using namespace boost;
36 using namespace std;
37
38 GenericRequest::GenericRequest()
39 {
40 }
41
42 GenericRequest::~GenericRequest()
43 {
44 }
45
46 HTTPRequest::HTTPRequest()
47 {
48 }
49
50 HTTPRequest::~HTTPRequest()
51 {
52 }
53
54 bool HTTPRequest::isSecure() const
55 {
56     return strcmp(getScheme(),"https")==0;
57 }
58
59 namespace {
60     void handle_cookie_fn(map<string,string>& cookieMap, vector<string>& nvpair, const string& s) {
61         nvpair.clear();
62         split(nvpair, s, is_any_of("="));
63         if (nvpair.size() == 2) {
64             trim(nvpair[0]);
65             cookieMap[nvpair[0]] = nvpair[1];
66         }
67     }
68 }
69
70 const char* HTTPRequest::getCookie(const char* name) const
71 {
72     if (m_cookieMap.empty()) {
73         string cookies=getHeader("Cookie");
74         vector<string> nvpair;
75         tokenizer< char_separator<char> > nvpairs(cookies, char_separator<char>(";"));
76         for_each(nvpairs.begin(), nvpairs.end(), boost::bind(handle_cookie_fn, boost::ref(m_cookieMap), boost::ref(nvpair), _1));
77     }
78     map<string,string>::const_iterator lookup=m_cookieMap.find(name);
79     return (lookup==m_cookieMap.end()) ? nullptr : lookup->second.c_str();
80 }