New function uh_get_var() but need to perfect
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>main
parent
3d92d0665f
commit
196c6e3814
|
@ -11,7 +11,8 @@ static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
|
|||
void route_test(struct uh_connection *con)
|
||||
{
|
||||
struct uh_value *url = uh_get_url(con);
|
||||
struct uh_value *path = uh_get_url(con);
|
||||
struct uh_value *path = uh_get_path(con);
|
||||
struct uh_value name = uh_get_var(con, "name");
|
||||
struct uh_value *header_host = uh_get_header(con, "Host");
|
||||
struct uh_value *header_ua = uh_get_header(con, "User-Agent");
|
||||
|
||||
|
@ -20,6 +21,7 @@ void route_test(struct uh_connection *con)
|
|||
uh_printf_chunk(con, "<h1>Libuhttp v%s</h1>", uh_version());
|
||||
uh_printf_chunk(con, "<h1>Url: %.*s</h1>", (int)url->len, url->at);
|
||||
uh_printf_chunk(con, "<h1>Path: %.*s</h1>", (int)path->len, path->at);
|
||||
uh_printf_chunk(con, "<h1>Name: %.*s</h1>", (int)name.len, name.at);
|
||||
|
||||
if (header_host)
|
||||
uh_printf_chunk(con, "<h1>Host: %.*s</h1>", (int)header_host->len, header_host->at);
|
||||
|
|
87
src/uhttp.c
87
src/uhttp.c
|
@ -645,6 +645,93 @@ inline struct uh_value *uh_get_query(struct uh_connection *con)
|
|||
return &con->req.query;
|
||||
}
|
||||
|
||||
#if 0
|
||||
static inline char c2hex(char c)
|
||||
{
|
||||
return c >= '0' && c <= '9' ? c - '0' : c >= 'A' && c <= 'F' ? c - 'A' + 10 : c - 'a' + 10; /* accept small letters just in case */
|
||||
}
|
||||
|
||||
static char *uh_unescape(char *str)
|
||||
{
|
||||
char *p = str;
|
||||
char *q = str;
|
||||
|
||||
if (!str)
|
||||
return ("");
|
||||
|
||||
while (*p) {
|
||||
if (*p == '%') {
|
||||
p++;
|
||||
if (*p)
|
||||
*q = c2hex(*p++) * 16;
|
||||
if (*p)
|
||||
*q = (*q + c2hex(*p++));
|
||||
q++;
|
||||
} else {
|
||||
if (*p == '+') {
|
||||
*q++ = ' ';
|
||||
p++;
|
||||
} else {
|
||||
*q++ = *p++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*q++ = 0;
|
||||
return str;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct uh_value uh_get_var(struct uh_connection *con, const char *name)
|
||||
{
|
||||
struct uh_value *query = &con->req.query;
|
||||
const char *pos = query->at, *tail = query->at + query->len - 1;
|
||||
const char *p, *q;
|
||||
struct uh_value var = {.at = NULL, .len = 0};
|
||||
|
||||
assert(con && name);
|
||||
|
||||
if (query->len == 0)
|
||||
return var;
|
||||
|
||||
while (pos < tail) {
|
||||
p = memchr(pos, '&', tail - pos);
|
||||
if (p) {
|
||||
q = memchr(pos, '=', p - pos);
|
||||
if (q) {
|
||||
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 {
|
||||
p = tail;
|
||||
q = memchr(pos, '=', tail - pos);
|
||||
if (q) {
|
||||
if (q - pos == strlen(name) && !strncmp(pos, name, strlen(name))) {
|
||||
var.at = q + 1;
|
||||
var.len = p - q;
|
||||
return var;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return var;
|
||||
}
|
||||
|
||||
struct uh_value *uh_get_header(struct uh_connection *con, const char *name)
|
||||
{
|
||||
int i;
|
||||
|
|
|
@ -149,6 +149,7 @@ int uh_register_route(struct uh_server *srv, const char *path, uh_route_handler_
|
|||
struct uh_value *uh_get_url(struct uh_connection *con);
|
||||
struct uh_value *uh_get_path(struct uh_connection *con);
|
||||
struct uh_value *uh_get_query(struct uh_connection *con);
|
||||
struct uh_value uh_get_var(struct uh_connection *con, const char *name);
|
||||
struct uh_value *uh_get_header(struct uh_connection *con, const char *name);
|
||||
|
||||
#if (UHTTP_SSL_ENABLED)
|
||||
|
|
Loading…
Reference in New Issue