2 * Copyright 2001-2009 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(NULL), m_mapper(NULL), m_app(NULL), m_sessionTried(false), m_session(NULL),
55 m_log(&Category::getInstance(category)), m_parser(NULL)
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 request to ApplicationOverride settings, 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 NULL back, or throw an exception out.
130 Session* session = getServiceProvider().getSessionCache()->find(
131 getApplication(), *this, ignoreAddress ? NULL : getRemoteAddr().c_str(), checkTimeout ? &timeout : NULL
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) ? NULL : 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 #ifdef HAVE_STRCASECMP
233 if (!resource || (strncasecmp(resource,"http://",7) && strncasecmp(resource,"https://",8)))
235 if (!resource || (strnicmp(resource,"http://",7) && strnicmp(resource,"https://",8)))
237 throw ConfigurationException("Target resource was not an absolute URL.");
240 const char* handler=NULL;
241 const PropertySet* props=getApplication().getPropertySet("Sessions");
243 pair<bool,bool> p=props->getBool("handlerSSL");
246 pair<bool,const char*> p2=props->getString("handlerURL");
251 // Should never happen...
252 if (!handler || (*handler!='/' && strncmp(handler,"http:",5) && strncmp(handler,"https:",6)))
253 throw ConfigurationException(
254 "Invalid handlerURL property ($1) in <Sessions> element for Application ($2)",
255 params(2, handler ? handler : "null", m_app->getId())
258 // The "handlerURL" property can be in one of three formats:
260 // 1) a full URI: http://host/foo/bar
261 // 2) a hostless URI: http:///foo/bar
262 // 3) a relative path: /foo/bar
264 // # Protocol Host Path
265 // 1 handler handler handler
266 // 2 handler resource handler
267 // 3 resource resource handler
269 // note: if ssl_only is true, make sure the protocol is https
271 const char* path = NULL;
273 // Decide whether to use the handler or the resource for the "protocol"
275 if (*handler != '/') {
283 // break apart the "protocol" string into protocol, host, and "the rest"
284 const char* colon=strchr(prot,':');
286 const char* slash=strchr(colon,'/');
290 // Compute the actual protocol and store in member.
292 m_handlerURL.assign("https://");
294 m_handlerURL.assign(prot, colon-prot);
296 // create the "host" from either the colon/slash or from the target string
297 // If prot == handler then we're in either #1 or #2, else #3.
298 // If slash == colon then we're in #2.
299 if (prot != handler || slash == colon) {
300 colon = strchr(resource, ':');
301 colon += 3; // Get past the ://
302 slash = strchr(colon, '/');
304 string host(colon, (slash ? slash-colon : strlen(colon)));
306 // Build the handler URL
307 m_handlerURL += host + path;
308 return m_handlerURL.c_str();
311 void AbstractSPRequest::log(SPLogLevel level, const std::string& msg) const
313 reinterpret_cast<Category*>(m_log)->log(
314 (level == SPDebug ? Priority::DEBUG :
315 (level == SPInfo ? Priority::INFO :
316 (level == SPWarn ? Priority::WARN :
317 (level == SPError ? Priority::ERROR : Priority::CRIT)))),
322 bool AbstractSPRequest::isPriorityEnabled(SPLogLevel level) const
324 return reinterpret_cast<Category*>(m_log)->isPriorityEnabled(
325 (level == SPDebug ? Priority::DEBUG :
326 (level == SPInfo ? Priority::INFO :
327 (level == SPWarn ? Priority::WARN :
328 (level == SPError ? Priority::ERROR : Priority::CRIT))))