From af7f1efa37a891c26dee19277e391c728cbd659b Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Tue, 27 Feb 2007 02:04:29 +0000 Subject: [PATCH] Moved CGI parsing class down to SAML utility class. --- saml/Makefile.am | 2 + saml/saml.vcproj | 8 ++++ saml/util/CGIParser.cpp | 122 ++++++++++++++++++++++++++++++++++++++++++++++++ saml/util/CGIParser.h | 65 ++++++++++++++++++++++++++ 4 files changed, 197 insertions(+) create mode 100644 saml/util/CGIParser.cpp create mode 100644 saml/util/CGIParser.h diff --git a/saml/Makefile.am b/saml/Makefile.am index 4460aaf..e963873 100644 --- a/saml/Makefile.am +++ b/saml/Makefile.am @@ -58,6 +58,7 @@ siginclude_HEADERS = \ signature/SignatureProfileValidator.h utilinclude_HEADERS = \ + util/CGIParser.h \ util/CommonDomainCookie.h \ util/SAMLConstants.h @@ -166,6 +167,7 @@ libsaml_la_SOURCES = \ encryption/EncryptedKeyResolver.cpp \ signature/ContentReference.cpp \ signature/SignatureProfileValidator.cpp \ + util/CGIParser.cpp \ util/CommonDomainCookie.cpp \ util/SAMLConstants.cpp diff --git a/saml/saml.vcproj b/saml/saml.vcproj index b1c0647..af783b3 100644 --- a/saml/saml.vcproj +++ b/saml/saml.vcproj @@ -189,6 +189,10 @@ Name="util" > + + @@ -591,6 +595,10 @@ Name="util" > + + diff --git a/saml/util/CGIParser.cpp b/saml/util/CGIParser.cpp new file mode 100644 index 0000000..390311a --- /dev/null +++ b/saml/util/CGIParser.cpp @@ -0,0 +1,122 @@ +/* + * Copyright 2001-2007 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * CGIParser.cpp + * + * CGI GET/POST parameter parsing + */ + +#include "internal.h" +#include "SAMLConfig.h" +#include "binding/URLEncoder.h" +#include "util/CGIParser.h" + +using namespace opensaml; +using namespace std; + + +CGIParser::CGIParser(const HTTPRequest& request) +{ + const char* pch=NULL; + if (!strcmp(request.getMethod(),"POST")) + pch=request.getRequestBody(); + else + pch=request.getQueryString(); + size_t cl=pch ? strlen(pch) : 0; + + URLEncoder* dec = SAMLConfig::getConfig().getURLEncoder(); + while (cl && pch) { + char *name; + char *value; + value=fmakeword('&',&cl,&pch); + plustospace(value); + dec->decode(value); + name=makeword(value,'='); + kvp_map.insert(pair(name,value)); + free(name); + } +} + +CGIParser::~CGIParser() +{ + for (multimap::iterator i=kvp_map.begin(); i!=kvp_map.end(); i++) + free(i->second); +} + +pair CGIParser::getParameters(const char* name) const +{ + return kvp_map.equal_range(name); +} + +/* Parsing routines modified from NCSA source. */ +char* CGIParser::makeword(char *line, char stop) +{ + int x = 0,y; + char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1)); + + for(x=0;((line[x]) && (line[x] != stop));x++) + word[x] = line[x]; + + word[x] = '\0'; + if(line[x]) + ++x; + y=0; + + while(line[x]) + line[y++] = line[x++]; + line[y] = '\0'; + return word; +} + +char* CGIParser::fmakeword(char stop, size_t *cl, const char** ppch) +{ + int wsize; + char *word; + int ll; + + wsize = 1024; + ll=0; + word = (char *) malloc(sizeof(char) * (wsize + 1)); + + while(1) + { + word[ll] = *((*ppch)++); + if(ll==wsize-1) + { + word[ll+1] = '\0'; + wsize+=1024; + word = (char *)realloc(word,sizeof(char)*(wsize+1)); + } + --(*cl); + if((word[ll] == stop) || word[ll] == EOF || (!(*cl))) + { + if(word[ll] != stop) + ll++; + word[ll] = '\0'; + return word; + } + ++ll; + } +} + +void CGIParser::plustospace(char *str) +{ + register int x; + + for(x=0;str[x];x++) + if(str[x] == '+') str[x] = ' '; +} diff --git a/saml/util/CGIParser.h b/saml/util/CGIParser.h new file mode 100644 index 0000000..5288b1a --- /dev/null +++ b/saml/util/CGIParser.h @@ -0,0 +1,65 @@ +/* + * Copyright 2001-2007 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file shibsp/util/CGIParser.h + * + * CGI GET/POST parameter parsing + */ + +#ifndef __saml_cgi_h__ +#define __saml_cgi_h__ + +#include + +namespace opensaml { + + /** + * CGI GET/POST parameter parsing + */ + class SAML_API CGIParser + { + MAKE_NONCOPYABLE(CGIParser); + public: + /** + * Constructor + * + * @param request HTTP request interface + */ + CGIParser(const HTTPRequest& request); + + ~CGIParser(); + + typedef std::multimap::const_iterator walker; + + /** + * Returns a pair of bounded iterators around the values of a parameter. + * + * @param name name of parameter + * @return a pair of multimap iterators surrounding the matching value(s) + */ + std::pair getParameters(const char* name) const; + + private: + char* fmakeword(char stop, unsigned int *cl, const char** ppch); + char* makeword(char *line, char stop); + void plustospace(char *str); + + std::multimap kvp_map; + }; +}; + +#endif /* __saml_cgi_h__ */ -- 2.1.4