-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetarg.cpp
More file actions
99 lines (88 loc) · 2.74 KB
/
getarg.cpp
File metadata and controls
99 lines (88 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 1999 Peter Karlsson
//
// $Id$
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include <string>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
#include <stdio.h>
#include "config.h"
#include "htmlerror.h"
// getarg
// - retrieves arguments from the QUERY_STRING
// input: s: name of variable to find
// optional: tells if the parameter is optional
// returns: string with the value of the variable on success
// doesn't return on failure
string getarg(char *s, bool optional)
{
// Check that we actually *do* have any parameters
char *request = getenv("QUERY_STRING");
if (!request)
{
if (optional) return string("");
htmlerror(NOPARAMETERS, "");
}
// Locate the variable in the request
char *p = strstr(request, s);
while (p)
{
// Variable name comes first or after an '&'
if (p == request || '&' == *(p - 1))
{
// ok
string str = "";
// Locate '=' sign
while (*p && *p != '=') p ++;
// If we reached the end-of-string, something is wrong
if (!*p)
{
if (optional) return string("");
htmlerror(ILLEGALFORMAT, s);
}
p ++;
// Retrieve the parameter
while (*p && *p != '&')
{
if ('%' == *p)
{
// Escaped hex code
static char hex[] = "00";
hex[0] = *(++ p);
hex[1] = *(++ p);
p ++;
unsigned c;
sscanf(hex, "%x", &c);
str += (char) c;
}
else
{
// Normal
str += *(p ++);
}
}
return str;
}
// find next
p = strstr(p + 1, s);
}
// If we get here, we didn't find the parameter.
// If it's an optional parameter, we just return an empty string
if (optional) return string("");
// Otherwise we die.
htmlerror(MISSINGPARAM, s);
return 0;
}