Merge branch '1.x' of ssh://authdev.it.ohio-state.edu/~scantor/git/cpp-xmltooling...
[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 using namespace xmltooling;
31 using namespace std;
32
33 GenericRequest::GenericRequest()
34 {
35 }
36
37 GenericRequest::~GenericRequest()
38 {
39 }
40
41 HTTPRequest::HTTPRequest()
42 {
43 }
44
45 HTTPRequest::~HTTPRequest()
46 {
47 }
48
49 bool HTTPRequest::isSecure() const
50 {
51     return strcmp(getScheme(),"https")==0;
52 }
53
54 const char* HTTPRequest::getCookie(const char* name) const
55 {
56     if (m_cookieMap.empty()) {
57         string cookies=getHeader("Cookie");
58
59         string::size_type pos=0,cname,namelen,val,vallen;
60         while (pos !=string::npos && pos < cookies.length()) {
61             while (isspace(cookies[pos])) pos++;
62             cname=pos;
63             pos=cookies.find_first_of("=",pos);
64             if (pos == string::npos)
65                 break;
66             namelen=pos-cname;
67             pos++;
68             if (pos==cookies.length())
69                 break;
70             val=pos;
71             pos=cookies.find_first_of(";",pos);
72             if (pos != string::npos) {
73                 vallen=pos-val;
74                 pos++;
75                 m_cookieMap.insert(make_pair(cookies.substr(cname,namelen),cookies.substr(val,vallen)));
76             }
77             else
78                 m_cookieMap.insert(make_pair(cookies.substr(cname,namelen),cookies.substr(val)));
79         }
80     }
81     map<string,string>::const_iterator lookup=m_cookieMap.find(name);
82     return (lookup==m_cookieMap.end()) ? nullptr : lookup->second.c_str();
83 }