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 <xmltooling/XMLToolingConfig.h>
31 #include <xmltooling/io/HTTPRequest.h>
32 #include <xmltooling/util/URLEncoder.h>
34 using namespace shibsp;
35 using namespace xmltooling;
39 CGIParser::CGIParser(const HTTPRequest& request)
41 const char* pch=nullptr;
42 if (!strcmp(request.getMethod(),"POST"))
43 pch=request.getRequestBody();
45 pch=request.getQueryString();
46 size_t cl=pch ? strlen(pch) : 0;
48 const URLEncoder* dec = XMLToolingConfig::getConfig().getURLEncoder();
52 value=fmakeword('&',&cl,&pch);
55 name=makeword(value,'=');
56 kvp_map.insert(pair<const string,char*>(name,value));
61 CGIParser::~CGIParser()
63 for (multimap<string,char*>::iterator i=kvp_map.begin(); i!=kvp_map.end(); i++)
67 pair<CGIParser::walker,CGIParser::walker> CGIParser::getParameters(const char* name) const
70 return kvp_map.equal_range(name);
71 return make_pair(kvp_map.begin(), kvp_map.end());
74 /* Parsing routines modified from NCSA source. */
75 char* CGIParser::makeword(char *line, char stop)
78 char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
80 for(x=0;((line[x]) && (line[x] != stop));x++)
89 line[y++] = line[x++];
94 char* CGIParser::fmakeword(char stop, size_t *cl, const char** ppch)
102 word = (char *) malloc(sizeof(char) * (wsize + 1));
106 word[ll] = *((*ppch)++);
111 word = (char *)realloc(word,sizeof(char)*(wsize+1));
114 if((word[ll] == stop) || word[ll] == EOF || (!(*cl)))
125 void CGIParser::plustospace(char *str)
130 if(str[x] == '+') str[x] = ' ';