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
99
100
101
102
103
104
105
106
107
|
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "query.h"
/*
* Return allocated string containing next query string parameter
* ("key=value"). The string is truncated to max characters. If
* truncation occurred, the -1th character of the string is set to 1.
*/
char *
nextparam(enum method method, int max)
{
char *buf;
int i, sz;
static int j = 0;
/* Return NULL if at end of parameter string. */
if(method == GET && !query.string[j-1])
return NULL;
#define STEP 256
sz = STEP;
if(!(buf = malloc(1+sz))) /* Leave space for -1th character. */
err(1, "malloc");
/*
* buf's -1th character is set to 1 if the string
* has been truncated (i.e. if input exceeded max).
*/
buf++;
TRUNCATED(buf) = 0;
#define READ(b) (method == GET? \
(*(b) = query.string[j++]): \
fread(b, 1, 1, stdin))
/*
* The parameter is read in a rather lopsided loop, treating
* the first character specially.
*/
first:
/* Return NULL if at end of parameter string. */
if(!READ(buf))
return NULL;
/* Skip empty parameters. */
if(*buf == '&')
goto first;
i = 0;
goto rest;
for(; READ(buf+i); i++){
rest:
if(buf[i] == '&'){
buf[i] = 0;
break;
}
if(i+1 > max){
TRUNCATED(buf) = 1;
/* Skip ahead to next parameter. */
while(READ(buf+i+1) && buf[i+1] != '&') ;
break;
}
if(i+1 >= sz){
sz += STEP;
/* Remember to adjust for -1th character. */
if(buf--, buf = realloc(buf, 1+sz), !buf++)
err(1, "realloc");
}
}
buf[i] = 0;
return buf;
}
/* Fill global query structure from CGI environment variables. */
void
setquery()
{
query.string = getenv("QUERY_STRING");
if(!query.string){
fprintf(stderr, "no QUERY_STRING\n");
exit(1);
}
query.method = strcmp(getenv("REQUEST_METHOD"), "POST")? GET: POST;
}
/*
* Split parameter string on equals sign and return value portion;
* return NULL if none found. For example, v = split(p) results in
* p pointing to the parameter key and v pointing to the value.
*/
char *
split(char *param)
{
int n;
n = strcspn(param, "=");
if(!param[n])
return NULL;
param[n] = 0;
return param+n+1;
}
|