2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * AbstractSPRequest.cpp
20 * Abstract base for SPRequest implementations
24 #include "AbstractSPRequest.h"
25 #include "Application.h"
26 #include "ServiceProvider.h"
27 #include "SessionCache.h"
29 #include <log4cpp/Category.hh>
30 #include <saml/util/CGIParser.h>
32 using namespace shibsp;
33 using namespace opensaml;
34 using namespace xmltooling;
35 using namespace log4cpp;
38 AbstractSPRequest::AbstractSPRequest()
39 : m_sp(NULL), m_mapper(NULL), m_app(NULL), m_sessionTried(false), m_session(NULL),
40 m_log(&Category::getInstance(SHIBSP_LOGCAT".SPRequest")), m_parser(NULL)
42 m_sp=SPConfig::getConfig().getServiceProvider();
46 AbstractSPRequest::~AbstractSPRequest()
57 RequestMapper::Settings AbstractSPRequest::getRequestSettings() const
62 // Map request to application and content settings.
63 m_mapper=m_sp->getRequestMapper();
65 return m_settings = m_mapper->getSettings(*this);
69 const Application& AbstractSPRequest::getApplication() const
72 // Now find the application from the URL settings
73 m_app=m_sp->getApplication(getRequestSettings().first->getString("applicationId").second);
75 throw ConfigurationException("Unable to map request to application settings, check configuration.");
80 Session* AbstractSPRequest::getSession() const
82 // Only attempt this once.
85 m_sessionTried = true;
87 // Get session ID from cookie.
88 const Application& app = getApplication();
89 pair<string,const char*> shib_cookie = app.getCookieNameProps("_shibsession_");
90 const char* session_id = getCookie(shib_cookie.first.c_str());
91 if (!session_id || !*session_id)
94 // Need address checking and timeout settings.
97 const PropertySet* props=app.getPropertySet("Sessions");
99 pair<bool,unsigned int> p=props->getUnsignedInt("timeout");
102 pair<bool,bool> pcheck=props->getBool("consistentAddress");
104 consistent = pcheck.second;
107 // The cache will either silently pass a session or NULL back, or throw an exception out.
108 return m_session = getServiceProvider().getSessionCache()->find(
109 session_id, app, consistent ? getRemoteAddr().c_str() : NULL, timeout
113 const char* AbstractSPRequest::getRequestURL() const {
115 // Compute the full target URL
116 int port = getPort();
117 const char* scheme = getScheme();
118 m_url = string(scheme) + "://" + getHostname();
119 if ((!strcmp(scheme,"http") && port!=80) || (!strcmp(scheme,"https") && port!=443)) {
120 ostringstream portstr;
122 m_url += ":" + portstr.str();
124 scheme = getRequestURI();
128 return m_url.c_str();
131 const char* AbstractSPRequest::getParameter(const char* name) const
134 m_parser=new CGIParser(*this);
136 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
137 return (bounds.first==bounds.second) ? NULL : bounds.first->second;
140 vector<const char*>::size_type AbstractSPRequest::getParameters(const char* name, vector<const char*>& values) const
143 m_parser=new CGIParser(*this);
145 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
146 while (bounds.first!=bounds.second) {
147 values.push_back(bounds.first->second);
150 return values.size();
153 const char* AbstractSPRequest::getCookie(const char* name) const
155 if (m_cookieMap.empty()) {
156 string cookies=getHeader("Cookie");
158 string::size_type pos=0,cname,namelen,val,vallen;
159 while (pos !=string::npos && pos < cookies.length()) {
160 while (isspace(cookies[pos])) pos++;
162 pos=cookies.find_first_of("=",pos);
163 if (pos == string::npos)
167 if (pos==cookies.length())
170 pos=cookies.find_first_of(";",pos);
171 if (pos != string::npos) {
174 m_cookieMap.insert(make_pair(cookies.substr(cname,namelen),cookies.substr(val,vallen)));
177 m_cookieMap.insert(make_pair(cookies.substr(cname,namelen),cookies.substr(val)));
180 map<string,string>::const_iterator lookup=m_cookieMap.find(name);
181 return (lookup==m_cookieMap.end()) ? NULL : lookup->second.c_str();
184 const char* AbstractSPRequest::getHandlerURL(const char* resource) const
186 if (!m_handlerURL.empty() && resource && !strcmp(getRequestURL(),resource))
187 return m_handlerURL.c_str();
189 #ifdef HAVE_STRCASECMP
190 if (!resource || (strncasecmp(resource,"http://",7) && strncasecmp(resource,"https://",8)))
192 if (!resource || (strnicmp(resource,"http://",7) && strnicmp(resource,"https://",8)))
194 throw ConfigurationException("Target resource was not an absolute URL.");
197 const char* handler=NULL;
198 const PropertySet* props=m_app->getPropertySet("Sessions");
200 pair<bool,bool> p=props->getBool("handlerSSL");
203 pair<bool,const char*> p2=props->getString("handlerURL");
208 // Should never happen...
209 if (!handler || (*handler!='/' && strncmp(handler,"http:",5) && strncmp(handler,"https:",6)))
210 throw ConfigurationException(
211 "Invalid handlerURL property ($1) in Application ($2)",
212 params(2, handler ? handler : "null", m_app->getId())
215 // The "handlerURL" property can be in one of three formats:
217 // 1) a full URI: http://host/foo/bar
218 // 2) a hostless URI: http:///foo/bar
219 // 3) a relative path: /foo/bar
221 // # Protocol Host Path
222 // 1 handler handler handler
223 // 2 handler resource handler
224 // 3 resource resource handler
226 // note: if ssl_only is true, make sure the protocol is https
228 const char* path = NULL;
230 // Decide whether to use the handler or the resource for the "protocol"
232 if (*handler != '/') {
240 // break apart the "protocol" string into protocol, host, and "the rest"
241 const char* colon=strchr(prot,':');
243 const char* slash=strchr(colon,'/');
247 // Compute the actual protocol and store in member.
249 m_handlerURL.assign("https://");
251 m_handlerURL.assign(prot, colon-prot);
253 // create the "host" from either the colon/slash or from the target string
254 // If prot == handler then we're in either #1 or #2, else #3.
255 // If slash == colon then we're in #2.
256 if (prot != handler || slash == colon) {
257 colon = strchr(resource, ':');
258 colon += 3; // Get past the ://
259 slash = strchr(colon, '/');
261 string host(colon, (slash ? slash-colon : strlen(colon)));
263 // Build the handler URL
264 m_handlerURL += host + path;
265 return m_handlerURL.c_str();
268 void AbstractSPRequest::log(SPLogLevel level, const std::string& msg) const
270 reinterpret_cast<Category*>(m_log)->log(
271 (level == SPDebug ? log4cpp::Priority::DEBUG :
272 (level == SPInfo ? log4cpp::Priority::INFO :
273 (level == SPWarn ? log4cpp::Priority::WARN :
274 (level == SPError ? log4cpp::Priority::ERROR : log4cpp::Priority::CRIT)))),
279 bool AbstractSPRequest::isPriorityEnabled(SPLogLevel level) const
281 return reinterpret_cast<Category*>(m_log)->isPriorityEnabled(
282 (level == SPDebug ? log4cpp::Priority::DEBUG :
283 (level == SPInfo ? log4cpp::Priority::INFO :
284 (level == SPWarn ? log4cpp::Priority::WARN :
285 (level == SPError ? log4cpp::Priority::ERROR : log4cpp::Priority::CRIT))))