Add default port method to request API
[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 #include "util/Threads.h"
30
31 #include <cstring>
32 #include <boost/algorithm/string.hpp>
33 #include <boost/bind.hpp>
34 #include <boost/lexical_cast.hpp>
35 #include <boost/tokenizer.hpp>
36 #include <xercesc/util/XMLStringTokenizer.hpp>
37
38 using namespace xmltooling;
39 using namespace xercesc;
40 using namespace boost;
41 using namespace std;
42
43 bool GenericRequest::m_langFromClient = true;
44 GenericRequest::langrange_t GenericRequest::m_defaultRange;
45
46 GenericRequest::GenericRequest() : m_langRangeIter(m_langRange.rend())
47 {
48 }
49
50 GenericRequest::~GenericRequest()
51 {
52 }
53
54 bool GenericRequest::isDefaultPort() const
55 {
56     return false;
57 }
58
59 void GenericRequest::setLangDefaults(bool langFromClient, const XMLCh* defaultRange)
60 {
61     m_langFromClient = langFromClient;
62     m_defaultRange.clear();
63     if (!defaultRange)
64         return;
65     float q = 0.0f;
66     XMLStringTokenizer tokens(defaultRange);
67     while (tokens.hasMoreTokens()) {
68         const XMLCh* t = tokens.nextToken();
69         if (t && *t) {
70             vector<xstring> tagArray;
71             static const XMLCh delims[] = {chDash, chNull};
72             XMLStringTokenizer tags(t, delims);
73             while (tags.hasMoreTokens())
74                 tagArray.push_back(tags.nextToken());
75             m_defaultRange.insert(langrange_t::value_type(q, tagArray));
76             q -= 0.0001f;
77         }
78     }
79 }
80
81 bool GenericRequest::startLangMatching() const
82 {
83     // This is a no-op except on the first call, to populate the
84     // range information to use in matching.
85     if (m_langRange.empty()) {
86         if (m_langFromClient) {
87             string hdr(getLanguageRange());
88             char_separator<char> sep1(", "); // tags are split by commas or spaces
89             char_separator<char> sep2("; "); // quality is separated by semicolon
90             tokenizer< char_separator<char> > tokens(hdr, sep1);
91             for (tokenizer< char_separator<char> >::iterator t = tokens.begin(); t != tokens.end(); ++t) {
92                 string tag = trim_copy(*t);   // handle any surrounding ws
93                 tokenizer< char_separator<char> > subtokens(tag, sep2);
94                 tokenizer< char_separator<char> >::iterator s = subtokens.begin();
95                 if (s != subtokens.end() && *s != "*") {
96                     float q = 1.0f;
97                     auto_ptr_XMLCh lang((s++)->c_str());
98
99                     // Check for quality tag
100                     if (s != subtokens.end() && starts_with(*s, "q=")) {
101                         try {
102                             q = lexical_cast<float,string>(s->c_str() + 2);
103                         }
104                         catch (bad_lexical_cast&) {
105                             q = 0.0f;
106                         }
107                     }
108
109                     // Split range into tokens.
110                     vector<xstring> tagArray;
111                     static const XMLCh delims[] = {chDash, chNull};
112                     XMLStringTokenizer tags(lang.get(), delims);
113                     const XMLCh* tag;
114                     while (tags.hasMoreTokens()) {
115                         tag = tags.nextToken();
116                         if (*tag != chAsterisk)
117                             tagArray.push_back(tag);
118                     }
119
120                     if (tagArray.empty())
121                         continue;
122
123                     // Adjust q using the server priority list. As long as the supplied q deltas are larger than
124                     // factors like .0001, the client settings will always trump ours.
125                     if (!m_defaultRange.empty()) {
126                         float adj = (m_defaultRange.size() + 1) * 0.0001f;
127                         for (langrange_t::const_iterator prio = m_defaultRange.begin(); prio != m_defaultRange.end(); ++prio) {
128                             if (prio->second == tagArray) {
129                                 adj = prio->first;
130                                 break;
131                             }
132                         }
133                         q -= adj;
134                     }
135                     m_langRange.insert(langrange_t::value_type(q, tagArray));
136                 }
137             }
138         }
139         else {
140             m_langRange = m_defaultRange;
141         }
142     }
143     
144     m_langRangeIter = m_langRange.rbegin();
145     return (m_langRangeIter != m_langRange.rend());
146 }
147
148 bool GenericRequest::continueLangMatching() const
149 {
150     return (++m_langRangeIter != m_langRange.rend());
151 }
152
153 bool GenericRequest::matchLang(const XMLCh* tag) const
154 {
155     if (m_langRangeIter == m_langRange.rend())
156         return false;
157
158     // To match against a given range, the range has to be built up and then
159     // truncated segment by segment to look for a match against the tag.
160     // That allows more specific ranges like en-US to match the tag en.
161     // The "end" fence tells us how much of the original range to recompose
162     // into a hyphenated string, and we stop on a match, or when the fence
163     // moves back to the beginning of the array.
164     bool match = false;
165     vector<xstring>::size_type end = m_langRangeIter->second.size();
166     do {
167         // Skip single-character private extension separators.
168         while (end > 1 && m_langRangeIter->second[end-1].length() <= 1)
169             --end;
170         // Build a range from 0 to end - 1 of segments.
171         xstring compareTo(m_langRangeIter->second[0]);
172         for (vector<xstring>::size_type ix = 1; ix <= end - 1; ++ix)
173             compareTo = compareTo + chDash + m_langRangeIter->second[ix];
174         match = (compareTo.length() > 1 && XMLString::compareIStringASCII(compareTo.c_str(), tag) == 0);
175     } while (!match && --end > 0);
176     return match;
177 }
178
179 HTTPRequest::HTTPRequest()
180 {
181 }
182
183 HTTPRequest::~HTTPRequest()
184 {
185 }
186
187 bool HTTPRequest::isSecure() const
188 {
189     return strcmp(getScheme(),"https")==0;
190 }
191
192 bool HTTPRequest::isDefaultPort() const
193 {
194     if (isSecure())
195         return getPort() == 443;
196     else
197         return getPort() == 80;
198 }
199
200 string HTTPRequest::getLanguageRange() const
201 {
202     return getHeader("Accept-Language");
203 }
204
205 namespace {
206     void handle_cookie_fn(map<string,string>& cookieMap, vector<string>& nvpair, const string& s) {
207         nvpair.clear();
208         split(nvpair, s, is_any_of("="));
209         if (nvpair.size() == 2) {
210             trim(nvpair[0]);
211             cookieMap[nvpair[0]] = nvpair[1];
212         }
213     }
214 }
215
216 const char* HTTPRequest::getCookie(const char* name) const
217 {
218     if (m_cookieMap.empty()) {
219         string cookies=getHeader("Cookie");
220         vector<string> nvpair;
221         tokenizer< char_separator<char> > nvpairs(cookies, char_separator<char>(";"));
222         for_each(nvpairs.begin(), nvpairs.end(), boost::bind(handle_cookie_fn, boost::ref(m_cookieMap), boost::ref(nvpair), _1));
223     }
224     map<string,string>::const_iterator lookup=m_cookieMap.find(name);
225     return (lookup==m_cookieMap.end()) ? nullptr : lookup->second.c_str();
226 }