-
Notifications
You must be signed in to change notification settings - Fork 1
/
cgi.c
116 lines (105 loc) · 3.1 KB
/
cgi.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
#define _POSIX_C_SOURCE 200809L
#include "cgi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
extern char **environ;
struct cgi_var* cgi_get_var(struct cgi* cgi, const char* var_name) {
for (int i = 0; i < cgi->n_vars; ++i) {
struct cgi_var* var = cgi->vars + i;
if (!strcmp(var->var, var_name)) {
return var;
}
}
return 0;
}
static char* unescape(const char* str) {
char* dup = strdup(str);
const char* read = dup;
char* write = dup;
char c;
while ((c = *read++)) {
if (c == '%') {
unsigned int x;
int n = sscanf(read, "%2x", &x);
if (n == 1) {
read += 2;
} else {
/*parsing got screwed up, so just cut the string off
here */
return dup;
}
*write++ = x;
} else if (c == '+') {
*write++ = ' ';
} else {
*write++ = c;
}
}
*write = 0;
return dup;
}
static struct cgi_var new_var(const char* var_name, const char* value) {
struct cgi_var var;
var.var = unescape(var_name);
var.values = malloc(sizeof(char*));
var.values[0] = unescape(value);
var.n_values = 1;
var.values_allocated = 1;
return var;
}
static void add_value(struct cgi_var* var, const char* value) {
if (var->n_values >= var->values_allocated) {
var->values_allocated *= 2;
var->values = realloc(var->values, var->values_allocated * sizeof(char*));
}
var->values[var->n_values++] = unescape(value);
}
static void add_var(struct cgi* cgi, struct cgi_var* var) {
if (cgi->n_vars >= cgi->vars_allocated) {
cgi->vars_allocated = (cgi->vars_allocated + 1) * 2;
cgi->vars = realloc(cgi->vars, cgi->vars_allocated * sizeof(struct cgi_var));
}
cgi->vars[cgi->n_vars++] = *var;
}
static struct cgi* parse_query_string(const char* query_string) {
struct cgi* cgi = malloc(sizeof(struct cgi));
cgi->vars = 0;
cgi->n_vars = 0;
cgi->vars_allocated = 0;
char* query_string_dup = strdup(query_string);
char* strtok_arg = query_string_dup;
char* save_ptr;
char* next_tok;
while ((next_tok = strtok_r(strtok_arg, "&", &save_ptr))) {
strtok_arg = 0;
char* var_name = next_tok;
char* value = strchr(next_tok, '=');
*value = 0;
value ++;
struct cgi_var* var = cgi_get_var(cgi, var_name);
if (var) {
add_value(var, value);
} else {
struct cgi_var created = new_var(var_name, value);
var = &created;
add_var(cgi, var);
}
}
free(query_string_dup);
return cgi;
}
struct cgi* cgi_init() {
const char* query_string = "QUERY_STRING";
int query_string_len = strlen(query_string);
int i = 0;
while(environ[i]) {
if (strncmp(environ[i], query_string, query_string_len) == 0) {
struct cgi* cgi = parse_query_string(strchr(environ[i], '=') + 1);
return cgi;
}
++i;
}
return parse_query_string("");
}