Commit f49df515 authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by rojer

Change mg_match_prefix_n to take mg_str

Part of general drive to use mg_str instead of pointer + length.
Adda unit test for mg_match_prefix.

PUBLISHED_FROM=1402506769b0941c6dbe6ae06e03e41df32d6008
parent 673796fb
...@@ -5044,8 +5044,8 @@ static mg_event_handler_t mg_http_get_endpoint_handler( ...@@ -5044,8 +5044,8 @@ static mg_event_handler_t mg_http_get_endpoint_handler(
ep = pd->endpoints; ep = pd->endpoints;
while (ep != NULL) { while (ep != NULL) {
if ((matched = mg_match_prefix_n(ep->name, ep->name_len, uri_path->p, const struct mg_str name_s = {ep->name, ep->name_len};
uri_path->len)) != -1) { if ((matched = mg_match_prefix_n(name_s, *uri_path)) != -1) {
if (matched > matched_max) { if (matched > matched_max) {
/* Looking for the longest suitable handler */ /* Looking for the longest suitable handler */
ret = ep->handler; ret = ep->handler;
...@@ -6740,9 +6740,9 @@ MG_INTERNAL int mg_uri_to_local_path(struct http_message *hm, ...@@ -6740,9 +6740,9 @@ MG_INTERNAL int mg_uri_to_local_path(struct http_message *hm,
} }
} else { } else {
/* Regular rewrite, URI=directory */ /* Regular rewrite, URI=directory */
int match_len = mg_match_prefix_n(a.p, a.len, cp, hm->uri.len); int match_len = mg_match_prefix_n(a, hm->uri);
if (match_len > 0) { if (match_len > 0) {
file_uri_start = cp + match_len; file_uri_start = hm->uri.p + match_len;
if (*file_uri_start == '/' || file_uri_start == cp_end) { if (*file_uri_start == '/' || file_uri_start == cp_end) {
/* Match ended at component boundary, ok. */ /* Match ended at component boundary, ok. */
} else if (*(file_uri_start - 1) == '/') { } else if (*(file_uri_start - 1) == '/') {
...@@ -8057,44 +8057,46 @@ const char *mg_next_comma_list_entry(const char *list, struct mg_str *val, ...@@ -8057,44 +8057,46 @@ const char *mg_next_comma_list_entry(const char *list, struct mg_str *val,
return list; return list;
} }
int mg_match_prefix_n(const char *pattern, int pattern_len, const char *str, int mg_match_prefix_n(const struct mg_str pattern, const struct mg_str str) {
int str_len) {
const char *or_str; const char *or_str;
int len, res, i = 0, j = 0; size_t len, i = 0, j = 0;
int res;
if ((or_str = (const char *) memchr(pattern, '|', pattern_len)) != NULL) { if ((or_str = (const char *) memchr(pattern.p, '|', pattern.len)) != NULL) {
res = mg_match_prefix_n(pattern, or_str - pattern, str, str_len); struct mg_str pstr = {pattern.p, (size_t)(or_str - pattern.p)};
return res > 0 ? res res = mg_match_prefix_n(pstr, str);
: mg_match_prefix_n(or_str + 1, if (res > 0) return res;
(pattern + pattern_len) - (or_str + 1), pstr.p = or_str + 1;
str, str_len); pstr.len = (pattern.p + pattern.len) - (or_str + 1);
return mg_match_prefix_n(pstr, str);
} }
for (; i < pattern_len; i++, j++) { for (; i < pattern.len; i++, j++) {
if (pattern[i] == '?' && j != str_len) { if (pattern.p[i] == '?' && j != str.len) {
continue; continue;
} else if (pattern[i] == '$') { } else if (pattern.p[i] == '$') {
return j == str_len ? j : -1; return j == str.len ? (int) j : -1;
} else if (pattern[i] == '*') { } else if (pattern.p[i] == '*') {
i++; i++;
if (pattern[i] == '*') { if (pattern.p[i] == '*') {
i++; i++;
len = str_len - j; len = str.len - j;
} else { } else {
len = 0; len = 0;
while (j + len != str_len && str[len] != '/') { while (j + len != str.len && str.p[len] != '/') {
len++; len++;
} }
} }
if (i == pattern_len) { if (i == pattern.len) {
return j + len; return j + len;
} }
do { do {
res = mg_match_prefix_n(pattern + i, pattern_len - i, str + j + len, const struct mg_str pstr = {pattern.p + i, pattern.len - i};
str_len - j - len); const struct mg_str sstr = {str.p + j + len, str.len - j - len};
res = mg_match_prefix_n(pstr, sstr);
} while (res == -1 && len-- > 0); } while (res == -1 && len-- > 0);
return res == -1 ? -1 : j + res + len; return res == -1 ? -1 : (int) (j + res + len);
} else if (lowercase(&pattern[i]) != lowercase(&str[j])) { } else if (lowercase(&pattern.p[i]) != lowercase(&str.p[j])) {
return -1; return -1;
} }
} }
...@@ -8102,7 +8104,8 @@ int mg_match_prefix_n(const char *pattern, int pattern_len, const char *str, ...@@ -8102,7 +8104,8 @@ int mg_match_prefix_n(const char *pattern, int pattern_len, const char *str,
} }
int mg_match_prefix(const char *pattern, int pattern_len, const char *str) { int mg_match_prefix(const char *pattern, int pattern_len, const char *str) {
return mg_match_prefix_n(pattern, pattern_len, str, strlen(str)); const struct mg_str pstr = {pattern, (size_t) pattern_len};
return mg_match_prefix_n(pstr, mg_mk_str(str));
} }
struct mg_str mg_mk_str(const char *s) { struct mg_str mg_mk_str(const char *s) {
......
...@@ -1881,8 +1881,7 @@ const char *mg_next_comma_list_entry(const char *list, struct mg_str *val, ...@@ -1881,8 +1881,7 @@ const char *mg_next_comma_list_entry(const char *list, struct mg_str *val,
* Match is case-insensitive. Return number of bytes matched, or -1 if no match. * Match is case-insensitive. Return number of bytes matched, or -1 if no match.
*/ */
int mg_match_prefix(const char *pattern, int pattern_len, const char *str); int mg_match_prefix(const char *pattern, int pattern_len, const char *str);
int mg_match_prefix_n(const char *pattern, int pattern_len, const char *str, int mg_match_prefix_n(const struct mg_str pattern, const struct mg_str str);
int str_len);
/* /*
* A helper function for creating mg_str struct from plain C string. * A helper function for creating mg_str struct from plain C string.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment