2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
24 * CGI GET/POST parameter parsing.
28 #include "util/CGIParser.h"
30 #include <boost/bind.hpp>
31 #include <xmltooling/XMLToolingConfig.h>
32 #include <xmltooling/io/HTTPRequest.h>
33 #include <xmltooling/util/URLEncoder.h>
35 using namespace shibsp;
36 using namespace xmltooling;
37 using namespace boost;
41 /* Parsing routines modified from NCSA source. */
42 char* makeword(char *line, char stop)
45 char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
47 for(x=0;((line[x]) && (line[x] != stop));x++)
56 line[y++] = line[x++];
61 char* fmakeword(char stop, size_t *cl, const char** ppch)
69 word = (char *) malloc(sizeof(char) * (wsize + 1));
73 word[ll] = *((*ppch)++);
78 word = (char *)realloc(word,sizeof(char)*(wsize+1));
81 if((word[ll] == stop) || word[ll] == EOF || (!(*cl)))
92 void plustospace(char *str)
97 if(str[x] == '+') str[x] = ' ';
101 CGIParser::CGIParser(const HTTPRequest& request, bool queryOnly)
103 parse(request.getQueryString());
104 if (!queryOnly && !strcmp(request.getMethod(),"POST")) {
105 if (request.getContentType().find("application/x-www-form-urlencoded") != string::npos)
106 parse(request.getRequestBody());
110 CGIParser::~CGIParser()
112 for_each(kvp_map.begin(), kvp_map.end(), boost::bind<void>(&free, boost::bind(&multimap<string,char*>::value_type::second, _1)));
115 void CGIParser::parse(const char* pch)
117 size_t cl = pch ? strlen(pch) : 0;
118 const URLEncoder* dec = XMLToolingConfig::getConfig().getURLEncoder();
122 value=fmakeword('&', &cl, &pch);
125 name=makeword(value, '=');
126 kvp_map.insert(pair<const string,char*>(name, value));
131 pair<CGIParser::walker,CGIParser::walker> CGIParser::getParameters(const char* name) const
134 return kvp_map.equal_range(name);
135 return make_pair(kvp_map.begin(), kvp_map.end());