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 using namespace shibsp;
30 using namespace opensaml;
31 using namespace xmltooling;
34 AbstractSPRequest::AbstractSPRequest(const char* category)
35 : m_sp(NULL), m_mapper(NULL), m_app(NULL), m_sessionTried(false), m_session(NULL),
36 m_log(&Category::getInstance(category)), m_parser(NULL)
38 m_sp=SPConfig::getConfig().getServiceProvider();
42 AbstractSPRequest::~AbstractSPRequest()
53 RequestMapper::Settings AbstractSPRequest::getRequestSettings() const
58 // Map request to application and content settings.
59 m_mapper=m_sp->getRequestMapper();
61 return m_settings = m_mapper->getSettings(*this);
65 const Application& AbstractSPRequest::getApplication() const
68 // Now find the application from the URL settings
69 m_app=m_sp->getApplication(getRequestSettings().first->getString("applicationId").second);
71 throw ConfigurationException("Unable to map request to application settings, check configuration.");
76 Session* AbstractSPRequest::getSession(bool checkTimeout, bool ignoreAddress, bool cache) const
78 // Only attempt this once.
79 if (cache && m_sessionTried)
82 m_sessionTried = true;
84 // Need address checking and timeout settings.
86 if (checkTimeout || !ignoreAddress) {
87 const PropertySet* props=getApplication().getPropertySet("Sessions");
90 pair<bool,unsigned int> p=props->getUnsignedInt("timeout");
94 pair<bool,bool> pcheck=props->getBool("consistentAddress");
96 ignoreAddress = !pcheck.second;
100 // The cache will either silently pass a session or NULL back, or throw an exception out.
101 Session* session = getServiceProvider().getSessionCache()->find(
102 getApplication(), *this, ignoreAddress ? NULL : getRemoteAddr().c_str(), checkTimeout ? &timeout : NULL
109 static char _x2c(const char *what)
113 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
115 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
119 void AbstractSPRequest::setRequestURI(const char* uri)
121 // Fix for bug 574, secadv 20061002
122 // Unescape URI up to query string delimiter by looking for %XX escapes.
123 // Adapted from Apache's util.c, ap_unescape_url function.
130 else if (*uri == ';') {
131 // If this is Java being stupid, skip everything up to the query string, if any.
132 if (!strncmp(uri, ";jsessionid=", 12)) {
133 if (uri = strchr(uri, '?'))
141 else if (*uri != '%') {
146 if (!isxdigit(*uri) || !isxdigit(*(uri+1)))
147 throw ConfigurationException("Bad request, contained unsupported encoded characters.");
156 const char* AbstractSPRequest::getRequestURL() const
159 // Compute the full target URL
160 int port = getPort();
161 const char* scheme = getScheme();
162 m_url = string(scheme) + "://" + getHostname();
163 if ((!strcmp(scheme,"http") && port!=80) || (!strcmp(scheme,"https") && port!=443)) {
164 ostringstream portstr;
166 m_url += ":" + portstr.str();
170 return m_url.c_str();
173 const char* AbstractSPRequest::getParameter(const char* name) const
176 m_parser=new CGIParser(*this);
178 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
179 return (bounds.first==bounds.second) ? NULL : bounds.first->second;
182 vector<const char*>::size_type AbstractSPRequest::getParameters(const char* name, vector<const char*>& values) const
185 m_parser=new CGIParser(*this);
187 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
188 while (bounds.first!=bounds.second) {
189 values.push_back(bounds.first->second);
192 return values.size();
195 const char* AbstractSPRequest::getHandlerURL(const char* resource) const
198 resource = getRequestURL();
200 if (!m_handlerURL.empty() && resource && !strcmp(getRequestURL(),resource))
201 return m_handlerURL.c_str();
203 #ifdef HAVE_STRCASECMP
204 if (!resource || (strncasecmp(resource,"http://",7) && strncasecmp(resource,"https://",8)))
206 if (!resource || (strnicmp(resource,"http://",7) && strnicmp(resource,"https://",8)))
208 throw ConfigurationException("Target resource was not an absolute URL.");
211 const char* handler=NULL;
212 const PropertySet* props=m_app->getPropertySet("Sessions");
214 pair<bool,bool> p=props->getBool("handlerSSL");
217 pair<bool,const char*> p2=props->getString("handlerURL");
222 // Should never happen...
223 if (!handler || (*handler!='/' && strncmp(handler,"http:",5) && strncmp(handler,"https:",6)))
224 throw ConfigurationException(
225 "Invalid handlerURL property ($1) in Application ($2)",
226 params(2, handler ? handler : "null", m_app->getId())
229 // The "handlerURL" property can be in one of three formats:
231 // 1) a full URI: http://host/foo/bar
232 // 2) a hostless URI: http:///foo/bar
233 // 3) a relative path: /foo/bar
235 // # Protocol Host Path
236 // 1 handler handler handler
237 // 2 handler resource handler
238 // 3 resource resource handler
240 // note: if ssl_only is true, make sure the protocol is https
242 const char* path = NULL;
244 // Decide whether to use the handler or the resource for the "protocol"
246 if (*handler != '/') {
254 // break apart the "protocol" string into protocol, host, and "the rest"
255 const char* colon=strchr(prot,':');
257 const char* slash=strchr(colon,'/');
261 // Compute the actual protocol and store in member.
263 m_handlerURL.assign("https://");
265 m_handlerURL.assign(prot, colon-prot);
267 // create the "host" from either the colon/slash or from the target string
268 // If prot == handler then we're in either #1 or #2, else #3.
269 // If slash == colon then we're in #2.
270 if (prot != handler || slash == colon) {
271 colon = strchr(resource, ':');
272 colon += 3; // Get past the ://
273 slash = strchr(colon, '/');
275 string host(colon, (slash ? slash-colon : strlen(colon)));
277 // Build the handler URL
278 m_handlerURL += host + path;
279 return m_handlerURL.c_str();
282 void AbstractSPRequest::log(SPLogLevel level, const std::string& msg) const
284 reinterpret_cast<Category*>(m_log)->log(
285 (level == SPDebug ? Priority::DEBUG :
286 (level == SPInfo ? Priority::INFO :
287 (level == SPWarn ? Priority::WARN :
288 (level == SPError ? Priority::ERROR : Priority::CRIT)))),
293 bool AbstractSPRequest::isPriorityEnabled(SPLogLevel level) const
295 return reinterpret_cast<Category*>(m_log)->isPriorityEnabled(
296 (level == SPDebug ? Priority::DEBUG :
297 (level == SPInfo ? Priority::INFO :
298 (level == SPWarn ? Priority::WARN :
299 (level == SPError ? Priority::ERROR : Priority::CRIT))))