forked from testkit/tinyweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgi-getfield.c
125 lines (109 loc) · 3.09 KB
/
cgi-getfield.c
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
/*
* Copyright (c) 2013-2014 Intel Corporation, All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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.
Authors:
Wang, Jing J <[email protected]>
*/
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#define MAX_QUERY_SIZE 1024
static int
hex(int digit) {
switch(digit) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
return digit - '0';
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
return 10 + digit - 'A';
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
return 10 + digit - 'a';
default:
return -1;
}
}
int decode_query(const char *query, char *pkey, char *pval, int *psize) {
char buf[256], *name, *value;
int i, k, j, L, R, done;
if (query == 0) {
return -1;
}
name = value = 0;
for (i = j = k = done = 0; done == 0; i++) {
switch (query[i]) {
case '=':
if (name != 0) {
break; /* treat extraneous '=' as data */
}
if (name == 0 && k > 0) {
name = buf;
buf[k++] = 0;
j = k;
value = buf + k;
}
continue;
case 0:
done = 1; /* fall through */
case '&':
buf[k] = 0;
if (name == 0 && k > 0) {
name = buf;
value = buf + k;
}
if (name != 0) {
if (strncmp(name, pkey, strlen(pkey)) == 0) {
*psize = k - j;
memcpy(pval, value, k - j);
return 0;
}
}
k = 0;
name = value = 0;
continue;
case '+':
buf[k++] = ' ';
continue;
case '%':
if ((L = hex(query[i + 1])) >= 0 &&
(R = hex(query[i + 2])) >= 0)
{
buf[k++] = (L << 4) + R;
i += 2;
continue;
}
break; /* treat extraneous '%' as data */
}
buf[k++] = query[i];
}
return -1;
}
int
main(int argc, char **argv) {
char qbuf[MAX_QUERY_SIZE], val[MAX_QUERY_SIZE];
int len = MAX_QUERY_SIZE;
int size = 0;
if (argc < 2)
return -1;
char *key = argv[1];
if (argc > 2)
len = atoi(argv[2]);
if ((len = fread(qbuf, 1, len, stdin)) > 0) {
qbuf[len] = 0;
if (decode_query(qbuf, key, val, &size) == 0) {
fwrite(val, 1, size, stdout);
return 0;
}
}
return -1;
}