switch4g: completely re-writen
[tomato.git] / release / src / router / httpd / cgi.c
blob111765405e9447ed67c5f31436648dd3b45da0b1
1 /*
2 * CGI helper functions
4 * Copyright 2005, Broadcom Corporation
5 * All Rights Reserved.
7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
12 * $Id: cgi.c,v 1.10 2005/03/07 08:35:32 kanki Exp $
15 #include "tomato.h"
17 #define __USE_GNU
18 #include <search.h>
21 /* CGI hash table */
22 static struct hsearch_data htab = { .table = NULL };
24 static void unescape(char *s)
26 unsigned int c;
28 while ((s = strpbrk(s, "%+"))) {
29 if (*s == '%') {
30 sscanf(s + 1, "%02x", &c);
31 *s++ = (char) c;
32 strcpy(s, s + 2);
34 else if (*s == '+') {
35 *s++ = ' ';
40 char *webcgi_get(const char *name)
42 ENTRY e, *ep;
44 if (!htab.table) return NULL;
46 e.key = (char *)name;
47 hsearch_r(e, FIND, &ep, &htab);
49 // cprintf("%s=%s\n", name, ep ? ep->data : "(null)");
51 return ep ? ep->data : NULL;
54 void webcgi_set(char *name, char *value)
56 ENTRY e, *ep;
58 if (!htab.table) {
59 hcreate_r(16, &htab);
62 e.key = name;
63 hsearch_r(e, FIND, &ep, &htab);
64 if (ep) {
65 ep->data = value;
67 else {
68 e.data = value;
69 hsearch_r(e, ENTER, &ep, &htab);
73 void webcgi_init(char *query)
75 int nel;
76 char *q, *end, *name, *value;
78 if (htab.table) hdestroy_r(&htab);
79 if (query == NULL) return;
81 // cprintf("query = %s\n", query);
83 end = query + strlen(query);
84 q = query;
85 nel = 1;
86 while (strsep(&q, "&;")) {
87 nel++;
89 hcreate_r(nel, &htab);
91 for (q = query; q < end; ) {
92 value = q;
93 q += strlen(q) + 1;
95 unescape(value);
96 name = strsep(&value, "=");
97 if (value) webcgi_set(name, value);