diff --git a/mongoose.c b/mongoose.c index aebd74f8..20b9210b 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1764,8 +1764,10 @@ int mg_strcmp(const struct mg_str str1, const struct mg_str str2) WEAK; int mg_strcmp(const struct mg_str str1, const struct mg_str str2) { size_t i = 0; while (i < str1.len && i < str2.len) { - if (str1.p[i] < str2.p[i]) return -1; - if (str1.p[i] > str2.p[i]) return 1; + int c1 = str1.p[i]; + int c2 = str2.p[i]; + if (c1 < c2) return -1; + if (c1 > c2) return 1; i++; } if (i < str1.len) return 1; @@ -1787,6 +1789,21 @@ int mg_strncmp(const struct mg_str str1, const struct mg_str str2, size_t n) { return mg_strcmp(s1, s2); } +int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2) WEAK; +int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2) { + size_t i = 0; + while (i < str1.len && i < str2.len) { + int c1 = tolower((int) str1.p[i]); + int c2 = tolower((int) str2.p[i]); + if (c1 < c2) return -1; + if (c1 > c2) return 1; + i++; + } + if (i < str1.len) return 1; + if (i < str2.len) return -1; + return 0; +} + void mg_strfree(struct mg_str *s) WEAK; void mg_strfree(struct mg_str *s) { char *sp = (char *) s->p; diff --git a/mongoose.h b/mongoose.h index ad35234f..1a38d759 100644 --- a/mongoose.h +++ b/mongoose.h @@ -2351,6 +2351,11 @@ int mg_strcmp(const struct mg_str str1, const struct mg_str str2); */ int mg_strncmp(const struct mg_str str1, const struct mg_str str2, size_t n); +/* + * Compare two `mg_str`s ignoreing case; return value is the same as `strcmp`. + */ +int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2); + /* * Free the string (assuming it was heap allocated). */ @@ -6239,6 +6244,7 @@ int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr, * Encodes a DNS name. */ int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len); +int mg_dns_encode_name_s(struct mbuf *io, struct mg_str name); /* Low-level: parses a DNS response. */ int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg);