Form generator for discovery along with sample form.
[shibboleth/sp.git] / shibsp / AbstractSPRequest.cpp
index 85dfe25..e7de1cd 100644 (file)
 #include "Application.h"
 #include "ServiceProvider.h"
 #include "SessionCache.h"
-#include "util/CGIParser.h"
-
-#include <log4cpp/Category.hh>
 
 using namespace shibsp;
+using namespace opensaml;
 using namespace xmltooling;
-using namespace log4cpp;
 using namespace std;
 
-AbstractSPRequest::AbstractSPRequest()
+AbstractSPRequest::AbstractSPRequest(const char* category)
     : m_sp(NULL), m_mapper(NULL), m_app(NULL), m_sessionTried(false), m_session(NULL),
-        m_log(&Category::getInstance(SHIBSP_LOGCAT)), m_parser(NULL)
+        m_log(&Category::getInstance(category)), m_parser(NULL)
 {
     m_sp=SPConfig::getConfig().getServiceProvider();
     m_sp->lock();
@@ -76,40 +73,88 @@ const Application& AbstractSPRequest::getApplication() const
     return *m_app;
 }
 
-Session* AbstractSPRequest::getSession() const
+Session* AbstractSPRequest::getSession(bool checkTimeout, bool ignoreAddress, bool cache) const
 {
     // Only attempt this once.
-    if (m_sessionTried)
+    if (cache && m_sessionTried)
         return m_session;
-    m_sessionTried = true;
-
-    // Get session ID from cookie.
-    const Application& app = getApplication();
-    pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
-    const char* session_id = getCookie(shib_cookie.first.c_str());
-    if (!session_id || !*session_id)
-        return NULL;
+    else if (cache)
+        m_sessionTried = true;
 
     // Need address checking and timeout settings.
-    int timeout=0;
-    bool consistent=true;
-    const PropertySet* props=app.getPropertySet("Sessions");
-    if (props) {
-        pair<bool,unsigned int> p=props->getUnsignedInt("timeout");
-        if (p.first)
-            timeout = p.second;
-        pair<bool,bool> pcheck=props->getBool("consistentAddress");
-        if (pcheck.first)
-            consistent = pcheck.second;
+    time_t timeout=0;
+    if (checkTimeout || !ignoreAddress) {
+        const PropertySet* props=getApplication().getPropertySet("Sessions");
+        if (props) {
+            if (checkTimeout) {
+                pair<bool,unsigned int> p=props->getUnsignedInt("timeout");
+                if (p.first)
+                    timeout = p.second;
+            }
+            pair<bool,bool> pcheck=props->getBool("consistentAddress");
+            if (pcheck.first)
+                ignoreAddress = !pcheck.second;
+        }
     }
 
     // The cache will either silently pass a session or NULL back, or throw an exception out.
-    return m_session = getServiceProvider().getSessionCache()->find(
-        session_id, app, consistent ? getRemoteAddr().c_str() : NULL, timeout
+    Session* session = getServiceProvider().getSessionCache()->find(
+        getApplication(), *this, ignoreAddress ? NULL : getRemoteAddr().c_str(), checkTimeout ? &timeout : NULL
         );
+    if (cache)
+        m_session = session;
+    return session;
+}
+
+static char _x2c(const char *what)
+{
+    register char digit;
+
+    digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
+    digit *= 16;
+    digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
+    return(digit);
 }
 
-const char* AbstractSPRequest::getRequestURL() const {
+void AbstractSPRequest::setRequestURI(const char* uri)
+{
+    // Fix for bug 574, secadv 20061002
+    // Unescape URI up to query string delimiter by looking for %XX escapes.
+    // Adapted from Apache's util.c, ap_unescape_url function.
+    if (uri) {
+        while (*uri) {
+            if (*uri == '?') {
+                m_uri += uri;
+                break;
+            }
+            else if (*uri == ';') {
+                // If this is Java being stupid, skip everything up to the query string, if any.
+                if (!strncmp(uri, ";jsessionid=", 12)) {
+                    if (uri = strchr(uri, '?'))
+                        m_uri += uri;
+                    break;
+                }
+                else {
+                    m_uri += *uri;
+                }
+            }
+            else if (*uri != '%') {
+                m_uri += *uri;
+            }
+            else {
+                ++uri;
+                if (!isxdigit(*uri) || !isxdigit(*(uri+1)))
+                    throw ConfigurationException("Bad request, contained unsupported encoded characters.");
+                m_uri += _x2c(uri);
+                ++uri;
+            }
+            ++uri;
+        }
+    }
+}
+
+const char* AbstractSPRequest::getRequestURL() const
+{
     if (m_url.empty()) {
         // Compute the full target URL
         int port = getPort();
@@ -120,9 +165,7 @@ const char* AbstractSPRequest::getRequestURL() const {
             portstr << port;
             m_url += ":" + portstr.str();
         }
-        scheme = getRequestURI();
-        if (scheme)
-            m_url += scheme;
+        m_url += m_uri;
     }
     return m_url.c_str();
 }
@@ -149,39 +192,11 @@ vector<const char*>::size_type AbstractSPRequest::getParameters(const char* name
     return values.size();
 }
 
-const char* AbstractSPRequest::getCookie(const char* name) const
-{
-    if (m_cookieMap.empty()) {
-        string cookies=getHeader("Cookie");
-
-        string::size_type pos=0,cname,namelen,val,vallen;
-        while (pos !=string::npos && pos < cookies.length()) {
-            while (isspace(cookies[pos])) pos++;
-            cname=pos;
-            pos=cookies.find_first_of("=",pos);
-            if (pos == string::npos)
-                break;
-            namelen=pos-cname;
-            pos++;
-            if (pos==cookies.length())
-                break;
-            val=pos;
-            pos=cookies.find_first_of(";",pos);
-            if (pos != string::npos) {
-                vallen=pos-val;
-                pos++;
-                m_cookieMap.insert(make_pair(cookies.substr(cname,namelen),cookies.substr(val,vallen)));
-            }
-            else
-                m_cookieMap.insert(make_pair(cookies.substr(cname,namelen),cookies.substr(val)));
-        }
-    }
-    map<string,string>::const_iterator lookup=m_cookieMap.find(name);
-    return (lookup==m_cookieMap.end()) ? NULL : lookup->second.c_str();
-}
-
 const char* AbstractSPRequest::getHandlerURL(const char* resource) const
 {
+    if (!resource)
+        resource = getRequestURL();
+
     if (!m_handlerURL.empty() && resource && !strcmp(getRequestURL(),resource))
         return m_handlerURL.c_str();
         
@@ -267,10 +282,10 @@ const char* AbstractSPRequest::getHandlerURL(const char* resource) const
 void AbstractSPRequest::log(SPLogLevel level, const std::string& msg) const
 {
     reinterpret_cast<Category*>(m_log)->log(
-        (level == SPDebug ? log4cpp::Priority::DEBUG :
-        (level == SPInfo ? log4cpp::Priority::INFO :
-        (level == SPWarn ? log4cpp::Priority::WARN :
-        (level == SPError ? log4cpp::Priority::ERROR : log4cpp::Priority::CRIT)))),
+        (level == SPDebug ? Priority::DEBUG :
+        (level == SPInfo ? Priority::INFO :
+        (level == SPWarn ? Priority::WARN :
+        (level == SPError ? Priority::ERROR : Priority::CRIT)))),
         msg
         );
 }
@@ -278,9 +293,9 @@ void AbstractSPRequest::log(SPLogLevel level, const std::string& msg) const
 bool AbstractSPRequest::isPriorityEnabled(SPLogLevel level) const
 {
     return reinterpret_cast<Category*>(m_log)->isPriorityEnabled(
-        (level == SPDebug ? log4cpp::Priority::DEBUG :
-        (level == SPInfo ? log4cpp::Priority::INFO :
-        (level == SPWarn ? log4cpp::Priority::WARN :
-        (level == SPError ? log4cpp::Priority::ERROR : log4cpp::Priority::CRIT))))
+        (level == SPDebug ? Priority::DEBUG :
+        (level == SPInfo ? Priority::INFO :
+        (level == SPWarn ? Priority::WARN :
+        (level == SPError ? Priority::ERROR : Priority::CRIT))))
         );
 }