aboutsummaryrefslogtreecommitdiff
path: root/query.c
blob: 96cfbcbb725c417794c35235f6c9f7f4b22b7ef3 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "query.h"

/*
 * Return an allocated string containing the next parameter ("key=value").
 * The method argument decides which data (GET or POST) to read from.
 * 
 * If len is NULL, the string will be NUL-terminated; if len is non-NULL,
 * the string will not be NUL-terminated, and len will be pointed to the
 * length of the string.
 * 
 * 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 *len, 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");
		}
	}
	if(len) *len = i;
	else 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;
	if(query.method == POST)
		query.length = atoi(getenv("CONTENT_LENGTH"));
}

/*
 * 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;
}

/*
 * Decode a URL-encoded string in place. If len is negative, the string
 * is assumed to be NUL-terminated. The length of the new string is
 * returned. Note that the new string is NOT NUL-terminated! To produce
 * a NUL-terminated string, use s[urldecode(s, -1)] = 0.
 */
int
urldecode(char *s, int len)
{
	unsigned int code, i, j;
	
	if(len < 0)
		len = strlen(s);
	
	for(i = j = 0; i < len; i++, j++){
		if(s[i] == '+')
			s[j] = ' ';
		else if(s[i] == '%'){
			if(!sscanf(s+i+1, "%2x", &code))
				code = '?';
			s[j] = code;
			i += 2;
		}else
			s[j] = s[i];
	}
	
	return j;
}