Change license header.
[shibboleth/cpp-sp.git] / shibsp / util / CGIParser.cpp
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * CGIParser.cpp
23  * 
24  * CGI GET/POST parameter parsing.
25  */
26
27 #include "internal.h"
28 #include "util/CGIParser.h"
29
30 #include <xmltooling/XMLToolingConfig.h>
31 #include <xmltooling/io/HTTPRequest.h>
32 #include <xmltooling/util/URLEncoder.h>
33
34 using namespace shibsp;
35 using namespace xmltooling;
36 using namespace std;
37
38
39 CGIParser::CGIParser(const HTTPRequest& request)
40 {
41     const char* pch=nullptr;
42     if (!strcmp(request.getMethod(),"POST"))
43         pch=request.getRequestBody();
44     else
45         pch=request.getQueryString();
46     size_t cl=pch ? strlen(pch) : 0;
47     
48     const URLEncoder* dec = XMLToolingConfig::getConfig().getURLEncoder();
49     while (cl && pch) {
50         char *name;
51         char *value;
52         value=fmakeword('&',&cl,&pch);
53         plustospace(value);
54         dec->decode(value);
55         name=makeword(value,'=');
56         kvp_map.insert(pair<const string,char*>(name,value));
57         free(name);
58     }
59 }
60
61 CGIParser::~CGIParser()
62 {
63     for (multimap<string,char*>::iterator i=kvp_map.begin(); i!=kvp_map.end(); i++)
64         free(i->second);
65 }
66
67 pair<CGIParser::walker,CGIParser::walker> CGIParser::getParameters(const char* name) const
68 {
69     if (name)
70         return kvp_map.equal_range(name);
71     return make_pair(kvp_map.begin(), kvp_map.end());
72 }
73
74 /* Parsing routines modified from NCSA source. */
75 char* CGIParser::makeword(char *line, char stop)
76 {
77     int x = 0,y;
78     char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
79
80     for(x=0;((line[x]) && (line[x] != stop));x++)
81         word[x] = line[x];
82
83     word[x] = '\0';
84     if(line[x])
85         ++x;
86     y=0;
87
88     while(line[x])
89       line[y++] = line[x++];
90     line[y] = '\0';
91     return word;
92 }
93
94 char* CGIParser::fmakeword(char stop, size_t *cl, const char** ppch)
95 {
96     int wsize;
97     char *word;
98     int ll;
99
100     wsize = 1024;
101     ll=0;
102     word = (char *) malloc(sizeof(char) * (wsize + 1));
103
104     while(1)
105     {
106         word[ll] = *((*ppch)++);
107         if(ll==wsize-1)
108         {
109             word[ll+1] = '\0';
110             wsize+=1024;
111             word = (char *)realloc(word,sizeof(char)*(wsize+1));
112         }
113         --(*cl);
114         if((word[ll] == stop) || word[ll] == EOF || (!(*cl)))
115         {
116             if(word[ll] != stop)
117                 ll++;
118             word[ll] = '\0';
119             return word;
120         }
121         ++ll;
122     }
123 }
124
125 void CGIParser::plustospace(char *str)
126 {
127     register int x;
128
129     for(x=0;str[x];x++)
130         if(str[x] == '+') str[x] = ' ';
131 }