mirror of
https://github.com/cesanta/mongoose.git
synced 2025-01-14 09:48:01 +08:00
mg_comma -> mg_commalist
This commit is contained in:
parent
714b7a8145
commit
bd64043377
@ -1152,7 +1152,47 @@ void mg_timer_poll(unsigned long uptime_ms);
|
|||||||
Traverse list of timers, and call them if current timestamp `uptime_ms` is
|
Traverse list of timers, and call them if current timestamp `uptime_ms` is
|
||||||
past the timer's expiration time.
|
past the timer's expiration time.
|
||||||
|
|
||||||
## Utility functions
|
## String
|
||||||
|
|
||||||
|
### mg\_globmatch()
|
||||||
|
|
||||||
|
```c
|
||||||
|
bool mg_globmatch(const char *pattern, int plen, const char *s, int n);
|
||||||
|
```
|
||||||
|
|
||||||
|
Return true if string `s`, `n` matches glob pattern `pattern`, `plen`.
|
||||||
|
The glob pattern matching rules are as follows:
|
||||||
|
|
||||||
|
- `?` matches any single character
|
||||||
|
- `*` matches zero or more characters except `/`
|
||||||
|
- `#` matches zero or more characters
|
||||||
|
- any other character matches itself
|
||||||
|
|
||||||
|
|
||||||
|
### mg\_commalist()
|
||||||
|
|
||||||
|
```c
|
||||||
|
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v);
|
||||||
|
```
|
||||||
|
|
||||||
|
Parse string `s`, which is a comma-separated list of entries. An entry could be
|
||||||
|
either an arbitrary string, which gets stored in `v`, or a `KEY=VALUE` which
|
||||||
|
gets stored in `k` and `v` respectively.
|
||||||
|
|
||||||
|
IMPORTANT: this function modifies `s` by pointing to the next entry. Usage
|
||||||
|
example:
|
||||||
|
|
||||||
|
```c
|
||||||
|
struct mg_str k, v, s = mg_str("a=333,b=777");
|
||||||
|
while (mg_commalist(&s, &k, &v)) // This loop output:
|
||||||
|
printf("[%.*s] set to [%.*s]\n", // [a] set to [333]
|
||||||
|
(int) k.len, k.ptr, (int) v.len, v.ptr); // [b] set to [777]
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Utility
|
||||||
|
|
||||||
|
|
||||||
### mg\_file\_read()
|
### mg\_file\_read()
|
||||||
@ -1196,41 +1236,6 @@ void mg_random(void *buf, size_t len);
|
|||||||
Fill in buffer `buf`, `len` with random data.
|
Fill in buffer `buf`, `len` with random data.
|
||||||
|
|
||||||
|
|
||||||
### mg\_globmatch()
|
|
||||||
|
|
||||||
```c
|
|
||||||
bool mg_globmatch(const char *pattern, int plen, const char *s, int n);
|
|
||||||
```
|
|
||||||
|
|
||||||
Return true if string `s`, `n` matches glob pattern `pattern`, `plen`.
|
|
||||||
The glob pattern matching rules are as follows:
|
|
||||||
|
|
||||||
- `?` matches any single character
|
|
||||||
- `*` matches zero or more characters except `/`
|
|
||||||
- `#` matches zero or more characters
|
|
||||||
- any other character matches itself
|
|
||||||
|
|
||||||
|
|
||||||
### mg\_comma()
|
|
||||||
|
|
||||||
```c
|
|
||||||
bool mg_comma(struct mg_str *s, struct mg_str *k, struct mg_str *v);
|
|
||||||
```
|
|
||||||
|
|
||||||
Parse string `s`, which is a comma-separated list of entries. An entry could be
|
|
||||||
either an arbitrary string, which gets stored in `v`, or a `KEY=VALUE` which
|
|
||||||
gets stored in `k` and `v` respectively.
|
|
||||||
|
|
||||||
IMPORTANT: this function modifies `s` by pointing to the next entry. Usage
|
|
||||||
example:
|
|
||||||
|
|
||||||
```c
|
|
||||||
struct mg_str k, v, s = mg_str("a=333,b=777");
|
|
||||||
while (mg_comma(&s, &k, &v)) // This loop output:
|
|
||||||
printf("[%.*s] set to [%.*s]\n", // [a] set to [333]
|
|
||||||
(int) k.len, k.ptr, (int) v.len, v.ptr); // [b] set to [777]
|
|
||||||
```
|
|
||||||
|
|
||||||
### mg\_ntohs()
|
### mg\_ntohs()
|
||||||
|
|
||||||
```c
|
```c
|
||||||
|
@ -1260,7 +1260,7 @@ static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
|
|||||||
path.len = i;
|
path.len = i;
|
||||||
|
|
||||||
// Process user-provided mime type overrides, if any
|
// Process user-provided mime type overrides, if any
|
||||||
while (mg_comma(&s, &k, &v)) {
|
while (mg_commalist(&s, &k, &v)) {
|
||||||
if (mg_strcmp(path, k) == 0) return v;
|
if (mg_strcmp(path, k) == 0) return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1825,7 +1825,7 @@ bool mg_log_prefix(int level, const char *file, int line, const char *fname) {
|
|||||||
if (p == NULL) p = strrchr(file, '\\');
|
if (p == NULL) p = strrchr(file, '\\');
|
||||||
p = p == NULL ? file : p + 1;
|
p = p == NULL ? file : p + 1;
|
||||||
|
|
||||||
while (mg_comma(&s, &k, &v)) {
|
while (mg_commalist(&s, &k, &v)) {
|
||||||
if (v.len == 0) max = atoi(k.ptr);
|
if (v.len == 0) max = atoi(k.ptr);
|
||||||
if (v.len > 0 && strncmp(p, k.ptr, k.len) == 0) max = atoi(v.ptr);
|
if (v.len > 0 && strncmp(p, k.ptr, k.len) == 0) max = atoi(v.ptr);
|
||||||
}
|
}
|
||||||
@ -4235,7 +4235,7 @@ static size_t mg_nce(const char *s, size_t n, size_t ofs, size_t *koff,
|
|||||||
return ofs > n ? n : ofs;
|
return ofs > n ? n : ofs;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mg_comma(struct mg_str *s, struct mg_str *k, struct mg_str *v) {
|
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v) {
|
||||||
size_t koff = 0, klen = 0, voff = 0, vlen = 0;
|
size_t koff = 0, klen = 0, voff = 0, vlen = 0;
|
||||||
size_t off = mg_nce(s->ptr, s->len, 0, &koff, &klen, &voff, &vlen);
|
size_t off = mg_nce(s->ptr, s->len, 0, &koff, &klen, &voff, &vlen);
|
||||||
if (k != NULL) *k = mg_str_n(s->ptr + koff, klen);
|
if (k != NULL) *k = mg_str_n(s->ptr + koff, klen);
|
||||||
@ -4413,7 +4413,7 @@ static int parse_net(const char *spec, uint32_t *net, uint32_t *mask) {
|
|||||||
int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip) {
|
int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip) {
|
||||||
struct mg_str k, v;
|
struct mg_str k, v;
|
||||||
int allowed = acl.len == 0 ? '+' : '-'; // If any ACL is set, deny by default
|
int allowed = acl.len == 0 ? '+' : '-'; // If any ACL is set, deny by default
|
||||||
while (mg_comma(&acl, &k, &v)) {
|
while (mg_commalist(&acl, &k, &v)) {
|
||||||
uint32_t net, mask;
|
uint32_t net, mask;
|
||||||
if (k.ptr[0] != '+' && k.ptr[0] != '-') return -1;
|
if (k.ptr[0] != '+' && k.ptr[0] != '-') return -1;
|
||||||
if (parse_net(&k.ptr[1], &net, &mask) == 0) return -2;
|
if (parse_net(&k.ptr[1], &net, &mask) == 0) return -2;
|
||||||
|
@ -537,7 +537,7 @@ bool mg_file_write(const char *path, const void *buf, size_t len);
|
|||||||
bool mg_file_printf(const char *path, const char *fmt, ...);
|
bool mg_file_printf(const char *path, const char *fmt, ...);
|
||||||
void mg_random(void *buf, size_t len);
|
void mg_random(void *buf, size_t len);
|
||||||
bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n);
|
bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n);
|
||||||
bool mg_comma(struct mg_str *s, struct mg_str *k, struct mg_str *v);
|
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v);
|
||||||
uint16_t mg_ntohs(uint16_t net);
|
uint16_t mg_ntohs(uint16_t net);
|
||||||
uint32_t mg_ntohl(uint32_t net);
|
uint32_t mg_ntohl(uint32_t net);
|
||||||
uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len);
|
uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len);
|
||||||
|
@ -489,7 +489,7 @@ static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
|
|||||||
path.len = i;
|
path.len = i;
|
||||||
|
|
||||||
// Process user-provided mime type overrides, if any
|
// Process user-provided mime type overrides, if any
|
||||||
while (mg_comma(&s, &k, &v)) {
|
while (mg_commalist(&s, &k, &v)) {
|
||||||
if (mg_strcmp(path, k) == 0) return v;
|
if (mg_strcmp(path, k) == 0) return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ bool mg_log_prefix(int level, const char *file, int line, const char *fname) {
|
|||||||
if (p == NULL) p = strrchr(file, '\\');
|
if (p == NULL) p = strrchr(file, '\\');
|
||||||
p = p == NULL ? file : p + 1;
|
p = p == NULL ? file : p + 1;
|
||||||
|
|
||||||
while (mg_comma(&s, &k, &v)) {
|
while (mg_commalist(&s, &k, &v)) {
|
||||||
if (v.len == 0) max = atoi(k.ptr);
|
if (v.len == 0) max = atoi(k.ptr);
|
||||||
if (v.len > 0 && strncmp(p, k.ptr, k.len) == 0) max = atoi(v.ptr);
|
if (v.len > 0 && strncmp(p, k.ptr, k.len) == 0) max = atoi(v.ptr);
|
||||||
}
|
}
|
||||||
|
@ -108,7 +108,7 @@ static size_t mg_nce(const char *s, size_t n, size_t ofs, size_t *koff,
|
|||||||
return ofs > n ? n : ofs;
|
return ofs > n ? n : ofs;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mg_comma(struct mg_str *s, struct mg_str *k, struct mg_str *v) {
|
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v) {
|
||||||
size_t koff = 0, klen = 0, voff = 0, vlen = 0;
|
size_t koff = 0, klen = 0, voff = 0, vlen = 0;
|
||||||
size_t off = mg_nce(s->ptr, s->len, 0, &koff, &klen, &voff, &vlen);
|
size_t off = mg_nce(s->ptr, s->len, 0, &koff, &klen, &voff, &vlen);
|
||||||
if (k != NULL) *k = mg_str_n(s->ptr + koff, klen);
|
if (k != NULL) *k = mg_str_n(s->ptr + koff, klen);
|
||||||
@ -286,7 +286,7 @@ static int parse_net(const char *spec, uint32_t *net, uint32_t *mask) {
|
|||||||
int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip) {
|
int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip) {
|
||||||
struct mg_str k, v;
|
struct mg_str k, v;
|
||||||
int allowed = acl.len == 0 ? '+' : '-'; // If any ACL is set, deny by default
|
int allowed = acl.len == 0 ? '+' : '-'; // If any ACL is set, deny by default
|
||||||
while (mg_comma(&acl, &k, &v)) {
|
while (mg_commalist(&acl, &k, &v)) {
|
||||||
uint32_t net, mask;
|
uint32_t net, mask;
|
||||||
if (k.ptr[0] != '+' && k.ptr[0] != '-') return -1;
|
if (k.ptr[0] != '+' && k.ptr[0] != '-') return -1;
|
||||||
if (parse_net(&k.ptr[1], &net, &mask) == 0) return -2;
|
if (parse_net(&k.ptr[1], &net, &mask) == 0) return -2;
|
||||||
|
@ -8,7 +8,7 @@ bool mg_file_write(const char *path, const void *buf, size_t len);
|
|||||||
bool mg_file_printf(const char *path, const char *fmt, ...);
|
bool mg_file_printf(const char *path, const char *fmt, ...);
|
||||||
void mg_random(void *buf, size_t len);
|
void mg_random(void *buf, size_t len);
|
||||||
bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n);
|
bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n);
|
||||||
bool mg_comma(struct mg_str *s, struct mg_str *k, struct mg_str *v);
|
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v);
|
||||||
uint16_t mg_ntohs(uint16_t net);
|
uint16_t mg_ntohs(uint16_t net);
|
||||||
uint32_t mg_ntohl(uint32_t net);
|
uint32_t mg_ntohl(uint32_t net);
|
||||||
uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len);
|
uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len);
|
||||||
|
@ -41,7 +41,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
|||||||
mg_globmatch((char *) data, size, (char *) data, size);
|
mg_globmatch((char *) data, size, (char *) data, size);
|
||||||
|
|
||||||
struct mg_str k, v, s = mg_str_n((char *) data, size);
|
struct mg_str k, v, s = mg_str_n((char *) data, size);
|
||||||
while (mg_comma(&s, &k, &v)) k.len = v.len = 0;
|
while (mg_commalist(&s, &k, &v)) k.len = v.len = 0;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -41,28 +41,28 @@ static void test_globmatch(void) {
|
|||||||
static void test_commalist(void) {
|
static void test_commalist(void) {
|
||||||
struct mg_str k, v, s1 = mg_str(""), s2 = mg_str("a"), s3 = mg_str("a,b");
|
struct mg_str k, v, s1 = mg_str(""), s2 = mg_str("a"), s3 = mg_str("a,b");
|
||||||
struct mg_str s4 = mg_str("a=123"), s5 = mg_str("a,b=123");
|
struct mg_str s4 = mg_str("a=123"), s5 = mg_str("a,b=123");
|
||||||
ASSERT(mg_comma(&s1, &k, &v) == false);
|
ASSERT(mg_commalist(&s1, &k, &v) == false);
|
||||||
|
|
||||||
ASSERT(mg_comma(&s2, &k, &v) == true);
|
ASSERT(mg_commalist(&s2, &k, &v) == true);
|
||||||
ASSERT(v.len == 0 && mg_vcmp(&k, "a") == 0);
|
ASSERT(v.len == 0 && mg_vcmp(&k, "a") == 0);
|
||||||
ASSERT(mg_comma(&s2, &k, &v) == false);
|
ASSERT(mg_commalist(&s2, &k, &v) == false);
|
||||||
|
|
||||||
ASSERT(mg_comma(&s3, &k, &v) == true);
|
ASSERT(mg_commalist(&s3, &k, &v) == true);
|
||||||
ASSERT(v.len == 0 && mg_vcmp(&k, "a") == 0);
|
ASSERT(v.len == 0 && mg_vcmp(&k, "a") == 0);
|
||||||
ASSERT(mg_comma(&s3, &k, &v) == true);
|
ASSERT(mg_commalist(&s3, &k, &v) == true);
|
||||||
ASSERT(v.len == 0 && mg_vcmp(&k, "b") == 0);
|
ASSERT(v.len == 0 && mg_vcmp(&k, "b") == 0);
|
||||||
ASSERT(mg_comma(&s3, &k, &v) == false);
|
ASSERT(mg_commalist(&s3, &k, &v) == false);
|
||||||
|
|
||||||
ASSERT(mg_comma(&s4, &k, &v) == true);
|
ASSERT(mg_commalist(&s4, &k, &v) == true);
|
||||||
ASSERT(mg_vcmp(&k, "a") == 0 && mg_vcmp(&v, "123") == 0);
|
ASSERT(mg_vcmp(&k, "a") == 0 && mg_vcmp(&v, "123") == 0);
|
||||||
ASSERT(mg_comma(&s4, &k, &v) == false);
|
ASSERT(mg_commalist(&s4, &k, &v) == false);
|
||||||
ASSERT(mg_comma(&s4, &k, &v) == false);
|
ASSERT(mg_commalist(&s4, &k, &v) == false);
|
||||||
|
|
||||||
ASSERT(mg_comma(&s5, &k, &v) == true);
|
ASSERT(mg_commalist(&s5, &k, &v) == true);
|
||||||
ASSERT(v.len == 0 && mg_vcmp(&k, "a") == 0);
|
ASSERT(v.len == 0 && mg_vcmp(&k, "a") == 0);
|
||||||
ASSERT(mg_comma(&s5, &k, &v) == true);
|
ASSERT(mg_commalist(&s5, &k, &v) == true);
|
||||||
ASSERT(mg_vcmp(&k, "b") == 0 && mg_vcmp(&v, "123") == 0);
|
ASSERT(mg_vcmp(&k, "b") == 0 && mg_vcmp(&v, "123") == 0);
|
||||||
ASSERT(mg_comma(&s4, &k, &v) == false);
|
ASSERT(mg_commalist(&s4, &k, &v) == false);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_http_get_var(void) {
|
static void test_http_get_var(void) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user