-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqstring.c
More file actions
143 lines (120 loc) · 3.7 KB
/
qstring.c
File metadata and controls
143 lines (120 loc) · 3.7 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright (c) 2014-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <jeffpc/qstring.h>
#include <jeffpc/urldecode.h>
#include <jeffpc/error.h>
/*
* The query string is a sequence of name-value pairs. Each name is
* separated from the value with a '=', and each pair is separated by '&'.
* We parse the input string, and produce a nvlist key-value pair for each
* of the input pairs. We map each input pair based on these rules:
*
* foo=bar -> { "foo" : "bar" }
* foo= -> { "foo" : "" }
* foo -> { "foo" : null }
* =bar -> { "" : "bar" }
*/
enum qs_state {
QS_STATE_NAME,
QS_STATE_VAL,
};
static int insert(struct nvlist *nvl, const char *namestart, size_t namelen,
const char *valstart, size_t vallen)
{
char name[namelen + 1];
ssize_t newlen;
/* decode & nul-terminate the name */
newlen = urldecode(namestart, namelen, name);
if (newlen < 0)
return newlen;
name[newlen] = '\0';
if (!valstart) {
/* we want a null value */
return nvl_set_null(nvl, name);
} else {
/* we want a string value (possibly empty string) */
struct str *val;
if (!vallen)
val = str_empty_string();
else
val = urldecode_str(valstart, vallen);
if (IS_ERR(val))
return PTR_ERR(val);
return nvl_set_str(nvl, name, val);
}
}
int qstring_parse_len(struct nvlist *nvl, const char *qs, size_t len)
{
const char *cur, *end;
const char *name;
const char *val;
size_t name_len;
enum qs_state state;
if (!nvl)
return -EINVAL;
if (!len && !qs)
return 0;
if (!qs)
return -EINVAL;
end = qs + len;
cur = qs;
state = QS_STATE_NAME;
name = qs;
name_len = 0;
val = NULL;
for (; end > cur; cur++) {
char c = *cur;
if (state == QS_STATE_NAME) {
if (c == '=') {
/* end of name */
name_len = cur - name;
val = cur + 1;
state = QS_STATE_VAL;
} else if (c == '&') {
/* end of name; no value */
insert(nvl, name, cur - name, NULL, 0);
name = cur + 1;
state = QS_STATE_NAME; /* no change */
}
} else if (state == QS_STATE_VAL) {
if (c == '&') {
/* end of value */
insert(nvl, name, name_len, val, cur - val);
name = cur + 1;
state = QS_STATE_NAME;
} else if (c == '=') {
/* value contains = */
return -EILSEQ;
}
}
}
/* qs ends with a name without a '=' (e.g., abc=def&ghi) */
if ((state == QS_STATE_NAME) && (name < end))
insert(nvl, name, end - name, NULL, 0);
/* qs ends with an empty value (e.g., abc=def&ghi=) */
if ((state == QS_STATE_VAL) && (val == end))
insert(nvl, name, name_len, val, 0);
/* qs ends with a value (e.g., abc=def&ghi=jkl) */
if ((state == QS_STATE_VAL) && (val < end))
insert(nvl, name, name_len, val, end - val);
return 0;
}