Incorporating boostisms
[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
33 using namespace xmltooling;
34 using namespace boost;
35 using namespace std;
36
37 GenericRequest::GenericRequest()
38 {
39 }
40
41 GenericRequest::~GenericRequest()
42 {
43 }
44
45 HTTPRequest::HTTPRequest()
46 {
47 }
48
49 HTTPRequest::~HTTPRequest()
50 {
51 }
52
53 bool HTTPRequest::isSecure() const
54 {
55     return strcmp(getScheme(),"https")==0;
56 }
57
58 namespace {
59     void handle_cookie_fn(map<string,string>& cookieMap, vector<string>& nvpair, const string& s) {
60         nvpair.clear();
61         split(nvpair, s, is_any_of("="));
62         if (nvpair.size() == 2) {
63             trim(nvpair[0]);
64             cookieMap[nvpair[0]] = nvpair[1];
65         }
66     }
67 }
68
69 const char* HTTPRequest::getCookie(const char* name) const
70 {
71     if (m_cookieMap.empty()) {
72         string cookies=getHeader("Cookie");
73         vector<string> nvpairs, nvpair;
74         split(nvpairs, cookies, is_any_of(";"), algorithm::token_compress_on);
75         for_each(nvpairs.begin(), nvpairs.end(), boost::bind(handle_cookie_fn, boost::ref(m_cookieMap), boost::ref(nvpair), _1));
76     }
77     map<string,string>::const_iterator lookup=m_cookieMap.find(name);
78     return (lookup==m_cookieMap.end()) ? nullptr : lookup->second.c_str();
79 }