Expose cookie map
[shibboleth/xmltooling.git] / xmltooling / io / HTTPRequest.cpp
index a068a58..f11e76f 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;
@@ -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;
@@ -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();
 }