diff --git a/docs/c-api/http_server.h/mg_get_http_var.md b/docs/c-api/http_server.h/mg_get_http_var.md index 55ee3e6f..8ca1d915 100644 --- a/docs/c-api/http_server.h/mg_get_http_var.md +++ b/docs/c-api/http_server.h/mg_get_http_var.md @@ -12,5 +12,6 @@ Fetches a HTTP form variable. Fetches a variable `name` from a `buf` into a buffer specified by `dst`, `dst_len`. The destination is always zero-terminated. Returns the length of a fetched variable. If not found, 0 is returned. `buf` must be valid -url-encoded buffer. If destination is too small, `-1` is returned. +url-encoded buffer. If destination is too small or an error occured, +negative number is returned. diff --git a/mongoose.c b/mongoose.c index 611b50a4..fc5c91bc 100644 --- a/mongoose.c +++ b/mongoose.c @@ -6492,6 +6492,13 @@ int mg_get_http_var(const struct mg_str *buf, const char *name, char *dst, size_t name_len; int len; + /* + * According to the documentation function returns negative + * value in case of error. For debug purposes it returns: + * -1 - src is wrong (NUUL) + * -2 - dst is wrong (NULL) + * -3 - failed to decode url or dst is to small + */ if (dst == NULL || dst_len == 0) { len = -2; } else if (buf->p == NULL || name == NULL || buf->len == 0) { @@ -6500,7 +6507,7 @@ int mg_get_http_var(const struct mg_str *buf, const char *name, char *dst, } else { name_len = strlen(name); e = buf->p + buf->len; - len = -1; + len = 0; dst[0] = '\0'; for (p = buf->p; p + name_len < e; p++) { @@ -6512,8 +6519,9 @@ int mg_get_http_var(const struct mg_str *buf, const char *name, char *dst, s = e; } len = mg_url_decode(p, (size_t)(s - p), dst, dst_len, 1); + /* -1 means: failed to decode or dst is too small */ if (len == -1) { - len = -2; + len = -3; } break; } diff --git a/mongoose.h b/mongoose.h index 5b17bdbf..eb18954b 100644 --- a/mongoose.h +++ b/mongoose.h @@ -4516,7 +4516,8 @@ size_t mg_parse_multipart(const char *buf, size_t buf_len, char *var_name, * Fetches a variable `name` from a `buf` into a buffer specified by `dst`, * `dst_len`. The destination is always zero-terminated. Returns the length of * a fetched variable. If not found, 0 is returned. `buf` must be valid - * url-encoded buffer. If destination is too small, `-1` is returned. + * url-encoded buffer. If destination is too small or an error occured, + * negative number is returned. */ int mg_get_http_var(const struct mg_str *buf, const char *name, char *dst, size_t dst_len);