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
|
/*
* CGI helper functions
*
* Copyright 2005, Broadcom Corporation
* All Rights Reserved.
*
* THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
* KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
* SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
*
* $Id: cgi.c,v 1.10 2005/03/07 08:35:32 kanki Exp $
*/
#include "tomato.h"
#define __USE_GNU
#include <search.h>
/* CGI hash table */
static struct hsearch_data htab = { .table = NULL };
static void unescape(char *s)
{
unsigned int c;
while ((s = strpbrk(s, "%+"))) {
if (*s == '%') {
sscanf(s + 1, "%02x", &c);
*s++ = (char) c;
strcpy(s, s + 2);
}
else if (*s == '+') {
*s++ = ' ';
}
}
}
char *webcgi_get(const char *name)
{
ENTRY e, *ep;
if (!htab.table) return NULL;
e.key = (char *)name;
hsearch_r(e, FIND, &ep, &htab);
// cprintf("%s=%s\n", name, ep ? ep->data : "(null)");
return ep ? ep->data : NULL;
}
void webcgi_set(char *name, char *value)
{
ENTRY e, *ep;
if (!htab.table) {
hcreate_r(16, &htab);
}
e.key = name;
hsearch_r(e, FIND, &ep, &htab);
if (ep) {
ep->data = value;
}
else {
e.data = value;
hsearch_r(e, ENTER, &ep, &htab);
}
}
void webcgi_init(char *query)
{
int nel;
char *q, *end, *name, *value;
if (htab.table) hdestroy_r(&htab);
if (query == NULL) return;
// cprintf("query = %s\n", query);
end = query + strlen(query);
q = query;
nel = 1;
while (strsep(&q, "&;")) {
nel++;
}
hcreate_r(nel, &htab);
for (q = query; q < end; ) {
value = q;
q += strlen(q) + 1;
unescape(value);
name = strsep(&value, "=");
if (value) webcgi_set(name, value);
}
}
|