forked from lewischeng-ms/uhttpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.c
91 lines (73 loc) · 2.06 KB
/
url.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
/*
* file: url.c
* description: parse URL and argument string
* author: Lewis Cheng
* date: 2011.09.14
*/
#include "uhttpd.h"
int parse_url(const char *url, char *path_buf, char *arg_str_buf, char *suffix_buf) {
assert(url != NULL);
assert(path_buf != NULL);
assert(arg_str_buf != NULL);
assert(suffix_buf != NULL);
// Document root.
if (*url++ != '/')
return -1;
// Parse path.
xstrcpy(path_buf, document_root, MAX_PATH_LENGTH);
url = copy_to_stop_char(path_buf, xstrlen(path_buf, MAX_PATH_LENGTH), url, '?', MAX_PATH_LENGTH);
if (*(url - 1) == '/')
strncat(path_buf, default_html_name, MAX_PATH_LENGTH);
// Parse suffix.
xstrcpy(suffix_buf, strrchr(path_buf, '.') + 1, MAX_SUFFIX_LENGTH);
// Parse argument string.
if (*url == '?') {
copy_to_stop_char(arg_str_buf, 0, url + 1, '\0', MAX_ARGUMENT_STRING_LENGTH);
} else if (*url == '\0') {
empty_string(arg_str_buf);
} else {
return -1;
}
return 0;
}
static arg_node_type *create_arg_node(arg_node_type *link) {
arg_node_type *node = xmalloc(sizeof(arg_node_type));
node->link = link;
node->key = NULL;
node->value = NULL;
return node;
}
arg_list_type create_arg_list(const char *arg_str) {
arg_list_type list = create_arg_node(NULL);
while (*arg_str != '\0') {
arg_node_type *new_node = create_arg_node(list->link);
list->link = new_node;
// Parse key.
new_node->key = xstralloc(MAX_KEY_VALUE_LENGTH);
arg_str = copy_to_stop_char(new_node->key, 0, arg_str, '=', MAX_KEY_VALUE_LENGTH);
if (*arg_str == '=') {
++arg_str; // Skip '='.
} else {
free_arg_list(list);
return NULL;
}
// Parse value.
new_node->value = xstralloc(MAX_KEY_VALUE_LENGTH);
arg_str = copy_to_stop_char(new_node->value, 0, arg_str, '&', MAX_KEY_VALUE_LENGTH);
if (*arg_str == '&')
++arg_str; // Skip '&'.
}
return list;
}
void free_arg_list(arg_list_type list) {
assert(list != NULL);
arg_node_type *curr = list->link;
while (curr != NULL) {
xfree(curr->key);
xfree(curr->value);
arg_node_type *next = curr->link;
xfree(curr);
curr = next;
}
xfree(list);
}