From 6a9494b68326fe3c8052a3fb6ed0abd6ea49b4ed Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Sat, 7 Dec 2013 17:46:56 +0000 Subject: [PATCH] Added mg_get_var() to core, with unit test --- build/src/core.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ build/src/core.h | 6 ++--- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/build/src/core.c b/build/src/core.c index 4e19cee4..67a0a8a6 100644 --- a/build/src/core.c +++ b/build/src/core.c @@ -2658,6 +2658,63 @@ void mg_add_uri_handler(struct mg_server *server, const char *uri, } } +static int get_var(const char *data, size_t data_len, 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; + } else if (data == NULL || name == NULL || data_len == 0) { + len = -1; + dst[0] = '\0'; + } else { + name_len = strlen(name); + e = data + data_len; + len = -1; + dst[0] = '\0'; + + // data is "var1=val1&var2=val2...". Find variable first + for (p = data; p + name_len < e; p++) { + if ((p == data || p[-1] == '&') && p[name_len] == '=' && + !mg_strncasecmp(name, p, name_len)) { + + // Point p to variable value + p += name_len + 1; + + // Point s to the end of the value + s = (const char *) memchr(p, '&', (size_t)(e - p)); + if (s == NULL) { + s = e; + } + assert(s >= p); + + // Decode variable into destination buffer + len = mg_url_decode(p, (size_t)(s - p), dst, dst_len, 1); + + // Redirect error code from -1 to -2 (destination buffer too small). + if (len == -1) { + len = -2; + } + break; + } + } + } + + return len; +} + +int mg_get_var(const struct mg_connection *conn, const char *name, + char *dst, size_t dst_len) { + int len = get_var(conn->query_string, conn->query_string == NULL ? 0 : + strlen(conn->query_string), name, dst, dst_len); + if (len < 0) { + len = get_var(conn->content, conn->content_len, name, dst, dst_len); + } + return len; +} + const char **mg_get_valid_option_names(void) { return static_config_options; } diff --git a/build/src/core.h b/build/src/core.h index b3798c49..925c34b3 100644 --- a/build/src/core.h +++ b/build/src/core.h @@ -77,10 +77,8 @@ int mg_websocket_write(struct mg_connection *, int opcode, const char *mg_get_header(const struct mg_connection *, const char *name); const char *mg_get_mime_type(const char *file_name); -int mg_get_var(const struct mg_connection *conn, - const char *var_name, char *dst, size_t dst_len); -int mg_get_cookie(const char *cookie, const char *var_name, - char *buf, size_t buf_len); +int mg_get_var(const struct mg_connection *conn, const char *var_name, + char *buf, size_t buf_len); int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t); // Utility functions