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 "ServiceProvider.h"
28 #include "SessionCache.h"
29 #include "util/CGIParser.h"
31 using namespace shibsp;
32 using namespace opensaml;
33 using namespace xmltooling;
36 SPRequest::SPRequest()
40 SPRequest::~SPRequest()
44 string SPRequest::getSecureHeader(const char* name) const
46 return getHeader(name);
49 void SPRequest::setAuthType(const char* authtype)
53 AbstractSPRequest::AbstractSPRequest(const char* category)
54 : m_sp(nullptr), m_mapper(nullptr), m_app(nullptr), m_sessionTried(false), m_session(nullptr),
55 m_log(&Category::getInstance(category)), m_parser(nullptr)
57 m_sp=SPConfig::getConfig().getServiceProvider();
61 AbstractSPRequest::~AbstractSPRequest()
72 const ServiceProvider& AbstractSPRequest::getServiceProvider() const
77 RequestMapper::Settings AbstractSPRequest::getRequestSettings() const
80 // Map request to application and content settings.
81 m_mapper=m_sp->getRequestMapper();
83 m_settings = m_mapper->getSettings(*this);
85 if (reinterpret_cast<Category*>(m_log)->isDebugEnabled()) {
86 reinterpret_cast<Category*>(m_log)->debug(
87 "mapped %s to %s", getRequestURL(), m_settings.first->getString("applicationId").second
94 const Application& AbstractSPRequest::getApplication() const
97 // Now find the application from the URL settings
98 m_app=m_sp->getApplication(getRequestSettings().first->getString("applicationId").second);
100 throw ConfigurationException("Unable to map non-default applicationId to an ApplicationOverride, check configuration.");
105 Session* AbstractSPRequest::getSession(bool checkTimeout, bool ignoreAddress, bool cache)
107 // Only attempt this once.
108 if (cache && m_sessionTried)
111 m_sessionTried = true;
113 // Need address checking and timeout settings.
115 if (checkTimeout || !ignoreAddress) {
116 const PropertySet* props=getApplication().getPropertySet("Sessions");
119 pair<bool,unsigned int> p=props->getUnsignedInt("timeout");
123 pair<bool,bool> pcheck=props->getBool("consistentAddress");
125 ignoreAddress = !pcheck.second;
129 // The cache will either silently pass a session or nullptr back, or throw an exception out.
130 Session* session = getServiceProvider().getSessionCache()->find(
131 getApplication(), *this, ignoreAddress ? nullptr : getRemoteAddr().c_str(), checkTimeout ? &timeout : nullptr
138 static char _x2c(const char *what)
142 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
144 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
148 void AbstractSPRequest::setRequestURI(const char* uri)
150 // Fix for bug 574, secadv 20061002
151 // Unescape URI up to query string delimiter by looking for %XX escapes.
152 // Adapted from Apache's util.c, ap_unescape_url function.
159 else if (*uri != '%') {
164 if (!isxdigit(*uri) || !isxdigit(*(uri+1)))
165 throw ConfigurationException("Bad request, contained unsupported encoded characters.");
174 const char* AbstractSPRequest::getRequestURI() const
176 return m_uri.c_str();
179 const char* AbstractSPRequest::getRequestURL() const
182 // Compute the full target URL
183 int port = getPort();
184 const char* scheme = getScheme();
185 m_url = string(scheme) + "://" + getHostname();
186 if ((!strcmp(scheme,"http") && port!=80) || (!strcmp(scheme,"https") && port!=443)) {
187 ostringstream portstr;
189 m_url += ":" + portstr.str();
193 return m_url.c_str();
196 string AbstractSPRequest::getRemoteAddr() const
198 pair<bool,const char*> addr = getRequestSettings().first->getString("REMOTE_ADDR");
199 return addr.first ? getHeader(addr.second) : "";
202 const char* AbstractSPRequest::getParameter(const char* name) const
205 m_parser=new CGIParser(*this);
207 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
208 return (bounds.first==bounds.second) ? nullptr : bounds.first->second;
211 vector<const char*>::size_type AbstractSPRequest::getParameters(const char* name, vector<const char*>& values) const
214 m_parser=new CGIParser(*this);
216 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
217 while (bounds.first!=bounds.second) {
218 values.push_back(bounds.first->second);
221 return values.size();
224 const char* AbstractSPRequest::getHandlerURL(const char* resource) const
227 resource = getRequestURL();
229 if (!m_handlerURL.empty() && resource && !strcmp(getRequestURL(),resource))
230 return m_handlerURL.c_str();
232 string stackresource;
233 if (resource && *resource == '/') {
234 // Compute a URL to the root of the site and point resource at constructed string.
235 int port = getPort();
236 const char* scheme = getScheme();
237 stackresource = string(scheme) + "://" + getHostname();
238 if ((!strcmp(scheme,"http") && port!=80) || (!strcmp(scheme,"https") && port!=443)) {
239 ostringstream portstr;
241 stackresource += ":" + portstr.str();
243 stackresource += resource;
244 resource = stackresource.c_str();
247 #ifdef HAVE_STRCASECMP
248 if (!resource || (strncasecmp(resource,"http://",7) && strncasecmp(resource,"https://",8)))
250 if (!resource || (strnicmp(resource,"http://",7) && strnicmp(resource,"https://",8)))
252 throw ConfigurationException("Target resource was not an absolute URL.");
255 const char* handler=nullptr;
256 const PropertySet* props=getApplication().getPropertySet("Sessions");
258 pair<bool,bool> p=props->getBool("handlerSSL");
261 pair<bool,const char*> p2=props->getString("handlerURL");
266 // Should never happen...
267 if (!handler || (*handler!='/' && strncmp(handler,"http:",5) && strncmp(handler,"https:",6)))
268 throw ConfigurationException(
269 "Invalid handlerURL property ($1) in <Sessions> element for Application ($2)",
270 params(2, handler ? handler : "null", m_app->getId())
273 // The "handlerURL" property can be in one of three formats:
275 // 1) a full URI: http://host/foo/bar
276 // 2) a hostless URI: http:///foo/bar
277 // 3) a relative path: /foo/bar
279 // # Protocol Host Path
280 // 1 handler handler handler
281 // 2 handler resource handler
282 // 3 resource resource handler
284 // note: if ssl_only is true, make sure the protocol is https
286 const char* path = nullptr;
288 // Decide whether to use the handler or the resource for the "protocol"
290 if (*handler != '/') {
298 // break apart the "protocol" string into protocol, host, and "the rest"
299 const char* colon=strchr(prot,':');
301 const char* slash=strchr(colon,'/');
305 // Compute the actual protocol and store in member.
307 m_handlerURL.assign("https://");
309 m_handlerURL.assign(prot, colon-prot);
311 // create the "host" from either the colon/slash or from the target string
312 // If prot == handler then we're in either #1 or #2, else #3.
313 // If slash == colon then we're in #2.
314 if (prot != handler || slash == colon) {
315 colon = strchr(resource, ':');
316 colon += 3; // Get past the ://
317 slash = strchr(colon, '/');
319 string host(colon, (slash ? slash-colon : strlen(colon)));
321 // Build the handler URL
322 m_handlerURL += host + path;
323 return m_handlerURL.c_str();
326 void AbstractSPRequest::log(SPLogLevel level, const std::string& msg) const
328 reinterpret_cast<Category*>(m_log)->log(
329 (level == SPDebug ? Priority::DEBUG :
330 (level == SPInfo ? Priority::INFO :
331 (level == SPWarn ? Priority::WARN :
332 (level == SPError ? Priority::ERROR : Priority::CRIT)))),
337 bool AbstractSPRequest::isPriorityEnabled(SPLogLevel level) const
339 return reinterpret_cast<Category*>(m_log)->isPriorityEnabled(
340 (level == SPDebug ? Priority::DEBUG :
341 (level == SPInfo ? Priority::INFO :
342 (level == SPWarn ? Priority::WARN :
343 (level == SPError ? Priority::ERROR : Priority::CRIT))))