mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-27 06:51:04 +08:00
Get rid of ctype dependencies
This commit is contained in:
parent
113abc71d4
commit
393dad97e0
35
mongoose.c
35
mongoose.c
@ -1114,14 +1114,18 @@ int mg_http_get_var(const struct mg_str *buf, const char *name, char *dst,
|
||||
return len;
|
||||
}
|
||||
|
||||
static bool isx(int c) {
|
||||
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
|
||||
(c >= 'A' && c <= 'F');
|
||||
}
|
||||
|
||||
int mg_url_decode(const char *src, size_t src_len, char *dst, size_t dst_len,
|
||||
int is_form_url_encoded) {
|
||||
size_t i, j;
|
||||
for (i = j = 0; i < src_len && j + 1 < dst_len; i++, j++) {
|
||||
if (src[i] == '%') {
|
||||
// Use `i + 2 < src_len`, not `i < src_len - 2`, note small src_len
|
||||
if (i + 2 < src_len && isxdigit(*(const unsigned char *) (src + i + 1)) &&
|
||||
isxdigit(*(const unsigned char *) (src + i + 2))) {
|
||||
if (i + 2 < src_len && isx(src[i + 1]) && isx(src[i + 2])) {
|
||||
mg_unhex(src + i + 1, 2, (uint8_t *) &dst[j]);
|
||||
i += 2;
|
||||
} else {
|
||||
@ -1137,11 +1141,14 @@ int mg_url_decode(const char *src, size_t src_len, char *dst, size_t dst_len,
|
||||
return i >= src_len && j < dst_len ? (int) j : -1;
|
||||
}
|
||||
|
||||
static bool isok(uint8_t c) {
|
||||
return c == '\n' || c == '\r' || c >= ' ';
|
||||
}
|
||||
|
||||
int mg_http_get_request_len(const unsigned char *buf, size_t buf_len) {
|
||||
size_t i;
|
||||
for (i = 0; i < buf_len; i++) {
|
||||
if (!isprint(buf[i]) && buf[i] != '\r' && buf[i] != '\n' && buf[i] < 128)
|
||||
return -1;
|
||||
if (!isok(buf[i])) return -1;
|
||||
if ((i > 0 && buf[i] == '\n' && buf[i - 1] == '\n') ||
|
||||
(i > 3 && buf[i] == '\n' && buf[i - 1] == '\r' && buf[i - 2] == '\n'))
|
||||
return (int) i + 1;
|
||||
@ -2043,7 +2050,7 @@ void mg_iobuf_free(struct mg_iobuf *io) {
|
||||
|
||||
#if MG_ENABLE_LOG
|
||||
static void mg_log_stdout(const void *buf, size_t len, void *userdata) {
|
||||
(void) userdata;
|
||||
(void) userdata, (void) buf, (void) len;
|
||||
#if MG_ENABLE_FILE
|
||||
fwrite(buf, 1, len, stdout);
|
||||
#endif
|
||||
@ -3031,8 +3038,6 @@ void mg_hmac_sha1(const unsigned char *key, size_t keylen,
|
||||
#define SNTP_INTERVAL_SEC 3600
|
||||
#define SNTP_TIME_OFFSET 2208988800UL
|
||||
|
||||
static unsigned long s_sntmp_next;
|
||||
|
||||
int64_t mg_sntp_parse(const unsigned char *buf, size_t len) {
|
||||
int64_t res = -1;
|
||||
int mode = len > 0 ? buf[0] & 7 : 0;
|
||||
@ -3050,7 +3055,6 @@ int64_t mg_sntp_parse(const unsigned char *buf, size_t len) {
|
||||
unsigned long useconds = mg_ntohl(data[1]);
|
||||
// MG_DEBUG(("%lu %lu %lu", time(0), seconds, useconds));
|
||||
res = ((int64_t) seconds) * 1000 + (int64_t) ((useconds / 1000) % 1000);
|
||||
s_sntmp_next = seconds + SNTP_INTERVAL_SEC;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@ -3060,12 +3064,12 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len);
|
||||
if (milliseconds > 0) {
|
||||
mg_call(c, MG_EV_SNTP_TIME, &milliseconds);
|
||||
MG_DEBUG(("%u.%u, next at %lu", (unsigned) (milliseconds / 1000),
|
||||
(unsigned) (milliseconds % 1000), s_sntmp_next));
|
||||
MG_DEBUG(("%u.%u", (unsigned) (milliseconds / 1000),
|
||||
(unsigned) (milliseconds % 1000)));
|
||||
}
|
||||
c->recv.len = 0; // Clear receive buffer
|
||||
} else if (ev == MG_EV_CONNECT) {
|
||||
mg_sntp_send(c, (unsigned long) time(NULL));
|
||||
mg_sntp_send(c, (unsigned long) 0);
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
}
|
||||
(void) fnd;
|
||||
@ -3075,12 +3079,11 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
void mg_sntp_send(struct mg_connection *c, unsigned long utc) {
|
||||
if (c->is_resolving) {
|
||||
MG_ERROR(("%lu wait until resolved", c->id));
|
||||
} else if (utc > s_sntmp_next) {
|
||||
} else {
|
||||
uint8_t buf[48] = {0};
|
||||
s_sntmp_next = utc + SNTP_INTERVAL_SEC;
|
||||
buf[0] = (0 << 6) | (4 << 3) | 3;
|
||||
mg_send(c, buf, sizeof(buf));
|
||||
MG_DEBUG(("%lu ct %lu, next at %lu", c->id, utc, s_sntmp_next));
|
||||
MG_DEBUG(("%lu ct %lu", c->id, utc));
|
||||
}
|
||||
}
|
||||
|
||||
@ -3834,7 +3837,9 @@ struct mg_str mg_str_n(const char *s, size_t n) {
|
||||
}
|
||||
|
||||
int mg_lower(const char *s) {
|
||||
return tolower(*(const unsigned char *) s);
|
||||
int c = *s;
|
||||
if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
|
||||
return c;
|
||||
}
|
||||
|
||||
int mg_ncasecmp(const char *s1, const char *s2, size_t len) {
|
||||
|
15
src/http.c
15
src/http.c
@ -118,14 +118,18 @@ int mg_http_get_var(const struct mg_str *buf, const char *name, char *dst,
|
||||
return len;
|
||||
}
|
||||
|
||||
static bool isx(int c) {
|
||||
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
|
||||
(c >= 'A' && c <= 'F');
|
||||
}
|
||||
|
||||
int mg_url_decode(const char *src, size_t src_len, char *dst, size_t dst_len,
|
||||
int is_form_url_encoded) {
|
||||
size_t i, j;
|
||||
for (i = j = 0; i < src_len && j + 1 < dst_len; i++, j++) {
|
||||
if (src[i] == '%') {
|
||||
// Use `i + 2 < src_len`, not `i < src_len - 2`, note small src_len
|
||||
if (i + 2 < src_len && isxdigit(*(const unsigned char *) (src + i + 1)) &&
|
||||
isxdigit(*(const unsigned char *) (src + i + 2))) {
|
||||
if (i + 2 < src_len && isx(src[i + 1]) && isx(src[i + 2])) {
|
||||
mg_unhex(src + i + 1, 2, (uint8_t *) &dst[j]);
|
||||
i += 2;
|
||||
} else {
|
||||
@ -141,11 +145,14 @@ int mg_url_decode(const char *src, size_t src_len, char *dst, size_t dst_len,
|
||||
return i >= src_len && j < dst_len ? (int) j : -1;
|
||||
}
|
||||
|
||||
static bool isok(uint8_t c) {
|
||||
return c == '\n' || c == '\r' || c >= ' ';
|
||||
}
|
||||
|
||||
int mg_http_get_request_len(const unsigned char *buf, size_t buf_len) {
|
||||
size_t i;
|
||||
for (i = 0; i < buf_len; i++) {
|
||||
if (!isprint(buf[i]) && buf[i] != '\r' && buf[i] != '\n' && buf[i] < 128)
|
||||
return -1;
|
||||
if (!isok(buf[i])) return -1;
|
||||
if ((i > 0 && buf[i] == '\n' && buf[i - 1] == '\n') ||
|
||||
(i > 3 && buf[i] == '\n' && buf[i - 1] == '\r' && buf[i - 2] == '\n'))
|
||||
return (int) i + 1;
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
#if MG_ENABLE_LOG
|
||||
static void mg_log_stdout(const void *buf, size_t len, void *userdata) {
|
||||
(void) userdata;
|
||||
(void) userdata, (void) buf, (void) len;
|
||||
#if MG_ENABLE_FILE
|
||||
fwrite(buf, 1, len, stdout);
|
||||
#endif
|
||||
|
14
src/sntp.c
14
src/sntp.c
@ -7,8 +7,6 @@
|
||||
#define SNTP_INTERVAL_SEC 3600
|
||||
#define SNTP_TIME_OFFSET 2208988800UL
|
||||
|
||||
static unsigned long s_sntmp_next;
|
||||
|
||||
int64_t mg_sntp_parse(const unsigned char *buf, size_t len) {
|
||||
int64_t res = -1;
|
||||
int mode = len > 0 ? buf[0] & 7 : 0;
|
||||
@ -26,7 +24,6 @@ int64_t mg_sntp_parse(const unsigned char *buf, size_t len) {
|
||||
unsigned long useconds = mg_ntohl(data[1]);
|
||||
// MG_DEBUG(("%lu %lu %lu", time(0), seconds, useconds));
|
||||
res = ((int64_t) seconds) * 1000 + (int64_t) ((useconds / 1000) % 1000);
|
||||
s_sntmp_next = seconds + SNTP_INTERVAL_SEC;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@ -36,12 +33,12 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len);
|
||||
if (milliseconds > 0) {
|
||||
mg_call(c, MG_EV_SNTP_TIME, &milliseconds);
|
||||
MG_DEBUG(("%u.%u, next at %lu", (unsigned) (milliseconds / 1000),
|
||||
(unsigned) (milliseconds % 1000), s_sntmp_next));
|
||||
MG_DEBUG(("%u.%u", (unsigned) (milliseconds / 1000),
|
||||
(unsigned) (milliseconds % 1000)));
|
||||
}
|
||||
c->recv.len = 0; // Clear receive buffer
|
||||
} else if (ev == MG_EV_CONNECT) {
|
||||
mg_sntp_send(c, (unsigned long) time(NULL));
|
||||
mg_sntp_send(c, (unsigned long) 0);
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
}
|
||||
(void) fnd;
|
||||
@ -51,12 +48,11 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
void mg_sntp_send(struct mg_connection *c, unsigned long utc) {
|
||||
if (c->is_resolving) {
|
||||
MG_ERROR(("%lu wait until resolved", c->id));
|
||||
} else if (utc > s_sntmp_next) {
|
||||
} else {
|
||||
uint8_t buf[48] = {0};
|
||||
s_sntmp_next = utc + SNTP_INTERVAL_SEC;
|
||||
buf[0] = (0 << 6) | (4 << 3) | 3;
|
||||
mg_send(c, buf, sizeof(buf));
|
||||
MG_DEBUG(("%lu ct %lu, next at %lu", c->id, utc, s_sntmp_next));
|
||||
MG_DEBUG(("%lu ct %lu", c->id, utc));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,9 @@ struct mg_str mg_str_n(const char *s, size_t n) {
|
||||
}
|
||||
|
||||
int mg_lower(const char *s) {
|
||||
return tolower(*(const unsigned char *) s);
|
||||
int c = *s;
|
||||
if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
|
||||
return c;
|
||||
}
|
||||
|
||||
int mg_ncasecmp(const char *s1, const char *s2, size_t len) {
|
||||
|
@ -1212,6 +1212,17 @@ static bool sn(const char *fmt, ...) {
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool sccmp(const char *s1, const char *s2) {
|
||||
int n1 = mg_casecmp(s1, s2);
|
||||
#if MG_ARCH == MG_ARCH_UNIX
|
||||
int n2 = strcasecmp(s1, s2);
|
||||
#else
|
||||
int n2 = mg_casecmp(s1, s2); // On MSVC98, _stricmp() is buggy
|
||||
#endif
|
||||
MG_INFO(("[%s] [%s] %d %d", s1, s2, n1, n2));
|
||||
return n1 == n2;
|
||||
}
|
||||
|
||||
static void test_str(void) {
|
||||
struct mg_str s = mg_strdup(mg_str("a"));
|
||||
ASSERT(mg_strcmp(s, mg_str("a")) == 0);
|
||||
@ -1223,6 +1234,12 @@ static void test_str(void) {
|
||||
ASSERT(mg_strstr(mg_str("abc"), mg_str("b")) != NULL);
|
||||
ASSERT(mg_strcmp(mg_str("hi"), mg_strstrip(mg_str(" \thi\r\n"))) == 0);
|
||||
|
||||
ASSERT(sccmp("", ""));
|
||||
ASSERT(sccmp("", "1"));
|
||||
ASSERT(sccmp("a", "A"));
|
||||
ASSERT(sccmp("a1", "A"));
|
||||
ASSERT(sccmp("a", "A1"));
|
||||
|
||||
ASSERT(sn("%d", 0));
|
||||
ASSERT(sn("%d", 1));
|
||||
ASSERT(sn("%d", -1));
|
||||
|
Loading…
x
Reference in New Issue
Block a user