2 * Copyright 2001-2010 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 "exceptions.h"
25 #include "AbstractSPRequest.h"
26 #include "Application.h"
27 #include "GSSRequest.h"
28 #include "ServiceProvider.h"
29 #include "SessionCache.h"
30 #include "util/CGIParser.h"
32 using namespace shibsp;
33 using namespace opensaml;
34 using namespace xmltooling;
37 SPRequest::SPRequest()
41 SPRequest::~SPRequest()
45 string SPRequest::getSecureHeader(const char* name) const
47 return getHeader(name);
50 void SPRequest::setAuthType(const char* authtype)
55 GSSRequest::GSSRequest()
59 GSSRequest::~GSSRequest()
64 AbstractSPRequest::AbstractSPRequest(const char* category)
65 : m_sp(nullptr), m_mapper(nullptr), m_app(nullptr), m_sessionTried(false), m_session(nullptr),
66 m_log(&Category::getInstance(category)), m_parser(nullptr)
68 m_sp=SPConfig::getConfig().getServiceProvider();
72 AbstractSPRequest::~AbstractSPRequest()
83 const ServiceProvider& AbstractSPRequest::getServiceProvider() const
88 RequestMapper::Settings AbstractSPRequest::getRequestSettings() const
91 // Map request to application and content settings.
92 m_mapper=m_sp->getRequestMapper();
94 m_settings = m_mapper->getSettings(*this);
96 if (reinterpret_cast<Category*>(m_log)->isDebugEnabled()) {
97 reinterpret_cast<Category*>(m_log)->debug(
98 "mapped %s to %s", getRequestURL(), m_settings.first->getString("applicationId").second
105 const Application& AbstractSPRequest::getApplication() const
108 // Now find the application from the URL settings
109 m_app=m_sp->getApplication(getRequestSettings().first->getString("applicationId").second);
111 throw ConfigurationException("Unable to map non-default applicationId to an ApplicationOverride, check configuration.");
116 Session* AbstractSPRequest::getSession(bool checkTimeout, bool ignoreAddress, bool cache)
118 // Only attempt this once.
119 if (cache && m_sessionTried)
122 m_sessionTried = true;
124 // Need address checking and timeout settings.
126 if (checkTimeout || !ignoreAddress) {
127 const PropertySet* props=getApplication().getPropertySet("Sessions");
130 pair<bool,unsigned int> p=props->getUnsignedInt("timeout");
134 pair<bool,bool> pcheck=props->getBool("consistentAddress");
136 ignoreAddress = !pcheck.second;
140 // The cache will either silently pass a session or nullptr back, or throw an exception out.
141 Session* session = getServiceProvider().getSessionCache()->find(
142 getApplication(), *this, ignoreAddress ? nullptr : getRemoteAddr().c_str(), checkTimeout ? &timeout : nullptr
149 static char _x2c(const char *what)
153 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
155 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
159 void AbstractSPRequest::setRequestURI(const char* uri)
161 // Fix for bug 574, secadv 20061002
162 // Unescape URI up to query string delimiter by looking for %XX escapes.
163 // Adapted from Apache's util.c, ap_unescape_url function.
170 else if (*uri != '%') {
175 if (!isxdigit(*uri) || !isxdigit(*(uri+1)))
176 throw ConfigurationException("Bad request, contained unsupported encoded characters.");
185 const char* AbstractSPRequest::getRequestURI() const
187 return m_uri.c_str();
190 const char* AbstractSPRequest::getRequestURL() const
193 // Compute the full target URL
194 int port = getPort();
195 const char* scheme = getScheme();
196 m_url = string(scheme) + "://" + getHostname();
197 if ((!strcmp(scheme,"http") && port!=80) || (!strcmp(scheme,"https") && port!=443)) {
198 ostringstream portstr;
200 m_url += ":" + portstr.str();
204 return m_url.c_str();
207 string AbstractSPRequest::getRemoteAddr() const
209 pair<bool,const char*> addr = getRequestSettings().first->getString("REMOTE_ADDR");
210 return addr.first ? getHeader(addr.second) : "";
213 const char* AbstractSPRequest::getParameter(const char* name) const
216 m_parser=new CGIParser(*this);
218 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
219 return (bounds.first==bounds.second) ? nullptr : bounds.first->second;
222 vector<const char*>::size_type AbstractSPRequest::getParameters(const char* name, vector<const char*>& values) const
225 m_parser=new CGIParser(*this);
227 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
228 while (bounds.first!=bounds.second) {
229 values.push_back(bounds.first->second);
232 return values.size();
235 const char* AbstractSPRequest::getHandlerURL(const char* resource) const
238 resource = getRequestURL();
240 if (!m_handlerURL.empty() && resource && !strcmp(getRequestURL(),resource))
241 return m_handlerURL.c_str();
243 string stackresource;
244 if (resource && *resource == '/') {
245 // Compute a URL to the root of the site and point resource at constructed string.
246 int port = getPort();
247 const char* scheme = getScheme();
248 stackresource = string(scheme) + "://" + getHostname();
249 if ((!strcmp(scheme,"http") && port!=80) || (!strcmp(scheme,"https") && port!=443)) {
250 ostringstream portstr;
252 stackresource += ":" + portstr.str();
254 stackresource += resource;
255 resource = stackresource.c_str();
258 #ifdef HAVE_STRCASECMP
259 if (!resource || (strncasecmp(resource,"http://",7) && strncasecmp(resource,"https://",8)))
261 if (!resource || (strnicmp(resource,"http://",7) && strnicmp(resource,"https://",8)))
263 throw ConfigurationException("Target resource was not an absolute URL.");
266 const char* handler=nullptr;
267 const PropertySet* props=getApplication().getPropertySet("Sessions");
269 pair<bool,bool> p=props->getBool("handlerSSL");
272 pair<bool,const char*> p2=props->getString("handlerURL");
278 handler = "/Shibboleth.sso";
280 else if (*handler!='/' && strncmp(handler,"http:",5) && strncmp(handler,"https:",6)) {
281 throw ConfigurationException(
282 "Invalid handlerURL property ($1) in <Sessions> element for Application ($2)",
283 params(2, handler ? handler : "null", m_app->getId())
287 // The "handlerURL" property can be in one of three formats:
289 // 1) a full URI: http://host/foo/bar
290 // 2) a hostless URI: http:///foo/bar
291 // 3) a relative path: /foo/bar
293 // # Protocol Host Path
294 // 1 handler handler handler
295 // 2 handler resource handler
296 // 3 resource resource handler
298 // note: if ssl_only is true, make sure the protocol is https
300 const char* path = nullptr;
302 // Decide whether to use the handler or the resource for the "protocol"
304 if (*handler != '/') {
312 // break apart the "protocol" string into protocol, host, and "the rest"
313 const char* colon=strchr(prot,':');
315 const char* slash=strchr(colon,'/');
319 // Compute the actual protocol and store in member.
321 m_handlerURL.assign("https://");
323 m_handlerURL.assign(prot, colon-prot);
325 // create the "host" from either the colon/slash or from the target string
326 // If prot == handler then we're in either #1 or #2, else #3.
327 // If slash == colon then we're in #2.
328 if (prot != handler || slash == colon) {
329 colon = strchr(resource, ':');
330 colon += 3; // Get past the ://
331 slash = strchr(colon, '/');
333 string host(colon, (slash ? slash-colon : strlen(colon)));
335 // Build the handler URL
336 m_handlerURL += host + path;
337 return m_handlerURL.c_str();
340 void AbstractSPRequest::log(SPLogLevel level, const std::string& msg) const
342 reinterpret_cast<Category*>(m_log)->log(
343 (level == SPDebug ? Priority::DEBUG :
344 (level == SPInfo ? Priority::INFO :
345 (level == SPWarn ? Priority::WARN :
346 (level == SPError ? Priority::ERROR : Priority::CRIT)))),
351 bool AbstractSPRequest::isPriorityEnabled(SPLogLevel level) const
353 return reinterpret_cast<Category*>(m_log)->isPriorityEnabled(
354 (level == SPDebug ? Priority::DEBUG :
355 (level == SPInfo ? Priority::INFO :
356 (level == SPWarn ? Priority::WARN :
357 (level == SPError ? Priority::ERROR : Priority::CRIT))))