new function: uh_foreach_var()
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>main
parent
f096369b5b
commit
38688ef5f6
|
@ -43,6 +43,11 @@
|
||||||
ev_timer_start(l, w); \
|
ev_timer_start(l, w); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
enum uh_query_state {
|
||||||
|
s_query_key,
|
||||||
|
s_query_value
|
||||||
|
};
|
||||||
|
|
||||||
struct uh_hook {
|
struct uh_hook {
|
||||||
char *path;
|
char *path;
|
||||||
uh_hookfn_t cb;
|
uh_hookfn_t cb;
|
||||||
|
|
|
@ -19,6 +19,8 @@
|
||||||
#define _UHTTP_UHTTP_H
|
#define _UHTTP_UHTTP_H
|
||||||
|
|
||||||
#include <ev.h>
|
#include <ev.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "uhttp/config.h"
|
#include "uhttp/config.h"
|
||||||
#include "uhttp/log.h"
|
#include "uhttp/log.h"
|
||||||
#include "uhttp/buf.h"
|
#include "uhttp/buf.h"
|
||||||
|
@ -118,6 +120,13 @@ struct uh_str uh_get_var(struct uh_connection *con, const char *name);
|
||||||
struct uh_str *uh_get_header(struct uh_connection *con, const char *name);
|
struct uh_str *uh_get_header(struct uh_connection *con, const char *name);
|
||||||
int uh_get_con_sock(struct uh_connection *con);
|
int uh_get_con_sock(struct uh_connection *con);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Traversing all variables, when a variable is found, the callback function is called,
|
||||||
|
* If the callback function returns to true, the traversal is terminated.
|
||||||
|
*/
|
||||||
|
bool uh_foreach_var(struct uh_connection *con,
|
||||||
|
bool (*found)(struct uh_str *key, struct uh_str *val, void *udata), void *udata);
|
||||||
|
|
||||||
/* Unescapes strings like '%7B1,%202,%203%7D' would become '{1, 2, 3}' */
|
/* Unescapes strings like '%7B1,%202,%203%7D' would become '{1, 2, 3}' */
|
||||||
int uh_unescape(const char *str, int len, char *out, int olen);
|
int uh_unescape(const char *str, int len, char *out, int olen);
|
||||||
|
|
||||||
|
|
129
src/uhttp.c
129
src/uhttp.c
|
@ -645,64 +645,97 @@ int uh_unescape(const char *str, int len, char *out, int olen)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct uh_str uh_get_var(struct uh_connection *con, const char *name)
|
static bool __uh_foreach_var(struct uh_str *data,
|
||||||
|
bool (*found)(struct uh_str *key, struct uh_str *val, void *udata), void *udata)
|
||||||
{
|
{
|
||||||
struct uh_str *query = &con->req.query;
|
struct uh_str key, val;
|
||||||
struct uh_str *body = &con->req.body;
|
int state = s_query_key;
|
||||||
const char *pos, *tail, *p, *q;
|
int i;
|
||||||
struct uh_str var = {.at = NULL, .len = 0};
|
char ch;
|
||||||
struct uh_str *content_type = uh_get_header(con, "Content-Type");
|
|
||||||
|
|
||||||
if (query->len > 0) {
|
key.len = 0;
|
||||||
pos = query->at;
|
val.len = 0;
|
||||||
tail = query->at + query->len - 1;
|
|
||||||
} else if (body->len > 0) {
|
|
||||||
if (!content_type || !uh_str_cmp(content_type, "application/x-www-form-urlencoded"))
|
|
||||||
return var;
|
|
||||||
pos = body->at;
|
|
||||||
tail = body->at + body->len - 1;
|
|
||||||
} else {
|
|
||||||
return var;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(con && name);
|
for (i = 0; i < data->len; i++) {
|
||||||
|
ch = data->at[i];
|
||||||
|
|
||||||
while (pos < tail) {
|
switch(state) {
|
||||||
p = memchr(pos, '&', tail - pos);
|
case s_query_key:
|
||||||
if (p) {
|
if (ch == '=') {
|
||||||
q = memchr(pos, '=', p - pos);
|
state = s_query_value;
|
||||||
if (q) {
|
break;
|
||||||
if (q - pos != strlen(name)) {
|
|
||||||
pos = p + 1;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strncmp(pos, name, strlen(name))) {
|
|
||||||
pos = p + 1;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var.at = q + 1;
|
|
||||||
var.len = p - q - 1;
|
|
||||||
|
|
||||||
return var;
|
|
||||||
}
|
}
|
||||||
pos = p + 1;
|
|
||||||
} else {
|
if (!key.len)
|
||||||
p = tail;
|
key.at = data->at + i;
|
||||||
q = memchr(pos, '=', tail - pos);
|
key.len++;
|
||||||
if (q) {
|
break;
|
||||||
if (q - pos == strlen(name) && !strncmp(pos, name, strlen(name))) {
|
|
||||||
var.at = q + 1;
|
case s_query_value:
|
||||||
var.len = p - q;
|
if (ch == '&') {
|
||||||
return var;
|
state = s_query_key;
|
||||||
}
|
|
||||||
|
if (found(&key, &val, udata))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
key.len = 0;
|
||||||
|
val.len = 0;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!val.len)
|
||||||
|
val.at = data->at + i;
|
||||||
|
val.len++;
|
||||||
|
|
||||||
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return var;
|
if (state == s_query_value && key.len > 0 && val.len > 0)
|
||||||
|
if (found(&key, &val, udata))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool uh_foreach_var(struct uh_connection *con,
|
||||||
|
bool (*found)(struct uh_str *key, struct uh_str *val, void *udata), void *udata)
|
||||||
|
{
|
||||||
|
struct uh_str *content_type = uh_get_header(con, "Content-Type");
|
||||||
|
|
||||||
|
if (__uh_foreach_var(&con->req.query, found, udata))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (content_type && uh_str_cmp(content_type, "application/x-www-form-urlencoded"))
|
||||||
|
return __uh_foreach_var(&con->req.body, found, udata);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool found_var(struct uh_str *key, struct uh_str *val, void *udata)
|
||||||
|
{
|
||||||
|
struct uh_str *name = (struct uh_str *)udata;
|
||||||
|
|
||||||
|
if (key->len == name->len && !strncmp(key->at, name->at, key->len)) {
|
||||||
|
name->at = val->at;
|
||||||
|
name->len = val->len;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct uh_str uh_get_var(struct uh_connection *con, const char *name)
|
||||||
|
{
|
||||||
|
struct uh_str val = {.at = name, .len = strlen(name)};
|
||||||
|
|
||||||
|
if (!uh_foreach_var(con, found_var, &val)) {
|
||||||
|
val.at = NULL;
|
||||||
|
val.len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct uh_str *uh_get_header(struct uh_connection *con, const char *name)
|
struct uh_str *uh_get_header(struct uh_connection *con, const char *name)
|
||||||
|
|
Loading…
Reference in New Issue