3#line 1 "./src/internal.h"
10#ifndef CS_MONGOOSE_SRC_INTERNAL_H_
11#define CS_MONGOOSE_SRC_INTERNAL_H_
14#define MG_MALLOC malloc
18#define MG_CALLOC calloc
22#define MG_REALLOC realloc
30#define MBUF_REALLOC MG_REALLOC
34#define MBUF_FREE MG_FREE
37#define MG_SET_PTRPTR(_ptr, _v) \
39 if (_ptr) *(_ptr) = _v; \
43#define MG_INTERNAL static
48#define MG_DISABLE_FILESYSTEM
49#define MG_DISABLE_POPEN
51#define MG_DISABLE_DIRECTORY_LISTING
52#define MG_DISABLE_SOCKETPAIR
59#define MG_CTL_MSG_MESSAGE_SIZE 8192
67 int *proto,
char *host,
size_t host_len);
76#ifndef MG_DISABLE_FILESYSTEM
102#ifndef MG_DISABLE_FILESYSTEM
112#ifndef MG_DISABLE_MQTT
118extern void *(*test_malloc)(
size_t size);
119extern void *(*test_calloc)(
size_t count,
size_t size);
122#ifdef MG_MODULE_LINES
123#line 1 "./src/../../common/base64.c"
130#ifndef EXCLUDE_COMMON
137#define NUM_UPPERCASES ('Z' - 'A' + 1)
138#define NUM_LETTERS (NUM_UPPERCASES * 2)
139#define NUM_DIGITS ('9' - '0' + 1)
184 const unsigned char *src = (
const unsigned char *)
str;
186 for (
i = 0;
i <
len;
i++) {
206#define BASE64_ENCODE_BODY \
207 static const char *b64 = \
208 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; \
211 for (i = j = 0; i < src_len; i += 3) { \
213 b = i + 1 >= src_len ? 0 : src[i + 1]; \
214 c = i + 2 >= src_len ? 0 : src[i + 2]; \
216 BASE64_OUT(b64[a >> 2]); \
217 BASE64_OUT(b64[((a & 3) << 4) | (b >> 4)]); \
218 if (i + 1 < src_len) { \
219 BASE64_OUT(b64[(b & 15) << 2 | (c >> 6)]); \
221 if (i + 2 < src_len) { \
222 BASE64_OUT(b64[c & 63]); \
226 while (j % 4 != 0) { \
231#define BASE64_OUT(ch) \
236#define BASE64_FLUSH() \
248#ifndef CS_DISABLE_STDIO
249#define BASE64_OUT(ch) \
251 fprintf(f, "%c", (ch)); \
255#define BASE64_FLUSH()
268 static const unsigned char tab[128] = {
302 return tab[
ch & 127];
306 unsigned char a, b,
c,
d;
313 if (a == 200 || b == 200)
break;
325#ifdef MG_MODULE_LINES
326#line 1 "./src/../../common/cs_dbg.c"
341#ifdef CS_ENABLE_DEBUG
347#ifndef CS_DISABLE_STDIO
384#if defined(CS_LOG_TS_DIFF) && !defined(CS_DISABLE_STDIO)
388#ifdef MG_MODULE_LINES
389#line 1 "./src/../../common/cs_dirent.c"
396#ifndef EXCLUDE_COMMON
410#define MG_MALLOC malloc
429 dir->result.d_name[0] =
'\0';
444 result =
FindClose(dir->handle) ? 0 : -1;
459 result = &dir->result;
461 result->d_name,
sizeof(result->d_name),
NULL,
480#ifdef CS_ENABLE_SPIFFS
508int rmdir(
const char *path) {
526#ifdef MG_MODULE_LINES
527#line 1 "./src/../../common/cs_time.c"
536#if !defined(CS_PLATFORM) || \
537 (CS_PLATFORM != CS_P_CC3200 && CS_PLATFORM != CS_P_MSP432)
549 now = (
double)
tv.tv_sec + (((
double)
tv.tv_usec) / 1000000.0);
555#ifdef MG_MODULE_LINES
556#line 1 "./src/../deps/frozen/frozen.c"
577#ifndef _CRT_SECURE_NO_WARNINGS
578#define _CRT_SECURE_NO_WARNINGS
588#define snprintf _snprintf
591#ifndef FROZEN_REALLOC
592#define FROZEN_REALLOC realloc
596#define FROZEN_FREE free
611#define EXPECT(cond, err_code) \
613 if (!(cond)) return (err_code); \
618 if (_n < 0) return _n; \
620#define END_OF_STRING (-1)
627 return ch ==
' ' ||
ch ==
'\t' ||
ch ==
'\r' ||
ch ==
'\n';
657 return (
ch >=
'a' &&
ch <=
'z') || (
ch >=
'A' &&
ch <=
'Z');
661 return ch >=
'0' &&
ch <=
'9';
727 if ((
ch & 0x80) == 0)
return 1;
744 ch = *(
unsigned char *) f->
cur;
751 }
else if (
ch ==
'"') {
764 if (
ch ==
'-') f->
cur++;
768 if (f->
cur < f->
end && f->
cur[0] ==
'.') {
774 if (f->
cur < f->
end && (f->
cur[0] ==
'e' || f->
cur[0] ==
'E')) {
777 if ((f->
cur[0] ==
'+' || f->
cur[0] ==
'-')) f->
cur++;
792 while (
cur(f) !=
']') {
794 if (
cur(f) ==
',') f->
cur++;
804 return i ==
len ? 1 : 0;
811 for (
i = 0;
i <
len;
i++) {
872 }
else if (
ch ==
'"') {
894 while (
cur(f) !=
'}') {
896 if (
cur(f) ==
',') f->
cur++;
954 while (p[
i] !=
'\0' && p[
i] !=
'[' && p[
i] !=
'.')
i++;
959 while (path != 0 && path[0] !=
'\0') {
961 if (path[0] ==
'[') {
963 for (
ind = 0,
n = 1; path[
n] !=
']' && path[
n] !=
'\0';
n++) {
966 ind += path[
n] -
'0';
968 if (path[
n++] !=
']')
return 0;
988 if (path[0] ==
'.') path++;
989 if (path[0] ==
'\0')
return &
toks[
i];
1015 if (s < end) *s = x; \
1078 while (*fmt !=
'\0') {
1152#ifdef MG_MODULE_LINES
1153#line 1 "./src/../../common/md5.c"
1172#if !defined(DISABLE_MD5) && !defined(EXCLUDE_COMMON)
1176#ifndef CS_ENABLE_NATIVE_MD5
1179#if BYTE_ORDER == BIG_ENDIAN
1182 ((
unsigned) buf[1] << 8 | buf[0]);
1192#define F1(x, y, z) (z ^ (x & (y ^ z)))
1193#define F2(x, y, z) F1(z, x, y)
1194#define F3(x, y, z) (x ^ y ^ z)
1195#define F4(x, y, z) (y ^ (x | ~z))
1197#define MD5STEP(f, w, x, y, z, data, s) \
1198 (w += f(x, y, z) + data, w = w << s | w >> (32 - s), w += x)
1205 ctx->
buf[0] = 0x67452301;
1206 ctx->
buf[1] = 0xefcdab89;
1207 ctx->
buf[2] = 0x98badcfe;
1208 ctx->
buf[3] = 0x10325476;
1303 t = (t >> 3) & 0x3f;
1306 unsigned char *p = (
unsigned char *) ctx->
in + t;
1352 a[14] = ctx->
bits[0];
1353 a[15] = ctx->
bits[1];
1358 memset((
char *) ctx, 0,
sizeof(*ctx));
1368 static const char *
hex =
"0123456789abcdef";
1370 for (;
len--; p++) {
1371 *
to++ =
hex[p[0] >> 4];
1372 *
to++ =
hex[p[0] & 0x0f];
1378 unsigned char hash[16];
1379 const unsigned char *p;
1386 while ((p =
va_arg(
ap,
const unsigned char *) ) !=
NULL) {
1399#ifdef MG_MODULE_LINES
1400#line 1 "./src/../../common/mbuf.c"
1407#ifndef EXCLUDE_COMMON
1414#define MBUF_REALLOC realloc
1418#define MBUF_FREE free
1460 if (~(
size_t) 0 - (
size_t) a->
buf <
len)
return 0;
1496#ifdef MG_MODULE_LINES
1497#line 1 "./src/../../common/sha1.c"
1502#if !defined(DISABLE_SHA1) && !defined(EXCLUDE_COMMON)
1512 unsigned char c[64];
1516#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
1520#if BYTE_ORDER == LITTLE_ENDIAN
1536 (block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] ^ \
1537 block->l[(i + 2) & 15] ^ block->l[i & 15], \
1539#define R0(v, w, x, y, z, i) \
1540 z += ((w & (x ^ y)) ^ y) + blk0(block, i) + 0x5A827999 + rol(v, 5); \
1542#define R1(v, w, x, y, z, i) \
1543 z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
1545#define R2(v, w, x, y, z, i) \
1546 z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
1548#define R3(v, w, x, y, z, i) \
1549 z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
1551#define R4(v, w, x, y, z, i) \
1552 z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
1565 R0(a, b,
c,
d,
e, 0);
1566 R0(
e, a, b,
c,
d, 1);
1567 R0(
d,
e, a, b,
c, 2);
1568 R0(
c,
d,
e, a, b, 3);
1569 R0(b,
c,
d,
e, a, 4);
1570 R0(a, b,
c,
d,
e, 5);
1571 R0(
e, a, b,
c,
d, 6);
1572 R0(
d,
e, a, b,
c, 7);
1573 R0(
c,
d,
e, a, b, 8);
1574 R0(b,
c,
d,
e, a, 9);
1575 R0(a, b,
c,
d,
e, 10);
1576 R0(
e, a, b,
c,
d, 11);
1577 R0(
d,
e, a, b,
c, 12);
1578 R0(
c,
d,
e, a, b, 13);
1579 R0(b,
c,
d,
e, a, 14);
1580 R0(a, b,
c,
d,
e, 15);
1581 R1(
e, a, b,
c,
d, 16);
1582 R1(
d,
e, a, b,
c, 17);
1583 R1(
c,
d,
e, a, b, 18);
1584 R1(b,
c,
d,
e, a, 19);
1585 R2(a, b,
c,
d,
e, 20);
1586 R2(
e, a, b,
c,
d, 21);
1587 R2(
d,
e, a, b,
c, 22);
1588 R2(
c,
d,
e, a, b, 23);
1589 R2(b,
c,
d,
e, a, 24);
1590 R2(a, b,
c,
d,
e, 25);
1591 R2(
e, a, b,
c,
d, 26);
1592 R2(
d,
e, a, b,
c, 27);
1593 R2(
c,
d,
e, a, b, 28);
1594 R2(b,
c,
d,
e, a, 29);
1595 R2(a, b,
c,
d,
e, 30);
1596 R2(
e, a, b,
c,
d, 31);
1597 R2(
d,
e, a, b,
c, 32);
1598 R2(
c,
d,
e, a, b, 33);
1599 R2(b,
c,
d,
e, a, 34);
1600 R2(a, b,
c,
d,
e, 35);
1601 R2(
e, a, b,
c,
d, 36);
1602 R2(
d,
e, a, b,
c, 37);
1603 R2(
c,
d,
e, a, b, 38);
1604 R2(b,
c,
d,
e, a, 39);
1605 R3(a, b,
c,
d,
e, 40);
1606 R3(
e, a, b,
c,
d, 41);
1607 R3(
d,
e, a, b,
c, 42);
1608 R3(
c,
d,
e, a, b, 43);
1609 R3(b,
c,
d,
e, a, 44);
1610 R3(a, b,
c,
d,
e, 45);
1611 R3(
e, a, b,
c,
d, 46);
1612 R3(
d,
e, a, b,
c, 47);
1613 R3(
c,
d,
e, a, b, 48);
1614 R3(b,
c,
d,
e, a, 49);
1615 R3(a, b,
c,
d,
e, 50);
1616 R3(
e, a, b,
c,
d, 51);
1617 R3(
d,
e, a, b,
c, 52);
1618 R3(
c,
d,
e, a, b, 53);
1619 R3(b,
c,
d,
e, a, 54);
1620 R3(a, b,
c,
d,
e, 55);
1621 R3(
e, a, b,
c,
d, 56);
1622 R3(
d,
e, a, b,
c, 57);
1623 R3(
c,
d,
e, a, b, 58);
1624 R3(b,
c,
d,
e, a, 59);
1625 R4(a, b,
c,
d,
e, 60);
1626 R4(
e, a, b,
c,
d, 61);
1627 R4(
d,
e, a, b,
c, 62);
1628 R4(
c,
d,
e, a, b, 63);
1629 R4(b,
c,
d,
e, a, 64);
1630 R4(a, b,
c,
d,
e, 65);
1631 R4(
e, a, b,
c,
d, 66);
1632 R4(
d,
e, a, b,
c, 67);
1633 R4(
c,
d,
e, a, b, 68);
1634 R4(b,
c,
d,
e, a, 69);
1635 R4(a, b,
c,
d,
e, 70);
1636 R4(
e, a, b,
c,
d, 71);
1637 R4(
d,
e, a, b,
c, 72);
1638 R4(
c,
d,
e, a, b, 73);
1639 R4(b,
c,
d,
e, a, 74);
1640 R4(a, b,
c,
d,
e, 75);
1641 R4(
e, a, b,
c,
d, 76);
1642 R4(
d,
e, a, b,
c, 77);
1643 R4(
c,
d,
e, a, b, 78);
1644 R4(b,
c,
d,
e, a, 79);
1653 a = b =
c =
d =
e = 0;
1662 context->
state[0] = 0x67452301;
1663 context->
state[1] = 0xEFCDAB89;
1664 context->
state[2] = 0x98BADCFE;
1665 context->
state[3] = 0x10325476;
1666 context->
state[4] = 0xC3D2E1F0;
1675 if ((context->
count[0] += len << 3) <
j) context->
count[1]++;
1676 context->
count[1] += (len >> 29);
1678 if ((
j + len) > 63) {
1681 for (;
i + 63 < len;
i += 64) {
1694 for (
i = 0;
i < 8;
i++) {
1696 ((3 - (
i & 3)) * 8)) &
1701 while ((context->
count[0] & 504) != 448) {
1706 for (
i = 0;
i < 20;
i++) {
1708 (
unsigned char) ((context->
state[
i >> 2] >> ((3 - (
i & 3)) * 8)) & 255);
1710 memset(context,
'\0',
sizeof(*context));
1716 unsigned char out[20]) {
1733 for (
i = 0;
i <
sizeof(
buf1);
i++) {
1750#ifdef MG_MODULE_LINES
1751#line 1 "./src/../../common/str_util.c"
1758#ifndef EXCLUDE_COMMON
1765 for (;
l <
maxlen && s[
l] !=
'\0';
l++) {
1770#define C_SNPRINTF_APPEND_CHAR(ch) \
1772 if (i < (int) buf_size) buf[i] = ch; \
1776#define C_SNPRINTF_FLAG_ZERO 1
1778#ifdef C_DISABLE_BUILTIN_SNPRINTF
1786 int i = 0,
k = 0,
neg = 0;
1827 while ((
ch = *fmt++) !=
'\0') {
1847 while (*fmt >=
'0' && *fmt <=
'9') {
1864 while (*fmt >=
'0' && *fmt <=
'9') {
1895 const char *s =
va_arg(
ap,
const char *);
1898 for (
j = 0;
j <
pad;
j++) {
1909 }
else if (
ch ==
'c') {
1915 }
else if (
ch ==
'd' &&
len_mod ==
'l') {
1919 }
else if (
ch ==
'd' &&
len_mod ==
'z') {
1923 }
else if (
ch ==
'd' &&
len_mod ==
'q') {
1926 }
else if ((
ch ==
'x' ||
ch ==
'u') &&
len_mod == 0) {
1929 }
else if ((
ch ==
'x' ||
ch ==
'u') &&
len_mod ==
'l') {
1932 }
else if ((
ch ==
'x' ||
ch ==
'u') &&
len_mod ==
'z') {
1935 }
else if (
ch ==
'p') {
1954 buf[
i < (
int) buf_size ?
i : (
int) buf_size - 1] =
'\0';
1961int c_snprintf(
char *buf,
size_t buf_size,
const char *fmt, ...) {
1975 strncpy(buf, path,
sizeof(buf));
1976 buf[
sizeof(buf) - 1] =
'\0';
1979 p = buf +
strlen(buf) - 1;
1980 while (p > buf && p[-1] !=
':' && (p[0] ==
'\\' || p[0] ==
'/')) *p-- =
'\0';
2019#ifdef MG_MODULE_LINES
2020#line 1 "./src/net.c"
2046#define MG_MAX_HOST_LEN 200
2048#define MG_COPY_COMMON_CONNECTION_OPTIONS(dst, src) \
2049 memcpy(dst, src, sizeof(*dst));
2052#define _MG_ALLOWED_CONNECT_FLAGS_MASK \
2053 (MG_F_USER_1 | MG_F_USER_2 | MG_F_USER_3 | MG_F_USER_4 | MG_F_USER_5 | \
2054 MG_F_USER_6 | MG_F_WEBSOCKET_NO_DEFRAG)
2056#define _MG_CALLBACK_MODIFIABLE_FLAGS_MASK \
2057 (MG_F_USER_1 | MG_F_USER_2 | MG_F_USER_3 | MG_F_USER_4 | MG_F_USER_5 | \
2058 MG_F_USER_6 | MG_F_WEBSOCKET_NO_DEFRAG | MG_F_SEND_AND_CLOSE | \
2059 MG_F_CLOSE_IMMEDIATELY | MG_F_IS_WEBSOCKET | MG_F_DELETE_CHUNK)
2062#define intptr_t long
2074 DBG((
"%p %p", mgr,
c));
2100 DBG((
"%p %s ev=%d ev_data=%p flags=%lu rmbl=%d smbl=%d", nc,
2104#if !defined(NO_LIBC) && !defined(MG_DISABLE_HEXDUMP)
2131 DBG((
"%p after %s flags=%lu rmbl=%d smbl=%d", nc,
2137 if (
c->ev_timer_time > 0 &&
now >=
c->ev_timer_time) {
2145 c->ev_timer_time = 0;
2168 memset(conn, 0,
sizeof(*conn));
2173 DBG((
"%p %lu", conn, conn->
flags));
2180 memset(m, 0,
sizeof(*m));
2181#ifndef MG_DISABLE_SOCKETPAIR
2191#elif defined(__unix__)
2208 DBG((
"=================================="));
2209 DBG((
"init mgr=%p", m));
2212#ifdef MG_ENABLE_JAVASCRIPT
2242 if (m ==
NULL)
return;
2246#ifndef MG_DISABLE_SOCKETPAIR
2283#ifndef MG_DISABLE_SYNC_RESOLVER
2286#ifdef MG_ENABLE_GETADDRINFO
2374 int *proto,
char *host,
size_t host_len) {
2375 unsigned int a, b,
c,
d, port = 0;
2377#ifdef MG_ENABLE_IPV6
2398 if (
sscanf(
str,
"%u.%u.%u.%u:%u%n", &a, &b, &
c, &
d, &port, &len) == 5) {
2400 sa->
sin.sin_addr.s_addr =
2403#ifdef MG_ENABLE_IPV6
2404 }
else if (
sscanf(
str,
"[%99[^]]]:%u%n",
buf, &port, &len) == 2 &&
2410#ifndef MG_DISABLE_RESOLVER
2412 sscanf(
str,
"%[^ :]:%u%n", host, &port, &len) == 2) {
2418 }
else if (
sscanf(
str,
":%u%n", &port, &len) == 1 ||
2419 sscanf(
str,
"%u%n", &port, &len) == 1) {
2427 return port < 0xffffUL && (
ch ==
'\0' ||
ch ==
',' ||
isspace(
ch)) ? len : -1;
2436#ifndef MG_DISABLE_PFS
2442#if defined(MG_SSL_CRYPTO_MODERN)
2443 "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:"
2444 "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:"
2445 "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:"
2446 "ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:"
2447 "ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:"
2448 "ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:"
2449 "DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:"
2450 "DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:"
2451 "!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK"
2452#elif defined(MG_SSL_CRYPTO_OLD)
2453 "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:"
2454 "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:"
2455 "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:"
2456 "ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:"
2457 "ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:"
2458 "ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:"
2459 "DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:"
2460 "DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:"
2461 "ECDHE-ECDSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:"
2462 "AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:DES-CBC3-SHA:"
2463 "HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:"
2464 "!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA"
2466 "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:"
2467 "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:"
2468 "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:"
2470 "ECDHE-ECDSA-AES128-SHA256:"
2472 "ECDHE-ECDSA-AES128-SHA:"
2474 "ECDHE-ECDSA-AES256-SHA384:"
2476 "ECDHE-ECDSA-AES256-SHA:"
2480 "AES128-GCM-SHA256:AES256-GCM-SHA384:"
2481 "AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:"
2483 "!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!RSA:!CBC-SHA:!CAMELLIA:"
2484 "!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA"
2494-----BEGIN DH PARAMETERS-----\n\
2495MIIBCAKCAQEAlvbgD/qh9znWIlGFcV0zdltD7rq8FeShIqIhkQ0C7hYFThrBvF2E\n\
2496Z9bmgaP+sfQwGpVlv9mtaWjvERbu6mEG7JTkgmVUJrUt/wiRzwTaCXBqZkdUO8Tq\n\
2497+E6VOEQAilstG90ikN1Tfo+K6+X68XkRUIlgawBTKuvKVwBhuvlqTGerOtnXWnrt\n\
2498ym//hd3cd5PBYGBix0i7oR4xdghvfR2WLVu0LgdThTBb6XP7gLd19cQ1JuBtAajZ\n\
2499wMuPn7qlUkEFDIkAZy59/Hue/H2Q2vU/JsvVhHWCQBL4F1ofEAt50il6ZxR1QfFK\n\
25009VGKDC4oOgm9DlxwwBoC2FjqmvQlqVV3kwIBAg==\n\
2501-----END DH PARAMETERS-----\n";
2523#ifndef MG_DISABLE_PFS
2572 const char *result =
NULL;
2576 return "SSL for UDP is not supported";
2590 result =
"SSL_CTX_new() failed";
2593 result =
"SSL_CTX_new() failed";
2595 result =
"Invalid ssl cert";
2597 result =
"Invalid CA cert";
2600 result =
"SSL_new() failed";
2612#ifndef MG_DISABLE_PFS
2649#if !defined(NO_LIBC) && !defined(MG_DISABLE_HEXDUMP)
2666 DBG((
"%p discarded %d bytes", nc, len));
2694 DBG((
"%p %u", nc, (
unsigned int) len));
2745 DBG((
"%p %s://%s:%hu", nc, proto ==
SOCK_DGRAM ?
"udp" :
"tcp",
2759 DBG((
"%p connect, err=%d", nc,
err));
2767#ifndef MG_DISABLE_RESOLVER
2834 sizeof(host))) < 0) {
2854 if (
opts.ssl_server_name ==
NULL)
opts.ssl_server_name = host;
2860 "Server name verification requested but is not supported");
2869#ifndef MG_DISABLE_RESOLVER
2935 DBG((
"Failed to open listener: %d",
rc));
2959#ifndef MG_DISABLE_SOCKETPAIR
2986 return n >= 0 &&
n <= 255;
2990 int n, a, b,
c,
d,
slash = 32, len = 0;
3015 if ((
flag !=
'+' &&
flag !=
'-') ||
3020 if (
net == (remote_ip &
mask)) {
3036 double result =
c->ev_timer_time;
3037 c->ev_timer_time = timestamp;
3044 (
unsigned long) timestamp));
3072#ifdef MG_MODULE_LINES
3073#line 1 "./src/net_if_socket.c"
3080#ifndef MG_DISABLE_SOCKET_IF
3085#define MG_TCP_RECV_BUFFER_SIZE 1024
3086#define MG_UDP_RECV_BUFFER_SIZE 1500
3096 unsigned long on = 1;
3098#elif defined(MG_SOCKET_SIMPLELINK)
3100 opt.NonblockingEnabled = 1;
3109#ifdef MG_SOCKET_SIMPLELINK
3115#ifdef MG_SOCKET_SIMPLELINK
3134#if !defined(MG_SOCKET_SIMPLELINK) && !defined(MG_ESP8266)
3139 DBG((
"%p sock %d err %d", nc, nc->
sock, nc->
err));
3212 DBG((
"%p: failed to accept: %d", lc,
errno));
3242 (
sa->sa.sa_family ==
AF_INET) ?
sizeof(
sa->sin) :
sizeof(
sa->sin6);
3244#if !defined(MG_SOCKET_SIMPLELINK) && !defined(MG_LWIP)
3251#if defined(_WIN32) && defined(SO_EXCLUSIVEADDRUSE)
3257#if !defined(_WIN32) || !defined(SO_EXCLUSIVEADDRUSE)
3273#if !defined(MG_SOCKET_SIMPLELINK) && \
3293 if (
io->len == 0)
return;
3296 assert(
io->len > 0);
3314 DBG((
"%p %d bytes -> %d (SSL)", nc,
n, nc->
sock));
3332 DBG((
"%p %d bytes -> %d", nc,
n, nc->
sock));
3364 DBG((
"%p %d bytes <- %d (SSL)", conn,
n, conn->
sock));
3385 DBG((
"%p %d bytes (PLAIN) <- %d", conn,
n, conn->
sock));
3401 DBG((
"Out of memory"));
3467#define _MG_F_FD_CAN_READ 1
3468#define _MG_F_FD_CAN_WRITE 1 << 1
3469#define _MG_F_FD_ERROR 1 << 2
3472 DBG((
"%p fd=%d fd_flags=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->
sock,
3478#if !defined(MG_SOCKET_SIMPLELINK) && !defined(MG_ESP8266)
3483 if (ret != 0) err = 1;
3492 if (nc->
ssl !=
NULL && err == 0) {
3501 }
else if (nc->
err != 0) {
3534 DBG((
"%p after fd=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->
sock, nc->
flags,
3538#ifndef MG_DISABLE_SOCKETPAIR
3544 DBG((
"read %d from ctl socket", len));
3565 DBG((
"%p using select()",
mgr));
3566#ifndef MG_DISABLE_SOCKETPAIR
3606#ifndef MG_DISABLE_SOCKETPAIR
3664#ifndef MG_DISABLE_SOCKETPAIR
3682#ifdef MG_SOCKET_SIMPLELINK
3710#ifndef MG_DISABLE_SOCKETPAIR
3722 sa.sin.sin_addr.s_addr =
htonl(0x7f000001);
3725 }
else if (
bind(sock, &
sa.sa, len) != 0) {
3729 }
else if (connect(
sp[0], &
sa.sa, len) != 0) {
3732 connect(sock, &
sa.sa, len) != 0)) {
3754#ifndef MG_SOCKET_SIMPLELINK
3775#ifndef MG_SOCKET_SIMPLELINK
3786#ifdef MG_MODULE_LINES
3787#line 1 "./src/multithreading.c"
3797#ifdef MG_ENABLE_THREADS
3898#ifdef MG_MODULE_LINES
3899#line 1 "./src/uri.c"
3917 for (; *p <
end; (*p)++) {
3922 res->len = (*p) -
res->p;
3923 if (*p <
end) (*p)++;
3928 unsigned int *port,
struct mg_str *path,
struct mg_str *query,
3932 unsigned int rport = 0;
3942 const char *
p = uri.
p, *
end =
p + uri.
len;
3952 for (;
p <
end;
p++) {
3956 }
else if (*
p ==
'/') {
3981 for (;
p <
end;
p++) {
3985 }
else if (*
p ==
'/') {
3989 if (
p ==
end || *
p ==
'/') {
3999 for (;
p <
end;
p++) {
4003 }
else if (*
p ==
'/') {
4012 for (;
p <
end;
p++) {
4032 if (host != 0) *host =
rhost;
4033 if (port != 0) *port =
rport;
4034 if (path != 0) *path =
rpath;
4035 if (query != 0) *query =
rquery;
4043 const char *s = in->
p, *
se = s + in->
len;
4044 char *
cp = (
char *)
out->p, *
d;
4046 if (in->
len == 0 || *s !=
'/') {
4054 const char *next = s;
4061 if (
d >
cp + 1 && *(
d - 1) ==
'/')
d--;
4062 while (
d >
cp && *(
d - 1) !=
'/')
d--;
4069 if (
d ==
cp) *
d++ =
'/';
4075#ifdef MG_MODULE_LINES
4076#line 1 "./src/http.c"
4083#ifndef MG_DISABLE_HTTP
4090#ifndef MG_DISABLE_HTTP_WEBSOCKET
4091#define MG_WS_NO_HOST_HEADER_MAGIC ((char *) 0x1)
4140#ifndef MG_DISABLE_FILESYSTEM
4143#ifndef MG_DISABLE_CGI
4146#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART
4158 if (
c->proto_data ==
NULL) {
4169#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART
4172 free((
void *)
mp->boundary);
4174 free((
void *)
mp->var_name);
4176 free((
void *)
mp->file_name);
4181#ifndef MG_DISABLE_FILESYSTEM
4184 if (
d->fp !=
NULL) {
4192#ifndef MG_DISABLE_CGI
4217#ifndef MG_DISABLE_FILESYSTEM
4220#ifndef MG_DISABLE_CGI
4223#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART
4248#define MIME_ENTRY(_ext, _type) \
4249 { _ext, sizeof(_ext) - 1, _type }
4250static const struct {
4261 MIME_ENTRY(
"js",
"application/x-javascript"),
4269 MIME_ENTRY(
"torrent",
"application/x-bittorrent"),
4283 MIME_ENTRY(
"exe",
"application/octet-stream"),
4284 MIME_ENTRY(
"zip",
"application/x-zip-compressed"),
4289 MIME_ENTRY(
"arj",
"application/x-arj-compressed"),
4290 MIME_ENTRY(
"rar",
"application/x-rar-compressed"),
4293 MIME_ENTRY(
"swf",
"application/x-shockwave-flash"),
4305#ifndef MG_DISABLE_FILESYSTEM
4307#ifndef MG_DISABLE_DAV
4357 const unsigned char *buf = (
unsigned char *) s;
4361 if (!
isprint(buf[
i]) && buf[
i] !=
'\r' && buf[
i] !=
'\n' && buf[
i] < 128) {
4363 }
else if (buf[
i] ==
'\n' &&
i + 1 <
buf_len && buf[
i + 1] ==
'\n') {
4365 }
else if (buf[
i] ==
'\n' &&
i + 2 <
buf_len && buf[
i + 1] ==
'\r' &&
4366 buf[
i + 2] ==
'\n') {
4383 while (v->len > 0 && v->p[v->len - 1] ==
' ') {
4387 if (
k->len == 0 || v->len == 0) {
4389 k->len = v->len = 0;
4403 const char *
end, *
qs;
4406 if (
len <= 0)
return len;
4410 hm->body.p = s +
len;
4411 hm->message.len =
hm->body.len = (
size_t) ~0;
4415 while (s <
end &&
isspace(*(
unsigned char *) s)) s++;
4422 if (
hm->uri.p <=
hm->method.p ||
hm->proto.p <=
hm->uri.p)
return -1;
4426 hm->query_string.p =
qs + 1;
4427 hm->query_string.len = &
hm->uri.p[
hm->uri.len] - (
qs + 1);
4428 hm->uri.len =
qs -
hm->uri.p;
4432 if (
end - s < 4 || s[3] !=
' ')
return -1;
4434 if (
hm->resp_code < 100 ||
hm->resp_code >= 600)
return -1;
4456 if (
hm->body.len == (
size_t) ~0 &&
is_req &&
4460 hm->message.len =
len;
4469 for (
i = 0;
hm->header_names[
i].len > 0;
i++) {
4470 struct mg_str *h = &
hm->header_names[
i], *v = &
hm->header_values[
i];
4478#ifndef MG_DISABLE_HTTP_WEBSOCKET
4481 return (flags & 0x80) == 0 || (flags & 0x0f) == 0;
4485 return (flags & 0x80) == 0 && (flags & 0x0f) != 0;
4490 if (
wsm->flags & 0x8) {
4501 unsigned char *
p = (
unsigned char *) nc->
recv_mbuf.
buf, *buf =
p,
4503 unsigned *
sizep = (
unsigned *) &
p[1];
4510 buf += 1 +
sizeof(*sizep) + *
sizep;
4542 for (
i = 0;
i < data_len;
i++) {
4552 buf = p + 1 +
sizeof(*sizep);
4558 (*sizep) +=
wsm.size;
4562 if (
wsm.flags & 0x80) {
4563 wsm.data = p + 1 +
sizeof(*sizep);
4604#ifdef MG_DISABLE_WS_RANDOM_MASK
4607 if (
sizeof(
long) >= 4) {
4609 }
else if (
sizeof(
long) == 2) {
4619 unsigned char header[10];
4625 }
else if (len < 65535) {
4642 header[1] |= 1 << 7;
4655 if (ctx->
pos == 0)
return;
4664 DBG((
"%p %d %d", nc,
op, (
int) len));
4698 const char *fmt, ...) {
4709 if (buf !=
mem && buf !=
NULL) {
4740 static const char *
magic =
"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
4752 "HTTP/1.1 101 Switching Protocols\r\n"
4753 "Upgrade: websocket\r\n"
4754 "Connection: Upgrade\r\n"
4755 "Sec-WebSocket-Accept: ",
4762#ifndef MG_DISABLE_FILESYSTEM
4771 if (
io->len <
sizeof(
buf)) {
4781 }
else if (
pd->file.sent <
pd->file.cl &&
4787#ifdef MG_DISABLE_HTTP_KEEP_ALIVE
4800 if (
n == 0 ||
pd->file.sent >=
pd->file.cl) {
4802#ifdef MG_DISABLE_HTTP_KEEP_ALIVE
4807#ifndef MG_DISABLE_CGI
4808 else if (
pd->cgi.cgi_nc !=
NULL) {
4810 if (
pd->cgi.cgi_nc !=
NULL) {
4827 unsigned char *s = (
unsigned char *)
buf;
4834 n += (s[
i] >=
'0' && s[
i] <=
'9') ? s[
i] -
'0' :
tolower(s[
i]) -
'a' + 10;
4839 if (
i == 0 ||
i + 2 >
len || s[
i] !=
'\r' || s[
i + 1] !=
'\n') {
4852 if (
i == 0 ||
i + 2 >
len || s[
i] !=
'\r' || s[
i + 1] !=
'\n') {
4866 body_len =
pd->chunk.body_len;
4867 assert(
blen >= body_len);
4875 body_len += data_len;
4876 hm->body.len = body_len;
4878 if (data_len == 0) {
4891 pd->chunk.body_len = body_len;
4899 memset(buf, 0, body_len);
4902 hm->body.len =
pd->chunk.body_len = 0;
4906 hm->message.len =
pd->chunk.body_len +
blen -
i;
4927 while (ep !=
NULL) {
4948 pd->endpoint_handler =
4957#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART
4990#ifndef MG_DISABLE_HTTP_WEBSOCKET
4994#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART
4995 if (
pd->mp_stream.boundary !=
NULL) {
5004 mp.var_name =
pd->mp_stream.var_name;
5005 mp.file_name =
pd->mp_stream.file_name;
5016 hm->message.len =
io->len;
5017 hm->body.len =
io->buf +
io->len -
hm->body.p;
5022#ifndef MG_DISABLE_FILESYSTEM
5024 printf(
"nc %p, pd %p, file fp %p cl %d\n", nc,
pd,
pd->file.fp, (
int)
pd->file.cl);
5026 assert(
pd->file.fp ==
NULL);
5027 if (
pd->file.fp !=
NULL) {
5037#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART
5038 if (
pd->mp_stream.boundary !=
NULL) {
5052#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART
5054 s->
len >= 9 &&
strncmp(s->
p,
"multipart", 9) == 0) {
5064 DBG((
"invalid request"));
5069#ifndef MG_DISABLE_HTTP_WEBSOCKET
5096 }
else if (
hm->message.len <=
io->len) {
5101#ifdef MG_ENABLE_JAVASCRIPT
5124 for (
i = 0;
hm->header_names[
i].len > 0;
i++) {
5161#ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART
5183 if (
ct->len < 9 ||
strncmp(
ct->p,
"multipart", 9) != 0) {
5189 if (boundary_len == 0) {
5195 DBG((
"invalid request"));
5201 if (
pd->mp_stream.boundary !=
NULL) {
5208 pd->mp_stream.boundary =
strdup(boundary);
5209 pd->mp_stream.boundary_len =
strlen(boundary);
5210 pd->mp_stream.var_name =
pd->mp_stream.file_name =
NULL;
5213 if (
pd->endpoint_handler ==
NULL) {
5225#define CONTENT_DISPOSITION "Content-Disposition: "
5228 const char *
data,
size_t data_len) {
5233 mp.var_name =
pd->mp_stream.var_name;
5234 mp.file_name =
pd->mp_stream.file_name;
5235 mp.user_data =
pd->mp_stream.user_data;
5237 mp.data.len = data_len;
5239 pd->mp_stream.user_data =
mp.user_data;
5244 struct mbuf *
io = &
c->recv_mbuf;
5247 pd->mp_stream.prev_io_len);
5249 pd->mp_stream.prev_io_len = 0;
5266 const char *boundary;
5267 struct mbuf *
io = &
c->recv_mbuf;
5270 if ((
int)
io->len <
pd->mp_stream.boundary_len + 2) {
5275 if (boundary !=
NULL) {
5276 if (
io->len - (boundary -
io->buf) < 4) {
5279 if (
memcmp(boundary +
pd->mp_stream.boundary_len,
"--", 2) == 0) {
5294 struct mbuf *
io = &
c->recv_mbuf;
5302 while (data_size > 0 &&
5322 if (
pd->mp_stream.processing_part != 0) {
5326 free((
void *)
pd->mp_stream.file_name);
5328 free((
void *)
pd->mp_stream.var_name);
5333 pd->mp_stream.processing_part++;
5347 struct mbuf *
io = &
c->recv_mbuf;
5349 const char *boundary;
5350 if ((
int)
io->len <
pd->mp_stream.boundary_len + 6 ) {
5355 if (boundary ==
NULL &&
pd->mp_stream.prev_io_len == 0) {
5356 pd->mp_stream.prev_io_len =
io->len;
5358 }
else if (boundary ==
NULL &&
5360 pd->mp_stream.prev_io_len +
pd->mp_stream.boundary_len + 4) {
5363 }
else if (boundary !=
NULL) {
5364 int data_size = (boundary -
io->buf - 4);
5367 pd->mp_stream.prev_io_len = 0;
5378 switch (
pd->mp_stream.state) {
5439 LOG(
LL_ERROR, (
"%p Not allowed to upload %s", nc,
mp->file_name));
5441 "HTTP/1.1 403 Not Allowed\r\n"
5442 "Content-Type: text/plain\r\n"
5443 "Connection: close\r\n\r\n"
5444 "Not allowed to upload %s\r\n",
5451 fus->lfn[
lfn.len] =
'\0';
5452 if (
lfn.p !=
mp->file_name) free((
char *)
lfn.p);
5454 (
"%p Receiving file %s -> %s", nc,
mp->file_name,
fus->lfn));
5458 "HTTP/1.1 500 Internal Server Error\r\n"
5459 "Content-Type: text/plain\r\n"
5460 "Connection: close\r\n\r\n");
5467 mp->user_data = (
void *)
fus;
5478 (
int)
fus->num_recd));
5485 "HTTP/1.1 413 Payload Too Large\r\n"
5486 "Content-Type: text/plain\r\n"
5487 "Connection: close\r\n\r\n");
5488 mg_printf(nc,
"Failed to write to %s: no space left; wrote %d\r\n",
5489 fus->lfn, (
int)
fus->num_recd);
5492 "HTTP/1.1 500 Internal Server Error\r\n"
5493 "Content-Type: text/plain\r\n"
5494 "Connection: close\r\n\r\n");
5495 mg_printf(nc,
"Failed to write to %s: %d, wrote %d",
mp->file_name,
5506 fus->num_recd +=
mp->data.len;
5507 LOG(
LL_DEBUG, (
"%p rec'd %d bytes, %d total", nc, (
int)
mp->data.len,
5508 (
int)
fus->num_recd));
5517 if (
mp->status >= 0 &&
fus->fp !=
NULL) {
5518 LOG(
LL_DEBUG, (
"%p Uploaded %s (%s), %d bytes", nc,
mp->file_name,
5519 fus->lfn, (
int)
fus->num_recd));
5521 "HTTP/1.1 200 OK\r\n"
5522 "Content-Type: text/plain\r\n"
5523 "Connection: close\r\n\r\n"
5524 "Ok, %s - %d bytes.\r\n",
5525 mp->file_name, (
int)
fus->num_recd);
5549#ifndef MG_DISABLE_HTTP_WEBSOCKET
5552 const char *host,
const char *
protocol,
5553 const char *extra_headers) {
5560 "GET %s HTTP/1.1\r\n"
5561 "Upgrade: websocket\r\n"
5562 "Connection: Upgrade\r\n"
5563 "Sec-WebSocket-Version: 13\r\n"
5564 "Sec-WebSocket-Key: %s\r\n",
5574 if (extra_headers !=
NULL) {
5581 const char *extra_headers) {
5589 const char *extra_headers) {
5591 switch (status_code) {
5621 if (extra_headers !=
NULL) {
5630 mg_printf(
c,
"%s",
"Transfer-Encoding: chunked\r\n");
5637#ifdef MG_DISABLE_FILESYSTEM
5648 "Content-Type: text/plain\r\nConnection: close");
5652#ifndef MG_DISABLE_SSI
5659 while ((
n =
fread(buf, 1,
sizeof(buf),
fp)) > 0) {
5693 mg_printf(nc,
"Bad SSI #include: [%s]", tag);
5712#ifndef MG_DISABLE_POPEN
5717 if (
sscanf(tag,
" \"%[^\"]\"", cmd) != 1) {
5718 mg_printf(nc,
"Bad SSI #exec: [%s]", tag);
5744#ifndef MG_DISABLE_POPEN
5751 mg_printf(nc,
"SSI #include level is too deep (%s)", path);
5763 while (
i > 0 && buf[
i] ==
' ') {
5772#ifndef MG_DISABLE_POPEN
5780 }
else if (
ch ==
'<') {
5786 buf[
len++] =
ch & 0xff;
5791 }
else if (
len == (
int)
sizeof(buf) - 2) {
5792 mg_printf(nc,
"%s: SSI tag is too large", path);
5795 buf[
len++] =
ch & 0xff;
5797 buf[
len++] =
ch & 0xff;
5798 if (
len == (
int)
sizeof(buf)) {
5825 "Content-Type: %.*s\r\n"
5826 "Connection: close\r\n\r\n",
5859 if (
p ==
NULL)
return 0;
5861 p[header->
len] =
'\0';
5873 DBG((
"%p [%s]", nc, path));
5896 int n, status_code = 200;
5907 if (
r1 >
r2 ||
r2 >= cl) {
5911 "Content-Range: bytes */%" INT64_FMT "\r\n",
5919#if _FILE_OFFSET_BITS == 64 || _POSIX_C_SOURCE >= 200112L || \
5920 _XOPEN_SOURCE >= 600
5942 "Last-Modified: %s\r\n"
5943 "Accept-Ranges: bytes\r\n"
5944 "Content-Type: %.*s\r\n"
5946 "Connection: close\r\n"
5950 "%sEtag: %s\r\n\r\n",
5952 (
size_t) cl, range,
etag);
5965#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
5968 if (src[
i] ==
'%') {
5970 isxdigit(*(
const unsigned char *) (src +
i + 2))) {
5971 a =
tolower(*(
const unsigned char *) (src +
i + 1));
5972 b =
tolower(*(
const unsigned char *) (src +
i + 2));
5992 const char *
p, *
e, *s;
6003 e = buf->
p + buf->
len;
6007 for (
p = buf->
p;
p + name_len <
e;
p++) {
6008 if ((
p == buf->
p ||
p[-1] ==
'&') &&
p[name_len] ==
'=' &&
6011 s = (
const char *)
memchr(
p,
'&', (
size_t)(
e -
p));
6028 char chunk_size[50];
6031 n =
snprintf(chunk_size,
sizeof(chunk_size),
"%lX\r\n", (
unsigned long)
len);
6051 if (buf !=
mem && buf !=
NULL) {
6067 for (
i =
j = 0;
i <
len;
i++) {
6068 if (buf[
i] ==
'<' || buf[
i] ==
'>') {
6070 mg_send(nc, buf[
i] ==
'<' ?
"<" :
">", 4);
6078 if (buf !=
mem && buf !=
NULL) {
6089 if (buf !=
NULL && buf_size > 0) buf[0] =
'\0';
6094 if ((s ==
hdr->p || s[-1] ==
ch || s[-1] ==
ch1) && s[
n] ==
'=' &&
6099 if (s !=
NULL && &s[
n + 1] <
end) {
6101 if (*s ==
'"' || *s ==
'\'') {
6105 while (
p <
end &&
p[0] !=
ch &&
p[0] !=
ch1 &&
len < (
int) buf_size) {
6106 if (
ch !=
' ' &&
p[0] ==
'\\' &&
p[1] ==
ch)
p++;
6109 if (
len >= (
int) buf_size || (
ch !=
' ' && *
p !=
ch)) {
6112 if (
len > 0 && s[
len - 1] ==
',')
len--;
6113 if (
len > 0 && s[
len - 1] ==
';')
len--;
6121#ifndef MG_DISABLE_FILESYSTEM
6125 const char *
p1 =
opts->per_directory_auth_file;
6126 const char *
p2 =
opts->hidden_file_pattern;
6140#ifndef MG_DISABLE_HTTP_DIGEST_AUTH
6143 const char *nonce,
size_t nonce_len,
const char *nc,
6146 static const char colon[] =
":";
6147 static const size_t one = 1;
6151 cs_md5(
resp,
ha1,
ha1_len,
colon,
one, nonce,
nonce_len,
colon,
one, nc,
6157 const char *method,
const char *uri,
6158 const char *auth_domain,
const char *user,
6160 static const char colon[] =
":", qop[] =
"auth";
6161 static const size_t one = 1;
6162 char ha1[33],
resp[33], cnonce[40];
6164 snprintf(cnonce,
sizeof(cnonce),
"%x", (
unsigned int) time(
NULL));
6170 sizeof(qop) - 1,
resp);
6172 "Authorization: Digest username=\"%s\","
6173 "realm=\"%s\",uri=\"%s\",qop=%s,nc=1,cnonce=%s,"
6174 "nonce=%s,response=%s\r\n",
6175 user, auth_domain, uri, qop, cnonce, cnonce,
resp);
6195 const char *auth_domain,
FILE *
fp) {
6198 char user[50], cnonce[33],
response[40], uri[200], qop[20], nc[20], nonce[30];
6227 hm->method.p,
hm->method.len,
hm->uri.p,
6228 hm->uri.len + (
hm->query_string.len ?
hm->query_string.len + 1 : 0),
6240 int is_directory,
const char *
domain,
6251 }
else if (is_directory) {
6257 snprintf(buf,
sizeof(buf),
"%.*s%c%s", (
int) (
p - path), path,
DIRSEP,
6274 int is_directory,
const char *
domain,
6279 (
void) is_directory;
6287#ifndef MG_DISABLE_DIRECTORY_LISTING
6291 static const char *
hex =
"0123456789abcdef";
6292 size_t i = 0,
j = 0;
6295 if (
isalnum(*(
const unsigned char *) (src +
i)) ||
6300 dst[
j + 1] =
hex[(*(
const unsigned char *) (src +
i)) >> 4];
6301 dst[
j + 2] =
hex[(*(
const unsigned char *) (src +
i)) & 0xf];
6312 while (*src !=
'\0' &&
n + 5 <
dst_len) {
6313 unsigned char ch = *(
unsigned char *) src++;
6331 snprintf(size,
sizeof(size),
"%s",
"[DIRECTORY]");
6339 }
else if (
fsize < 0x100000) {
6340 snprintf(size,
sizeof(size),
"%.1fk", (
double)
fsize / 1024.0);
6341 }
else if (
fsize < 0x40000000) {
6342 snprintf(size,
sizeof(size),
"%.1fM", (
double)
fsize / 1048576);
6344 snprintf(size,
sizeof(size),
"%.1fG", (
double)
fsize / 1073741824);
6351 "<tr><td><a href=\"%s%s\">%s%s</a></td>"
6352 "<td>%s</td><td name=%" INT64_FMT ">%s</td></tr>\n",
6366 DBG((
"%p [%s]", nc, dir));
6373 snprintf(path,
sizeof(path),
"%s/%s", dir,
dp->d_name);
6375 func(nc, (
const char *)
dp->d_name, &
st);
6380 DBG((
"%p opendir(%s) -> %d", nc, dir,
errno));
6388 "<script>function srt(tb, col) {"
6389 "var tr = Array.prototype.slice.call(tb.rows, 0),"
6390 "tr = tr.sort(function (a, b) { var c1 = a.cells[col], c2 = b.cells[col],"
6391 "n1 = c1.getAttribute('name'), n2 = c2.getAttribute('name'), "
6392 "t1 = a.cells[2].getAttribute('name'), "
6393 "t2 = b.cells[2].getAttribute('name'); "
6394 "return t1 < 0 && t2 >= 0 ? -1 : t2 < 0 && t1 >= 0 ? 1 : "
6395 "n1 ? parseInt(n2) - parseInt(n1) : "
6396 "c1.textContent.trim().localeCompare(c2.textContent.trim()); });";
6398 "for (var i = 0; i < tr.length; i++) tb.appendChild(tr[i]);}"
6399 "window.onload = function() { "
6400 "var tb = document.getElementById('tb');"
6401 "document.onclick = function(ev){ "
6402 "var c = ev.target.rel; if (c) srt(tb, c)}; srt(tb, 2); };</script>";
6405 mg_printf(nc,
"%s: %s\r\n%s: %s\r\n\r\n",
"Transfer-Encoding",
"chunked",
6406 "Content-Type",
"text/html; charset=utf-8");
6410 "<html><head><title>Index of %.*s</title>%s%s"
6411 "<style>th,td {text-align: left; padding-right: 1em; }</style></head>"
6412 "<body><h1>Index of %.*s</h1><pre><table cellpadding=\"0\"><thead>"
6413 "<tr><th><a href=# rel=0>Name</a></th><th>"
6414 "<a href=# rel=1>Modified</a</th>"
6415 "<th><a href=# rel=2>Size</a></th></tr>"
6416 "<tr><td colspan=\"3\"><hr></td></tr></thead><tbody id=tb>",
6418 (
int)
hm->uri.len,
hm->uri.p);
6427#ifndef MG_DISABLE_DAV
6436 "<d:href>%s</d:href>"
6439 "<d:resourcetype>%s</d:resourcetype>"
6441 "</d:getcontentlength>"
6442 "<d:getlastmodified>%s</d:getlastmodified>"
6444 "<d:status>HTTP/1.1 200 OK</d:status>"
6447 buf,
S_ISDIR(
stp->st_mode) ?
"<d:collection/>" :
"",
6454 static const char header[] =
6455 "HTTP/1.1 207 Multi-Status\r\n"
6456 "Connection: close\r\n"
6457 "Content-Type: text/xml; charset=utf-8\r\n\r\n"
6458 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
6459 "<d:multistatus xmlns:d='DAV:'>\n";
6460 static const char footer[] =
"</d:multistatus>\n";
6465 strcmp(
opts->enable_directory_listing,
"yes") != 0) {
6466 mg_printf(nc,
"%s",
"HTTP/1.1 403 Directory Listing Denied\r\n\r\n");
6469 mg_send(nc, header,
sizeof(header) - 1);
6470 snprintf(uri,
sizeof(uri),
"%.*s", (
int)
hm->uri.len,
hm->uri.p);
6480#ifdef MG_ENABLE_FAKE_DAVLOCK
6493 static const char *
reply =
6494 "HTTP/1.1 207 Multi-Status\r\n"
6495 "Connection: close\r\n"
6496 "Content-Type: text/xml; charset=utf-8\r\n\r\n"
6497 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
6498 "<d:multistatus xmlns:d='DAV:'>\n"
6499 "<D:lockdiscovery>\n"
6503 "opaquelocktoken:%s%u"
6507 "</D:lockdiscovery>"
6508 "</d:multistatus>\n";
6516 int status_code = 500;
6517 if (
hm->body.len != (
size_t) ~0 &&
hm->body.len > 0) {
6519 }
else if (!
mg_mkdir(path, 0755)) {
6546 snprintf(path,
sizeof(path),
"%s%c%s", dir,
'/',
dp->d_name);
6568 if (
p !=
NULL &&
p[1] ==
'/' &&
6571 snprintf(buf,
sizeof(buf),
"%s%.*s",
opts->dav_document_root,
6573 if (
rename(path, buf) == 0) {
6593 }
else if (
remove(path) == 0) {
6605 for (s = path + 1; *s !=
'\0'; s++) {
6609 snprintf(buf,
sizeof(buf),
"%.*s", (
int) (s - path), path);
6610 buf[
sizeof(buf) - 1] =
'\0';
6625 int rc, status_code =
mg_stat(path, &
st) == 0 ? 200 : 201;
6629 mg_printf(nc,
"HTTP/1.1 %d OK\r\nContent-Length: 0\r\n\r\n", status_code);
6630 }
else if (
rc == -1) {
6634 }
else if ((
pd->file.fp =
fopen(path,
"w+b")) ==
NULL) {
6648 mg_printf(nc,
"HTTP/1.1 %d OK\r\nContent-Length: 0\r\n\r\n", status_code);
6657 static const char *
methods[] = {
"PUT",
"DELETE",
"MKCOL",
"PROPFIND",
"MOVE"
6658#ifdef MG_ENABLE_FAKE_DAVLOCK
6725 mg_printf(
c,
"Content-Length: 0\r\nLocation: %.*s%.*s\r\n\r\n",
6726 (
int) b.
len, b.
p, (
int) (
hm->proto.p -
hm->uri.p - 1),
6753 if (a.
len > 1 && a.
p[0] ==
'@') {
6781#ifndef MG_DISABLE_DAV
6813 const char *next =
u;
6821 if (*(
u - 1) ==
'/')
u--;
6881 DBG((
"'%.*s' -> '%s' + '%.*s'", (
int)
hm->uri.len,
hm->uri.p,
6886#ifndef MG_DISABLE_CGI
6902 int n, sent,
stop = 0;
6907 (
n =
recv(
tp->s, buf,
sizeof(buf), 0)) > 0) {
6909 for (sent = 0; !
stop && sent <
n; sent +=
k) {
6913 DBG((
"%s",
"FORWARED EVERYTHING TO CGI"));
6922 int k = 0,
stop = 0;
6927 for (sent = 0; !
stop && sent <
n; sent +=
k) {
6929 (
k =
send(
tp->s, buf + sent,
n - sent, 0)) <= 0)
6933 DBG((
"%s",
"EOF FROM CGI"));
6943 void *(*func)(
void *)) {
6960 const char *
env,
const char *
envp[],
6961 const char *dir,
sock_t sock) {
6985 buf[0] = buf[1] =
'\0';
6987 buf[
sizeof(buf) - 1] =
'\0';
6988 if (buf[0] ==
'#' && buf[1] ==
'!') {
6998 snprintf(buf,
sizeof(buf),
"%s/%s", dir, cmd);
7021 DBG((
"CGI command: [%ls] -> %p",
wcmd,
pi.hProcess));
7034 const char *
env,
const char *
envp[],
7035 const char *dir,
sock_t sock) {
7065 "Status: 500\r\n\r\n"
7066 "500 Server Error: %s%s%s: %s",
7114 const struct mg_str *path_info,
7151 hm->query_string.len == 0 ?
"" :
"?", (
int)
hm->query_string.len,
7152 hm->query_string.p);
7154 s =
hm->uri.p +
hm->uri.len - path_info->
len - 1;
7164 if (path_info !=
NULL && path_info->
len > 0) {
7167 mg_addenv(
blk,
"PATH_TRANSLATED=%.*s", (
int) path_info->
len, path_info->
p);
7177 if (
hm->query_string.len > 0) {
7179 hm->query_string.p);
7206 for (
i = 0;
hm->header_names[
i].len > 0;
i++) {
7208 hm->header_names[
i].p, (
int)
hm->header_values[
i].len,
7209 hm->header_values[
i].p);
7212 for (; *
p !=
'=' && *
p !=
'\0';
p++) {
7213 if (*
p ==
'-') *
p =
'_';
7219 blk->buf[
blk->len++] =
'\0';
7227 if (nc ==
NULL)
return;
7248 if (
len == 0)
break;
7257 mg_printf(nc,
"%s",
"HTTP/1.1 302 Moved\r\n");
7261 mg_printf(nc,
"%s",
"HTTP/1.1 200 OK\r\n");
7278 const struct mg_str *path_info,
7294 snprintf(dir,
sizeof(dir),
"%s",
".");
7315 cgi_pd->cgi.cgi_nc = cgi_nc;
7335 static const char *
month_names[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
7336 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"};
7352 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
7391 "HTTP/1.1 401 Unauthorized\r\n"
7392 "WWW-Authenticate: Digest qop=\"auth\", "
7393 "realm=\"%s\", nonce=\"%lu\"\r\n"
7394 "Content-Length: 0\r\n\r\n",
7400 "HTTP/1.1 200 OK\r\nAllow: GET, POST, HEAD, CONNECT, OPTIONS"
7402 ", MKCOL, PUT, DELETE, PROPFIND, MOVE\r\nDAV: 1,2"
7413 const struct mg_str *path_info,
7422 is_directory = exists &&
S_ISDIR(
st.st_mode);
7431 DBG((
"%p %.*s [%s] exists=%d is_dir=%d is_dav=%d is_cgi=%d index=%s", nc,
7432 (
int)
hm->method.len,
hm->method.p, path, exists, is_directory,
is_dav,
7435 if (is_directory &&
hm->uri.p[
hm->uri.len - 1] !=
'/' && !
is_dav) {
7437 "HTTP/1.1 301 Moved\r\nLocation: %.*s/\r\n"
7438 "Content-Length: 0\r\n\r\n",
7439 (
int)
hm->uri.len,
hm->uri.p);
7454 opts->global_auth_file, 1) ||
7456 opts->per_directory_auth_file, 0)) {
7459#if !defined(MG_DISABLE_CGI)
7464 }
else if ((!exists ||
7468#ifndef MG_DISABLE_DAV
7469 }
else if (!
mg_vcmp(&
hm->method,
"PROPFIND")) {
7471#ifndef MG_DISABLE_DAV_AUTH
7476 opts->dav_auth_file, 1)))) {
7479 }
else if (!
mg_vcmp(&
hm->method,
"MKCOL")) {
7481 }
else if (!
mg_vcmp(&
hm->method,
"DELETE")) {
7483 }
else if (!
mg_vcmp(&
hm->method,
"PUT")) {
7485 }
else if (!
mg_vcmp(&
hm->method,
"MOVE")) {
7487#ifdef MG_ENABLE_FAKE_DAVLOCK
7488 }
else if (!
mg_vcmp(&
hm->method,
"LOCK")) {
7492 }
else if (!
mg_vcmp(&
hm->method,
"OPTIONS")) {
7495#ifndef MG_DISABLE_DIRECTORY_LISTING
7496 if (
strcmp(
opts->enable_directory_listing,
"yes") == 0) {
7530 opts.document_root =
".";
7532 if (
opts.per_directory_auth_file ==
NULL) {
7533 opts.per_directory_auth_file =
".htpasswd";
7535 if (
opts.enable_directory_listing ==
NULL) {
7536 opts.enable_directory_listing =
"yes";
7538 if (
opts.cgi_file_pattern ==
NULL) {
7539 opts.cgi_file_pattern =
"**.cgi$|**.php$";
7542 opts.ssi_pattern =
"**.shtml$|**.shtm$";
7545 opts.index_files =
"index.html,index.htm,index.shtml,index.cgi,index.php";
7562 if (
mg_vcmp(&
hm->proto,
"HTTP/1.1") != 0 ||
7577 const char **path) {
7585#ifndef MG_ENABLE_SSL
7590 while (*
url !=
'\0') {
7612 if (*path ==
NULL) *path =
url;
7614 if (**path ==
'\0') *path =
"/";
7628 const char *
url,
const char **path,
char **
addr) {
7638#ifndef MG_ENABLE_SSL
7670 const char *extra_headers,
7673 const char *path =
NULL;
7685 extra_headers ==
NULL ?
"" : extra_headers,
7695 const char *extra_headers,
7707 const char *extra_headers) {
7709 const char *path =
NULL;
7726 const char *extra_headers) {
7736 static const char cd[] =
"Content-Disposition: ";
7737 size_t hl,
bl,
n, ll, pos,
cdl =
sizeof(cd) - 1;
7741 if (buf[0] !=
'-' || buf[1] !=
'-' || buf[2] ==
'\n')
return 0;
7751 header.
p = buf +
n +
cdl;
7752 header.
len = ll - (
cdl + 2);
7760 if (buf[pos] ==
'-' && !
memcmp(buf, &buf[pos],
bl - 2)) {
7761 if (data_len !=
NULL) *data_len = (pos - 2) -
hl;
7783#ifdef MG_MODULE_LINES
7784#line 1 "./src/util.c"
7805 return tolower(*(
const unsigned char *) s);
7813 }
while (
diff == 0 &&
s1[-1] !=
'\0' && --len > 0);
7840#ifndef MG_DISABLE_FILESYSTEM
7882#ifdef MG_ENABLE_THREADS
7893#if defined(MG_STACK_SIZE) && MG_STACK_SIZE > 1
7900 return (
void *) thread_id;
7909#elif defined(__unix__)
7919 if (buf ==
NULL || len <= 0)
return;
7921#if defined(MG_ENABLE_IPV6)
7927#if defined(MG_ENABLE_IPV6)
7934 addr = (
void *) &sa->
sin6.sin6_addr;
7944#elif defined(_WIN32) || defined(MG_LWIP)
7952 int port =
ntohs(sa->
sin.sin_port);
7955 (
is_v6 ?
"]" :
""), port);
7970#ifndef MG_DISABLE_HEXDUMP
7972 const unsigned char *p = (
const unsigned char *) buf;
7973 char ascii[17] =
"";
7976 for (
i = 0;
i < len;
i++) {
7983 ascii[
idx] = p[
i] < 0x20 || p[
i] > 0x7e ?
'.' : p[
i];
8016 }
else if (len >= (
int) size) {
8030#if !defined(MG_DISABLE_HEXDUMP)
8033#if !defined(NO_LIBC) && !defined(MG_DISABLE_STDIO)
8035 char *
hexbuf, src[60], dst[60];
8038 if (
strcmp(path,
"-") == 0) {
8040 }
else if (
strcmp(path,
"--") == 0) {
8042#ifndef MG_DISABLE_FILESYSTEM
8055 fp,
"%lu %p %s %s %s %d\n", (
unsigned long) time(
NULL), (
void *) nc, src,
8073 static const int n = 1;
8075 return ((
char *) &
n)[0] == 0;
8113 size_t len,
i = 0,
j = 0;
8125 for (;
i < pattern.
len;
i++,
j++) {
8126 if (pattern.
p[
i] ==
'?' &&
j !=
str.len) {
8128 }
else if (pattern.
p[
i] ==
'$') {
8129 return j ==
str.len ? (
int)
j : -1;
8130 }
else if (pattern.
p[
i] ==
'*') {
8132 if (pattern.
p[
i] ==
'*') {
8141 if (
i == pattern.
len) {
8148 }
while (
res == -1 &&
len-- > 0);
8163 struct mg_str ret = {s, 0};
8167#ifdef MG_MODULE_LINES
8168#line 1 "./src/json-rpc.c"
8173#ifndef MG_DISABLE_JSON_RPC
8204 const char *
id,
const char *
params_fmt, ...) {
8208 n +=
json_emit(buf +
n,
len -
n,
"{s:s,s:s,s:s,s:",
"jsonrpc",
"2.0",
"id",
8209 id,
"method", method,
"params");
8220 int code,
const char *
message,
const char *fmt, ...) {
8224 n +=
json_emit(buf +
n,
len -
n,
"{s:s,s:V,s:{s:i,s:s,s:",
"jsonrpc",
"2.0",
8226 req->
id ==
NULL ? 4 : req->
id->
len,
"error",
"code",
8252 message =
"invalid parameters";
8258 message =
"unspecified error";
8271 memset(&req, 0,
sizeof(req));
8272 n =
parse_json(buf, len, tokens,
sizeof(tokens) /
sizeof(tokens[0]));
8328#ifdef MG_MODULE_LINES
8329#line 1 "./src/mqtt.c"
8336#ifndef MG_DISABLE_MQTT
8346 char *
vlen = &
io->buf[1];
8348 if (
io->len < 2)
return -1;
8350 header =
io->buf[0];
8355 len += (*
vlen & 127) << 7 * (
vlen - &
io->buf[1]);
8358 if (len != 0 &&
io->len < (
size_t)(len - 1))
return -1;
8369 mm->connack_ret_code =
io->buf[1];
8420 if (
len == -1)
break;
8421 mm.payload.p =
io->buf;
8422 mm.payload.len =
len;
8459 mg_send(nc,
"\00\06MQIsdp\03", 9);
8462 if (
opts.keep_alive == 0) {
8463 opts.keep_alive = 60;
8489 if (len > 0) *
vlen |= 0x80;
8537 unsigned char *buf = (
unsigned char *) msg->
payload.
p + pos;
8542 topic->
len = buf[0] << 8 | buf[1];
8543 topic->
p = (
char *) buf + 2;
8544 *qos = buf[2 + topic->
len];
8545 return pos + 2 + topic->
len + 1;
8629#ifdef MG_MODULE_LINES
8630#line 1 "./src/mqtt-broker.c"
8640#ifdef MG_ENABLE_MQTT_BROKER
8646 s->subscriptions =
NULL;
8647 s->num_subscriptions = 0;
8652 s->
next = s->brk->sessions;
8653 s->brk->sessions = s;
8659 if (s->prev ==
NULL) s->brk->sessions = s->
next;
8666 for (
i = 0;
i < s->num_subscriptions;
i++) {
8667 MG_FREE((
void *) s->subscriptions[
i].topic);
8680 brk->user_data = user_data;
8719 ss->subscriptions,
sizeof(*
ss->subscriptions) *
qoss_len);
8722 ss->num_subscriptions++) {
8723 te = &
ss->subscriptions[
ss->num_subscriptions];
8754 for (
i = 0;
i < s->num_subscriptions;
i++) {
8798 return s ==
NULL ?
brk->sessions : s->next;
8802#ifdef MG_MODULE_LINES
8803#line 1 "./src/dns.c"
8810#ifndef MG_DISABLE_DNS
8833 if (
rr->rtype == query) {
8843 switch (
rr->rtype) {
8845 if (data_len <
sizeof(
struct in_addr)) {
8848 if (
rr->rdata.p + data_len > msg->
pkt.
p + msg->
pkt.
len) {
8853#ifdef MG_ENABLE_IPV6
8855 if (data_len <
sizeof(
struct in6_addr)) {
8873 memset(&header, 0,
sizeof(header));
8890 size_t pos =
io->len;
8897 if (s -
name > 127) {
8910 }
while (*s !=
'\0');
8913 return io->len - pos;
8917 const char *
name,
size_t nlen,
const void *rdata,
8919 size_t pos =
io->len;
8943 size_t off =
io->len;
8958 return io->len - pos;
9007 if (((
unsigned char *)
data)[0] & 0xc0) {
9018 rr->name.p = (
char *)
name;
9038 data_len = *
data << 8 | *(
data + 1);
9041 rr->rdata.p = (
char *)
data;
9042 rr->rdata.len = data_len;
9050 unsigned char *
data = (
unsigned char *) buf +
sizeof(*header);
9051 unsigned char *
end = (
unsigned char *) buf + len;
9054 memset(msg, 0,
sizeof(*msg));
9058 if (len < (
int)
sizeof(*header))
return -1;
9088 const unsigned char *
data = (
unsigned char *)
name->p;
9089 const unsigned char *
end = (
unsigned char *) msg->
pkt.
p + msg->
pkt.
len;
9147 memset(&msg, 0,
sizeof(msg));
9169#ifdef MG_MODULE_LINES
9170#line 1 "./src/dns-server.c"
9177#ifdef MG_ENABLE_DNS_SERVER
9190 msg->flags |= 0x8080;
9193 msg->num_answers = 0;
9198 size_t sent = r->io->
len - r->start;
9206 mg_send(nc, r->io->buf + r->start, r->io->len - r->start);
9207 r->io->len = r->start;
9213 const char *
name,
int rtype,
int ttl,
const void *rdata,
9243#ifdef MG_MODULE_LINES
9244#line 1 "./src/resolv.c"
9251#ifndef MG_DISABLE_RESOLVER
9256#ifndef MG_DEFAULT_NAMESERVER
9257#define MG_DEFAULT_NAMESERVER "8.8.8.8"
9291 *
key =
"SYSTEM\\ControlSet001\\Services\\Tcpip\\Parameters\\Interfaces";
9297 for (ret = -1,
i = 0;
9313 if (
value[0] ==
'\0') {
9327#elif !defined(MG_DISABLE_FILESYSTEM)
9331 if ((
fp =
fopen(
"/etc/resolv.conf",
"r")) ==
NULL) {
9335 for (ret = -1;
fgets(line,
sizeof(line),
fp) !=
NULL;) {
9337 if (
sscanf(line,
"nameserver %255[^\n\t #]s", buf) == 1) {
9353#ifndef MG_DISABLE_FILESYSTEM
9359 unsigned int a, b,
c,
d;
9367 if (line[0] ==
'#')
continue;
9369 if (
sscanf(line,
"%u.%u.%u.%u%n", &a, &b, &
c, &
d, &len) == 0) {
9373 for (p = line + len;
sscanf(p,
"%s%n",
alias, &len) == 1; p += len) {
9375 usa->
sin.sin_addr.s_addr =
htonl(a << 24 | b << 16 |
c << 8 |
d);
9459 const char *nameserver =
opts.nameserver_url;
9485 if (nameserver ==
NULL) {
9503#ifdef MG_MODULE_LINES
9504#line 1 "./src/coap.c"
9526#ifdef MG_ENABLE_COAP
9529 while (
cm->options !=
NULL) {
9546 if (
cm->options ==
NULL) {
9602 if (((
uint8_t) *ptr >> 6) != 1) {
9612 cm->msg_type = ((
uint8_t) *ptr & 0x30) >> 4;
9621 cm->token.len = *ptr & 0x0F;
9622 if (
cm->token.len > 8) {
9634 cm->code_detail = *ptr & 0x1F;
9655 if (
cm->token.len != 0) {
9656 if (ptr +
cm->token.len >
io->buf +
io->len) {
9661 ptr +=
cm->token.len;
9724 if (ptr ==
io->buf +
io->len) {
9794 if (ptr ==
io->buf +
io->len) {
9822 cm->payload.len =
io->len - (ptr -
io->buf);
9823 if (
cm->payload.len != 0) {
9824 cm->payload.p = ptr;
9841 }
else if (
value > 0xFF + 13 &&
value <= 0xFFFF + 269) {
9858 }
else if (
value >= 13 &&
value <= 0xFF + 13) {
9862 }
else if (
value > 0xFF + 13 &&
value <= 0xFFFF + 269) {
9879 *ptr =
val & 0x00FF;
9893 }
else if (len ==
sizeof(
uint16_t)) {
9914 if (
cm->token.len > 8) {
9917 if (
cm->code_class > 7) {
9920 if (
cm->code_detail > 31) {
9924 *len +=
cm->token.len;
9925 if (
cm->payload.len != 0) {
9926 *len +=
cm->payload.len + 1;
9941 if ((
opt->next !=
NULL &&
opt->number >
opt->next->number) ||
9942 opt->value.len > 0xFFFF + 269 ||
9946 *len +=
opt->value.len;
9965 prev_io_len =
io->len;
9967 ptr =
io->buf + prev_io_len;
9975 *ptr = (1 << 6) | (
cm->msg_type << 4) | (
cm->token.len);
9979 *ptr = (
cm->code_class << 5) | (
cm->code_detail);
9984 if (
cm->token.len != 0) {
9986 ptr +=
cm->token.len;
10006 if (
opt->value.len != 0) {
10008 ptr +=
opt->value.len;
10015 if (
cm->payload.len != 0) {
10018 memcpy(ptr,
cm->payload.p,
cm->payload.len);
10100#ifdef MG_MODULE_LINES
10101#line 1 "./src/../../common/platforms/cc3200/cc3200_libc.c"
10108#if CS_PLATFORM == CS_P_CC3200
10113#ifndef __TI_COMPILER_VERSION__
10115#include <sys/stat.h>
10116#include <sys/time.h>
10120#include <inc/hw_types.h>
10121#include <inc/hw_memmap.h>
10122#include <driverlib/prcm.h>
10123#include <driverlib/rom.h>
10124#include <driverlib/rom_map.h>
10125#include <driverlib/uart.h>
10126#include <driverlib/utils.h>
10128#define CONSOLE_UART UARTA0_BASE
10130#ifndef __TI_COMPILER_VERSION__
10135 unsigned long long r1 = 0,
r2;
10140 }
while (
r1 !=
r2);
10142 tp->tv_sec = (
r1 >> 15);
10145 tp->tv_usec = (
r1 & 0x7FFF) * 30;
10154 while (*
str !=
'\0') {
10192#ifdef MG_MODULE_LINES
10193#line 1 "./src/../../common/platforms/msp432/msp432_libc.c"
10200#if CS_PLATFORM == CS_P_MSP432
10202#include <ti/sysbios/BIOS.h>
10203#include <ti/sysbios/knl/Clock.h>
10208 tp->tv_usec = (
ticks % 1000) * 1000;
10217#ifdef MG_MODULE_LINES
10218#line 1 "./src/../../common/platforms/simplelink/sl_fs_slfs.h"
10225#ifndef CS_COMMON_PLATFORMS_SIMPLELINK_SL_FS_SLFS_H_
10226#define CS_COMMON_PLATFORMS_SIMPLELINK_SL_FS_SLFS_H_
10228#if defined(MG_FS_SLFS)
10231#ifndef __TI_COMPILER_VERSION__
10233#include <sys/stat.h>
10236#define MAX_OPEN_SLFS_FILES 8
10252#ifdef MG_MODULE_LINES
10253#line 1 "./src/../../common/platforms/simplelink/sl_fs_slfs.c"
10262#if defined(MG_FS_SLFS) || defined(CC3200_FS_SLFS)
10268#if CS_PLATFORM == CS_P_CC3200
10269#include <inc/hw_types.h>
10271#include <simplelink/include/simplelink.h>
10272#include <simplelink/include/fs.h>
10282#ifndef FS_SLFS_MAX_FILE_SIZE
10283#define FS_SLFS_MAX_FILE_SIZE (64 * 1024)
10295 DBG((
"SL error: %d", (
int) r));
10327 int rw = (flags & 3);
10348 DBG((
"sl_FsOpen(%s, 0x%x) = %d, %d",
pathname, (
int) am, (
int) r,
10364 DBG((
"sl_FsClose(%d) = %d", (
int)
fi->fh, (
int) r));
10374 if (
fi->pos ==
fi->size)
return 0;
10376 DBG((
"sl_FsRead(%d, %d, %d) = %d", (
int)
fi->fh, (
int)
fi->pos, (
int)
count,
10389 DBG((
"sl_FsWrite(%d, %d, %d) = %d", (
int)
fi->fh, (
int)
fi->pos, (
int)
count,
10404 s->st_size =
sl_fi.FileLen;
10416 s->st_size =
fi->size;
10444#ifdef MG_MODULE_LINES
10445#line 1 "./src/../../common/platforms/simplelink/sl_fs.c"
10452#if defined(MG_SOCKET_SIMPLELINK) && \
10453 (defined(MG_FS_SLFS) || defined(MG_FS_SPIFFS))
10459#ifdef __TI_COMPILER_VERSION__
10463#if CS_PLATFORM == CS_P_CC3200
10464#include <inc/hw_types.h>
10465#include <inc/hw_memmap.h>
10466#include <driverlib/rom.h>
10467#include <driverlib/rom_map.h>
10468#include <driverlib/uart.h>
10474#ifdef CC3200_FS_SPIFFS
10482#define NUM_SYS_FDS 3
10483#define SPIFFS_FD_BASE 10
10484#define SLFS_FD_BASE 100
10486#define CONSOLE_UART UARTA0_BASE
10510#ifdef CC3200_FS_SPIFFS
10519#ifdef CC3200_FS_SPIFFS
10541#ifdef CC3200_FS_SPIFFS
10546 DBG((
"open(%s, 0x%x) = %d",
pathname, flags, fd));
10570#ifdef CC3200_FS_SPIFFS
10587#ifdef CC3200_FS_SPIFFS
10598 DBG((
"close(%d) = %d", fd, r));
10611#ifdef CC3200_FS_SPIFFS
10628 memset(s, 0,
sizeof(*s));
10635 memset(s, 0,
sizeof(*s));
10641#ifdef CC3200_FS_SPIFFS
10652 DBG((
"fstat(%d) = %d", fd, r));
10671#ifdef CC3200_FS_SPIFFS
10682 DBG((
"read(%d, %u) = %d", fd,
count, r));
10698#if CS_PLATFORM == CS_P_CC3200
10700 const char c = ((
const char *) buf)[
i];
10710#ifdef CC3200_FS_SPIFFS
10733#ifdef CC3200_FS_SPIFFS
10737 DBG((
"rename(%s, %s) = %d",
from,
to, r));
10741int _link(
const char *
from,
const char *
to) {
10746int _unlink(
const char *filename) {
10754#ifdef CC3200_FS_SPIFFS
10758 DBG((
"unlink(%s) = %d", filename, r));
10762#ifdef CC3200_FS_SPIFFS
10777 DBG((
"readdir(%p) = %p", dir,
res));
10783 DBG((
"closedir(%p) = %d", dir,
res));
10787int rmdir(
const char *path) {
10801#ifdef __TI_COMPILER_VERSION__
10813#ifdef MG_MODULE_LINES
10814#line 1 "./src/../../common/platforms/simplelink/sl_socket.c"
10821#ifdef MG_SOCKET_SIMPLELINK
10828#include <simplelink/include/netapp.h>
10848int inet_pton(
int af,
const char *src,
void *dst) {
10866#ifdef MG_MODULE_LINES
10867#line 1 "./src/../../common/platforms/simplelink/sl_mg_task.c"
10869#if defined(MG_SOCKET_SIMPLELINK)
10873#include <oslib/osi.h>
10884static void mg_task(
void *arg);
10897static void mg_task(
void *arg) {
10906 switch (msg.type) {
10908 msg.cb(&mgr, msg.arg);
int mg_start_thread(mg_thread_func_t f, void *p)
int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded)
int(* mg_event_handler_t)(struct mg_event *event)
time_t mg_mgr_poll(struct mg_mgr *mgr, int timeout_ms)
void mg_mqtt_ping(struct mg_connection *nc)
void MD5_Init(MD5_CTX *ctx)
void mg_set_protocol_mqtt(struct mg_connection *nc)
#define JSON_STRING_INCOMPLETE
void mg_mgr_free(struct mg_mgr *m)
const char * hexdump_file
int mg_resolve_from_hosts_file(const char *name, union socket_address *usa)
void cs_base64_update(struct cs_base64_ctx *ctx, const char *str, size_t len)
void cs_to_hex(char *to, const unsigned char *p, size_t len)
#define WEBSOCKET_DONT_FIN
#define MG_MK_STR(str_literal)
void mg_send_dns_query(struct mg_connection *nc, const char *name, int query_type)
#define WEBSOCKET_OP_PING
char * cs_md5(char buf[33],...)
struct json_token * message
void mg_mqtt_disconnect(struct mg_connection *nc)
#define MG_MQTT_CMD_PUBREC
void mg_mqtt_publish(struct mg_connection *nc, const char *topic, uint16_t message_id, int flags, const void *data, size_t len)
void mg_if_get_conn_addr(struct mg_connection *nc, int remote, union socket_address *sa)
#define MG_MQTT_CMD_UNSUBACK
struct mg_str header_names[MG_MAX_HTTP_HEADERS]
int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap)
int c_vsnprintf(char *buf, size_t buf_size, const char *fmt, va_list ap)
void mg_if_destroy_conn(struct mg_connection *nc)
#define MG_MAX_HTTP_SEND_MBUF
#define MG_MAX_DNS_ANSWERS
int mg_dns_copy_body(struct mbuf *io, struct mg_dns_message *msg)
#define MG_EV_WEBSOCKET_FRAME
int mg_stat(const char *path, cs_stat_t *st)
#define JSON_RPC_INVALID_PARAMS_ERROR
struct mg_connection * mg_connect_http(struct mg_mgr *mgr, mg_event_handler_t ev_handler, const char *url, const char *extra_headers, const char *post_data)
#define MG_EV_WEBSOCKET_HANDSHAKE_DONE
int mg_resolve_async_opt(struct mg_mgr *mgr, const char *name, int query, mg_resolve_callback_t cb, void *data, struct mg_resolve_async_opts opts)
mg_event_handler_t handler
struct mg_connection * active_connections
void mg_mqtt_connack(struct mg_connection *nc, uint8_t return_code)
#define MG_MQTT_CMD_PUBACK
int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr, const char *name, size_t nlen, const void *rdata, size_t rlen)
void mg_mqtt_pong(struct mg_connection *nc)
struct mg_connection ** dns_conn
int mg_is_big_endian(void)
#define MG_F_CLOSE_IMMEDIATELY
size_t mbuf_insert(struct mbuf *a, size_t off, const void *buf, size_t len)
size_t mg_parse_multipart(const char *buf, size_t buf_len, char *var_name, size_t var_name_len, char *file_name, size_t file_name_len, const char **data, size_t *data_len)
#define MG_SOCK_STRINGIFY_PORT
void mbuf_remove(struct mbuf *mb, size_t n)
void mg_mgr_init(struct mg_mgr *m, void *user_data)
int mg_dns_insert_header(struct mbuf *io, size_t pos, struct mg_dns_message *msg)
void mg_send_head(struct mg_connection *c, int status_code, int64_t content_length, const char *extra_headers)
void mg_mqtt_pubcomp(struct mg_connection *nc, uint16_t message_id)
void mg_if_tcp_send(struct mg_connection *nc, const void *buf, size_t len)
void(* proto_data_destructor)(void *proto_data)
struct mg_connection * mg_next(struct mg_mgr *s, struct mg_connection *conn)
int mg_resolve_async(struct mg_mgr *mgr, const char *name, int query, mg_resolve_callback_t cb, void *data)
void mg_if_connect_udp(struct mg_connection *nc)
void mg_if_accept_tcp_cb(struct mg_connection *nc, union socket_address *sa, size_t sa_len)
int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out)
#define WEBSOCKET_OP_CLOSE
const char * c_strnstr(const char *s, const char *find, size_t slen)
int mg_vprintf(struct mg_connection *nc, const char *fmt, va_list ap)
const char * mg_set_ssl(struct mg_connection *nc, const char *cert, const char *ca_cert)
void mg_broadcast(struct mg_mgr *mgr, mg_event_handler_t cb, void *data, size_t len)
#define MG_DNS_AAAA_RECORD
int json_emit_unquoted_str(char *buf, int buf_len, const char *str, int len)
void mg_send_response_line(struct mg_connection *nc, int status_code, const char *extra_headers)
#define MG_MQTT_CMD_CONNACK
void mg_send_mqtt_handshake(struct mg_connection *nc, const char *client_id)
#define MG_VPRINTF_BUFFER_SIZE
struct mg_str header_values[MG_MAX_HTTP_HEADERS]
void mg_if_recv_udp_cb(struct mg_connection *nc, void *buf, int len, union socket_address *sa, size_t sa_len)
void mbuf_free(struct mbuf *mbuf)
#define MG_EV_WEBSOCKET_CONTROL_FRAME
void mg_serve_http(struct mg_connection *nc, struct http_message *hm, struct mg_serve_http_opts opts)
#define MG_MAX_CGI_ENVIR_VARS
#define MG_SOCK_STRINGIFY_REMOTE
void mg_if_poll(struct mg_connection *nc, time_t now)
int mg_open(const char *path, int flag, int mode)
struct json_token * method
double mg_set_timer(struct mg_connection *c, double timestamp)
struct mg_connection * mg_connect(struct mg_mgr *mgr, const char *address, mg_event_handler_t callback)
void mg_conn_addr_to_str(struct mg_connection *nc, char *buf, size_t len, int flags)
#define MG_MQTT_GET_QOS(flags)
int mg_parse_http(const char *s, int n, struct http_message *hm, int is_req)
int mg_match_prefix_n(const struct mg_str pattern, const struct mg_str str)
#define MG_SEND_FUNC(s, b, l, f)
union mg_connection::@11 priv_1
void mbuf_resize(struct mbuf *a, size_t new_size)
void mg_if_recv_tcp_cb(struct mg_connection *nc, void *buf, int len)
void mg_printf_html_escape(struct mg_connection *nc, const char *fmt,...)
cs_base64_putc_t b64_putc
void mg_if_sent_cb(struct mg_connection *nc, int num_sent)
#define MG_DNS_CNAME_RECORD
struct mg_connection * mg_if_accept_new_conn(struct mg_connection *lc)
#define MG_CGI_ENVIRONMENT_SIZE
int mg_casecmp(const char *s1, const char *s2)
int mg_if_create_conn(struct mg_connection *nc)
void mg_send(struct mg_connection *nc, const void *buf, int len)
int mg_socketpair(sock_t sp[2], int sock_type)
#define MG_MQTT_CMD_PUBREL
int mg_rpc_create_error(char *buf, int len, struct mg_rpc_request *req, int code, const char *message, const char *fmt,...)
struct mg_connection * next
void mg_send_http_chunk(struct mg_connection *nc, const char *buf, size_t len)
int json_emit_double(char *buf, int buf_len, double value)
#define MG_MQTT_CMD_PINGREQ
#define MG_EV_MQTT_CONNACK_SERVER_UNAVAILABLE
#define MG_ENV_EXPORT_TO_CGI
void mg_hexdump_connection(struct mg_connection *nc, const char *path, const void *buf, int num_bytes, int ev)
#define MG_F_WEBSOCKET_NO_DEFRAG
#define ARRAY_SIZE(array)
void mg_if_connect_cb(struct mg_connection *nc, int err)
int mg_http_create_digest_auth_header(char *buf, size_t buf_len, const char *method, const char *uri, const char *auth_domain, const char *user, const char *passwd)
struct mg_connection * mg_add_sock_opt(struct mg_mgr *s, sock_t sock, mg_event_handler_t callback, struct mg_add_sock_opts opts)
#define MG_EV_MQTT_CONNECT
int mg_if_listen_udp(struct mg_connection *nc, union socket_address *sa)
void mg_sock_set(struct mg_connection *nc, sock_t sock)
void mg_send_websocket_handshake2(struct mg_connection *nc, const char *path, const char *host, const char *protocol, const char *extra_headers)
void mg_if_udp_send(struct mg_connection *nc, const void *buf, size_t len)
int(* mg_rpc_handler_t)(char *buf, int len, struct mg_rpc_request *req)
void cs_base64_finish(struct cs_base64_ctx *ctx)
int mg_if_listen_tcp(struct mg_connection *nc, union socket_address *sa)
struct mg_connection * mg_connect_opt(struct mg_mgr *mgr, const char *address, mg_event_handler_t callback, struct mg_connect_opts opts)
void mg_register_http_endpoint(struct mg_connection *nc, const char *uri_path, mg_event_handler_t handler)
void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags)
#define MG_MQTT_CMD_PUBCOMP
void mg_mqtt_puback(struct mg_connection *nc, uint16_t message_id)
void mg_printf_websocket_frame(struct mg_connection *nc, int op, const char *fmt,...)
#define MG_MQTT_EVENT_BASE
#define MG_MQTT_CMD_PUBLISH
#define MBUF_SIZE_MULTIPLIER
struct mg_dns_resource_record questions[MG_MAX_DNS_QUESTIONS]
void cs_log_set_level(enum cs_log_level level)
void MD5_Update(MD5_CTX *ctx, const unsigned char *buf, size_t len)
#define JSON_RPC_SERVER_ERROR
int parse_json(const char *s, int s_len, struct json_token *arr, int arr_len)
int cs_base64_decode(const unsigned char *s, int len, char *dst)
#define MG_SOCK_STRINGIFY_IP
size_t c_strnlen(const char *s, size_t maxlen)
int mg_match_prefix(const char *pattern, int pattern_len, const char *str)
#define MG_MQTT_CMD_PINGRESP
#define MG_MQTT_CMD_CONNECT
void cs_hmac_sha1(const unsigned char *key, size_t keylen, const unsigned char *data, size_t datalen, unsigned char out[20])
void mg_set_protocol_http_websocket(struct mg_connection *nc)
void mg_send_websocket_framev(struct mg_connection *nc, int op, const struct mg_str *strv, int strvcnt)
void cs_fprint_base64(FILE *f, const unsigned char *src, int src_len)
int mg_hexdump(const void *buf, int len, char *dst, int dst_len)
int mg_vcasecmp(const struct mg_str *str1, const char *str2)
#define MG_WEBSOCKET_PING_INTERVAL_SECONDS
int mg_base64_decode(const unsigned char *s, int len, char *dst)
struct mg_connection * mg_connect_ws(struct mg_mgr *mgr, mg_event_handler_t ev_handler, const char *url, const char *protocol, const char *extra_headers)
struct mg_connection * listener
struct mg_connection * prev
void mg_mqtt_pubrec(struct mg_connection *nc, uint16_t message_id)
void mg_set_close_on_exec(sock_t sock)
void cs_log_print_prefix(const char *func)
void(* cs_base64_putc_t)(char, void *)
struct mg_connection * mg_connect_ws_opt(struct mg_mgr *mgr, mg_event_handler_t ev_handler, struct mg_connect_opts opts, const char *url, const char *protocol, const char *extra_headers)
#define MG_F_DELETE_CHUNK
int mg_resolve(const char *host, char *buf, size_t n)
void mg_set_protocol_dns(struct mg_connection *nc)
const char * mg_skip(const char *s, const char *end, const char *delims, struct mg_str *v)
int json_emit(char *buf, int buf_len, const char *fmt,...)
void mg_mqtt_suback(struct mg_connection *nc, uint8_t *qoss, size_t qoss_len, uint16_t message_id)
int json_emit_va(char *s, int s_len, const char *fmt, va_list ap)
#define MG_MAX_HTTP_REQUEST_SIZE
#define MG_MQTT_CMD_UNSUBSCRIBE
int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg)
int c_snprintf(char *buf, size_t buf_size, const char *fmt,...)
void mg_mqtt_unsuback(struct mg_connection *nc, uint16_t message_id)
void cs_base64_init(struct cs_base64_ctx *ctx, cs_base64_putc_t b64_putc, void *user_data)
struct mg_dns_resource_record * mg_dns_next_record(struct mg_dns_message *msg, int query, struct mg_dns_resource_record *prev)
void mbuf_init(struct mbuf *mbuf, size_t initial_size)
struct mg_connection * mg_bind_opt(struct mg_mgr *mgr, const char *address, mg_event_handler_t callback, struct mg_bind_opts opts)
void cs_log_printf(const char *fmt,...)
void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id, struct mg_send_mqtt_handshake_opts opts)
#define JSON_RPC_PARSE_ERROR
size_t mbuf_append(struct mbuf *a, const void *buf, size_t len)
int json_emit_quoted_str(char *s, int s_len, const char *str, int len)
#define MG_F_SSL_HANDSHAKE_DONE
void mg_sock_addr_to_str(const union socket_address *sa, char *buf, size_t len, int flags)
void mg_base64_encode(const unsigned char *src, int src_len, char *dst)
#define JSON_TOKEN_ARRAY_TOO_SMALL
void mg_send_websocket_frame(struct mg_connection *nc, int op, const void *data, size_t len)
#define MG_EV_MQTT_PUBLISH
struct mg_connection * mg_bind(struct mg_mgr *srv, const char *address, mg_event_handler_t event_handler)
void(* mg_resolve_callback_t)(struct mg_dns_message *dns_message, void *user_data, enum mg_resolve_err)
#define MG_F_IS_WEBSOCKET
#define MG_RECV_FUNC(s, b, l, f)
struct json_token * find_json_token(struct json_token *toks, const char *path)
struct json_token * parse_json2(const char *s, int s_len)
void cs_sha1_final(unsigned char digest[20], cs_sha1_ctx *context)
int mg_http_parse_header(struct mg_str *hdr, const char *var_name, char *buf, size_t buf_size)
int mg_rpc_parse_reply(const char *buf, int len, struct json_token *toks, int max_toks, struct mg_rpc_reply *rep, struct mg_rpc_error *er)
struct mg_str mg_mk_str(const char *s)
int mg_rpc_create_request(char *buf, int len, const char *method, const char *id, const char *params_fmt,...)
void cs_base64_encode(const unsigned char *src, int src_len, char *dst)
int mg_rpc_create_std_error(char *buf, int len, struct mg_rpc_request *req, int code)
int mg_get_http_var(const struct mg_str *buf, const char *name, char *dst, size_t dst_len)
void mg_send_websocket_handshake(struct mg_connection *nc, const char *path, const char *extra_headers)
#define JSON_RPC_METHOD_NOT_FOUND_ERROR
void mg_printf_http_chunk(struct mg_connection *nc, const char *fmt,...)
void mg_if_connect_tcp(struct mg_connection *nc, const union socket_address *sa)
#define MG_EV_MQTT_SUBSCRIBE
const char * mg_next_comma_list_entry(const char *list, struct mg_str *val, struct mg_str *eq_val)
struct json_token * params
size_t mg_dns_uncompress_name(struct mg_dns_message *msg, struct mg_str *name, char *dst, int dst_len)
struct mg_connection * mg_connect_http_opt(struct mg_mgr *mgr, mg_event_handler_t ev_handler, struct mg_connect_opts opts, const char *url, const char *extra_headers, const char *post_data)
void mbuf_trim(struct mbuf *mbuf)
#define MG_MQTT_CMD_DISCONNECT
#define JSON_STRING_INVALID
FILE * mg_fopen(const char *path, const char *mode)
void mg_enable_multithreading(struct mg_connection *nc)
struct mg_str * mg_get_http_header(struct http_message *hm, const char *name)
void cs_sha1_update(cs_sha1_ctx *context, const unsigned char *data, uint32_t len)
#define MG_MQTT_CMD_SUBSCRIBE
#define MG_MQTT_CMD_SUBACK
void mg_if_recved(struct mg_connection *nc, size_t len)
int mg_vcmp(const struct mg_str *str1, const char *str2)
int mg_parse_uri(struct mg_str uri, struct mg_str *scheme, struct mg_str *user_info, struct mg_str *host, unsigned int *port, struct mg_str *path, struct mg_str *query, struct mg_str *fragment)
int mg_check_ip_acl(const char *acl, uint32_t remote_ip)
struct mg_dns_resource_record answers[MG_MAX_DNS_ANSWERS]
void mg_if_timer(struct mg_connection *c, double now)
mg_event_handler_t proto_handler
void mg_close_conn(struct mg_connection *conn)
void cs_sha1_init(cs_sha1_ctx *context)
int mg_ncasecmp(const char *s1, const char *s2, size_t len)
int mg_dns_parse_record_data(struct mg_dns_message *msg, struct mg_dns_resource_record *rr, void *data, size_t data_len)
int mg_rpc_dispatch(const char *buf, int len, char *dst, int dst_len, const char **methods, mg_rpc_handler_t *handlers)
struct mg_connection * mg_add_sock(struct mg_mgr *s, sock_t sock, mg_event_handler_t callback)
#define MG_EV_MQTT_CONNACK_ACCEPTED
void mg_mqtt_unsubscribe(struct mg_connection *nc, char **topics, size_t topics_len, uint16_t message_id)
#define MG_EV_WEBSOCKET_HANDSHAKE_REQUEST
#define JSON_RPC_INVALID_REQUEST_ERROR
int mg_printf(struct mg_connection *conn, const char *fmt,...)
void mg_mqtt_subscribe(struct mg_connection *nc, const struct mg_mqtt_topic_expression *topics, size_t topics_len, uint16_t message_id)
int mg_rpc_create_reply(char *buf, int len, const struct mg_rpc_request *req, const char *result_fmt,...)
void mg_mqtt_pubrel(struct mg_connection *nc, uint16_t message_id)
void cs_log_set_file(FILE *file)
int mg_mqtt_next_subscribe_topic(struct mg_mqtt_message *msg, struct mg_str *topic, uint8_t *qos, int pos)
#define MG_F_SEND_AND_CLOSE
#define MG_EV_HTTP_REQUEST
@ MG_RESOLVE_EXCEEDED_RETRY_COUNT
#define message(type, str)
static std::string remove(const std::string s, char c)
static char * skip(char **buf, const char *delimiters)
static const char * month_names[]
static char * addenv(struct cgi_env_block *block, PRINTF_FORMAT_STRING(const char *fmt),...) PRINTF_ARGS(2
bool mg_start_task(int priority, int stack_size, mg_init_cb mg_init)
char * inet_ntoa(struct in_addr n)
static void mg_task(void *arg)
const char * inet_ntop(int af, const void *src, char *dst, socklen_t size)
int inet_pton(int af, const char *src, void *dst)
static int mg_is_error(void)
void mg_run_in_task(void(*cb)(struct mg_mgr *mgr, void *arg), void *cb_arg)
#define MG_DISABLE_HTTP_KEEP_ALIVE
int mg_mqtt_match_topic_expression(struct mg_str exp, struct mg_str topic)
#define MG_CTL_MSG_MESSAGE_SIZE
#define MG_COPY_COMMON_CONNECTION_OPTIONS(dst, src)
static size_t mg_http_parse_chunk(char *buf, size_t len, char **chunk_data, size_t *chunk_len)
#define MD5STEP(f, w, x, y, z, data, s)
mg_http_multipart_stream_state
@ MPS_WAITING_FOR_BOUNDARY
int _kill(int pid, int sig)
MG_INTERNAL int parse_mqtt(struct mbuf *io, struct mg_mqtt_message *mm)
static void mg_send_directory_listing(struct mg_connection *nc, const char *dir, struct http_message *hm, struct mg_serve_http_opts *opts)
static void do_ssi_exec(struct mg_connection *nc, char *tag)
static int parse_key(struct frozen *f)
MG_INTERNAL int mg_uri_to_local_path(struct http_message *hm, const struct mg_serve_http_opts *opts, char **local_path, struct mg_str *remainder)
static int capture_ptr(struct frozen *f, const char *ptr, enum json_type type)
static void mg_http_free_proto_data_cgi(struct mg_http_proto_data_cgi *d)
static void mqtt_handler(struct mg_connection *nc, int ev, void *ev_data)
static void mg_recv_common(struct mg_connection *nc, void *buf, int len)
static int path_part_len(const char *p)
static int is_alpha(int ch)
static int mg_resolve2(const char *host, struct in_addr *ina)
static void mg_handle_cgi(struct mg_connection *nc, const char *prog, const struct mg_str *path_info, const struct http_message *hm, const struct mg_serve_http_opts *opts)
static struct mg_http_proto_data * mg_http_get_proto_data(struct mg_connection *c)
static void mg_send_file_data(struct mg_connection *nc, FILE *fp)
void cs_sha1_transform(uint32_t state[5], const unsigned char buffer[64])
#define MG_WS_NO_HOST_HEADER_MAGIC
MG_INTERNAL void mg_remove_conn(struct mg_connection *c)
static void mg_resolve_async_eh(struct mg_connection *nc, int ev, void *data)
void mg_ev_mgr_add_conn(struct mg_connection *nc)
static const struct @23 mg_static_builtin_mime_types[]
#define MIME_ENTRY(_ext, _type)
MG_INTERNAL struct mg_connection * mg_create_connection_base(struct mg_mgr *mgr, mg_event_handler_t callback, struct mg_add_sock_opts opts)
#define R1(v, w, x, y, z, i)
MG_INTERNAL struct mg_connection * mg_create_connection(struct mg_mgr *mgr, mg_event_handler_t callback, struct mg_add_sock_opts opts)
MG_INTERNAL void mg_call(struct mg_connection *nc, mg_event_handler_t ev_handler, int ev, void *ev_data)
static int mg_recvfrom(struct mg_connection *nc, union socket_address *sa, socklen_t *sa_len, char **buf)
static void mg_write_to_socket(struct mg_connection *nc)
static int parse_value(struct frozen *f)
#define _MG_CALLBACK_MODIFIABLE_FLAGS_MASK
static unsigned char from_b64(unsigned char ch)
static int mg_create_itermediate_directories(const char *path)
MG_INTERNAL void mg_send_http_file(struct mg_connection *nc, char *path, const struct mg_str *path_info, struct http_message *hm, struct mg_serve_http_opts *opts)
static void dns_handler(struct mg_connection *nc, int ev, void *ev_data)
static void mg_websocket_handler(struct mg_connection *nc, int ev, void *ev_data)
static int c_itoa(char *buf, size_t buf_size, int64_t num, int base, int flags, int field_width)
#define MG_UDP_RECV_BUFFER_SIZE
static int parse_net(const char *spec, uint32_t *net, uint32_t *mask)
static int parse_object(struct frozen *f)
static int capture_len(struct frozen *f, int token_index, const char *ptr)
static void mg_http_construct_etag(char *buf, size_t buf_len, const cs_stat_t *st)
MG_INTERNAL size_t recv_avail_size(struct mg_connection *conn, size_t max)
static void mg_http_conn_destructor(void *proto_data)
static int parse_array(struct frozen *f)
MG_INTERNAL char mg_dns_server[300]
void _not_implemented(const char *what)
MG_INTERNAL time_t mg_parse_date_string(const char *datetime)
static void resolve_cb(struct mg_dns_message *msg, void *data, enum mg_resolve_err e)
static sock_t mg_open_listening_socket(union socket_address *sa, int proto)
static int parse_number(struct frozen *f)
static mg_event_handler_t mg_http_get_endpoint_handler(struct mg_connection *nc, struct mg_str *uri_path)
static int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len)
static int mg_is_ws_first_fragment(unsigned char flags)
static int mg_remove_directory(const struct mg_serve_http_opts *opts, const char *dir)
static int mg_http_common_url_parse(const char *url, const char *schema, const char *schema_tls, int *use_ssl, char **addr, int *port_i, const char **path)
static int mg_check_nonce(const char *nonce)
static void mg_scan_directory(struct mg_connection *nc, const char *dir, const struct mg_serve_http_opts *opts, void(*func)(struct mg_connection *, const char *, cs_stat_t *))
static void mg_do_ssi_call(struct mg_connection *nc, char *tag)
static void MD5Transform(uint32_t buf[4], uint32_t const in[16])
static void mg_print_dir_entry(struct mg_connection *nc, const char *file_name, cs_stat_t *stp)
static void mg_destroy_conn(struct mg_connection *conn)
static void cs_base64_emit_chunk(struct cs_base64_ctx *ctx)
#define _MG_ALLOWED_CONNECT_FLAGS_MASK
struct mg_connection * mg_connect_http_base(struct mg_mgr *mgr, mg_event_handler_t ev_handler, struct mg_connect_opts opts, const char *schema, const char *schema_ssl, const char *url, const char **path, char **addr)
static void mg_http_send_error(struct mg_connection *nc, int code, const char *reason)
static void mg_handle_udp_read(struct mg_connection *nc)
static int left(const struct frozen *f)
static void mg_handle_put(struct mg_connection *nc, const char *path, struct http_message *hm)
static const char * mg_http_parse_headers(const char *s, const char *end, int len, struct http_message *req)
static size_t mg_url_encode(const char *src, size_t s_len, char *dst, size_t dst_len)
static int is_space(int ch)
static int parse_string(struct frozen *f)
static void mg_escape(const char *src, char *dst, size_t dst_len)
#define R2(v, w, x, y, z, i)
static void mg_send_ssi_file(struct mg_connection *, const char *, FILE *, int, const struct mg_serve_http_opts *)
MG_INTERNAL int mg_is_not_modified(struct http_message *hm, cs_stat_t *st)
static int parse_pair(struct frozen *f)
static int mg_get_month_index(const char *s)
static int get_utf8_char_len(unsigned char ch)
static uint32_t mg_ws_random_mask(void)
static void mg_sock_get_addr(sock_t sock, int remote, union socket_address *sa)
void mg_ev_mgr_remove_conn(struct mg_connection *nc)
static void skip_whitespaces(struct frozen *f)
static const char * mg_default_dns_server
MG_INTERNAL int mg_parse_address(const char *str, union socket_address *sa, int *proto, char *host, size_t host_len)
static void mg_http_free_proto_data_file(struct mg_http_proto_data_file *d)
static int parse_identifier(struct frozen *f)
#define _MG_F_FD_CAN_READ
static void byteReverse(unsigned char *buf, unsigned longs)
static int mg_get_ip_address_of_nameserver(char *name, size_t name_len)
static void parse_uri_component(const char **p, const char *end, char sep, struct mg_str *res)
static int mg_is_dav_request(const struct mg_str *s)
#define R0(v, w, x, y, z, i)
static unsigned char * mg_parse_dns_resource_record(unsigned char *data, unsigned char *end, struct mg_dns_resource_record *rr, int reply)
static int doit(struct frozen *f)
static void mg_mgr_handle_ctl_sock(struct mg_mgr *mgr)
#define MG_SET_PTRPTR(_ptr, _v)
static char * mg_addenv(struct mg_cgi_env_block *block, const char *fmt,...)
static int lowercase(const char *s)
static int test_and_skip(struct frozen *f, int expected)
static void mg_read_from_socket(struct mg_connection *conn)
static void cs_base64_emit_code(struct cs_base64_ctx *ctx, int v)
static void mg_ws_mask_frame(struct mbuf *mbuf, struct ws_mask_ctx *ctx)
static void mg_prepare_cgi_environment(struct mg_connection *nc, const char *prog, const struct mg_str *path_info, const struct http_message *hm, const struct mg_serve_http_opts *opts, struct mg_cgi_env_block *blk)
void mg_ev_mgr_init(struct mg_mgr *mgr)
static void mg_handle_incoming_websocket_frame(struct mg_connection *nc, struct websocket_message *wsm)
void mg_forward(struct mg_connection *from, struct mg_connection *to)
static int mg_is_file_hidden(const char *path, const struct mg_serve_http_opts *opts, int exclude_specials)
static void mg_handle_ssi_request(struct mg_connection *nc, const char *path, const struct mg_serve_http_opts *opts)
static int mg_http_get_request_len(const char *s, int buf_len)
MG_INTERNAL size_t mg_handle_chunked(struct mg_connection *nc, struct http_message *hm, char *buf, size_t blen)
int _gettimeofday_r(struct _reent *r, struct timeval *tp, void *tzp)
static int expect(struct frozen *f, const char *s, int len, enum json_type t)
static void mg_addenv2(struct mg_cgi_env_block *blk, const char *name)
static int is_hex_digit(int ch)
void mg_set_non_blocking_mode(sock_t sock)
static void mg_send_ws_header(struct mg_connection *nc, int op, size_t len, struct ws_mask_ctx *ctx)
#define C_SNPRINTF_FLAG_ZERO
static int mg_http_parse_range_header(const struct mg_str *header, int64_t *a, int64_t *b)
static void mg_print_props(struct mg_connection *nc, const char *name, cs_stat_t *stp)
#define BASE64_ENCODE_BODY
static pid_t mg_start_process(const char *interp, const char *cmd, const char *env, const char *envp[], const char *dir, sock_t sock)
#define R3(v, w, x, y, z, i)
static void mg_accept_conn(struct mg_connection *lc)
static int test_no_skip(struct frozen *f, int expected)
static void mg_http_send_file2(struct mg_connection *nc, const char *path, cs_stat_t *st, struct http_message *hm, struct mg_serve_http_opts *opts)
#define MG_TCP_RECV_BUFFER_SIZE
int gettimeofday(struct timeval *tp, void *tzp)
static int mg_is_ws_fragment(unsigned char flags)
MG_INTERNAL void mg_find_index_file(const char *path, const char *list, char **index_file, cs_stat_t *stp)
static int mg_deliver_websocket_data(struct mg_connection *nc)
MG_INTERNAL void mg_add_conn(struct mg_mgr *mgr, struct mg_connection *c)
static void mg_http_free_proto_data_endpoints(struct mg_http_endpoint **ep)
static void mg_cgi_ev_handler(struct mg_connection *cgi_nc, int ev, void *ev_data)
#define _MG_F_FD_CAN_WRITE
static int cur(struct frozen *f)
static void mg_http_send_digest_auth_request(struct mg_connection *c, const char *domain)
static size_t mg_get_line_len(const char *buf, size_t buf_len)
MG_INTERNAL struct mg_connection * mg_do_connect(struct mg_connection *nc, int proto, union socket_address *sa)
static void mg_http_call_endpoint_handler(struct mg_connection *nc, int ev, struct http_message *hm)
static void mg_handle_mkcol(struct mg_connection *nc, const char *path, struct http_message *hm)
static void mg_http_transfer_file_data(struct mg_connection *nc)
static void mg_mkmd5resp(const char *method, size_t method_len, const char *uri, size_t uri_len, const char *ha1, size_t ha1_len, const char *nonce, size_t nonce_len, const char *nc, size_t nc_len, const char *cnonce, size_t cnonce_len, const char *qop, size_t qop_len, char *resp)
static int mg_is_authorized(struct http_message *hm, const char *path, int is_directory, const char *domain, const char *passwords_file, int is_global_pass_file)
void mg_http_handler(struct mg_connection *nc, int ev, void *ev_data)
#define EXPECT(cond, err_code)
static void mg_mqtt_prepend_header(struct mg_connection *nc, uint8_t cmd, uint8_t flags, size_t len)
static int mg_http_send_port_based_redirect(struct mg_connection *c, struct http_message *hm, const struct mg_serve_http_opts *opts)
static void mg_handle_delete(struct mg_connection *nc, const struct mg_serve_http_opts *opts, const char *path)
int json_emit_long(char *buf, int buf_len, long int value)
static int mg_http_check_digest_auth(struct http_message *hm, const char *auth_domain, FILE *fp)
void mg_add_to_set(sock_t sock, fd_set *set, sock_t *max_fd)
void fprint_str(FILE *fp, const char *str)
static void mg_handle_move(struct mg_connection *c, const struct mg_serve_http_opts *opts, const char *path, struct http_message *hm)
static int get_escape_len(const char *s, int len)
static void mg_handle_propfind(struct mg_connection *nc, const char *path, cs_stat_t *stp, struct http_message *hm, struct mg_serve_http_opts *opts)
void MD5_Final(unsigned char digest[16], MD5_CTX *ctx)
static void mg_gmt_time_string(char *buf, size_t buf_len, time_t *t)
static int compare(const char *s, const char *str, int len)
static void mg_http_send_options(struct mg_connection *nc)
static uint32_t blk0(union char64long16 *block, int i)
#define MG_DEFAULT_NAMESERVER
#define C_SNPRINTF_APPEND_CHAR(ch)
static int is_digit(int ch)
static void mg_ws_handshake(struct mg_connection *nc, const struct mg_str *key)
static int mg_is_creation_request(const struct http_message *hm)
void mg_ev_mgr_free(struct mg_mgr *mgr)
static int mg_num_leap_years(int year)
#define R4(v, w, x, y, z, i)
void mg_mgr_handle_conn(struct mg_connection *nc, int fd_flags, double now)
static struct mg_str mg_get_mime_type(const char *path, const char *dflt, const struct mg_serve_http_opts *opts)
static void mg_send_mqtt_short_command(struct mg_connection *nc, uint8_t cmd, uint16_t message_id)
struct callback_addr callback
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
char message[MG_CTL_MSG_MESSAGE_SIZE]
mg_event_handler_t callback
struct json_token * tokens
const char * vars[MG_MAX_CGI_ENVIR_VARS]
struct mg_connection * nc
char buf[MG_CGI_ENVIRONMENT_SIZE]
mg_event_handler_t handler
struct mg_http_endpoint * next
enum mg_http_multipart_stream_state state
struct mg_connection * cgi_nc
enum mg_http_proto_data_type type
struct mg_http_proto_data_chuncked chunk
struct mg_http_endpoint * endpoints
struct mg_http_proto_data_cgi cgi
mg_event_handler_t endpoint_handler
void(* cb)(struct mg_mgr *mgr, void *arg)
mg_resolve_callback_t callback
static double comma(double a, double b)
static te_expr * base(state *s)
static te_expr * list(state *s)