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 /* Parsing routines modified from NCSA source. */
40 char* makeword(char *line, char stop)
43 char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
45 for(x=0;((line[x]) && (line[x] != stop));x++)
54 line[y++] = line[x++];
59 char* fmakeword(char stop, size_t *cl, const char** ppch)
67 word = (char *) malloc(sizeof(char) * (wsize + 1));
71 word[ll] = *((*ppch)++);
76 word = (char *)realloc(word,sizeof(char)*(wsize+1));
79 if((word[ll] == stop) || word[ll] == EOF || (!(*cl)))
90 void plustospace(char *str)
95 if(str[x] == '+') str[x] = ' ';
99 CGIParser::CGIParser(const HTTPRequest& request, bool queryOnly)
101 parse(request.getQueryString());
102 if (!queryOnly && !strcmp(request.getMethod(),"POST")) {
103 if (request.getContentType().find("application/x-www-form-urlencoded") != string::npos)
104 parse(request.getRequestBody());
108 CGIParser::~CGIParser()
110 for (multimap<string,char*>::iterator i=kvp_map.begin(); i!=kvp_map.end(); i++)
114 void CGIParser::parse(const char* pch)
116 size_t cl = pch ? strlen(pch) : 0;
117 const URLEncoder* dec = XMLToolingConfig::getConfig().getURLEncoder();
121 value=fmakeword('&', &cl, &pch);
124 name=makeword(value, '=');
125 kvp_map.insert(pair<const string,char*>(name, value));
130 pair<CGIParser::walker,CGIParser::walker> CGIParser::getParameters(const char* name) const
133 return kvp_map.equal_range(name);
134 return make_pair(kvp_map.begin(), kvp_map.end());