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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 * Interface to HTTP requests.
28 #include "HTTPRequest.h"
29 #include "util/Threads.h"
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>
38 using namespace xmltooling;
39 using namespace xercesc;
40 using namespace boost;
43 bool GenericRequest::m_langFromClient = true;
44 GenericRequest::langrange_t GenericRequest::m_defaultRange;
46 GenericRequest::GenericRequest() : m_langRangeIter(m_langRange.crend())
50 GenericRequest::~GenericRequest()
54 void GenericRequest::setLangDefaults(bool langFromClient, const XMLCh* defaultRange)
56 m_langFromClient = langFromClient;
57 m_defaultRange.clear();
61 XMLStringTokenizer tokens(defaultRange);
62 while (tokens.hasMoreTokens()) {
63 const XMLCh* t = tokens.nextToken();
65 vector<xstring> tagArray;
66 static const XMLCh delims[] = {chDash, chNull};
67 XMLStringTokenizer tags(t, delims);
68 while (tags.hasMoreTokens())
69 tagArray.push_back(tags.nextToken());
70 m_defaultRange.insert(langrange_t::value_type(q, tagArray));
76 bool GenericRequest::startLangMatching() const
78 // This is a no-op except on the first call, to populate the
79 // range information to use in matching.
80 if (m_langRange.empty()) {
81 if (m_langFromClient) {
82 string hdr(getLanguageRange());
83 char_separator<char> sep1(", "); // tags are split by commas or spaces
84 char_separator<char> sep2("; "); // quality is separated by semicolon
85 tokenizer< char_separator<char> > tokens(hdr, sep1);
86 for (tokenizer< char_separator<char> >::iterator t = tokens.begin(); t != tokens.end(); ++t) {
87 string tag = trim_copy(*t); // handle any surrounding ws
88 tokenizer< char_separator<char> > subtokens(tag, sep2);
89 tokenizer< char_separator<char> >::iterator s = subtokens.begin();
90 if (s != subtokens.end() && *s != "*") {
92 auto_ptr_XMLCh lang((s++)->c_str());
94 // Check for quality tag
95 if (s != subtokens.end() && starts_with(*s, "q=")) {
97 q = lexical_cast<float,string>(s->c_str() + 2);
99 catch (bad_lexical_cast&) {
104 // Split range into tokens.
105 vector<xstring> tagArray;
106 static const XMLCh delims[] = {chDash, chNull};
107 XMLStringTokenizer tags(lang.get(), delims);
109 while (tags.hasMoreTokens()) {
110 tag = tags.nextToken();
111 if (*tag != chAsterisk)
112 tagArray.push_back(tag);
115 if (tagArray.empty())
118 // Adjust q using the server priority list. As long as the supplied q deltas are larger than
119 // factors like .0001, the client settings will always trump ours.
120 if (!m_defaultRange.empty()) {
121 float adj = (m_defaultRange.size() + 1) * 0.0001f;
122 for (langrange_t::const_iterator prio = m_defaultRange.begin(); prio != m_defaultRange.end(); ++prio) {
123 if (prio->second == tagArray) {
130 m_langRange.insert(langrange_t::value_type(q, tagArray));
135 m_langRange = m_defaultRange;
139 m_langRangeIter = m_langRange.crbegin();
140 return (m_langRangeIter != m_langRange.crend());
143 bool GenericRequest::continueLangMatching() const
145 return (++m_langRangeIter != m_langRange.crend());
148 bool GenericRequest::matchLang(const XMLCh* tag) const
150 if (m_langRangeIter == m_langRange.crend())
153 // To match against a given range, the range has to be built up and then
154 // truncated segment by segment to look for a match against the tag.
155 // That allows more specific ranges like en-US to match the tag en.
156 // The "end" fence tells us how much of the original range to recompose
157 // into a hyphenated string, and we stop on a match, or when the fence
158 // moves back to the beginning of the array.
160 vector<xstring>::size_type end = m_langRangeIter->second.size();
162 // Skip single-character private extension separators.
163 while (end > 1 && m_langRangeIter->second[end-1].length() <= 1)
165 // Build a range from 0 to end - 1 of segments.
166 xstring compareTo(m_langRangeIter->second[0]);
167 for (vector<xstring>::size_type ix = 1; ix <= end - 1; ++ix)
168 compareTo = compareTo + chDash + m_langRangeIter->second[ix];
169 match = (compareTo.length() > 1 && XMLString::compareIStringASCII(compareTo.c_str(), tag) == 0);
170 } while (!match && --end > 0);
174 HTTPRequest::HTTPRequest()
178 HTTPRequest::~HTTPRequest()
182 bool HTTPRequest::isSecure() const
184 return strcmp(getScheme(),"https")==0;
187 string HTTPRequest::getLanguageRange() const
189 return getHeader("Accept-Language");
193 void handle_cookie_fn(map<string,string>& cookieMap, vector<string>& nvpair, const string& s) {
195 split(nvpair, s, is_any_of("="));
196 if (nvpair.size() == 2) {
198 cookieMap[nvpair[0]] = nvpair[1];
203 const char* HTTPRequest::getCookie(const char* name) const
205 if (m_cookieMap.empty()) {
206 string cookies=getHeader("Cookie");
207 vector<string> nvpair;
208 tokenizer< char_separator<char> > nvpairs(cookies, char_separator<char>(";"));
209 for_each(nvpairs.begin(), nvpairs.end(), boost::bind(handle_cookie_fn, boost::ref(m_cookieMap), boost::ref(nvpair), _1));
211 map<string,string>::const_iterator lookup=m_cookieMap.find(name);
212 return (lookup==m_cookieMap.end()) ? nullptr : lookup->second.c_str();