New function: uh_unescape()

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
main
Jianhui Zhao 2017-11-14 22:16:26 +08:00
parent a01d37430d
commit f9184839ff
4 changed files with 42 additions and 24 deletions

View File

@ -83,9 +83,20 @@ Run
~/libuhttp$ ./build/example/helloworld
Then use the command curl or browser to access https://127.0.0.1:8000/test
Then use the command curl or browser to test
$ curl -k https://127.0.0.1:8000/test -v
$ curl -k 'https://127.0.0.1:8000/test?name=context%3d%7b"nid"%3a"test"%7d' -v
If use browser to test, it will be show
Hello World
Libuhttp v0.1
Url: /test?name=context%3d%7b%22nid%22%3a%22test%22%7d
Path: /test
Name: context%3d%7b%22nid%22%3a%22test%22%7d
Unescaped Name: context={"nid":"test"}
Host: 192.168.0.100:8000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
# [Example](https://github.com/zhaojh329/libuhttp/blob/master/example/helloworld.c)

View File

@ -15,6 +15,9 @@ void route_test(struct uh_connection *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");
char unescaped_name[128];
uh_unescape(name.at, name.len, unescaped_name, sizeof(unescaped_name));
uh_send_head(con, UH_STATUS_OK, -1, NULL);
uh_printf_chunk(con, "<h1>Hello World</h1>");
@ -22,6 +25,7 @@ void route_test(struct uh_connection *con)
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);
uh_printf_chunk(con, "<h1>Unescaped Name: %s</h1>", unescaped_name);
if (header_host)
uh_printf_chunk(con, "<h1>Host: %.*s</h1>", (int)header_host->len, header_host->at);

View File

@ -577,42 +577,42 @@ 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)
int uh_unescape(const char *str, int len, char *out, int olen)
{
char *p = str;
char *q = str;
const char *p = str;
char *o = out;
if (!str)
return ("");
while (*p) {
assert(str && out);
olen -= 1;
while ((p - str < len) && (o - out < olen)) {
if (*p == '%') {
p++;
if (*p)
*q = c2hex(*p++) * 16;
if (*p)
*q = (*q + c2hex(*p++));
q++;
} else {
if (*p == '+') {
*q++ = ' ';
p++;
} else {
*q++ = *p++;
if (p + 1 - str < len) {
*o = c2hex(*p++) << 4;
*o += c2hex(*p++);
o++;
}
} else if (*p == '+') {
*o++ = ' ';
p++;
} else {
*o++ = *p++;
}
}
*q++ = 0;
return str;
*o = 0;
return 0;
}
#endif
struct uh_value uh_get_var(struct uh_connection *con, const char *name)
{

View File

@ -157,6 +157,9 @@ 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);
/* Unescapes strings like '%7B1,%202,%203%7D' would become '{1, 2, 3}' */
int uh_unescape(const char *str, int len, char *out, int olen);
#if (UHTTP_SSL_ENABLED)
/* Init ssl for the server */
int uh_ssl_init(struct uh_server *srv, const char *cert, const char *key);