1
/* $Id: parse.cpp,v 1.5 2003/06/23 14:38:43 terpstra Exp $
3
* parse.cpp - Deal with CGI ugliness
5
* Copyright (C) 2002 - Wesley W. Terpstra
9
* Authors: 'Wesley W. Terpstra' <wesley@terpstra.ca>
11
* This program is free software; you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation; version 2.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
#define _XOPEN_SOURCE 500
26
#define _FILE_OFFSET_BITS 64
32
inline int fromHex(char c)
34
if (c >= 'A' && c <= 'Z') return c - 'A' + 10;
35
if (c >= 'a' && c <= 'z') return c - 'a' + 10;
39
string decipherHalf(const string& str)
41
// cout << "deciper: " << str << endl;
45
string::size_type b = 0, e;
46
while ((e = str.find_first_of("%+", b)) != string::npos)
48
out.append(str, b, e - b);
49
if (str[e] == '+') out.append(" ");
50
else if (str.length() > e+2)
52
int ch = fromHex(str[e+1]) << 4 | fromHex(str[e+2]);
60
out.append(str, b, str.length() - b);
65
pair<string, string> splitParam(const string& str)
67
// cout << "split: " << str << endl;
69
string::size_type split = str.find('=');
70
if (split == string::npos)
71
return pair<string, string>(decipherHalf(str), "");
72
else return pair<string, string>(
73
decipherHalf(str.substr(0, split)),
74
decipherHalf(str.substr(split+1, string::npos)));
77
map<string, string> getParams()
79
map<string, string> out;
81
char* x = getenv("QUERY_STRING");
85
// cout << "parse: " << str << endl;
87
string::size_type b = str.find_first_not_of('&', 0), e;
88
if (b == string::npos) return out;
90
while ((e = str.find_first_of('&', b)) != string::npos)
92
out.insert(splitParam(str.substr(b, e - b)));
93
b = str.find_first_not_of('&', e+1);
95
out.insert(splitParam(str.substr(b, str.length() - b)));
100
int redirectUrl(const string& url)
102
cout << "Status: 303 Moved Permanently\r\n"
103
<< "Location: " << url << "\r\n"
104
<< "Content-type: text/html\r\n\r\n"
105
<< "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
106
<< "<html><head>\r\n"
107
<< "<title>301 Moved Permanently</title>\r\n"
108
<< "</head><body>\r\n"
109
<< "<h1>Moved Permanently</h1>\r\n"
110
<< "The document has moved <a href=\"" << url << "\">here</a>.\r\n"
112
<< "</body></html>\r\n";