Add mg_http_var

This commit is contained in:
Sergey Lyubka committed 2022-06-18 04:25:11 +01:00
1 parent adcf5bab0e
commit 1640287897
5 files changed
+58 -35

No files matched your search

+17 -17
View File
@@ -87,32 +87,32 @@ void mg_http_bauth(struct mg_connection *c, const char *user,
}
}
struct mg_str mg_http_var(struct mg_str buf, struct mg_str name) {
struct mg_str k, v, result = mg_str_n(NULL, 0);
while (mg_split(&buf, &k, &v, '&')) {
if (name.len == k.len && mg_ncasecmp(name.ptr, k.ptr, k.len) == 0) {
result = v;
break;
}
}
return result;
}
int mg_http_get_var(const struct mg_str *buf, const char *name, char *dst,
size_t dst_len) {
const char *p, *e, *s;
size_t name_len;
int len;
if (dst == NULL || dst_len == 0) {
len = -2; // Bad destination
} else if (buf->ptr == NULL || name == NULL || buf->len == 0) {
len = -1; // Bad source
dst[0] = '\0';
} else {
name_len = strlen(name);
e = buf->ptr + buf->len;
len = -4; // Name does not exist
dst[0] = '\0';
for (p = buf->ptr; p + name_len < e; p++) {
if ((p == buf->ptr || p[-1] == '&') && p[name_len] == '=' &&
!mg_ncasecmp(name, p, name_len)) {
p += name_len + 1;
s = (const char *) memchr(p, '&', (size_t) (e - p));
if (s == NULL) s = e;
len = mg_url_decode(p, (size_t) (s - p), dst, dst_len, 1);
if (len < 0) len = -3; // Failed to decode
break;
}
struct mg_str v = mg_http_var(*buf, mg_str(name));
if (v.ptr == NULL) {
len = -4; // Name does not exist
} else {
len = mg_url_decode(v.ptr, v.len, dst, dst_len, 1);
if (len < 0) len = -3; // Failed to decode
}
}
return len;