Update build revision
[shibboleth/cpp-xmltooling.git] / xmltooling / io / HTTPRequest.cpp
index 6f97384..e4f5038 100644 (file)
@@ -34,6 +34,7 @@
 #include <boost/lexical_cast.hpp>
 #include <boost/tokenizer.hpp>
 #include <xercesc/util/XMLStringTokenizer.hpp>
+#include <xercesc/util/XMLUniDefs.hpp>
 
 using namespace xmltooling;
 using namespace xercesc;
@@ -43,7 +44,7 @@ using namespace std;
 bool GenericRequest::m_langFromClient = true;
 GenericRequest::langrange_t GenericRequest::m_defaultRange;
 
-GenericRequest::GenericRequest() : m_langRangeIter(m_langRange.crend())
+GenericRequest::GenericRequest() : m_langRangeIter(m_langRange.rend())
 {
 }
 
@@ -51,6 +52,25 @@ GenericRequest::~GenericRequest()
 {
 }
 
+bool GenericRequest::isDefaultPort() const
+{
+    return false;
+}
+
+void GenericRequest::absolutize(string& url) const
+{
+    if (url.empty())
+        url = '/';
+    if (url[0] == '/') {
+        // Compute a URL to the root of the site.
+        const char* scheme = getScheme();
+        string root = string(scheme) + "://" + getHostname();
+        if (!isDefaultPort())
+            root += ":" + lexical_cast<string>(getPort());
+        url = root + url;
+    }
+}
+
 void GenericRequest::setLangDefaults(bool langFromClient, const XMLCh* defaultRange)
 {
     m_langFromClient = langFromClient;
@@ -137,17 +157,17 @@ bool GenericRequest::startLangMatching() const
     }
     
     m_langRangeIter = m_langRange.rbegin();
-    return (m_langRangeIter != m_langRange.rend());
+    return (m_langRangeIter != const_cast<const langrange_t&>(m_langRange).rend());
 }
 
 bool GenericRequest::continueLangMatching() const
 {
-    return (++m_langRangeIter != m_langRange.rend());
+    return (++m_langRangeIter != const_cast<const langrange_t&>(m_langRange).rend());
 }
 
 bool GenericRequest::matchLang(const XMLCh* tag) const
 {
-    if (m_langRangeIter == m_langRange.rend())
+    if (m_langRangeIter == const_cast<const langrange_t&>(m_langRange).rend())
         return false;
 
     // To match against a given range, the range has to be built up and then
@@ -184,6 +204,14 @@ bool HTTPRequest::isSecure() const
     return strcmp(getScheme(),"https")==0;
 }
 
+bool HTTPRequest::isDefaultPort() const
+{
+    if (isSecure())
+        return getPort() == 443;
+    else
+        return getPort() == 80;
+}
+
 string HTTPRequest::getLanguageRange() const
 {
     return getHeader("Accept-Language");
@@ -200,7 +228,7 @@ namespace {
     }
 }
 
-const char* HTTPRequest::getCookie(const char* name) const
+const map<string,string>& HTTPRequest::getCookies() const
 {
     if (m_cookieMap.empty()) {
         string cookies=getHeader("Cookie");
@@ -208,6 +236,11 @@ const char* HTTPRequest::getCookie(const char* name) const
         tokenizer< char_separator<char> > nvpairs(cookies, char_separator<char>(";"));
         for_each(nvpairs.begin(), nvpairs.end(), boost::bind(handle_cookie_fn, boost::ref(m_cookieMap), boost::ref(nvpair), _1));
     }
-    map<string,string>::const_iterator lookup=m_cookieMap.find(name);
+    return m_cookieMap;
+}
+
+const char* HTTPRequest::getCookie(const char* name) const
+{
+    map<string,string>::const_iterator lookup = getCookies().find(name);
     return (lookup==m_cookieMap.end()) ? nullptr : lookup->second.c_str();
 }