5#line 1 "mongoose/src/mg_internal.h"
12#ifndef CS_MONGOOSE_SRC_INTERNAL_H_
13#define CS_MONGOOSE_SRC_INTERNAL_H_
18#define MBUF_REALLOC MG_REALLOC
22#define MBUF_FREE MG_FREE
25#define MG_SET_PTRPTR(_ptr, _v) \
27 if (_ptr) *(_ptr) = _v; \
31#define MG_INTERNAL static
43#ifndef MG_CTL_MSG_MESSAGE_SIZE
44#define MG_CTL_MSG_MESSAGE_SIZE 8192
53 int *proto,
char *host,
size_t host_len);
76#define MG_MQTT_ERROR_INCOMPLETE_MSG -1
77#define MG_MQTT_ERROR_MALFORMED_MSG -2
83extern void *(*test_malloc)(
size_t size);
84extern void *(*test_calloc)(
size_t count,
size_t size);
87#define MIN(a, b) ((a) < (b) ? (a) : (b))
111#if MG_ENABLE_FILESYSTEM
119#if MG_ENABLE_HTTP_CGI
121 const struct mg_str *path_info,
127#if MG_ENABLE_HTTP_SSI
133#if MG_ENABLE_HTTP_WEBDAV
150#if MG_ENABLE_HTTP_WEBSOCKET
169#ifdef MG_MODULE_LINES
170#line 1 "common/mg_mem.h"
189#ifndef CS_COMMON_MG_MEM_H_
190#define CS_COMMON_MG_MEM_H_
197#define MG_MALLOC malloc
201#define MG_CALLOC calloc
205#define MG_REALLOC realloc
217#ifdef MG_MODULE_LINES
218#line 1 "common/cs_base64.c"
237#ifndef EXCLUDE_COMMON
247#define NUM_UPPERCASES ('Z' - 'A' + 1)
248#define NUM_LETTERS (NUM_UPPERCASES * 2)
249#define NUM_DIGITS ('9' - '0' + 1)
294 const unsigned char *src = (
const unsigned char *)
str;
296 for (
i = 0;
i < len;
i++) {
316#define BASE64_ENCODE_BODY \
317 static const char *b64 = \
318 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; \
321 for (i = j = 0; i < src_len; i += 3) { \
323 b = i + 1 >= src_len ? 0 : src[i + 1]; \
324 c = i + 2 >= src_len ? 0 : src[i + 2]; \
326 BASE64_OUT(b64[a >> 2]); \
327 BASE64_OUT(b64[((a & 3) << 4) | (b >> 4)]); \
328 if (i + 1 < src_len) { \
329 BASE64_OUT(b64[(b & 15) << 2 | (c >> 6)]); \
331 if (i + 2 < src_len) { \
332 BASE64_OUT(b64[c & 63]); \
336 while (j % 4 != 0) { \
341#define BASE64_OUT(ch) \
346#define BASE64_FLUSH() \
359#define BASE64_OUT(ch) \
361 fprintf(f, "%c", (ch)); \
365#define BASE64_FLUSH()
378 static const unsigned char tab[128] = {
412 return tab[
ch & 127];
416 unsigned char a, b,
c,
d;
419 while (len >= 4 && (a =
from_b64(s[0])) != 255 &&
424 if (a == 200 || b == 200)
break;
437#ifdef MG_MODULE_LINES
438#line 1 "common/cs_dbg.h"
457#ifndef CS_COMMON_CS_DBG_H_
458#define CS_COMMON_CS_DBG_H_
466#ifndef CS_ENABLE_DEBUG
467#define CS_ENABLE_DEBUG 0
470#ifndef CS_LOG_PREFIX_LEN
471#define CS_LOG_PREFIX_LEN 24
474#ifndef CS_LOG_ENABLE_TS_DIFF
475#define CS_LOG_ENABLE_TS_DIFF 0
552 if (cs_log_print_prefix(l, __FILE__, __LINE__)) { \
559#define LOG(l, x) ((void) l)
568#define DBG(x) LOG(LL_VERBOSE_DEBUG, x)
588#ifdef MG_MODULE_LINES
589#line 1 "common/cs_dbg.c"
631#if CS_LOG_ENABLE_TS_DIFF
651 size_t fl = 0, ll = 0,
pl = 0;
658 const char c = *(p - 1);
659 if (
c ==
'/' ||
c ==
'\\')
break;
664 ll = (
ln < 10000 ? (
ln < 1000 ? (
ln < 100 ? (
ln < 10 ? 1 : 2) : 3) : 4) : 5);
672 *(--
q) =
'0' + (
ln % 10);
693#if CS_LOG_ENABLE_TS_DIFF
730#if CS_LOG_ENABLE_TS_DIFF && CS_ENABLE_STDIO
734#ifdef MG_MODULE_LINES
735#line 1 "common/cs_dirent.h"
754#ifndef CS_COMMON_CS_DIRENT_H_
755#define CS_COMMON_CS_DIRENT_H_
765#ifdef CS_DEFINE_DIRENT
788#ifdef MG_MODULE_LINES
789#line 1 "common/cs_dirent.c"
808#ifndef EXCLUDE_COMMON
841 dir->result.d_name[0] =
'\0';
857 result =
FindClose(dir->handle) ? 0 : -1;
872 memset(&dir->result, 0,
sizeof(dir->result));
874 result = &dir->result;
876 result->d_name,
sizeof(result->d_name),
NULL,
899#ifdef MG_MODULE_LINES
900#line 1 "common/cs_time.c"
926#if !(defined(__ARMCC_VERSION) || defined(__ICCARM__)) && \
927 !defined(__TI_COMPILER_VERSION__) && \
928 (!defined(CS_PLATFORM) || CS_PLATFORM != CS_P_NXP_LPC)
941 now = (
double)
tv.tv_sec + (((
double)
tv.tv_usec) / 1000000.0);
965 static const int month_day[12] = {0, 31, 59, 90, 120, 151,
966 181, 212, 243, 273, 304, 334};
970 int year =
tm->tm_year +
tm->tm_mon / 12;
997#ifdef MG_MODULE_LINES
998#line 1 "common/cs_endian.h"
1017#ifndef CS_COMMON_CS_ENDIAN_H_
1018#define CS_COMMON_CS_ENDIAN_H_
1029#if !defined(BYTE_ORDER) && defined(__BYTE_ORDER)
1030#define BYTE_ORDER __BYTE_ORDER
1031#ifndef LITTLE_ENDIAN
1032#define LITTLE_ENDIAN __LITTLE_ENDIAN
1035#define BIG_ENDIAN __LITTLE_ENDIAN
1044#ifdef MG_MODULE_LINES
1045#line 1 "common/cs_md5.c"
1067#if !defined(EXCLUDE_COMMON)
1074#if BYTE_ORDER == BIG_ENDIAN
1077 ((
unsigned) buf[1] << 8 | buf[0]);
1087#define F1(x, y, z) (z ^ (x & (y ^ z)))
1088#define F2(x, y, z) F1(z, x, y)
1089#define F3(x, y, z) (x ^ y ^ z)
1090#define F4(x, y, z) (y ^ (x | ~z))
1092#define MD5STEP(f, w, x, y, z, data, s) \
1093 (w += f(x, y, z) + data, w = w << s | w >> (32 - s), w += x)
1100 ctx->
buf[0] = 0x67452301;
1101 ctx->
buf[1] = 0xefcdab89;
1102 ctx->
buf[2] = 0x98badcfe;
1103 ctx->
buf[3] = 0x10325476;
1198 t = (t >> 3) & 0x3f;
1201 unsigned char *p = (
unsigned char *) ctx->
in + t;
1247 a[14] = ctx->
bits[0];
1248 a[15] = ctx->
bits[1];
1253 memset((
char *) ctx, 0,
sizeof(*ctx));
1258#ifdef MG_MODULE_LINES
1259#line 1 "common/cs_sha1.c"
1266#if !CS_DISABLE_SHA1 && !defined(EXCLUDE_COMMON)
1276 unsigned char c[64];
1280#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
1284#if BYTE_ORDER == LITTLE_ENDIAN
1300 (block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] ^ \
1301 block->l[(i + 2) & 15] ^ block->l[i & 15], \
1303#define R0(v, w, x, y, z, i) \
1304 z += ((w & (x ^ y)) ^ y) + blk0(block, i) + 0x5A827999 + rol(v, 5); \
1306#define R1(v, w, x, y, z, i) \
1307 z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
1309#define R2(v, w, x, y, z, i) \
1310 z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
1312#define R3(v, w, x, y, z, i) \
1313 z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
1315#define R4(v, w, x, y, z, i) \
1316 z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
1329 R0(a, b,
c,
d,
e, 0);
1330 R0(
e, a, b,
c,
d, 1);
1331 R0(
d,
e, a, b,
c, 2);
1332 R0(
c,
d,
e, a, b, 3);
1333 R0(b,
c,
d,
e, a, 4);
1334 R0(a, b,
c,
d,
e, 5);
1335 R0(
e, a, b,
c,
d, 6);
1336 R0(
d,
e, a, b,
c, 7);
1337 R0(
c,
d,
e, a, b, 8);
1338 R0(b,
c,
d,
e, a, 9);
1339 R0(a, b,
c,
d,
e, 10);
1340 R0(
e, a, b,
c,
d, 11);
1341 R0(
d,
e, a, b,
c, 12);
1342 R0(
c,
d,
e, a, b, 13);
1343 R0(b,
c,
d,
e, a, 14);
1344 R0(a, b,
c,
d,
e, 15);
1345 R1(
e, a, b,
c,
d, 16);
1346 R1(
d,
e, a, b,
c, 17);
1347 R1(
c,
d,
e, a, b, 18);
1348 R1(b,
c,
d,
e, a, 19);
1349 R2(a, b,
c,
d,
e, 20);
1350 R2(
e, a, b,
c,
d, 21);
1351 R2(
d,
e, a, b,
c, 22);
1352 R2(
c,
d,
e, a, b, 23);
1353 R2(b,
c,
d,
e, a, 24);
1354 R2(a, b,
c,
d,
e, 25);
1355 R2(
e, a, b,
c,
d, 26);
1356 R2(
d,
e, a, b,
c, 27);
1357 R2(
c,
d,
e, a, b, 28);
1358 R2(b,
c,
d,
e, a, 29);
1359 R2(a, b,
c,
d,
e, 30);
1360 R2(
e, a, b,
c,
d, 31);
1361 R2(
d,
e, a, b,
c, 32);
1362 R2(
c,
d,
e, a, b, 33);
1363 R2(b,
c,
d,
e, a, 34);
1364 R2(a, b,
c,
d,
e, 35);
1365 R2(
e, a, b,
c,
d, 36);
1366 R2(
d,
e, a, b,
c, 37);
1367 R2(
c,
d,
e, a, b, 38);
1368 R2(b,
c,
d,
e, a, 39);
1369 R3(a, b,
c,
d,
e, 40);
1370 R3(
e, a, b,
c,
d, 41);
1371 R3(
d,
e, a, b,
c, 42);
1372 R3(
c,
d,
e, a, b, 43);
1373 R3(b,
c,
d,
e, a, 44);
1374 R3(a, b,
c,
d,
e, 45);
1375 R3(
e, a, b,
c,
d, 46);
1376 R3(
d,
e, a, b,
c, 47);
1377 R3(
c,
d,
e, a, b, 48);
1378 R3(b,
c,
d,
e, a, 49);
1379 R3(a, b,
c,
d,
e, 50);
1380 R3(
e, a, b,
c,
d, 51);
1381 R3(
d,
e, a, b,
c, 52);
1382 R3(
c,
d,
e, a, b, 53);
1383 R3(b,
c,
d,
e, a, 54);
1384 R3(a, b,
c,
d,
e, 55);
1385 R3(
e, a, b,
c,
d, 56);
1386 R3(
d,
e, a, b,
c, 57);
1387 R3(
c,
d,
e, a, b, 58);
1388 R3(b,
c,
d,
e, a, 59);
1389 R4(a, b,
c,
d,
e, 60);
1390 R4(
e, a, b,
c,
d, 61);
1391 R4(
d,
e, a, b,
c, 62);
1392 R4(
c,
d,
e, a, b, 63);
1393 R4(b,
c,
d,
e, a, 64);
1394 R4(a, b,
c,
d,
e, 65);
1395 R4(
e, a, b,
c,
d, 66);
1396 R4(
d,
e, a, b,
c, 67);
1397 R4(
c,
d,
e, a, b, 68);
1398 R4(b,
c,
d,
e, a, 69);
1399 R4(a, b,
c,
d,
e, 70);
1400 R4(
e, a, b,
c,
d, 71);
1401 R4(
d,
e, a, b,
c, 72);
1402 R4(
c,
d,
e, a, b, 73);
1403 R4(b,
c,
d,
e, a, 74);
1404 R4(a, b,
c,
d,
e, 75);
1405 R4(
e, a, b,
c,
d, 76);
1406 R4(
d,
e, a, b,
c, 77);
1407 R4(
c,
d,
e, a, b, 78);
1408 R4(b,
c,
d,
e, a, 79);
1417 a = b =
c =
d =
e = 0;
1426 context->
state[0] = 0x67452301;
1427 context->
state[1] = 0xEFCDAB89;
1428 context->
state[2] = 0x98BADCFE;
1429 context->
state[3] = 0x10325476;
1430 context->
state[4] = 0xC3D2E1F0;
1439 if ((context->
count[0] += len << 3) <
j) context->
count[1]++;
1440 context->
count[1] += (len >> 29);
1442 if ((
j + len) > 63) {
1445 for (;
i + 63 < len;
i += 64) {
1458 for (
i = 0;
i < 8;
i++) {
1460 ((3 - (
i & 3)) * 8)) &
1465 while ((context->
count[0] & 504) != 448) {
1470 for (
i = 0;
i < 20;
i++) {
1472 (
unsigned char) ((context->
state[
i >> 2] >> ((3 - (
i & 3)) * 8)) & 255);
1474 memset(context,
'\0',
sizeof(*context));
1480 unsigned char out[20]) {
1497 for (
i = 0;
i <
sizeof(
buf1);
i++) {
1514#ifdef MG_MODULE_LINES
1515#line 1 "common/mbuf.c"
1534#ifndef EXCLUDE_COMMON
1541#define MBUF_REALLOC realloc
1545#define MBUF_FREE free
1592 if (~(
size_t) 0 - (
size_t) a->
buf < len)
return 0;
1668#ifdef MG_MODULE_LINES
1669#line 1 "common/mg_str.c"
1700 struct mg_str ret = {s, 0};
1759 for (
i = 0;
i < s.
len;
i++) {
1760 if (s.
p[
i] ==
c)
return &s.
p[
i];
1773 if (
i <
str1.len)
return 1;
1774 if (
i <
str2.len)
return -1;
1794 char *
sp = (
char *) s->
p;
1832#ifdef MG_MODULE_LINES
1833#line 1 "common/str_util.c"
1852#ifndef EXCLUDE_COMMON
1858#ifndef C_DISABLE_BUILTIN_SNPRINTF
1859#define C_DISABLE_BUILTIN_SNPRINTF 0
1867 for (; l <
maxlen && s[l] !=
'\0'; l++) {
1872#define C_SNPRINTF_APPEND_CHAR(ch) \
1874 if (i < (int) buf_size) buf[i] = ch; \
1878#define C_SNPRINTF_FLAG_ZERO 1
1880#if C_DISABLE_BUILTIN_SNPRINTF
1889 int i = 0,
k = 0,
neg = 0;
1931 while ((
ch = *fmt++) !=
'\0') {
1951 while (*fmt >=
'0' && *fmt <=
'9') {
1968 while (*fmt >=
'0' && *fmt <=
'9') {
1999 const char *s =
va_arg(
ap,
const char *);
2002 for (
j = 0;
j <
pad;
j++) {
2013 }
else if (
ch ==
'c') {
2019 }
else if (
ch ==
'd' &&
len_mod ==
'l') {
2023 }
else if (
ch ==
'd' &&
len_mod ==
'z') {
2027 }
else if (
ch ==
'd' &&
len_mod ==
'q') {
2030 }
else if ((
ch ==
'x' ||
ch ==
'u') &&
len_mod == 0) {
2033 }
else if ((
ch ==
'x' ||
ch ==
'u') &&
len_mod ==
'l') {
2036 }
else if ((
ch ==
'x' ||
ch ==
'u') &&
len_mod ==
'z') {
2039 }
else if (
ch ==
'p') {
2058 buf[
i < (
int) buf_size ?
i : (
int) buf_size - 1] =
'\0';
2065int c_snprintf(
char *buf,
size_t buf_size,
const char *fmt, ...)
WEAK;
2080 strncpy(buf, path,
sizeof(buf));
2081 buf[
sizeof(buf) - 1] =
'\0';
2085 while (
p > buf &&
p[-1] !=
':' && (
p[0] ==
'\\' ||
p[0] ==
'/')) *
p-- =
'\0';
2126char *
strdup(
const char *src) {
2138 static const char *
hex =
"0123456789abcdef";
2140 for (;
len--;
p++) {
2142 *
to++ =
hex[
p[0] & 0x0f];
2148 if (
ch >=
'0' &&
ch <=
'9') {
2150 }
else if (
ch >=
'a' &&
ch <=
'f') {
2151 return ch -
'a' + 10;
2152 }
else if (
ch >=
'A' &&
ch <=
'F') {
2153 return ch -
'A' + 10;
2162 for (
i = 0;
i <
len;
i += 2) {
2173 while (*s &&
isspace((
unsigned char) *s)) s++;
2178 while (
isdigit((
unsigned char) *s)) {
2180 result += (*s -
'0');
2183 return result *
neg;
2188 return tolower(*(
const unsigned char *) s);
2197 }
while (
diff == 0 &&
s1[-1] !=
'\0' && --
len > 0);
2252 }
else if (
len >= (
int) size) {
2278 if (
list.len == 0) {
2315 size_t res = 0,
len = 0,
i = 0,
j = 0;
2327 for (;
i < pattern.
len &&
j <
str.len;
i++,
j++) {
2328 if (pattern.
p[
i] ==
'?') {
2330 }
else if (pattern.
p[
i] ==
'*') {
2332 if (
i < pattern.
len && pattern.
p[
i] ==
'*') {
2339 if (
i == pattern.
len || (pattern.
p[
i] ==
'$' &&
i == pattern.
len - 1))
2345 }
while (
res == 0 &&
len != 0 &&
len-- > 0);
2352 if (
i < pattern.
len && pattern.
p[
i] ==
'$') {
2353 return j ==
str.len ?
str.len : 0;
2355 return i == pattern.
len ?
j : 0;
2367#ifdef MG_MODULE_LINES
2368#line 1 "mongoose/src/mg_net.c"
2394#define MG_MAX_HOST_LEN 200
2396#ifndef MG_TCP_IO_SIZE
2398#define MG_TCP_IO_SIZE 1460000
2400#ifndef MG_UDP_IO_SIZE
2401#define MG_UDP_IO_SIZE 1460
2404#define MG_COPY_COMMON_CONNECTION_OPTIONS(dst, src) \
2405 memcpy(dst, src, sizeof(*dst));
2408#define _MG_ALLOWED_CONNECT_FLAGS_MASK \
2409 (MG_F_USER_1 | MG_F_USER_2 | MG_F_USER_3 | MG_F_USER_4 | MG_F_USER_5 | \
2410 MG_F_USER_6 | MG_F_WEBSOCKET_NO_DEFRAG | MG_F_ENABLE_BROADCAST)
2412#define _MG_CALLBACK_MODIFIABLE_FLAGS_MASK \
2413 (MG_F_USER_1 | MG_F_USER_2 | MG_F_USER_3 | MG_F_USER_4 | MG_F_USER_5 | \
2414 MG_F_USER_6 | MG_F_WEBSOCKET_NO_DEFRAG | MG_F_SEND_AND_CLOSE | \
2415 MG_F_CLOSE_IMMEDIATELY | MG_F_IS_WEBSOCKET | MG_F_DELETE_CHUNK)
2418#define intptr_t long
2422 DBG((
"%p %p", mgr,
c));
2452 DBG((
"%p %s ev=%d ev_data=%p flags=0x%lx rmbl=%d smbl=%d", nc,
2457#if !defined(NO_LIBC) && MG_ENABLE_HEXDUMP
2474 DBG((
"%p after %s flags=0x%lx rmbl=%d smbl=%d", nc,
2478#if !MG_ENABLE_CALLBACK_USERDATA
2484 if (
c->ev_timer_time > 0 &&
now >=
c->ev_timer_time) {
2486 c->ev_timer_time = 0;
2548 memset(conn, 0,
sizeof(*conn));
2584 memset(m, 0,
sizeof(*m));
2585#if MG_ENABLE_BROADCAST
2595#elif defined(__unix__)
2603 if (
opts.num_ifaces == 0) {
2613 for (
i = 0;
i <
opts.num_ifaces;
i++) {
2621 DBG((
"=================================="));
2622 DBG((
"init mgr=%p", m));
2638 if (m ==
NULL)
return;
2642#if MG_ENABLE_BROADCAST
2699#if MG_ENABLE_SYNC_RESOLVER
2702#if MG_ENABLE_GETADDRINFO
2714 memcpy(&h, &p->ai_addr,
sizeof(h));
2731int mg_resolve(
const char *host,
char *buf,
size_t n) {
2794 int *proto,
char *host,
size_t host_len) {
2795 unsigned int a, b,
c,
d, port = 0;
2818 if (
sscanf(
str,
"%u.%u.%u.%u:%u%n", &a, &b, &
c, &
d, &port, &len) == 5) {
2820 sa->
sin.sin_addr.s_addr =
2824 }
else if (
sscanf(
str,
"[%99[^]]]:%u%n",
buf, &port, &len) == 2 &&
2830#if MG_ENABLE_ASYNC_RESOLVER
2832 sscanf(
str,
"%[^ :]:%u%n", host, &port, &len) == 2) {
2845#if MG_ENABLE_SYNC_RESOLVER
2854 }
else if (
sscanf(
str,
":%u%n", &port, &len) == 1 ||
2855 sscanf(
str,
"%u%n", &port, &len) == 1) {
2867 return port < 0xffffUL && (
ch ==
'\0' ||
ch ==
',' ||
isspace(
ch)) ? len : -1;
2984 DBG((
"%p <- %d bytes (SSL)", nc,
n));
3002 DBG((
"%p <- %d bytes", nc,
n));
3007#if !defined(NO_LIBC) && MG_ENABLE_HEXDUMP
3082#if !defined(NO_LIBC) && MG_ENABLE_HEXDUMP
3114 DBG((
"%p -> %d bytes (SSL)", nc,
n));
3137 DBG((
"%p -> %d bytes", nc,
n));
3140#if !defined(NO_LIBC) && MG_ENABLE_HEXDUMP
3179 (
"%p %s://%s:%hu -> %d", nc, (nc->
flags &
MG_F_UDP ?
"udp" :
"tcp"),
3195#if MG_ENABLE_ASYNC_RESOLVER
3255#if MG_ENABLE_CALLBACK_USERDATA
3287#if MG_ENABLE_CALLBACK_USERDATA
3295 (
"%p %s %s,%s,%s", nc, address, (
opts.ssl_cert ?
opts.ssl_cert :
"-"),
3296 (
opts.ssl_key ?
opts.ssl_key :
"-"),
3297 (
opts.ssl_ca_cert ?
opts.ssl_ca_cert :
"-")));
3308 memset(¶ms, 0,
sizeof(params));
3309 params.cert =
opts.ssl_cert;
3310 params.key =
opts.ssl_key;
3311 params.ca_cert =
opts.ssl_ca_cert;
3312 params.cipher_suites =
opts.ssl_cipher_suites;
3313 params.psk_identity =
opts.ssl_psk_identity;
3314 params.psk_key =
opts.ssl_psk_key;
3316 if (
opts.ssl_server_name !=
NULL) {
3317 if (
strcmp(
opts.ssl_server_name,
"*") != 0) {
3318 params.server_name =
opts.ssl_server_name;
3320 }
else if (
rc == 0) {
3321 params.server_name = host;
3334#if MG_ENABLE_ASYNC_RESOLVER
3343 o.nameserver =
opts.nameserver;
3382#if MG_ENABLE_CALLBACK_USERDATA
3405 DBG((
"%p %s %s,%s,%s", nc, address, (
opts.ssl_cert ?
opts.ssl_cert :
"-"),
3406 (
opts.ssl_key ?
opts.ssl_key :
"-"),
3407 (
opts.ssl_ca_cert ?
opts.ssl_ca_cert :
"-")));
3417 memset(¶ms, 0,
sizeof(params));
3418 params.cert =
opts.ssl_cert;
3419 params.key =
opts.ssl_key;
3420 params.ca_cert =
opts.ssl_ca_cert;
3421 params.cipher_suites =
opts.ssl_cipher_suites;
3437 DBG((
"Failed to open listener: %d",
rc));
3451#if MG_ENABLE_BROADCAST
3478 return n >= 0 &&
n <= 255;
3482 int n, a, b,
c,
d,
slash = 32, len = 0;
3507 if ((
flag !=
'+' &&
flag !=
'-') ||
3512 if (
net == (remote_ip &
mask)) {
3517 DBG((
"%08x %c", (
unsigned int) remote_ip,
allowed));
3528 double result =
c->ev_timer_time;
3529 c->ev_timer_time = timestamp;
3536 (
unsigned long) timestamp));
3558#if MG_ENABLE_CALLBACK_USERDATA
3581#ifdef MG_MODULE_LINES
3582#line 1 "mongoose/src/mg_net_if_socket.h"
3589#ifndef CS_MONGOOSE_SRC_NET_IF_SOCKET_H_
3590#define CS_MONGOOSE_SRC_NET_IF_SOCKET_H_
3598#ifndef MG_ENABLE_NET_IF_SOCKET
3599#define MG_ENABLE_NET_IF_SOCKET MG_NET_IF == MG_NET_IF_SOCKET
3609#ifdef MG_MODULE_LINES
3610#line 1 "mongoose/src/mg_net_if_socks.h"
3617#ifndef CS_MONGOOSE_SRC_NET_IF_SOCKS_H_
3618#define CS_MONGOOSE_SRC_NET_IF_SOCKS_H_
3634#ifdef MG_MODULE_LINES
3635#line 1 "mongoose/src/mg_net_if.c"
3690#ifdef MG_MODULE_LINES
3691#line 1 "mongoose/src/mg_net_if_null.c"
3819#define MG_NULL_IFACE_VTABLE \
3821 mg_null_if_init, mg_null_if_free, mg_null_if_add_conn, \
3822 mg_null_if_remove_conn, mg_null_if_poll, mg_null_if_listen_tcp, \
3823 mg_null_if_listen_udp, mg_null_if_connect_tcp, mg_null_if_connect_udp, \
3824 mg_null_if_tcp_send, mg_null_if_udp_send, mg_null_if_tcp_recv, \
3825 mg_null_if_udp_recv, mg_null_if_create_conn, mg_null_if_destroy_conn, \
3826 mg_null_if_sock_set, mg_null_if_get_conn_addr, \
3831#if MG_NET_IF == MG_NET_IF_NULL
3834#ifdef MG_MODULE_LINES
3835#line 1 "mongoose/src/mg_net_if_socket.c"
3842#if MG_ENABLE_NET_IF_SOCKET
3853 unsigned long on = 1;
3881#if !defined(MG_ESP8266)
4006 (
sa->sa.sa_family ==
AF_INET) ?
sizeof(
sa->sin) :
sizeof(
sa->sin6);
4020#if !defined(_WIN32) || !defined(SO_EXCLUSIVEADDRUSE)
4049#define _MG_F_FD_CAN_READ 1
4050#define _MG_F_FD_CAN_WRITE 1 << 1
4051#define _MG_F_FD_ERROR 1 << 2
4057 DBG((
"%p fd=%d fd_flags=%d nc_flags=0x%lx rmbl=%d smbl=%d", nc, nc->
sock,
4067#if !defined(MG_ESP8266)
4085 }
else if (nc->
err != 0) {
4110 DBG((
"%p after fd=%d nc_flags=0x%lx rmbl=%d smbl=%d", nc, nc->
sock,
4115#if MG_ENABLE_BROADCAST
4120 DBG((
"read %d from ctl socket", len));
4146#if MG_ENABLE_BROADCAST
4192#if MG_ENABLE_BROADCAST
4218 DBG((
"new sock is still larger than FD_SETSIZE, disregard"));
4269#if MG_ENABLE_BROADCAST
4301#if MG_ENABLE_BROADCAST
4332 sa.sin.sin_addr.s_addr =
htonl(0x7f000001);
4336 }
else if (
bind(sock, &
sa.sa, len) != 0) {
4344 connect(
sp[0], &
sa.sa, len) != 0 ||
4345 connect(sock, &
sa2.sa, len) != 0)) {
4347 sock, &
sa, len))) ==
4393#define MG_SOCKET_IFACE_VTABLE \
4395 mg_socket_if_init, \
4396 mg_socket_if_free, \
4397 mg_socket_if_add_conn, \
4398 mg_socket_if_remove_conn, \
4399 mg_socket_if_poll, \
4400 mg_socket_if_listen_tcp, \
4401 mg_socket_if_listen_udp, \
4402 mg_socket_if_connect_tcp, \
4403 mg_socket_if_connect_udp, \
4404 mg_socket_if_tcp_send, \
4405 mg_socket_if_udp_send, \
4406 mg_socket_if_tcp_recv, \
4407 mg_socket_if_udp_recv, \
4408 mg_socket_if_create_conn, \
4409 mg_socket_if_destroy_conn, \
4410 mg_socket_if_sock_set, \
4411 mg_socket_if_get_conn_addr, \
4416#if MG_NET_IF == MG_NET_IF_SOCKET
4421#ifdef MG_MODULE_LINES
4422#line 1 "mongoose/src/mg_net_if_socks.c"
4441 d->c->user_data =
NULL;
4446 d->s->user_data =
NULL;
4463 if (
d ==
NULL)
return;
4470 LOG(
LL_DEBUG, (
"Sent handshake to %s",
d->proxy_addr));
4483 if (
c->recv_mbuf.len < 2)
return;
4493 memcpy(buf + 4, &
d->c->sa.sin.sin_addr, 4);
4494 memcpy(buf + 8, &
d->c->sa.sin.sin_port, 2);
4501 if (
c->recv_mbuf.len < 10)
return;
4503 LOG(
LL_ERROR, (
"Socks connection error: %d",
c->recv_mbuf.buf[1]));
4523 d->s->user_data =
d;
4550 if (
d->s ==
NULL)
return -1;
4552 DBG((
"%p -> %d -> %p",
c,
res,
d->s));
4566 if (
d->s ==
NULL)
return -1;
4567 if (len >
d->s->recv_mbuf.len) len =
d->s->recv_mbuf.len;
4569 memcpy(buf,
d->s->recv_mbuf.buf, len);
4572 DBG((
"%p <- %d <- %p",
c, (
int) len,
d->s));
4592 c->iface->vtable->free(
c->iface);
4661#ifdef MG_MODULE_LINES
4662#line 1 "mongoose/src/mg_ssl_if_openssl.c"
4669#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_OPENSSL
4672#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4675#include <openssl/ssl.h>
4677#include <openssl/tls1.h>
4696 nc->ssl_if_data = ctx;
4698 ctx->ssl_ctx =
lc_ctx->ssl_ctx;
4718 DBG((
"%p %s,%s,%s", nc, (params->cert ? params->cert :
""),
4719 (params->
key ? params->
key :
""),
4725 nc->ssl_if_data = ctx;
4731 if (ctx->ssl_ctx ==
NULL) {
4741#ifdef MG_SSL_OPENSSL_NO_COMPRESSION
4744#ifdef MG_SSL_OPENSSL_CIPHER_SERVER_PREFERENCE
4751 if (params->cert !=
NULL &&
4757 if (params->ca_cert !=
NULL &&
4781 if (params->server_name !=
NULL) {
4800 DBG((
"%p %p SSL error: %d %d", nc, ctx->ssl_ctx,
res, err));
4820 int n =
SSL_read(ctx->ssl, buf, buf_size);
4821 DBG((
"%p %d -> %d", nc, (
int) buf_size,
n));
4830 DBG((
"%p %d -> %d", nc, (
int) len,
n));
4837 if (ctx ==
NULL)
return;
4843 if (ctx ==
NULL)
return;
4844 nc->ssl_if_data =
NULL;
4848 memset(ctx, 0,
sizeof(*ctx));
4857#if defined(MG_SSL_CRYPTO_MODERN)
4858 "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:"
4859 "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:"
4860 "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:"
4861 "ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:"
4862 "ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:"
4863 "ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:"
4864 "DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:"
4865 "DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:"
4866 "!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK"
4867#elif defined(MG_SSL_CRYPTO_OLD)
4868 "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:"
4869 "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:"
4870 "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:"
4871 "ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:"
4872 "ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:"
4873 "ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:"
4874 "DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:"
4875 "DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:"
4876 "ECDHE-ECDSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:"
4877 "AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:DES-CBC3-SHA:"
4878 "HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:"
4879 "!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA"
4881 "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:"
4882 "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:"
4883 "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:"
4884 "ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:"
4885 "ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:"
4886 "ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:"
4887 "DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:"
4888 "DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:"
4889 "AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:"
4890 "DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:"
4891 "!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:"
4904#if !MG_DISABLE_PFS && !defined(KR_VERSION)
4907-----BEGIN DH PARAMETERS-----\n\
4908MIIBCAKCAQEAlvbgD/qh9znWIlGFcV0zdltD7rq8FeShIqIhkQ0C7hYFThrBvF2E\n\
4909Z9bmgaP+sfQwGpVlv9mtaWjvERbu6mEG7JTkgmVUJrUt/wiRzwTaCXBqZkdUO8Tq\n\
4910+E6VOEQAilstG90ikN1Tfo+K6+X68XkRUIlgawBTKuvKVwBhuvlqTGerOtnXWnrt\n\
4911ym//hd3cd5PBYGBix0i7oR4xdghvfR2WLVu0LgdThTBb6XP7gLd19cQ1JuBtAajZ\n\
4912wMuPn7qlUkEFDIkAZy59/Hue/H2Q2vU/JsvVhHWCQBL4F1ofEAt50il6ZxR1QfFK\n\
49139VGKDC4oOgm9DlxwwBoC2FjqmvQlqVV3kwIBAg==\n\
4914-----END DH PARAMETERS-----\n";
4943#if !MG_DISABLE_PFS && !defined(KR_VERSION)
4967#if OPENSSL_VERSION_NUMBER > 0x10002000L
4981#if !defined(KR_VERSION) && !defined(LIBRESSL_VERSION_NUMBER)
4989 size_t key_len = ctx->psk.len - ctx->identity_len - 1;
4992 DBG((
"identity too long"));
4996 DBG((
"key too long"));
5008 unsigned char key[32];
5020 if (
hc >=
'0' &&
hc <=
'9') {
5022 }
else if (
hc >=
'a' &&
hc <=
'f') {
5056 memset(¶ms, 0,
sizeof(params));
5066#ifdef MG_MODULE_LINES
5067#line 1 "mongoose/src/mg_ssl_if_mbedtls.c"
5074#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_MBEDTLS
5076#include <mbedtls/debug.h>
5077#include <mbedtls/ecp.h>
5078#include <mbedtls/net.h>
5079#include <mbedtls/platform.h>
5080#include <mbedtls/ssl.h>
5081#include <mbedtls/ssl_internal.h>
5082#include <mbedtls/x509_crt.h>
5083#include <mbedtls/version.h>
5132 nc->ssl_if_data = ctx;
5142 const char *
cert,
const char *
key,
5148#ifdef MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
5159 DBG((
"%p %s,%s,%s", nc, (params->cert ? params->cert :
""),
5160 (params->
key ? params->
key :
""),
5167 nc->ssl_if_data = ctx;
5185 if (params->cert !=
NULL &&
5190 if (params->ca_cert !=
NULL &&
5201#ifdef MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
5216 if (params->server_name !=
NULL &&
5222#ifdef MG_SSL_IF_MBEDTLS_MAX_FRAG_LEN
5249 if (
n > 0)
return n;
5257 if (
n > 0)
return n;
5270 LOG(
LL_DEBUG, (
"%p TLS connection closed by peer", nc));
5298 LOG(
LL_ERROR, (
"%p mbedTLS error: -0x%04x", nc, -ret));
5317#ifdef MBEDTLS_X509_CA_CHAIN_ON_DISK
5318 if (
ctx->conf->ca_chain_file !=
NULL) {
5320 ctx->conf->ca_chain_file =
NULL;
5333 if (ctx->ssl->p_bio ==
NULL) {
5339#ifdef MG_SSL_IF_MBEDTLS_FREE_CERTS
5346 ctx->ssl->session->peer_cert =
NULL;
5349 if (ctx->conf->key_cert !=
NULL) {
5352 ctx->conf->key_cert =
NULL;
5364 DBG((
"%p %d -> %d", nc, (
int) len,
n));
5379 if (ctx->saved_len > 0 && ctx->saved_len < l) l = ctx->saved_len;
5381 DBG((
"%p %d,%d,%d -> %d", nc, (
int) len, (
int) ctx->saved_len, (
int) l,
n));
5384 ctx->saved_len = len;
5395 if (ctx ==
NULL)
return;
5401 if (ctx ==
NULL)
return;
5402 nc->ssl_if_data =
NULL;
5403 if (ctx->ssl !=
NULL) {
5408 if (ctx->conf !=
NULL) {
5413 memset(ctx, 0,
sizeof(*ctx));
5425#ifdef MBEDTLS_X509_CA_CHAIN_ON_DISK
5439 const char *
cert,
const char *
key,
5465#if CS_PLATFORM != CS_P_ESP8266
5522 DBG((
"%s -> %04x",
tmp,
id));
5524 mbuf_append(&ctx->cipher_suites, &
id,
sizeof(
id));
5530 mbuf_append(&ctx->cipher_suites, &
id,
sizeof(
id));
5533 (
const int *) ctx->cipher_suites.buf);
5540#ifdef MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
5544 unsigned char key[32];
5556 if (
hc >=
'0' &&
hc <=
'9') {
5558 }
else if (
hc >=
'a' &&
hc <=
'f') {
5583 memset(¶ms, 0,
sizeof(params));
5593#ifdef MG_SSL_MBED_DUMMY_RANDOM
5596 while (len--) *buf++ =
rand();
5602#ifdef MG_MODULE_LINES
5603#line 1 "mongoose/src/mg_uri.c"
5623 for (; *p <
end; (*p)++) {
5624 for (
q =
seps; *
q !=
'\0';
q++) {
5625 if (**p == *
q)
break;
5627 if (*
q !=
'\0')
break;
5629 res->len = (*p) -
res->p;
5630 if (*p <
end) (*p)++;
5635 unsigned int *port,
struct mg_str *path,
struct mg_str *query,
5639 unsigned int rport = 0;
5649 const char *
p = uri.
p, *
end =
p + uri.
len;
5664 for (;
p <
end;
p++) {
5668 }
else if (*
p ==
'/') {
5692 for (;
p <
end;
p++) {
5693 if (*
p ==
'@' || *
p ==
'[' || *
p ==
'/') {
5697 if (
p ==
end || *
p ==
'/' || *
p ==
'[') {
5712 if (!
found)
return -1;
5714 for (;
p <
end;
p++) {
5715 if (*
p ==
':' || *
p ==
'/')
break;
5723 }
else if (*
p ==
'/') {
5731 for (;
p <
end;
p++) {
5743 if (
p <
end && *(
p - 1) ==
'?') {
5753 if (host != 0) *host =
rhost;
5754 if (port != 0) *port =
rport;
5755 if (path != 0) *path =
rpath;
5756 if (query != 0) *query =
rquery;
5764 const char *s = in->
p, *
se = s + in->
len;
5765 char *
cp = (
char *)
out->p, *
d;
5767 if (in->
len == 0 || *s !=
'/') {
5775 const char *next = s;
5782 if (
d >
cp + 1 && *(
d - 1) ==
'/')
d--;
5783 while (
d >
cp && *(
d - 1) !=
'/')
d--;
5790 if (
d ==
cp) *
d++ =
'/';
5798 const struct mg_str *host,
unsigned int port,
5816 if (host !=
NULL && host->
len > 0) {
5826 if (path !=
NULL && path->
len > 0) {
5831 free((
void *)
npath.p);
5835 free((
void *)
npath.p);
5843 if (query !=
NULL && query->
len > 0) {
5866#ifdef MG_MODULE_LINES
5867#line 1 "mongoose/src/mg_http.c"
5899 ab->user_buf_size = buf_size;
5907 if (
ab->len <
ab->user_buf_size) {
5909 ab->user_buf[
ab->len++] =
c;
5918 if (
ab->len > 0 &&
ab->m.len == 0) {
5923 ab->len =
ab->m.len;
5940 return ab->len >
ab->user_buf_size;
5954 return ab->user_buf;
5972#if MG_ENABLE_HTTP_CGI
5989#if MG_ENABLE_CALLBACK_USERDATA
6029#if MG_ENABLE_FILESYSTEM
6032#if MG_ENABLE_HTTP_CGI
6035#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6038#if MG_ENABLE_HTTP_WEBSOCKET
6059 if (
c->proto_data !=
NULL) {
6060 void *
pd =
c->proto_data;
6061 c->proto_data =
NULL;
6074#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6084#if MG_ENABLE_FILESYSTEM
6087 if (
d->fp !=
NULL) {
6111 if (
rpd->linked_conn !=
NULL) {
6118 if (
pd->reverse_proxy_data.linked_conn !=
NULL) {
6120 pd->reverse_proxy_data.linked_conn =
NULL;
6128#if MG_ENABLE_FILESYSTEM
6131#if MG_ENABLE_HTTP_CGI
6134#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6142#if MG_ENABLE_FILESYSTEM
6144#define MIME_ENTRY(_ext, _type) \
6145 { _ext, sizeof(_ext) - 1, _type }
6146static const struct {
6157 MIME_ENTRY(
"js",
"application/x-javascript"),
6165 MIME_ENTRY(
"torrent",
"application/x-bittorrent"),
6179 MIME_ENTRY(
"exe",
"application/octet-stream"),
6180 MIME_ENTRY(
"zip",
"application/x-zip-compressed"),
6185 MIME_ENTRY(
"arj",
"application/x-arj-compressed"),
6186 MIME_ENTRY(
"rar",
"application/x-rar-compressed"),
6189 MIME_ENTRY(
"swf",
"application/x-shockwave-flash"),
6240 const unsigned char *buf = (
unsigned char *) s;
6244 if (!
isprint(buf[
i]) && buf[
i] !=
'\r' && buf[
i] !=
'\n' && buf[
i] < 128) {
6246 }
else if (buf[
i] ==
'\n' &&
i + 1 <
buf_len && buf[
i + 1] ==
'\n') {
6248 }
else if (buf[
i] ==
'\n' &&
i + 2 <
buf_len && buf[
i + 1] ==
'\r' &&
6249 buf[
i + 2] ==
'\n') {
6266 while (v->len > 0 && v->p[v->len - 1] ==
' ') {
6275 if (
k->len != 0 && v->len == 0) {
6279 if (
k->len == 0 || v->len == 0) {
6281 k->len = v->len = 0;
6297 const char *
end, *
qs;
6300 if (
len <= 0)
return len;
6304 hm->body.p = s +
len;
6305 hm->message.len =
hm->body.len = (
size_t) ~0;
6309 while (s <
end &&
isspace(*(
unsigned char *) s)) s++;
6316 if (
hm->uri.p <=
hm->method.p ||
hm->proto.p <=
hm->uri.p)
return -1;
6320 hm->query_string.p =
qs + 1;
6321 hm->query_string.len = &
hm->uri.p[
hm->uri.len] - (
qs + 1);
6322 hm->uri.len =
qs -
hm->uri.p;
6326 if (
end - s < 4 || s[0] <
'0' || s[0] >
'9' || s[3] !=
' ')
return -1;
6328 if (
hm->resp_code < 100 ||
hm->resp_code >= 600)
return -1;
6350 if (
hm->body.len == (
size_t) ~0 &&
is_req &&
6354 hm->message.len =
len;
6363 for (
i = 0;
hm->header_names[
i].len > 0;
i++) {
6364 struct mg_str *h = &
hm->header_names[
i], *v = &
hm->header_values[
i];
6372#if MG_ENABLE_FILESYSTEM
6393 DBG((
"%p sent %d (total %d)", nc, (
int)
n, (
int)
pd->file.sent));
6398 if (
pd->file.sent >=
pd->file.cl) {
6399 LOG(
LL_DEBUG, (
"%p done, %d bytes, ka %d", nc, (
int)
pd->file.sent,
6400 pd->file.keepalive));
6412 if (
n == 0 ||
pd->file.sent >=
pd->file.cl) {
6417#if MG_ENABLE_HTTP_CGI
6418 else if (
pd->cgi.cgi_nc !=
NULL) {
6420 if (
pd->cgi.cgi_nc !=
NULL) {
6437 unsigned char *s = (
unsigned char *)
buf;
6444 n += (s[
i] >=
'0' && s[
i] <=
'9') ? s[
i] -
'0' :
tolower(s[
i]) -
'a' + 10;
6453 if (
i == 0 ||
i + 2 >
len || s[
i] !=
'\r' || s[
i + 1] !=
'\n') {
6466 if (
i == 0 ||
i + 2 >
len || s[
i] !=
'\r' || s[
i + 1] !=
'\n') {
6479 body_len = (
size_t)
pd->chunk.body_len;
6480 assert(
blen >= body_len);
6488 body_len += data_len;
6489 hm->body.len = body_len;
6491 if (data_len == 0) {
6504 pd->chunk.body_len = body_len;
6512 memset(buf, 0, body_len);
6516 pd->chunk.body_len = 0;
6543 while (ep !=
NULL) {
6558#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6607#if MG_ENABLE_HTTP_WEBSOCKET
6611#if MG_ENABLE_HTTP_CGI
6614 pd->cgi.cgi_nc->user_data =
NULL;
6618#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6627 mp.user_data =
pd->mp_stream.user_data;
6628 mp.var_name =
pd->mp_stream.var_name;
6629 mp.file_name =
pd->mp_stream.file_name;
6630 mg_call(nc, (
pd->endpoint_handler ?
pd->endpoint_handler : nc->handler),
6634 mg_call(nc, (
pd->endpoint_handler ?
pd->endpoint_handler : nc->handler),
6645 hm->message.len =
io->len;
6646 hm->body.len =
io->buf +
io->len -
hm->body.p;
6656#if MG_ENABLE_FILESYSTEM
6664#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6670 }
else if (
pd->mp_stream.data_avail) {
6698#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6700 s->
len >= 9 &&
strncmp(s->
p,
"multipart", 9) == 0) {
6710 DBG((
"invalid request"));
6715#if MG_ENABLE_HTTP_WEBSOCKET
6718 DBG((
"%p WebSocket upgrade code %d", nc,
hm->resp_code));
6719 if (
hm->resp_code == 101 &&
6750#if MG_ENABLE_CALLBACK_USERDATA
6768 else if (
hm->message.len >
pd->rcvd) {
6772 LOG(
LL_ERROR, (
"%p recv buffer (%lu bytes) exceeds the limit "
6773 "%lu bytes, and not drained, closing",
6785 DBG((
"%p %s %.*s %.*s", nc,
addr, (
int)
hm->method.len,
hm->method.p,
6786 (
int)
hm->uri.len,
hm->uri.p));
6791 pd->rcvd -=
hm->message.len;
6792#if MG_ENABLE_FILESYSTEM
6799#if MG_ENABLE_HTTP_CGI
6810 while (len <
buf_len && buf[len] !=
'\n') len++;
6811 return len ==
buf_len ? 0 : len + 1;
6814#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6832 if (
ct->len < 9 ||
strncmp(
ct->p,
"multipart", 9) != 0) {
6838 if (boundary_len == 0) {
6844 DBG((
"invalid request"));
6850 if (
pd->mp_stream.boundary !=
NULL) {
6859 pd->mp_stream.boundary =
strdup(boundary);
6860 pd->mp_stream.boundary_len =
strlen(boundary);
6861 pd->mp_stream.var_name =
pd->mp_stream.file_name =
NULL;
6877#define CONTENT_DISPOSITION "Content-Disposition: "
6886 mp.var_name =
pd->mp_stream.var_name;
6887 mp.file_name =
pd->mp_stream.file_name;
6888 mp.user_data =
pd->mp_stream.user_data;
6890 mp.data.len = data_len;
6891 mp.num_data_consumed = data_len;
6893 pd->mp_stream.user_data =
mp.user_data;
6894 pd->mp_stream.data_avail = (
mp.num_data_consumed != data_len);
6895 return mp.num_data_consumed;
6902 MG_FREE((
void *)
pd->mp_stream.file_name);
6903 pd->mp_stream.file_name =
NULL;
6904 MG_FREE((
void *)
pd->mp_stream.var_name);
6905 pd->mp_stream.var_name =
NULL;
6914 const char *boundary;
6915 struct mbuf *
io = &
c->recv_mbuf;
6918 if (
pd->mp_stream.boundary ==
NULL) {
6920 DBG((
"Invalid request: boundary not initialized"));
6924 if ((
int)
io->len <
pd->mp_stream.boundary_len + 2) {
6929 if (boundary !=
NULL) {
6930 const char *
boundary_end = (boundary +
pd->mp_stream.boundary_len);
6954 struct mbuf *
io = &
c->recv_mbuf;
6965 while (data_size > 0 &&
6990 if (
pd->mp_stream.processing_part != 0) {
7000 MG_FREE((
void *)
pd->mp_stream.file_name);
7002 MG_FREE((
void *)
pd->mp_stream.var_name);
7007 pd->mp_stream.processing_part++;
7024 struct mbuf *
io = &
c->recv_mbuf;
7026 const char *boundary;
7027 if ((
int)
io->len <
pd->mp_stream.boundary_len + 6 ) {
7032 if (boundary ==
NULL) {
7033 int data_len = (
io->len - (
pd->mp_stream.boundary_len + 6));
7040 }
else if (boundary !=
NULL) {
7041 size_t data_len = ((
size_t)(boundary -
io->buf) - 4);
7060 switch (
pd->mp_stream.state) {
7109 switch (status_code) {
7111 return "Partial Content";
7117 return "Bad Request";
7119 return "Unauthorized";
7125 return "Requested Range Not Satisfiable";
7127 return "I'm a teapot";
7129 return "Internal Server Error";
7131 return "Bad Gateway";
7133 return "Service Unavailable";
7135#if MG_ENABLE_EXTRA_ERRORS_DESC
7139 return "Switching Protocols";
7141 return "Processing";
7149 return "Non-Authoritative Information";
7151 return "No Content";
7153 return "Reset Content";
7155 return "Multi-Status";
7157 return "Already Reported";
7161 return "Multiple Choices";
7165 return "Not Modified";
7169 return "Switch Proxy";
7171 return "Temporary Redirect";
7173 return "Permanent Redirect";
7175 return "Payment Required";
7177 return "Method Not Allowed";
7179 return "Not Acceptable";
7181 return "Proxy Authentication Required";
7183 return "Request Timeout";
7189 return "Length Required";
7191 return "Precondition Failed";
7193 return "Payload Too Large";
7195 return "URI Too Long";
7197 return "Unsupported Media Type";
7199 return "Expectation Failed";
7201 return "Unprocessable Entity";
7205 return "Failed Dependency";
7207 return "Upgrade Required";
7209 return "Precondition Required";
7211 return "Too Many Requests";
7213 return "Request Header Fields Too Large";
7215 return "Unavailable For Legal Reasons";
7217 return "Not Implemented";
7219 return "Gateway Timeout";
7221 return "HTTP Version Not Supported";
7223 return "Variant Also Negotiates";
7225 return "Insufficient Storage";
7227 return "Loop Detected";
7229 return "Not Extended";
7231 return "Network Authentication Required";
7240 const struct mg_str extra_headers) {
7241 mg_printf(nc,
"HTTP/1.1 %d %s\r\n", status_code,
7243#ifndef MG_HIDE_SERVER_INFO
7246 if (extra_headers.
len > 0) {
7247 mg_printf(nc,
"%.*s\r\n", (
int) extra_headers.
len, extra_headers.
p);
7252 const char *extra_headers) {
7258 const struct mg_str extra_headers) {
7261 "<p>Moved <a href='%.*s'>here</a>.\r\n",
7265 "Location: %.*s\r\n"
7266 "Content-Type: text/html\r\n"
7267 "Content-Length: %d\r\n"
7268 "Cache-Control: no-cache\r\n"
7271 extra_headers.
p, (extra_headers.
len > 0 ?
"\r\n" :
""));
7282 mg_printf(
c,
"%s",
"Transfer-Encoding: chunked\r\n");
7294 "Content-Type: text/plain\r\nConnection: close");
7299#if MG_ENABLE_FILESYSTEM
7323 if (p ==
NULL)
return 0;
7325 p[header->
len] =
'\0';
7333 const struct mg_str extra_headers) {
7355 int n, status_code = 200;
7366 if (
r1 >
r2 ||
r2 >= cl) {
7370 "Content-Range: bytes */%" INT64_FMT "\r\n",
7378#if _FILE_OFFSET_BITS == 64 || _POSIX_C_SOURCE >= 200112L || \
7379 _XOPEN_SOURCE >= 600
7387#if !MG_DISABLE_HTTP_KEEP_ALIVE
7393 pd->file.keepalive = (
mg_vcmp(&
hm->proto,
"HTTP/1.1") == 0);
7411 "Last-Modified: %s\r\n"
7412 "Accept-Ranges: bytes\r\n"
7413 "Content-Type: %.*s\r\n"
7414 "Connection: %s\r\n"
7417 "%sEtag: %s\r\n\r\n",
7419 (
pd->file.keepalive ?
"keep-alive" :
"close"), (
size_t) cl, range,
7431#if MG_ENABLE_HTTP_SSI
7446#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
7449 if (src[
i] ==
'%') {
7451 isxdigit(*(
const unsigned char *) (src +
i + 2))) {
7452 a =
tolower(*(
const unsigned char *) (src +
i + 1));
7453 b =
tolower(*(
const unsigned char *) (src +
i + 2));
7473 const char *
p, *
e, *s;
7492 e = buf->
p + buf->
len;
7496 for (
p = buf->
p;
p + name_len <
e;
p++) {
7497 if ((
p == buf->
p ||
p[-1] ==
'&') &&
p[name_len] ==
'=' &&
7500 s = (
const char *)
memchr(
p,
'&', (
size_t)(
e -
p));
7518 char chunk_size[50];
7521 n =
snprintf(chunk_size,
sizeof(chunk_size),
"%lX\r\n", (
unsigned long)
len);
7541 if (buf !=
mem && buf !=
NULL) {
7557 for (
i =
j = 0;
i <
len;
i++) {
7558 if (buf[
i] ==
'<' || buf[
i] ==
'>') {
7560 mg_send(nc, buf[
i] ==
'<' ?
"<" :
">", 4);
7568 if (buf !=
mem && buf !=
NULL) {
7582 if ((s ==
hdr->p || s[-1] ==
ch || s[-1] ==
ch1 || s[-1] ==
';') &&
7587 if (s !=
NULL && &s[
n + 1] <
end) {
7589 if (*s ==
'"' || *s ==
'\'') {
7594 if (
ch !=
' ' &&
p[0] ==
'\\' &&
p[1] ==
ch)
p++;
7598 if (
ch !=
' ' && *
p !=
ch) {
7614 if (*buf !=
NULL && buf_size > 0) *buf[0] =
'\0';
7623 return ab.len > 0 ?
ab.len - 1 : 0;
7670#if MG_ENABLE_FILESYSTEM
7674 const char *
p1 =
opts->per_directory_auth_file;
7675 const char *
p2 =
opts->hidden_file_pattern;
7688#if !MG_DISABLE_HTTP_DIGEST_AUTH
7706void cs_md5(
char buf[33], ...) {
7707 unsigned char hash[16];
7727 const char *nonce,
size_t nonce_len,
const char *nc,
7730 static const char colon[] =
":";
7731 static const size_t one = 1;
7734 cs_md5(
resp,
ha1,
ha1_len,
colon,
one, nonce,
nonce_len,
colon,
one, nc,
7740 const char *method,
const char *uri,
7741 const char *auth_domain,
const char *user,
7742 const char *
passwd,
const char *nonce) {
7743 static const char colon[] =
":", qop[] =
"auth";
7744 static const size_t one = 1;
7745 char ha1[33],
resp[33], cnonce[40];
7753 sizeof(qop) - 1,
resp);
7755 "Authorization: Digest username=\"%s\","
7756 "realm=\"%s\",uri=\"%s\",qop=%s,nc=1,cnonce=%s,"
7757 "nonce=%s,response=%s\r\n",
7758 user, auth_domain, uri, qop, cnonce, nonce,
resp);
7806 hm->uri.len + (
hm->query_string.len ?
hm->query_string.len + 1 : 0)),
7842 nonce.
p, nonce.
len, nc.
p, nc.
len, cnonce.
p, cnonce.
len,
7902#if MG_ENABLE_DIRECTORY_LISTING
7905 while (*src !=
'\0' &&
n + 5 <
dst_len) {
7906 unsigned char ch = *(
unsigned char *) src++;
7925 snprintf(size,
sizeof(size),
"%s",
"[DIRECTORY]");
7933 }
else if (
fsize < 0x100000) {
7934 snprintf(size,
sizeof(size),
"%.1fk", (
double)
fsize / 1024.0);
7935 }
else if (
fsize < 0x40000000) {
7936 snprintf(size,
sizeof(size),
"%.1fM", (
double)
fsize / 1048576);
7938 snprintf(size,
sizeof(size),
"%.1fG", (
double)
fsize / 1073741824);
7945 "<tr><td><a href=\"%s%s\">%s%s</a></td>"
7946 "<td>%s</td><td name=%" INT64_FMT ">%s</td></tr>\n",
7949 free((
void *)
href.p);
7968 snprintf(path,
sizeof(path),
"%s/%s", dir,
dp->d_name);
7970 func(nc, (
const char *)
dp->d_name, &
st);
7983 "<script>function srt(tb, sc, so, d) {"
7984 "var tr = Array.prototype.slice.call(tb.rows, 0),"
7985 "tr = tr.sort(function (a, b) { var c1 = a.cells[sc], c2 = b.cells[sc],"
7986 "n1 = c1.getAttribute('name'), n2 = c2.getAttribute('name'), "
7987 "t1 = a.cells[2].getAttribute('name'), "
7988 "t2 = b.cells[2].getAttribute('name'); "
7989 "return so * (t1 < 0 && t2 >= 0 ? -1 : t2 < 0 && t1 >= 0 ? 1 : "
7990 "n1 ? parseInt(n2) - parseInt(n1) : "
7991 "c1.textContent.trim().localeCompare(c2.textContent.trim())); });";
7993 "for (var i = 0; i < tr.length; i++) tb.appendChild(tr[i]); "
7994 "if (!d) window.location.hash = ('sc=' + sc + '&so=' + so); "
7996 "window.onload = function() {"
7997 "var tb = document.getElementById('tb');"
7998 "var m = /sc=([012]).so=(1|-1)/.exec(window.location.hash) || [0, 2, 1];"
7999 "var sc = m[1], so = m[2]; document.onclick = function(ev) { "
8000 "var c = ev.target.rel; if (c) {if (c == sc) so *= -1; srt(tb, c, so); "
8001 "sc = c; ev.preventDefault();}};"
8002 "srt(tb, sc, so, true);"
8007 mg_printf(nc,
"%s: %s\r\n%s: %s\r\n\r\n",
"Transfer-Encoding",
"chunked",
8008 "Content-Type",
"text/html; charset=utf-8");
8012 "<html><head><title>Index of %.*s</title>%s%s"
8013 "<style>th,td {text-align: left; padding-right: 1em; "
8014 "font-family: monospace; }</style></head>\n"
8015 "<body><h1>Index of %.*s</h1>\n<table cellpadding=0><thead>"
8016 "<tr><th><a href=# rel=0>Name</a></th><th>"
8017 "<a href=# rel=1>Modified</a</th>"
8018 "<th><a href=# rel=2>Size</a></th></tr>"
8019 "<tr><td colspan=3><hr></td></tr>\n"
8023 (
int)
hm->uri.len,
hm->uri.p);
8026 "</tbody><tr><td colspan=3><hr></td></tr>\n"
8028 "<address>%s</address>\n"
8075#if MG_ENABLE_HTTP_URL_REWRITES
8089 mg_printf(
c,
"Content-Length: 0\r\nLocation: %.*s%.*s\r\n\r\n",
8090 (
int) b.len, b.p, (
int) (
hm->proto.p -
hm->uri.p - 1),
8103 if (pos == std::string::npos) {
8107 return msg.length() - pos - 4;
8112 const char*
CRLF =
"\r\n";
8113 const char*
CL =
"Content-Length: ";
8114 size_t pos = msg.find(
CL);
8115 if (pos == std::string::npos) {
8120 if (
pos2 == std::string::npos) {
8124 sprintf(buf,
"%llu", (
long long unsigned)
len);
8125 msg.replace(pos,
pos2-pos, buf);
8136 if (pos == std::string::npos) {
8150 if (
pd ==
NULL ||
pd->reverse_proxy_data.linked_conn ==
NULL) {
8151 DBG((
"%p: upstream closed", nc));
8178 xstr.
p =
"Transfer-Encoding: chunked";
8207 mg_send(
pd->reverse_proxy_data.linked_conn,
hm->message.p,
hm->message.len);
8210 mg_send(
pd->reverse_proxy_data.linked_conn,
hm->message.p,
hm->message.len);
8220 mg_send(
pd->reverse_proxy_data.linked_conn,
io->buf,
io->len);
8227#if MG_ENABLE_CALLBACK_USERDATA
8242 opts.error_string = &error;
8245 (
int) (
hm->uri.len -
mount.len),
hm->uri.p +
mount.len, (
hm->query_string.len > 0 ?
"?" :
""), (
int)
hm->query_string.
len,
hm->query_string.
p);
8252 LOG(
LL_DEBUG, (
"Proxying %.*s to %s (rule: %.*s)", (
int)
hm->uri.len,
8268 mg_printf(
be,
"%.*s %.*s HTTP/1.1\r\n", (
int)
hm->method.len,
hm->method.p,
8269 (
int) path.
len, path.
p);
8271 mg_printf(
be,
"Host: %.*s\r\n", (
int) host.len, host.p);
8338#if MG_ENABLE_HTTP_URL_REWRITES
8347 if (a.len > 1 && a.p[0] ==
'@') {
8349 if (
hh !=
NULL &&
hh->len == a.len - 1 &&
8375#if MG_ENABLE_HTTP_WEBDAV
8407 const char *next =
u;
8415 if (*(
u - 1) ==
'/')
u--;
8477 (
"'%.*s' -> '%s' + '%.*s'", (
int)
hm->uri.len,
hm->uri.p,
8484 static const char *
month_names[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
8485 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"};
8501 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
8540 "HTTP/1.1 401 Unauthorized\r\n"
8541 "WWW-Authenticate: Digest qop=\"auth\", "
8542 "realm=\"%s\", nonce=\"%lx\"\r\n"
8543 "Content-Length: 0\r\n\r\n",
8551 "Allow: GET, POST, HEAD, CONNECT, OPTIONS"
8553 ", MKCOL, PUT, DELETE, PROPFIND, MOVE\r\nDAV: 1,2"
8564 const struct mg_str *path_info,
8567 int exists, is_directory,
is_cgi;
8568#if MG_ENABLE_HTTP_WEBDAV
8577 is_directory = exists &&
S_ISDIR(
st.st_mode);
8587 (
"%p %.*s [%s] exists=%d is_dir=%d is_dav=%d is_cgi=%d index=%s", nc,
8588 (
int)
hm->method.len,
hm->method.p, path, exists, is_directory,
is_dav,
8591 if (is_directory &&
hm->uri.p[
hm->uri.len - 1] !=
'/' && !
is_dav) {
8593 "HTTP/1.1 301 Moved\r\nLocation: %.*s/\r\n"
8594 "Content-Length: 0\r\n\r\n",
8595 (
int)
hm->uri.len,
hm->uri.p);
8616 opts->per_directory_auth_file,
8621#if MG_ENABLE_HTTP_CGI
8626 }
else if ((!exists ||
8630#if MG_ENABLE_HTTP_WEBDAV
8631 }
else if (!
mg_vcmp(&
hm->method,
"PROPFIND")) {
8633#if !MG_DISABLE_DAV_AUTH
8644 }
else if (!
mg_vcmp(&
hm->method,
"MKCOL")) {
8646 }
else if (!
mg_vcmp(&
hm->method,
"DELETE")) {
8648 }
else if (!
mg_vcmp(&
hm->method,
"PUT")) {
8650 }
else if (!
mg_vcmp(&
hm->method,
"MOVE")) {
8652#if MG_ENABLE_FAKE_DAVLOCK
8653 }
else if (!
mg_vcmp(&
hm->method,
"LOCK")) {
8657 }
else if (!
mg_vcmp(&
hm->method,
"OPTIONS")) {
8660#if MG_ENABLE_DIRECTORY_LISTING
8661 if (
strcmp(
opts->enable_directory_listing,
"yes") == 0) {
8691#if MG_ENABLE_HTTP_URL_REWRITES
8702 opts.document_root =
".";
8704 if (
opts.per_directory_auth_file ==
NULL) {
8705 opts.per_directory_auth_file =
".htpasswd";
8707 if (
opts.enable_directory_listing ==
NULL) {
8708 opts.enable_directory_listing =
"yes";
8710 if (
opts.cgi_file_pattern ==
NULL) {
8711 opts.cgi_file_pattern =
"**.cgi$|**.php$";
8714 opts.ssi_pattern =
"**.shtml$|**.shtm$";
8717 opts.index_files =
"index.html,index.htm,index.shtml,index.cgi,index.php";
8734 if (
mg_vcmp(&
hm->proto,
"HTTP/1.1") != 0 ||
8744#if MG_ENABLE_HTTP_STREAMING_MULTIPART
8756 LOG(
LL_ERROR, (
"%p Not allowed to upload %s", nc,
mp->file_name));
8758 "HTTP/1.1 403 Not Allowed\r\n"
8759 "Content-Type: text/plain\r\n"
8760 "Connection: close\r\n\r\n"
8761 "Not allowed to upload %s\r\n",
8773 fus->lfn[
lfn.len] =
'\0';
8776 (
"%p Receiving file %s -> %s", nc,
mp->file_name,
fus->lfn));
8780 "HTTP/1.1 500 Internal Server Error\r\n"
8781 "Content-Type: text/plain\r\n"
8782 "Connection: close\r\n\r\n");
8789 mp->user_data = (
void *)
fus;
8807 "HTTP/1.1 413 Payload Too Large\r\n"
8808 "Content-Type: text/plain\r\n"
8809 "Connection: close\r\n\r\n");
8810 mg_printf(nc,
"Failed to write to %s: no space left; wrote %d\r\n",
8811 fus->lfn, (
int)
fus->num_recd);
8814 "HTTP/1.1 500 Internal Server Error\r\n"
8815 "Content-Type: text/plain\r\n"
8816 "Connection: close\r\n\r\n");
8817 mg_printf(nc,
"Failed to write to %s: %d, wrote %d",
mp->file_name,
8828 fus->num_recd +=
mp->data.len;
8829 LOG(
LL_DEBUG, (
"%p rec'd %d bytes, %d total", nc, (
int)
mp->data.len,
8830 (
int)
fus->num_recd));
8839 if (
mp->status >= 0 &&
fus->fp !=
NULL) {
8840 LOG(
LL_DEBUG, (
"%p Uploaded %s (%s), %d bytes", nc,
mp->file_name,
8841 fus->lfn, (
int)
fus->num_recd));
8858 "HTTP/1.1 200 OK\r\n"
8859 "Content-Type: text/plain\r\n"
8860 "Connection: close\r\n\r\n"
8867#if MG_ENABLE_CALLBACK_USERDATA
8894 if (query.len > 0) path->
len += query.len + 1;
8921 opts.ssl_ca_cert =
"*";
8960 if (extra_headers ==
NULL) extra_headers =
"";
8961 if (path.len == 0) path =
mg_mk_str(
"/");
8966 (
post_data[0] ==
'\0' ?
"GET" :
"POST"), (
int) path.len, path.p,
8976 const char *
url,
const char *extra_headers,
const char *
post_data) {
8987 static const char cd[] =
"Content-Disposition: ";
8988 size_t hl,
bl,
n, ll, pos,
cdl =
sizeof(cd) - 1;
8994 if (buf[0] !=
'-' || buf[1] !=
'-' || buf[2] ==
'\n')
return 0;
9004 header.
p = buf +
n +
cdl;
9005 header.
len = ll - (
cdl + 2);
9029 if (buf[pos] ==
'-' && !
strncmp(buf, &buf[pos],
bl - 2)) {
9030 if (data_len !=
NULL) *data_len = (pos - 2) -
hl;
9046 if (nc ==
NULL)
return;
9058#if MG_ENABLE_CALLBACK_USERDATA
9078#if MG_ENABLE_FILESYSTEM && !MG_DISABLE_HTTP_DIGEST_AUTH
9086#if MG_ENABLE_CALLBACK_USERDATA
9087 user_data = ep->user_data;
9100#if MG_ENABLE_CALLBACK_USERDATA
9107#ifdef MG_MODULE_LINES
9108#line 1 "mongoose/src/mg_http_cgi.c"
9119#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_CGI
9121#ifndef MG_MAX_CGI_ENVIR_VARS
9122#define MG_MAX_CGI_ENVIR_VARS 64
9125#ifndef MG_ENV_EXPORT_TO_CGI
9126#define MG_ENV_EXPORT_TO_CGI "MONGOOSE_CGI"
9129#define MG_F_HTTP_CGI_PARSE_HEADERS MG_F_USER_1
9164 int n, sent,
stop = 0;
9169 (
n =
recv(
tp->s, buf,
sizeof(buf), 0)) > 0) {
9171 for (sent = 0; !
stop && sent <
n; sent +=
k) {
9175 DBG((
"%s",
"FORWARED EVERYTHING TO CGI"));
9183 int k = 0,
stop = 0;
9188 for (sent = 0; !
stop && sent <
n; sent +=
k) {
9190 (
k =
send(
tp->s, buf + sent,
n - sent, 0)) <= 0)
9194 DBG((
"%s",
"EOF FROM CGI"));
9203 void *(*func)(
void *)) {
9220 const char *
env,
const char *
envp[],
9221 const char *dir,
sock_t sock) {
9245 buf[0] = buf[1] =
'\0';
9247 buf[
sizeof(buf) - 1] =
'\0';
9248 if (buf[0] ==
'#' && buf[1] ==
'!') {
9258 snprintf(buf,
sizeof(buf),
"%s/%s", dir, cmd);
9287 DBG((
"CGI command: [%ls] -> %p",
wcmd,
pi.hProcess));
9291 return (
pi.hProcess !=
NULL);
9295 const char *
env,
const char *
envp[],
9296 const char *dir,
sock_t sock) {
9326 "Status: 500\r\n\r\n"
9327 "500 Server Error: %s%s%s: %s",
9375 const struct mg_str *path_info,
9386 blk->len =
blk->nvars = 0;
9407 hm->query_string.len == 0 ?
"" :
"?", (
int)
hm->query_string.
len,
9408 hm->query_string.
p);
9426 if (path_info !=
NULL && path_info->
len > 0) {
9429 mg_addenv(
blk,
"PATH_TRANSLATED=%.*s", (
int) path_info->
len, path_info->
p);
9443 if (
hm->query_string.len > 0) {
9445 hm->query_string.p);
9472 for (
i = 0;
hm->header_names[
i].len > 0;
i++) {
9474 hm->header_names[
i].p, (
int)
hm->header_values[
i].len,
9475 hm->header_values[
i].p);
9478 for (; *
p !=
'=' && *
p !=
'\0';
p++) {
9479 if (*
p ==
'-') *
p =
'_';
9485 blk->buf[
blk->len++] =
'\0';
9490#if !MG_ENABLE_CALLBACK_USERDATA
9521 if (
len == 0)
break;
9530 mg_printf(nc,
"%s",
"HTTP/1.1 302 Moved\r\n");
9534 mg_printf(nc,
"%s",
"HTTP/1.1 200 OK\r\n");
9544 DBG((
"%p CLOSE", cgi_nc));
9552 const struct mg_str *path_info,
9568 snprintf(dir,
sizeof(dir),
"%s",
".");
9593 cgi_pd->cgi.cgi_nc = cgi_nc;
9594#if !MG_ENABLE_CALLBACK_USERDATA
9599 if (
hm->body.len > 0) {
9614 if (
d ==
NULL)
return;
9615 if (
d->cgi_nc !=
NULL) {
9617 d->cgi_nc->user_data =
NULL;
9623#ifdef MG_MODULE_LINES
9624#line 1 "mongoose/src/mg_http_ssi.c"
9631#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_SSI && MG_ENABLE_FILESYSTEM
9640 while ((
n =
mg_fread(buf, 1,
sizeof(buf),
fp)) > 0) {
9674 mg_printf(nc,
"Bad SSI #include: [%s]", tag);
9679 mg_printf(nc,
"SSI include error: mg_fopen(%s): %s", path,
9694#if MG_ENABLE_HTTP_SSI_EXEC
9699 if (
sscanf(tag,
" \"%[^\"]\"", cmd) != 1) {
9700 mg_printf(nc,
"Bad SSI #exec: [%s]", tag);
9721#if MG_ENABLE_HTTP_SSI_EXEC
9728 mg_printf(nc,
"SSI #include level is too deep (%s)", path);
9740 while (
i > 0 && buf[
i] ==
' ') {
9755 (
void *)
cctx.arg.p);
9757#if MG_ENABLE_HTTP_SSI_EXEC
9765 }
else if (
ch ==
'<') {
9768 mg_send(nc, buf, (
size_t) len);
9771 buf[len++] =
ch & 0xff;
9776 }
else if (len == (
int)
sizeof(buf) - 2) {
9777 mg_printf(nc,
"%s: SSI tag is too large", path);
9780 buf[len++] =
ch & 0xff;
9782 buf[len++] =
ch & 0xff;
9783 if (len == (
int)
sizeof(buf)) {
9784 mg_send(nc, buf, (
size_t) len);
9792 mg_send(nc, buf, (
size_t) len);
9802 DBG((
"%p %s", nc, path));
9812 "Content-Type: %.*s\r\n"
9813 "Connection: close\r\n\r\n",
9823#ifdef MG_MODULE_LINES
9824#line 1 "mongoose/src/mg_http_webdav.c"
9831#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_WEBDAV
9834 static const char *
methods[] = {
9840#if MG_ENABLE_FAKE_DAVLOCK
9874 "<d:href>%s</d:href>"
9877 "<d:resourcetype>%s</d:resourcetype>"
9879 "</d:getcontentlength>"
9880 "<d:getlastmodified>%s</d:getlastmodified>"
9882 "<d:status>HTTP/1.1 200 OK</d:status>"
9893 static const char header[] =
9894 "HTTP/1.1 207 Multi-Status\r\n"
9895 "Connection: close\r\n"
9896 "Content-Type: text/xml; charset=utf-8\r\n\r\n"
9897 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
9898 "<d:multistatus xmlns:d='DAV:'>\n";
9899 static const char footer[] =
"</d:multistatus>\n";
9904 strcmp(
opts->enable_directory_listing,
"yes") != 0) {
9905 mg_printf(nc,
"%s",
"HTTP/1.1 403 Directory Listing Denied\r\n\r\n");
9908 mg_send(nc, header,
sizeof(header) - 1);
9909 snprintf(uri,
sizeof(uri),
"%.*s", (
int)
hm->uri.len,
hm->uri.p);
9919#if MG_ENABLE_FAKE_DAVLOCK
9932 static const char *
reply =
9933 "HTTP/1.1 207 Multi-Status\r\n"
9934 "Connection: close\r\n"
9935 "Content-Type: text/xml; charset=utf-8\r\n\r\n"
9936 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
9937 "<d:multistatus xmlns:d='DAV:'>\n"
9938 "<D:lockdiscovery>\n"
9942 "opaquelocktoken:%s%u"
9946 "</D:lockdiscovery>"
9947 "</d:multistatus>\n";
9955 int status_code = 500;
9956 if (
hm->body.len != (
size_t) ~0 &&
hm->body.len > 0) {
9958 }
else if (!
mg_mkdir(path, 0755)) {
9985 snprintf(path,
sizeof(path),
"%s%c%s", dir,
'/',
dp->d_name);
10007 if (
p !=
NULL &&
p[1] ==
'/' &&
10010 snprintf(buf,
sizeof(buf),
"%s%.*s",
opts->dav_document_root,
10012 if (
rename(path, buf) == 0) {
10025 const char *path) {
10032 }
else if (
remove(path) == 0) {
10044 for (s = path + 1; *s !=
'\0'; s++) {
10048 snprintf(buf,
sizeof(buf),
"%.*s", (
int) (s - path), path);
10049 buf[
sizeof(buf) - 1] =
'\0';
10064 int rc, status_code =
mg_stat(path, &
st) == 0 ? 200 : 201;
10068 mg_printf(nc,
"HTTP/1.1 %d OK\r\nContent-Length: 0\r\n\r\n", status_code);
10069 }
else if (
rc == -1) {
10087 mg_printf(nc,
"HTTP/1.1 %d OK\r\nContent-Length: 0\r\n\r\n", status_code);
10095#ifdef MG_MODULE_LINES
10096#line 1 "mongoose/src/mg_http_websocket.c"
10103#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_WEBSOCKET
10107#ifndef MG_WEBSOCKET_PING_INTERVAL_SECONDS
10108#define MG_WEBSOCKET_PING_INTERVAL_SECONDS 5
10111#define FLAGS_MASK_FIN (1 << 7)
10112#define FLAGS_MASK_OP 0x0f
10132 if (
wsm->flags & 0x8) {
10150 if ((
int) len == ~0) {
10167 if (
wsd->reass_len > 0) {
10195 mg_ws_close(nc,
"fragmented control frames are illegal", ~0);
10198 wsd->reass_len > 0) {
10203 mg_ws_close(nc,
"non-continuation in the middle of a fragmented message",
10243 for (
i = 0;
i < data_len;
i++) {
10257 wsd->reass_len = 1 ;
10262 wsd->reass_len +=
wsm.size;
10269 wsm.size =
wsd->reass_len - 1 ;
10271 wsd->reass_len = 0;
10331#if MG_DISABLE_WS_RANDOM_MASK
10334 if (
sizeof(
long) >= 4) {
10336 }
else if (
sizeof(
long) == 2) {
10346 unsigned char header[10];
10351 header[1] = (
unsigned char) len;
10353 }
else if (len < 65535) {
10370 header[1] |= 1 << 7;
10383 if (ctx->
pos == 0)
return;
10392 DBG((
"%p %d %d", nc,
op, (
int) len));
10409 len +=
strv[
i].len;
10426 const char *fmt, ...) {
10437 if (buf !=
mem && buf !=
NULL) {
10464#if MG_ENABLE_CALLBACK_USERDATA
10488 static const char *
magic =
"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
10491 unsigned char sha[20];
10498 "HTTP/1.1 101 Switching Protocols\r\n"
10499 "Upgrade: websocket\r\n"
10500 "Connection: Upgrade\r\n");
10504 mg_printf(nc,
"Sec-WebSocket-Protocol: %.*s\r\n", (
int) s->
len, s->
p);
10512 const char *host,
const char *
protocol,
10513 const char *extra_headers) {
10519 const char *host,
const char *
protocol,
10520 const char *extra_headers,
const char *user,
10521 const char *
pass) {
10528 const struct mg_str path,
10529 const struct mg_str host,
10531 const struct mg_str extra_headers,
10532 const struct mg_str user,
10544 if (user.
len > 0) {
10555 "GET %.*s HTTP/1.1\r\n"
10556 "Upgrade: websocket\r\n"
10557 "Connection: Upgrade\r\n"
10559 "Sec-WebSocket-Version: 13\r\n"
10560 "Sec-WebSocket-Key: %s\r\n",
10561 (
int) path.
len, path.
p, (
int)
auth.len,
10565 if (host.
len > 0) {
10573 if (extra_headers.
len > 0) {
10574 mg_printf(nc,
"%.*s", (
int) extra_headers.
len, extra_headers.
p);
10584 const char *extra_headers) {
10594 const char *extra_headers) {
10610 const char *
url,
const char *
protocol,
const char *extra_headers) {
10617#ifdef MG_MODULE_LINES
10618#line 1 "mongoose/src/mg_util.c"
10631#define MAX(a, b) ((a) > (b) ? (a) : (b))
10643#if MG_ENABLE_FILESYSTEM && !defined(MG_USER_FILE_FUNCTIONS)
10667#if defined(_WIN32) && !defined(WINCE)
10693#if MG_ENABLE_THREADS
10697#elif defined(_WIN32)
10706#if defined(MG_STACK_SIZE) && MG_STACK_SIZE > 1
10713 return (
void *) thread_id;
10720#if defined(_WIN32) && !defined(WINCE)
10722#elif defined(__unix__)
10732 if (buf ==
NULL || len <= 0)
return 0;
10747 addr = (
void *) &sa->
sin6.sin6_addr;
10757#elif defined(_WIN32) || MG_LWIP || (MG_NET_IF == MG_NET_IF_PIC32)
10772 int port =
ntohs(sa->
sin.sin_port);
10797#if MG_ENABLE_HEXDUMP
10800 const unsigned char *p = (
const unsigned char *) buf;
10801 char ascii[17] =
"";
10804 for (
i = 0;
i < len;
i++) {
10814 ascii[
idx] = p[
i] < 0x20 || p[
i] > 0x7e ?
'.' : p[
i];
10832 n = (len < 16 ? len : 16);
10843 char src[60], dst[60];
10844 const char *tag =
NULL;
10862 if (tag ==
NULL)
return;
10864 if (
strcmp(path,
"-") == 0) {
10866 }
else if (
strcmp(path,
"--") == 0) {
10868#if MG_ENABLE_FILESYSTEM
10880 fprintf(
fp,
"%lu %p %s %s %s %d\n", (
unsigned long)
mg_time(), (
void *) nc,
10890 static const int n = 1;
10892 return ((
char *) &
n)[0] == 0;
10917 struct mbuf *buf) {
10927 if (
pass.len > 0) {
10939 :
"0123456789abcdef");
10944 for (
i = 0;
i < src.len;
i++) {
10945 const unsigned char c = *((
const unsigned char *) src.p +
i);
10964#ifdef MG_MODULE_LINES
10965#line 1 "mongoose/src/mg_mqtt.c"
10981 return (
up[0] << 8) +
up[1];
10984static const char *
scanto(
const char *p,
struct mg_str *s) {
10987 return s->
p + s->
len;
10993 const char *p, *
end, *
eop = &
io->buf[
io->len];
10994 unsigned char lc = 0;
10998 header =
io->
buf[0];
11005 lc = *((
const unsigned char *) p++);
11006 len += (lc & 0x7f) << 7 *
len_len;
11008 if (!(lc & 0x80))
break;
11020 p =
scanto(p, &
mm->protocol_name);
11022 mm->protocol_version = *(
uint8_t *) p++;
11048 (
"%d %2x %d proto [%.*s] client_id [%.*s] will_topic [%.*s] "
11049 "will_msg [%.*s] user_name [%.*s] password [%.*s]",
11050 (
int) len, (
int)
mm->connect_flags, (
int)
mm->keep_alive_timer,
11051 (
int)
mm->protocol_name.len,
mm->protocol_name.p,
11052 (
int)
mm->client_id.len,
mm->client_id.p, (
int)
mm->will_topic.len,
11053 mm->will_topic.p, (
int)
mm->will_message.len,
mm->will_message.p,
11054 (
int)
mm->user_name.len,
mm->user_name.p, (
int)
mm->password.len,
11060 mm->connack_ret_code = p[1];
11080 mm->payload.len =
end - p;
11092 mm->payload.len =
end - p;
11128 LOG(
LL_ERROR, (
"%p recv buffer (%lu bytes) exceeds the limit "
11129 "%lu bytes, and not drained, closing",
11151 if (
pd->keep_alive > 0 &&
pd->last_control_time > 0 &&
11152 (
now -
pd->last_control_time) >
pd->keep_alive) {
11169 res.len = (
c - topic->p);
11170 topic->len -= (
res.len + 1);
11171 topic->p += (
res.len + 1);
11181 if (
exp.len == 0)
return 0;
11186 if (
tc.len != 0)
return 0;
11187 if (
exp.len == 0)
break;
11191 if (
tc.len == 0 && topic.
len == 0)
return 0;
11196 return (
exp.len == 0);
11202 return (
tc.len == 0 && topic.
len == 0);
11221 buf[0] = (cmd << 4) | flags;
11225 *
vlen = len % 0x80;
11227 if (len > 0) *
vlen |= 0x80;
11247 if (client_id !=
NULL) {
11264 if (
opts.keep_alive == 0) {
11265 opts.keep_alive = 60;
11281 mg_send(nc,
"\00\04MQTT\04", 7);
11313 pd->keep_alive =
opts.keep_alive;
11370 unsigned char *buf = (
unsigned char *) msg->
payload.
p + pos;
11373 if ((
size_t) pos >= msg->payload.len)
return -1;
11375 topic->len = buf[0] << 8 | buf[1];
11376 topic->p = (
char *) buf + 2;
11377 new_pos = pos + 2 + topic->len + 1;
11378 if ((
size_t)
new_pos > msg->payload.len)
return -1;
11379 *qos = buf[2 + topic->len];
11478#ifdef MG_MODULE_LINES
11479#line 1 "mongoose/src/mg_mqtt_server.c"
11489#if MG_ENABLE_MQTT_BROKER
11495 s->subscriptions =
NULL;
11496 s->num_subscriptions = 0;
11510 for (
i = 0;
i < s->num_subscriptions;
i++) {
11511 MG_FREE((
void *) s->subscriptions[
i].topic);
11524 brk->user_data = user_data;
11571 sizeof(*
ss->subscriptions) * (
ss->num_subscriptions +
num_subs));
11576 ss->subscriptions =
te;
11578 pos < (
int) msg->payload.len &&
11580 ss->num_subscriptions++) {
11581 te = &
ss->subscriptions[
ss->num_subscriptions];
11585 ((
char *)
te->topic)[
topic.len] =
'\0';
11589 if (pos == (
int) msg->payload.len) {
11603 for (
i = 0;
i < s->num_subscriptions;
i++) {
11606 char buf[100], *p = buf;
11675#ifdef MG_MODULE_LINES
11676#line 1 "mongoose/src/mg_dns.c"
11706 if (
rr->rtype == query) {
11716 switch (
rr->rtype) {
11718 if (data_len <
sizeof(
struct in_addr)) {
11721 if (
rr->rdata.p + data_len > msg->
pkt.
p + msg->
pkt.
len) {
11728 if (data_len <
sizeof(
struct in6_addr)) {
11746 memset(&header, 0,
sizeof(header));
11768 size_t pos =
io->len;
11775 if (s -
name > 127) {
11788 }
while (*s !=
'\0');
11791 return io->len - pos;
11797 size_t pos =
io->len;
11821 size_t off =
io->len;
11836 return io->len - pos;
11851 msg->
flags = 0x100;
11871 mg_send(nc, pkt.buf, pkt.len);
11885 if (((
unsigned char *)
data)[0] & 0xc0) {
11896 rr->name.p = (
char *)
name;
11916 data_len = *
data << 8 | *(
data + 1);
11919 rr->rdata.p = (
char *)
data;
11920 rr->rdata.len = data_len;
11928 unsigned char *
data = (
unsigned char *) buf +
sizeof(*header);
11929 unsigned char *
end = (
unsigned char *) buf + len;
11932 memset(msg, 0,
sizeof(*msg));
11936 if (len < (
int)
sizeof(*header))
return -1;
11966 const unsigned char *
data = (
unsigned char *)
name->p;
11967 const unsigned char *
end = (
unsigned char *) msg->
pkt.
p + msg->
pkt.
len;
12033 memset(&msg, 0,
sizeof(msg));
12034 msg.
flags = 0x8081;
12055#ifdef MG_MODULE_LINES
12056#line 1 "mongoose/src/mg_dns_server.c"
12063#if MG_ENABLE_DNS_SERVER
12073 rep.start =
io->len;
12076 msg->
flags |= 0x8080;
12084 size_t sent = r->io->len - r->start;
12092 mg_send(nc, r->io->buf + r->start, r->io->len - r->start);
12093 r->io->len = r->start;
12099 const char *
name,
int rtype,
int ttl,
const void *rdata,
12129#ifdef MG_MODULE_LINES
12130#line 1 "mongoose/src/mg_resolv.c"
12137#if MG_ENABLE_ASYNC_RESOLVER
12142#ifndef MG_DEFAULT_NAMESERVER
12143#define MG_DEFAULT_NAMESERVER "8.8.8.8"
12173 *
key =
L"SYSTEM\\ControlSet001\\Services\\Tcpip\\Parameters\\Interfaces";
12180 for (ret = -1,
i = 0; 1;
i++) {
12189 value[0] !=
'\0') ||
12192 value[0] !=
'\0'))) {
12213#elif MG_ENABLE_FILESYSTEM && defined(MG_RESOLV_CONF_FILE_NAME)
12221 for (ret = -1;
fgets(line,
sizeof(line),
fp) !=
NULL;) {
12222 unsigned int a, b,
c,
d;
12223 if (
sscanf(line,
"nameserver %u.%u.%u.%u", &a, &b, &
c, &
d) == 4) {
12239#if MG_ENABLE_FILESYSTEM && defined(MG_HOSTS_FILE_NAME)
12245 unsigned int a, b,
c,
d;
12253 if (line[0] ==
'#')
continue;
12255 if (
sscanf(line,
"%u.%u.%u.%u%n", &a, &b, &
c, &
d, &len) == 0) {
12259 for (p = line + len;
sscanf(p,
"%s%n",
alias, &len) == 1; p += len) {
12261 usa->
sin.sin_addr.s_addr =
htonl(a << 24 | b << 16 |
c << 8 |
d);
12282#if !MG_ENABLE_CALLBACK_USERDATA
12287 DBG((
"ev=%d user_data=%p",
ev, user_data));
12342#ifdef MG_LOG_DNS_FAILURES
12365 const char *nameserver =
opts.nameserver;
12368 if (nameserver ==
NULL) {
12381 req->
name[
sizeof(req->
name) - 1] =
'\0';
12391 if (nameserver ==
NULL) {
12400 snprintf(nameserver_url,
sizeof(nameserver_url),
"udp://%s:53", nameserver);
12407 dns_nc->user_data = req;
12418 if (nameserver !=
NULL) {
12424#ifdef MG_MODULE_LINES
12425#line 1 "mongoose/src/mg_coap.c"
12450 while (
cm->options !=
NULL) {
12453 cm->options = next;
12467 if (
cm->options ==
NULL) {
12523 if (((
uint8_t) *ptr >> 6) != 1) {
12533 cm->msg_type = ((
uint8_t) *ptr & 0x30) >> 4;
12542 cm->token.len = *ptr & 0x0F;
12543 if (
cm->token.len > 8) {
12555 cm->code_detail = *ptr & 0x1F;
12576 if (
cm->token.len != 0) {
12577 if (ptr +
cm->token.len >
io->buf +
io->len) {
12582 ptr +=
cm->token.len;
12645 if (ptr ==
io->buf +
io->len) {
12715 if (ptr ==
io->buf +
io->len) {
12743 cm->payload.len =
io->len - (ptr -
io->buf);
12744 if (
cm->payload.len != 0) {
12745 cm->payload.p = ptr;
12762 }
else if (
value > 0xFF + 13 &&
value <= 0xFFFF + 269) {
12779 }
else if (
value >= 13 &&
value <= 0xFF + 13) {
12783 }
else if (
value > 0xFF + 13 &&
value <= 0xFFFF + 269) {
12785 *ext =
value - 269;
12800 *ptr =
val & 0x00FF;
12811 if (len ==
sizeof(
uint8_t)) {
12814 }
else if (len ==
sizeof(
uint16_t)) {
12835 if (
cm->token.len > 8) {
12838 if (
cm->code_class > 7) {
12841 if (
cm->code_detail > 31) {
12845 *len +=
cm->token.len;
12846 if (
cm->payload.len != 0) {
12847 *len +=
cm->payload.len + 1;
12862 if ((
opt->next !=
NULL &&
opt->number >
opt->next->number) ||
12863 opt->value.len > 0xFFFF + 269 ||
12867 *len +=
opt->value.len;
12887 prev_io_len =
io->len;
12889 ptr =
io->buf + prev_io_len;
12897 *ptr = (1 << 6) | (
cm->msg_type << 4) | (
uint8_t)(
cm->token.len);
12901 *ptr = (
cm->code_class << 5) | (
cm->code_detail);
12906 if (
cm->token.len != 0) {
12908 ptr +=
cm->token.len;
12928 if (
opt->value.len != 0) {
12930 ptr +=
opt->value.len;
12937 if (
cm->payload.len != 0) {
12940 memcpy(ptr,
cm->payload.p,
cm->payload.len);
13024#ifdef MG_MODULE_LINES
13025#line 1 "mongoose/src/mg_sntp.c"
13038#define SNTP_TIME_OFFSET 2208988800
13040#ifndef SNTP_TIMEOUT
13041#define SNTP_TIMEOUT 10
13044#ifndef SNTP_ATTEMPTS
13045#define SNTP_ATTEMPTS 3
13049 return (
val & 0xFFFFFFFF00000000) >> 32;
13084 buf[0] = (3 << 6) | (4 << 3) | 3;
13106#ifndef MG_SNTP_NO_DELAY_CORRECTION
13115#ifndef MG_SNTP_NO_DELAY_CORRECTION
13140 if ((
hdr & 0x38) >> 3 != 4) {
13151 memset(msg, 0,
sizeof(*msg));
13153 msg->kiss_of_death = (buf[1] == 0);
13157#ifndef MG_SNTP_NO_DELAY_CORRECTION
13168 msg->time = (
double)
tv.tv_sec + (((
double)
tv.tv_usec + delay) / 1000000.0);
13175 struct mbuf *
io = &
c->recv_mbuf;
13183 DBG((
"Invalid SNTP packet received (%d)", (
int)
io->len));
13211 const char *proto =
"", *port =
"", *
tmp;
13253#if !MG_ENABLE_CALLBACK_USERDATA
13254 void *user_data =
c->user_data;
13285 c->user_data =
NULL;
13306 sd->hander = event_handler;
13307#if !MG_ENABLE_CALLBACK_USERDATA
13315#ifdef MG_MODULE_LINES
13316#line 1 "mongoose/src/mg_socks.c"
13338 struct mbuf *r = &
c->recv_mbuf;
13341 }
else if (r->
len > 2 && (
size_t) r->
buf[1] + 2 <= r->
len) {
13345 for (
i = 2;
i < r->
buf[1] + 2;
i++) {
13362 c->user_data =
NULL;
13388 serv->user_data =
c;
13389 c->user_data =
serv;
13402 struct mbuf *r = &
c->recv_mbuf;
13403 unsigned char *p = (
unsigned char *) r->
buf;
13408 if (r->
len < 8)
return;
13426 p[4] << 8 | p[5], p[6] << 8 | p[7], p[8] << 8 | p[9],
13427 p[10] << 8 | p[11], p[12] << 8 | p[13], p[14] << 8 | p[15],
13428 p[16] << 8 | p[17], p[18] << 8 | p[19], p[20] << 8 | p[21]);
13477#ifdef MG_MODULE_LINES
13478#line 1 "common/platforms/cc3200/cc3200_libc.c"
13497#if CS_PLATFORM == CS_P_CC3200
13503#ifndef __TI_COMPILER_VERSION__
13505#include <sys/stat.h>
13506#include <sys/time.h>
13510#include <inc/hw_types.h>
13511#include <inc/hw_memmap.h>
13512#include <driverlib/prcm.h>
13513#include <driverlib/rom.h>
13514#include <driverlib/rom_map.h>
13515#include <driverlib/uart.h>
13516#include <driverlib/utils.h>
13518#define CONSOLE_UART UARTA0_BASE
13520#ifdef __TI_COMPILER_VERSION__
13546#if MG_TI_NO_HOST_INTERFACE
13557 while (*
str !=
'\0') {
13595#ifdef MG_MODULE_LINES
13596#line 1 "common/platforms/msp432/msp432_libc.c"
13615#if CS_PLATFORM == CS_P_MSP432
13617#include <ti/sysbios/BIOS.h>
13618#include <ti/sysbios/knl/Clock.h>
13623 tp->tv_usec = (
ticks % 1000) * 1000;
13628#ifdef MG_MODULE_LINES
13629#line 1 "common/platforms/nrf5/nrf5_libc.c"
13648#if (CS_PLATFORM == CS_P_NRF51 || CS_PLATFORM == CS_P_NRF52) && \
13649 defined(__ARMCC_VERSION)
13657#ifdef MG_MODULE_LINES
13658#line 1 "common/platforms/simplelink/sl_fs_slfs.h"
13677#ifndef CS_COMMON_PLATFORMS_SIMPLELINK_SL_FS_SLFS_H_
13678#define CS_COMMON_PLATFORMS_SIMPLELINK_SL_FS_SLFS_H_
13680#if defined(MG_FS_SLFS)
13683#ifndef __TI_COMPILER_VERSION__
13685#include <sys/stat.h>
13688#define MAX_OPEN_SLFS_FILES 8
13708#ifdef MG_MODULE_LINES
13709#line 1 "common/platforms/simplelink/sl_fs_slfs.c"
13730#if defined(MG_FS_SLFS) || defined(CC3200_FS_SLFS)
13736#if CS_PLATFORM == CS_P_CC3200
13737#include <inc/hw_types.h>
13743#if SL_MAJOR_VERSION_NUM < 2
13747 return (r < 0 ? r :
fh);
13763#ifndef FS_SLFS_MAX_FILE_SIZE
13764#define FS_SLFS_MAX_FILE_SIZE (64 * 1024)
13787 DBG((
"SL error: %d", (
int) r));
13824 int rw = (flags & 3);
13830 (
unsigned int)
foi->flags,
foi->token));
13846 if (
foi->size > 0) {
13855#if SL_MAJOR_VERSION_NUM >= 2
13881 LOG(
LL_DEBUG, (
"sl_FsClose(%d) = %d", (
int)
fi->fh, (
int) r));
13891 if (
fi->pos ==
fi->size)
return 0;
13893 DBG((
"sl_FsRead(%d, %d, %d) = %d", (
int)
fi->fh, (
int)
fi->pos, (
int)
count,
13906 DBG((
"sl_FsWrite(%d, %d, %d) = %d", (
int)
fi->fh, (
int)
fi->pos, (
int)
count,
13938 s->st_size =
fi->size;
14001 foi->flags = flags;
14013#ifdef MG_MODULE_LINES
14014#line 1 "common/platforms/simplelink/sl_fs.c"
14033#if MG_NET_IF == MG_NET_IF_SIMPLELINK && \
14034 (defined(MG_FS_SLFS) || defined(MG_FS_SPIFFS))
14038 return (
e == 0 ? 0 : -1);
14060#if !defined(MG_FS_NO_VFS)
14063#include <stdbool.h>
14067#ifdef __TI_COMPILER_VERSION__
14074#ifdef CC3200_FS_SPIFFS
14082#define NUM_SYS_FDS 3
14083#define SPIFFS_FD_BASE 10
14084#define SLFS_FD_BASE 100
14086#if !defined(MG_UART_CHAR_PUT) && !defined(MG_UART_WRITE)
14087#if CS_PLATFORM == CS_P_CC3200
14088#include <inc/hw_types.h>
14089#include <inc/hw_memmap.h>
14090#include <driverlib/rom.h>
14091#include <driverlib/rom_map.h>
14092#include <driverlib/uart.h>
14093#define MG_UART_CHAR_PUT(fd, c) MAP_UARTCharPut(UARTA0_BASE, c);
14095#define MG_UART_WRITE(fd, buf, len)
14102#ifdef CC3200_FS_SPIFFS
14111#ifdef CC3200_FS_SPIFFS
14124#if MG_TI_NO_HOST_INTERFACE
14125int open(
const char *
pathname,
unsigned flags,
int mode) {
14138#ifdef CC3200_FS_SPIFFS
14144 (
"open(%s, 0x%x) = %d, fname = %s",
pathname, flags, fd,
fname));
14166#ifdef CC3200_FS_SPIFFS
14174#if MG_TI_NO_HOST_INTERFACE
14187#ifdef CC3200_FS_SPIFFS
14198 DBG((
"close(%d) = %d", fd, r));
14202#if MG_TI_NO_HOST_INTERFACE
14215#ifdef CC3200_FS_SPIFFS
14232 memset(s, 0,
sizeof(*s));
14239 memset(s, 0,
sizeof(*s));
14245#ifdef CC3200_FS_SPIFFS
14256 DBG((
"fstat(%d) = %d", fd, r));
14260#if MG_TI_NO_HOST_INTERFACE
14261int read(
int fd,
char *buf,
unsigned count) {
14279#ifdef CC3200_FS_SPIFFS
14290 DBG((
"read(%d, %u) = %d", fd,
count, r));
14294#if MG_TI_NO_HOST_INTERFACE
14295int write(
int fd,
const char *buf,
unsigned count) {
14309#ifdef MG_UART_WRITE
14311#elif defined(MG_UART_CHAR_PUT)
14315 const char c = ((
const char *) buf)[
i];
14324#ifdef CC3200_FS_SPIFFS
14342#if MG_TI_NO_HOST_INTERFACE || defined(_NEWLIB_VERSION)
14351#ifdef CC3200_FS_SPIFFS
14355 DBG((
"rename(%s, %s) = %d",
from,
to, r));
14360#if MG_TI_NO_HOST_INTERFACE
14373#ifdef CC3200_FS_SPIFFS
14381#ifdef CC3200_FS_SPIFFS
14398 DBG((
"readdir(%p) = %p", dir,
res));
14404 DBG((
"closedir(%p) = %d", dir,
res));
14408int rmdir(
const char *path) {
14422#ifdef __TI_COMPILER_VERSION__
14425#pragma diag_suppress 169
14439#ifdef MG_MODULE_LINES
14440#line 1 "common/platforms/simplelink/sl_socket.c"
14459#if MG_NET_IF == MG_NET_IF_SIMPLELINK
14502#ifdef MG_MODULE_LINES
14503#line 1 "common/platforms/simplelink/sl_mg_task.c"
14505#if MG_NET_IF == MG_NET_IF_SIMPLELINK && !defined(MG_SIMPLELINK_NO_OSI)
14509#include <oslib/osi.h>
14520static void mg_task(
void *arg);
14542 switch (msg.
type) {
14544 msg.
cb(&mgr, msg.
arg);
14557#ifdef MG_MODULE_LINES
14558#line 1 "common/platforms/simplelink/sl_net_if.h"
14577#ifndef CS_COMMON_PLATFORMS_SIMPLELINK_SL_NET_IF_H_
14578#define CS_COMMON_PLATFORMS_SIMPLELINK_SL_NET_IF_H_
14586#ifndef MG_ENABLE_NET_IF_SIMPLELINK
14587#define MG_ENABLE_NET_IF_SIMPLELINK MG_NET_IF == MG_NET_IF_SIMPLELINK
14597#ifdef MG_MODULE_LINES
14598#line 1 "common/platforms/simplelink/sl_net_if.c"
14619#if MG_ENABLE_NET_IF_SIMPLELINK
14624#define MG_TCP_RECV_BUFFER_SIZE 1024
14625#define MG_UDP_RECV_BUFFER_SIZE 1500
14633#if SL_MAJOR_VERSION_NUM < 2
14634 opt.NonblockingEnabled = 1;
14636 opt.NonBlockingEnabled = 1;
14648#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_SIMPLELINK
14657#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_SIMPLELINK
14659 if (nc->
err != 0)
goto out;
14663 DBG((
"%p to %s:%d sock %d %d err %d", nc,
inet_ntoa(sa->
sin.sin_addr),
14682 if (sock < 0)
return sock;
14750 DBG((
"%p: failed to accept: %d", lc, sock));
14771 (
sa->sa.sa_family ==
AF_INET) ?
sizeof(
sa->sin) :
sizeof(
sa->sin6);
14773 if (sock < 0)
return sock;
14774#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_SIMPLELINK
14790#define _MG_F_FD_CAN_READ 1
14791#define _MG_F_FD_CAN_WRITE 1 << 1
14792#define _MG_F_FD_ERROR 1 << 2
14795 DBG((
"%p fd=%d fd_flags=%d nc_flags=0x%lx rmbl=%d smbl=%d", nc, nc->
sock,
14809 DBG((
"%p conn res=%d", nc, nc->
err));
14846 DBG((
"%p after fd=%d nc_flags=0x%lx rmbl=%d smbl=%d", nc, nc->
sock, nc->
flags,
14854 DBG((
"%p %d", nc, sock));
14859 DBG((
"%p using sl_Select()", iface->
mgr));
14942 DBG((
"sl_Select @ %ld num_ev=%d of %d, timeout=%d", (
long)
now,
num_ev,
14992 if (
res == 0)
continue;
15004#define MG_SL_IFACE_VTABLE \
15008 mg_sl_if_add_conn, \
15009 mg_sl_if_remove_conn, \
15011 mg_sl_if_listen_tcp, \
15012 mg_sl_if_listen_udp, \
15013 mg_sl_if_connect_tcp, \
15014 mg_sl_if_connect_udp, \
15015 mg_sl_if_tcp_send, \
15016 mg_sl_if_udp_send, \
15017 mg_sl_if_tcp_recv, \
15018 mg_sl_if_udp_recv, \
15019 mg_sl_if_create_conn, \
15020 mg_sl_if_destroy_conn, \
15021 mg_sl_if_sock_set, \
15022 mg_sl_if_get_conn_addr, \
15027#if MG_NET_IF == MG_NET_IF_SIMPLELINK
15032#ifdef MG_MODULE_LINES
15033#line 1 "common/platforms/simplelink/sl_ssl_if.c"
15052#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_SIMPLELINK
15056#ifndef MG_SSL_IF_SIMPLELINK_SLFS_PREFIX
15057#define MG_SSL_IF_SIMPLELINK_SLFS_PREFIX "SL:"
15060#define MG_SSL_IF_SIMPLELINK_SLFS_PREFIX_LEN \
15061 (sizeof(MG_SSL_IF_SIMPLELINK_SLFS_PREFIX) - 1)
15082 nc->ssl_if_data = ctx;
15084 if (params->cert !=
NULL || params->key !=
NULL) {
15085 if (params->cert !=
NULL && params->key !=
NULL) {
15086 ctx->ssl_cert =
strdup(params->cert);
15087 ctx->ssl_key =
strdup(params->key);
15093 if (params->ca_cert !=
NULL &&
strcmp(params->ca_cert,
"*") != 0) {
15094 ctx->ssl_ca_cert =
strdup(params->ca_cert);
15097 if (params->server_name !=
NULL) {
15098 ctx->ssl_server_name =
strdup(params->server_name);
15135 if (ctx ==
NULL)
return;
15136 nc->ssl_if_data =
NULL;
15140 MG_FREE(ctx->ssl_server_name);
15141 memset(ctx, 0,
sizeof(*ctx));
15186#if MG_ENABLE_FILESYSTEM && defined(MG_FS_SLFS)
15198 bool result =
false;
15226 DBG((
"%p ssl ctx: %p", nc, ctx));
15228 if (ctx ==
NULL)
return 0;
15229 DBG((
"%p %s,%s,%s,%s", nc, (ctx->ssl_cert ? ctx->ssl_cert :
"-"),
15233 if (ctx->ssl_cert !=
NULL && ctx->ssl_key !=
NULL) {
15254 if (err != 0)
return err;
15256 if (ctx->ssl_ca_cert !=
NULL) {
15257 if (ctx->ssl_ca_cert[0] !=
'\0') {
15268 if (err != 0)
return err;
15271 if (ctx->ssl_server_name !=
NULL) {
15274 ctx->ssl_server_name,
strlen(ctx->ssl_server_name));
15275 DBG((
"DOMAIN_NAME_VERIFICATION %s -> %d", ctx->ssl_server_name, err));
15286#ifdef MG_MODULE_LINES
15287#line 1 "common/platforms/lwip/mg_lwip_net_if.h"
15306#ifndef CS_COMMON_PLATFORMS_LWIP_MG_NET_IF_LWIP_H_
15307#define CS_COMMON_PLATFORMS_LWIP_MG_NET_IF_LWIP_H_
15309#ifndef MG_ENABLE_NET_IF_LWIP_LOW_LEVEL
15310#define MG_ENABLE_NET_IF_LWIP_LOW_LEVEL MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL
15313#if MG_ENABLE_NET_IF_LWIP_LOW_LEVEL
15354#ifdef MG_MODULE_LINES
15355#line 1 "common/platforms/lwip/mg_lwip_net_if.c"
15374#if MG_ENABLE_NET_IF_LWIP_LOW_LEVEL
15378#include <lwip/init.h>
15379#include <lwip/pbuf.h>
15380#include <lwip/tcp.h>
15381#include <lwip/tcpip.h>
15382#if ((LWIP_VERSION_MAJOR << 8) | LWIP_VERSION_MINOR) >= 0x0105
15383#include <lwip/priv/tcp_priv.h>
15384#include <lwip/priv/tcpip_priv.h>
15386#include <lwip/tcp_impl.h>
15388#include <lwip/udp.h>
15398#define ip_2_ip4(addr) ipX_2_ip(addr)
15400#define ip_2_ip4(addr) (addr)
15409#define TCP_NEW tcp_new_ip6
15410#define TCP_BIND tcp_bind_ip6
15411#define UDP_BIND udp_bind_ip6
15412#define IPADDR_NTOA(x) ip6addr_ntoa((const ip6_addr_t *)(x))
15413#define SET_ADDR(dst, src) \
15414 memcpy((dst)->sin6.sin6_addr.s6_addr, (src)->ip6.addr, \
15415 sizeof((dst)->sin6.sin6_addr.s6_addr))
15417#define TCP_NEW tcp_new
15418#define TCP_BIND tcp_bind
15419#define UDP_BIND udp_bind
15420#define IPADDR_NTOA ipaddr_ntoa
15421#define SET_ADDR(dst, src) (dst)->sin.sin_addr.s_addr = ip_2_ip4(src)->addr
15425#if LWIP_TCPIP_CORE_LOCKING
15452#define mg_lwip_netif_run_on_tcpip(fn, arg) (fn)(arg)
15467#define mgos_unlock()
15472#if LWIP_TCP_KEEPALIVE
15481 tpcb->keep_idle =
idle * 1000;
15482 tpcb->keep_intvl =
interval * 1000;
15483 tpcb->keep_cnt =
count;
15489#elif !defined(MG_NO_LWIP_TCP_KEEPALIVE)
15490#warning LWIP TCP keepalive is disabled. Please consider enabling it.
15496 tpcb->remote_port,
err));
15503#if LWIP_TCP_KEEPALIVE
15512 DBG((
"%p conn error %d", nc,
err));
15529 DBG((
"%p %p %p %p %u %d",
nc,
cs, tpcb, p, (p !=
NULL ? p->tot_len : 0),
15533 if (
cs->rx_chain !=
NULL) {
15546 }
else if (
nc ==
NULL) {
15554 if (p->next !=
NULL) {
15555 struct pbuf *
q = p->next;
15559 if (
cs->rx_chain ==
NULL) {
15581 DBG((
"%p %p %u %p %p", nc, tpcb, num_sent, tpcb->unsent, tpcb->unacked));
15607 cs->pcb.tcp = tpcb;
15615 DBG((
"%p tcp_bind = %d", nc,
cs->
err));
15621 DBG((
"%p tcp_connect %p = %d", nc, tpcb,
cs->
err));
15638#if ((LWIP_VERSION_MAJOR << 8) | LWIP_VERSION_MINOR) >= 0x0105
15656#if ((LWIP_VERSION_MAJOR << 8) | LWIP_VERSION_MINOR) >= 0x0105
15659 sa->sin.sin_addr.s_addr =
addr->addr;
15661 sa->sin.sin_port =
htons(port);
15673 if (
cs->rx_chain ==
NULL) {
15678 if (!
cs->recv_pending) {
15679 cs->recv_pending = 1;
15694 if (
cs->rx_chain !=
NULL) {
15713 DBG((
"%p udp_bind %p = %d", nc, upcb,
cs->
err));
15716 cs->pcb.udp = upcb;
15733 if (
cs->pcb.tcp ==
NULL)
return;
15737 sa.
sin.sin_port =
htons(tpcb->remote_port);
15746 (
"%p conn %p from %s:%u", lc,
newtpcb,
15754#if TCP_LISTEN_BACKLOG
15771#if LWIP_TCP_KEEPALIVE
15803 cs->pcb.tcp = tpcb;
15829 cs->pcb.udp = upcb;
15856 size_t len =
MIN(tpcb->mss,
MIN(ctx->
len, tpcb->snd_buf));
15859 DBG((
"%p no buf avail %u %u %p %p", tpcb, tpcb->snd_buf, tpcb->snd_queuelen,
15860 tpcb->unsent, tpcb->unacked));
15865 unsent = (tpcb->unsent !=
NULL ? tpcb->unsent->len : 0);
15866 unacked = (tpcb->unacked !=
NULL ? tpcb->unacked->len : 0);
15873#if CS_PLATFORM == CS_P_ESP8266
15881 unsent = (tpcb->unsent !=
NULL ? tpcb->unsent->len : 0);
15882 unacked = (tpcb->unacked !=
NULL ? tpcb->unacked->len : 0);
15902 if (tpcb ==
NULL)
return -1;
15903 if (tpcb->snd_buf <= 0)
return 0;
15927#if defined(LWIP_IPV4) && LWIP_IPV4 && defined(LWIP_IPV6) && LWIP_IPV6
15928 ip_addr_t ip = {.u_addr.ip4.addr = nc->
sa.
sin.sin_addr.s_addr, .type = 0};
15933 if (p ==
NULL)
return 0;
15939 return (
cs->err ==
ERR_OK ? (
int) len : -2);
15953#if CS_PLATFORM == CS_P_ESP8266
15978 while (
cs->rx_chain !=
NULL && len > 0) {
15988 if (
cs->rx_offset ==
cs->rx_chain->len) {
16005 if (
cs ==
NULL)
return 0;
16020 if (tpcb !=
NULL) {
16022 DBG((
"%p tcp_close %p", nc, tpcb));
16026 while (
cs->rx_chain !=
NULL) {
16036 if (upcb !=
NULL) {
16037 DBG((
"%p udp_remove %p", nc, upcb));
16048 memset(sa, 0,
sizeof(*sa));
16055 }
else if (upcb !=
NULL) {
16056 sa->
sin.sin_port =
htons(upcb->local_port);
16063 }
else if (tpcb !=
NULL) {
16064 sa->
sin.sin_port =
htons(tpcb->local_port);
16075#define MG_LWIP_IFACE_VTABLE \
16079 mg_lwip_if_add_conn, \
16080 mg_lwip_if_remove_conn, \
16082 mg_lwip_if_listen_tcp, \
16083 mg_lwip_if_listen_udp, \
16084 mg_lwip_if_connect_tcp, \
16085 mg_lwip_if_connect_udp, \
16086 mg_lwip_if_tcp_send, \
16087 mg_lwip_if_udp_send, \
16088 mg_lwip_if_tcp_recv, \
16089 mg_lwip_if_udp_recv, \
16090 mg_lwip_if_create_conn, \
16091 mg_lwip_if_destroy_conn, \
16092 mg_lwip_if_sock_set, \
16093 mg_lwip_if_get_conn_addr, \
16098#if MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL
16103#ifdef MG_MODULE_LINES
16104#line 1 "common/platforms/lwip/mg_lwip_ev_mgr.c"
16123#if MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL
16125#ifndef MG_SIG_QUEUE_LEN
16126#define MG_SIG_QUEUE_LEN 32
16151 md->sig_queue_len++;
16159 while (
md->sig_queue_len > 0) {
16161 int i =
md->start_index;
16162 int sig =
md->sig_queue[
i].sig;
16166 md->sig_queue_len--;
16179 cs->recv_pending = 0;
16199#if !NO_SYS && !LWIP_TCPIP_CORE_LOCKING
16219 if (
md->sig_queue[
i].nc == nc) {
16233 DBG((
"begin poll @%u", (
unsigned int) (
now * 1000)));
16243 cs->pcb.tcp->unsent !=
NULL) {
16258 if (
cs->rx_chain !=
NULL) {
16260 }
else if (
cs->draining_rx_chain) {
16270 DBG((
"end poll @%u, %d conns, %d timers (min %u), next in %d ms",
16279#ifdef MG_MODULE_LINES
16280#line 1 "common/platforms/wince/wince_libc.c"
16306 static char buf[10];
16311int open(
const char *filename,
int oflag,
int pmode) {
16356 ft.dwHighDateTime = (
filetime & 0xFFFFFFFF00000000) >> 32;
16368#ifdef MG_MODULE_LINES
16369#line 1 "common/platforms/pic32/pic32_net_if.h"
16388#ifndef CS_COMMON_PLATFORMS_PIC32_NET_IF_H_
16389#define CS_COMMON_PLATFORMS_PIC32_NET_IF_H_
16397#ifndef MG_ENABLE_NET_IF_PIC32
16398#define MG_ENABLE_NET_IF_PIC32 MG_NET_IF == MG_NET_IF_PIC32
16408#ifdef MG_MODULE_LINES
16409#line 1 "common/platforms/pic32/pic32_net_if.c"
16428#if MG_ENABLE_NET_IF_PIC32
16526 sa.sin.sin_port =
htons(
si.remotePort);
16527 sa.sin.sin_addr.s_addr =
si.remoteIPaddress.v4Add.Val;
16539 static char addr[17];
16541 (
int) in.S_un.S_un_b.s_b2, (
int) in.S_un.S_un_b.s_b3,
16542 (
int) in.S_un.S_un_b.s_b4);
16568 if (bytes_written != 0) {
16574 bytes_written) != bytes_written) {
16589 if (bytes_read != 0 &&
16602 if (bytes_read != 0) {
16617 if (bytes_read != 0) {
16688#define MG_PIC32_IFACE_VTABLE \
16690 mg_pic32_if_init, \
16691 mg_pic32_if_free, \
16692 mg_pic32_if_add_conn, \
16693 mg_pic32_if_remove_conn, \
16694 mg_pic32_if_poll, \
16695 mg_pic32_if_listen_tcp, \
16696 mg_pic32_if_listen_udp, \
16697 mg_pic32_if_connect_tcp, \
16698 mg_pic32_if_connect_udp, \
16699 mg_pic32_if_tcp_send, \
16700 mg_pic32_if_udp_send, \
16701 mg_pic32_if_recved, \
16702 mg_pic32_if_create_conn, \
16703 mg_pic32_if_destroy_conn, \
16704 mg_pic32_if_sock_set, \
16705 mg_pic32_if_get_conn_addr, \
16710#if MG_NET_IF == MG_NET_IF_PIC32
16715#ifdef MG_MODULE_LINES
16716#line 1 "common/platforms/windows/windows_direct.c"
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)
int mg_mgr_poll(struct mg_mgr *m, int timeout_ms)
void mg_mqtt_ping(struct mg_connection *nc)
void mg_set_protocol_mqtt(struct mg_connection *nc)
void mg_mgr_free(struct mg_mgr *m)
const char * hexdump_file
int mg_resolve_from_hosts_file(const char *host, 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) WEAK
#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],...)
void mg_mqtt_disconnect(struct mg_connection *nc)
#define MG_MQTT_HAS_USER_NAME
#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) WEAK
#define MG_MAX_HTTP_HEADERS
int c_vsnprintf(char *buf, size_t buf_size, const char *fmt, va_list ap) WEAK
#define MG_MAX_HTTP_SEND_MBUF
#define MG_MAX_DNS_ANSWERS
#define MG_EV_WEBSOCKET_FRAME
int mg_stat(const char *path, cs_stat_t *st)
struct mg_connection * mg_connect_http(struct mg_mgr *mgr, mg_event_handler_t event_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) WEAK
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 **chunk, size_t *chunk_len)
#define MG_SOCK_STRINGIFY_PORT
void mbuf_remove(struct mbuf *mb, size_t n) WEAK
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 *n, int status_code, int64_t content_length, const char *extra_headers)
void mg_mqtt_pubcomp(struct mg_connection *nc, uint16_t message_id)
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_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) WEAK
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 *, mg_event_handler_t func, void *, size_t)
#define MG_DNS_AAAA_RECORD
void mg_send_response_line(struct mg_connection *c, 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 mbuf_free(struct mbuf *mbuf) WEAK
#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
int mg_open(const char *path, int flag, int mode)
double mg_set_timer(struct mg_connection *c, double timestamp)
int 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)
size_t mg_match_prefix_n(const struct mg_str, const struct mg_str) WEAK
#define MG_SEND_FUNC(s, b, l, f)
void mbuf_resize(struct mbuf *a, size_t new_size) WEAK
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) WEAK
void mg_send(struct mg_connection *nc, const void *buf, int len)
int mg_socketpair(sock_t[2], int sock_type)
#define WEBSOCKET_OP_PONG
#define MG_MQTT_CMD_PUBREL
struct mg_connection * next
void mg_send_http_chunk(struct mg_connection *nc, const char *buf, size_t len)
#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)
#define MG_EV_MQTT_CONNECT
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 cs_base64_finish(struct cs_base64_ctx *ctx)
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_and_flags, 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)
#define MG_SOCK_STRINGIFY_IP
size_t c_strnlen(const char *s, size_t maxlen) WEAK
size_t mg_match_prefix(const char *, int, const char *) WEAK
#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_and_flags, const struct mg_str *strings, int num_strings)
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) WEAK
#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 event_handler, const char *url, const char *protocol, const char *extra_headers)
struct mg_connection * listener
struct mg_connection * prev
#define MG_MQTT_HAS_PASSWORD
void mg_mqtt_pubrec(struct mg_connection *nc, uint16_t message_id)
void mg_set_close_on_exec(sock_t sock)
double cs_time(void) WEAK
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 *domain_name, char *ip_addr_buf, size_t buf_len)
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)
void mg_mqtt_suback(struct mg_connection *nc, uint8_t *qoss, size_t qoss_len, uint16_t message_id)
#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,...) WEAK
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) WEAK
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)
size_t mbuf_append(struct mbuf *a, const void *buf, size_t len) WEAK
#define MG_F_SSL_HANDSHAKE_DONE
int 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)
void mg_send_websocket_frame(struct mg_connection *nc, int op_and_flags, const void *data, size_t data_len)
#define MG_EV_MQTT_PUBLISH
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)
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)
struct mg_str mg_mk_str(const char *s) WEAK
void cs_base64_encode(const unsigned char *src, int src_len, char *dst)
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 *uri, const char *extra_headers)
void mg_printf_http_chunk(struct mg_connection *nc, const char *fmt,...)
#define MG_EV_MQTT_SUBSCRIBE
const char * mg_next_comma_list_entry(const char *, struct mg_str *, struct mg_str *) WEAK
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) WEAK
#define MG_MQTT_CMD_DISCONNECT
FILE * mg_fopen(const char *path, const char *mode)
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
int mg_vcmp(const struct mg_str *str1, const char *str2) WEAK
int mg_parse_uri(const 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]
mg_event_handler_t proto_handler
MG_INTERNAL 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) WEAK
int mg_dns_parse_record_data(struct mg_dns_message *msg, struct mg_dns_resource_record *rr, void *data, size_t data_len)
#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
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)
void mg_mqtt_pubrel(struct mg_connection *nc, uint16_t message_id)
void cs_log_set_file(FILE *file)
#define WEBSOCKET_OP_CONTINUE
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
static std::string q(const char *s)
#define message(type, str)
#define write(n, a, f, d)
static std::string remove(const std::string s, char c)
static void do_ssi_exec(struct mg_connection *conn, char *tag)
static const char * month_names[]
static struct mg_str mg_strdup_common(const struct mg_str s, int nul_terminate)
void mg_set_nameserver(struct mg_mgr *mgr, const char *nameserver)
#define MBUF_SIZE_MAX_HEADROOM
#define MG_AUTH_FLAG_IS_GLOBAL_PASS_FILE
#define DO_NOT_WARN_UNUSED
void mg_http_send_error(struct mg_connection *nc, int code, const char *reason)
int mg_get_http_basic_auth(struct http_message *hm, char *user, size_t user_len, char *pass, size_t pass_len)
int mg_parse_http_basic_auth(struct mg_str *hdr, char *user, size_t user_len, char *pass, size_t pass_len)
#define MG_AUTH_FLAG_IS_DIRECTORY
void mg_hash_sha1_v(size_t num_msgs, const uint8_t *msgs[], const size_t *msg_lens, uint8_t *digest)
void mg_send_websocket_handshake3v(struct mg_connection *nc, const struct mg_str path, const struct mg_str host, const struct mg_str protocol, const struct mg_str extra_headers, const struct mg_str user, const struct mg_str pass)
#define MG_ENABLE_HTTP_WEBDAV
#define MG_MK_STR_N(str_literal, len)
#define LIST_REMOVE(elm, field)
#define MG_EV_SSI_CALL_CTX
void mg_http_send_digest_auth_request(struct mg_connection *c, const char *domain)
#define PRINTF_LIKE(f, a)
#define MG_AUTH_FLAG_ALLOW_MISSING_FILE
#define MG_F_RECV_AND_CLOSE
void mg_http_send_redirect(struct mg_connection *nc, int status_code, const struct mg_str location, const struct mg_str extra_headers)
int mg_check_digest_auth(struct mg_str method, struct mg_str uri, struct mg_str username, struct mg_str cnonce, struct mg_str response, struct mg_str qop, struct mg_str nc, struct mg_str nonce, struct mg_str auth_domain, FILE *fp)
int mg_mqtt_match_topic_expression(struct mg_str exp, struct mg_str topic)
int mg_http_check_digest_auth(struct http_message *hm, const char *auth_domain, FILE *fp)
int mg_http_parse_header2(struct mg_str *hdr, const char *var_name, char **buf, size_t buf_size)
void mg_hash_md5_v(size_t num_msgs, const uint8_t *msgs[], const size_t *msg_lens, uint8_t *digest)
int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len)
#define MG_ENABLE_HTTP_STREAMING_MULTIPART
int mg_mqtt_vmatch_topic_expression(const char *exp, struct mg_str topic)
#define LIST_NEXT(elm, field)
void mg_send_websocket_handshake3(struct mg_connection *nc, const char *path, const char *host, const char *protocol, const char *extra_headers, const char *user, const char *pass)
#define LIST_INSERT_HEAD(head, elm, field)
int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg)
#define MG_F_ENABLE_BROADCAST
int mg_http_is_authorized(struct http_message *hm, struct mg_str path, const char *domain, const char *passwords_file, int flags)
void mg_register_http_endpoint_opt(struct mg_connection *nc, const char *uri_path, mg_event_handler_t handler, struct mg_http_endpoint_opts opts)
#define MG_COPY_COMMON_CONNECTION_OPTIONS(dst, src)
int mg_pic32_if_listen_udp(struct mg_connection *nc, union socket_address *sa)
void mg_pic32_if_udp_send(struct mg_connection *nc, const void *buf, size_t len)
void mg_socket_if_sock_set(struct mg_connection *nc, sock_t sock)
#define MD5STEP(f, w, x, y, z, data, s)
int _kill(int pid, int sig)
void cs_md5_final(unsigned char digest[16], cs_md5_ctx *ctx)
void mg_lwip_if_connect_udp(struct mg_connection *nc)
struct mg_connection * mg_bind(struct mg_mgr *srv, const char *address, MG_CB(mg_event_handler_t event_handler, void *user_data))
int mg_pic32_if_listen_tcp(struct mg_connection *nc, union socket_address *sa)
static int mg_accept_conn(struct mg_connection *lc)
static int mg_sl_if_listen_tcp(struct mg_connection *nc, union socket_address *sa)
void mg_socket_if_connect_udp(struct mg_connection *nc)
void mg_sl_if_sock_set(struct mg_connection *nc, sock_t sock)
static sock_t mg_open_listening_socket(union socket_address *sa, int type, int proto)
struct mg_str mg_strdup_nul(const struct mg_str s) WEAK
void cs_sha1_transform(uint32_t state[5], const unsigned char buffer[64])
int mg_strcmp(const struct mg_str str1, const struct mg_str str2) WEAK
MG_INTERNAL void mg_remove_conn(struct mg_connection *c)
void sl_restart_cb(struct mg_mgr *mgr)
void cs_md5_init(cs_md5_ctx *ctx)
struct mg_connection * mg_add_sock_opt(struct mg_mgr *s, sock_t sock, MG_CB(mg_event_handler_t callback, void *user_data), struct mg_add_sock_opts opts)
static int mg_sl_if_udp_recv(struct mg_connection *nc, void *buf, size_t len, union socket_address *sa, size_t *sa_len)
const struct mg_iface_vtable * mg_ifaces[]
double cs_timegm(const struct tm *tm)
static void mg_lwip_if_connect_tcp_tcpip(void *arg)
MG_INTERNAL struct mg_connection * mg_create_connection_base(struct mg_mgr *mgr, mg_event_handler_t callback, struct mg_add_sock_opts opts)
static int mg_socket_if_tcp_send(struct mg_connection *nc, const void *buf, size_t len)
#define MG_LWIP_IFACE_VTABLE
#define R1(v, w, x, y, z, i)
static int mg_do_recv(struct mg_connection *nc)
MG_INTERNAL struct mg_connection * mg_create_connection(struct mg_mgr *mgr, mg_event_handler_t callback, struct mg_add_sock_opts opts)
void mg_lwip_if_destroy_conn(struct mg_connection *nc)
static int mg_socket_if_udp_recv(struct mg_connection *nc, void *buf, size_t len, union socket_address *sa, size_t *sa_len)
void mg_pic32_if_tcp_send(struct mg_connection *nc, const void *buf, size_t len)
time_t mg_pic32_if_poll(struct mg_iface *iface, int timeout_ms)
static void mg_null_if_add_conn(struct mg_connection *c)
int mg_null_if_tcp_recv(struct mg_connection *c, void *buf, size_t len)
void mg_lwip_if_add_conn(struct mg_connection *nc)
static void mg_lwip_tcp_error_cb(void *arg, err_t err)
void mg_mbuf_append_base64_putc(char ch, void *user_data)
#define _MG_CALLBACK_MODIFIABLE_FLAGS_MASK
void mg_basic_auth_header(const struct mg_str user, const struct mg_str pass, struct mbuf *buf)
static unsigned char from_b64(unsigned char ch)
static void tcp_output_tcpip(void *arg)
bool mg_start_task(int priority, int stack_size, mg_init_cb mg_init)
double mg_mgr_min_timer(const struct mg_mgr *mgr)
static int c_itoa(char *buf, size_t buf_size, int64_t num, int base, int flags, int field_width)
static void mg_lwip_if_connect_udp_tcpip(void *arg)
static void mg_null_if_get_conn_addr(struct mg_connection *c, int remote, union socket_address *sa)
int mg_str_starts_with(struct mg_str s, struct mg_str prefix) WEAK
#define MG_UDP_RECV_BUFFER_SIZE
static int parse_net(const char *spec, uint32_t *net, uint32_t *mask)
struct mg_str mg_next_comma_list_entry_n(struct mg_str list, struct mg_str *val, struct mg_str *eq_val) WEAK
int mg_lwip_if_create_conn(struct mg_connection *nc)
time_t mg_lwip_if_poll(struct mg_iface *iface, int timeout_ms)
void mg_pic32_if_recved(struct mg_connection *nc, size_t len)
static void mg_null_if_free(struct mg_iface *iface)
void mg_pic32_if_add_conn(struct mg_connection *nc)
void mg_lwip_handle_accept(struct mg_connection *nc)
struct mg_str mg_url_encode(const struct mg_str src)
static int mg_sl_if_create_conn(struct mg_connection *nc)
int mg_asprintf(char **buf, size_t size, const char *fmt,...) WEAK
MG_INTERNAL size_t recv_avail_size(struct mg_connection *conn, size_t max)
static void mg_sl_if_connect_tcp(struct mg_connection *nc, const union socket_address *sa)
void mg_lwip_if_get_conn_addr(struct mg_connection *nc, int remote, union socket_address *sa)
void _not_implemented(const char *what)
size_t mbuf_append_and_free(struct mbuf *a, void *buf, size_t len) WEAK
static err_t mg_lwip_tcp_sent_cb(void *arg, struct tcp_pcb *tpcb, u16_t num_sent)
void mg_lwip_if_free(struct mg_iface *iface)
struct mg_str mg_url_encode_opt(const struct mg_str src, const struct mg_str safe, unsigned int flags)
int mg_strncmp(const struct mg_str, const struct mg_str, size_t n) WEAK
#define MG_CTL_MSG_MESSAGE_SIZE
int mg_if_poll(struct mg_connection *nc, double now)
static err_t mg_lwip_accept_cb(void *arg, struct tcp_pcb *newtpcb, err_t err)
static int mg_lwip_if_can_send(struct mg_connection *nc, struct mg_lwip_conn_state *cs)
void mg_socket_if_add_conn(struct mg_connection *nc)
void mg_sl_if_add_conn(struct mg_connection *nc)
void mg_sl_if_remove_conn(struct mg_connection *nc)
static void mg_handle_recv(struct mg_connection *nc)
static err_t mg_lwip_tcp_conn_cb(void *arg, struct tcp_pcb *tpcb, err_t err)
static int mg_lwip_if_udp_send(struct mg_connection *nc, const void *data, size_t len)
void mg_lwip_if_remove_conn(struct mg_connection *nc)
void mg_socket_if_get_conn_addr(struct mg_connection *nc, int remote, union socket_address *sa)
static time_t mg_null_if_poll(struct mg_iface *iface, int timeout_ms)
void mg_mgr_init_opt(struct mg_mgr *m, void *user_data, struct mg_mgr_init_opts opts)
static void cs_md5_transform(uint32_t buf[4], uint32_t const in[16])
static void udp_sendto_tcpip(void *arg)
void mg_socket_if_init(struct mg_iface *iface)
struct mg_connection * mg_connect(struct mg_mgr *mgr, const char *address, MG_CB(mg_event_handler_t callback, void *user_data))
static void cs_base64_emit_chunk(struct cs_base64_ctx *ctx)
#define MG_PIC32_IFACE_VTABLE
#define _MG_ALLOWED_CONNECT_FLAGS_MASK
static int mg_recv_tcp(struct mg_connection *nc, char *buf, size_t len)
enum cs_log_level cs_log_level WEAK
void mg_pic32_if_init(struct mg_iface *iface)
static void mg_null_if_init(struct mg_iface *iface)
static void mg_null_if_destroy_conn(struct mg_connection *c)
struct mg_str mg_strdup(const struct mg_str s) WEAK
static int mg_null_if_create_conn(struct mg_connection *c)
void mg_set_non_blocking_mode(sock_t sock)
void mbuf_clear(struct mbuf *mb) WEAK
static void mg_null_if_sock_set(struct mg_connection *c, sock_t sock)
void mg_socket_if_connect_tcp(struct mg_connection *nc, const union socket_address *sa)
void mg_socket_if_remove_conn(struct mg_connection *nc)
#define R2(v, w, x, y, z, i)
void mg_pic32_if_connect_udp(struct mg_connection *nc)
void mg_lwip_if_init(struct mg_iface *iface)
const struct mg_iface_vtable mg_default_iface_vtable
void mg_pic32_if_free(struct mg_iface *iface)
static void mg_lwip_recv_common(struct mg_connection *nc, struct pbuf *p)
void mg_if_can_recv_cb(struct mg_connection *nc)
#define CS_LOG_PREFIX_LEN
static int mg_lwip_if_udp_recv(struct mg_connection *nc, void *buf, size_t len, union socket_address *sa, size_t *sa_len)
static int mg_socket_if_udp_send(struct mg_connection *nc, const void *buf, size_t len)
static void mg_sock_get_addr(sock_t sock, int remote, union socket_address *sa)
MG_INTERNAL int mg_parse_address(const char *str, union socket_address *sa, int *proto, char *host, size_t host_len)
static int mg_sl_if_listen_udp(struct mg_connection *nc, union socket_address *sa)
void mg_ev_mgr_lwip_process_signals(struct mg_mgr *mgr)
void mg_mbuf_append_base64(struct mbuf *mbuf, const void *data, size_t len)
void mg_lwip_netif_run_on_tcpip(void(*fn)(void *), void *arg)
#define _MG_F_FD_CAN_READ
static void byteReverse(unsigned char *buf, unsigned longs)
#define MG_SL_IFACE_VTABLE
static void mg_null_if_connect_udp(struct mg_connection *c)
#define R0(v, w, x, y, z, i)
static void mg_sl_if_connect_udp(struct mg_connection *nc)
static int mg_sl_if_tcp_recv(struct mg_connection *nc, void *buf, size_t len)
static void parse_uri_component(const char **p, const char *end, const char *seps, struct mg_str *res)
#define MG_SET_PTRPTR(_ptr, _v)
int cs_base64_decode(const unsigned char *s, int len, char *dst, int *dec_len)
static void tcp_close_tcpip(void *arg)
const struct mg_iface_vtable mg_simplelink_iface_vtable
static int mg_null_if_listen_tcp(struct mg_connection *c, union socket_address *sa)
int mg_assemble_uri(const struct mg_str *scheme, const struct mg_str *user_info, const struct mg_str *host, unsigned int port, const struct mg_str *path, const struct mg_str *query, const struct mg_str *fragment, int normalize_path, struct mg_str *uri)
void mg_pic32_if_destroy_conn(struct mg_connection *nc)
void mg_lwip_if_sock_set(struct mg_connection *nc, sock_t sock)
struct mg_iface * mg_if_create_iface(const struct mg_iface_vtable *vtable, struct mg_mgr *mgr)
static sys_sem_t s_tcpip_call_lock_sem
static int mg_socket_if_listen_udp(struct mg_connection *nc, union socket_address *sa)
static err_t mg_lwip_tcp_recv_cb(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
struct mg_connection * mg_bind_opt(struct mg_mgr *mgr, const char *address, MG_CB(mg_event_handler_t callback, void *user_data), struct mg_bind_opts opts)
const char * mg_strstr(const struct mg_str haystack, const struct mg_str needle) WEAK
void mg_lwip_post_signal(enum mg_sig_type sig, struct mg_connection *nc)
static void cs_base64_emit_code(struct cs_base64_ctx *ctx, int v)
const struct mg_iface_vtable mg_pic32_iface_vtable
static int mg_socket_if_tcp_recv(struct mg_connection *nc, void *buf, size_t len)
static sys_sem_t s_tcpip_call_sync_sem
static void mg_lwip_tcp_write_tcpip(void *arg)
char * inet_ntoa(struct in_addr n)
void mg_forward(struct mg_connection *from, struct mg_connection *to)
void cs_from_hex(char *to, const char *p, size_t len) WEAK
struct mg_str mg_strstrip(struct mg_str s) WEAK
static int mg_null_if_tcp_send(struct mg_connection *c, const void *buf, size_t len)
static int str_util_lowercase(const char *s)
static int mg_lwip_if_tcp_recv(struct mg_connection *nc, void *buf, size_t len)
static int mg_null_if_udp_send(struct mg_connection *c, const void *buf, size_t len)
static void mg_task(void *arg)
void mg_sl_if_get_conn_addr(struct mg_connection *nc, int remote, union socket_address *sa)
void mg_sl_if_destroy_conn(struct mg_connection *nc)
const char * mg_strchr(const struct mg_str s, int c) WEAK
static void mg_lwip_if_listen_tcp_tcpip(void *arg)
void mbuf_move(struct mbuf *from, struct mbuf *to) WEAK
int mg_null_if_udp_recv(struct mg_connection *c, void *buf, size_t len, union socket_address *sa, size_t *sa_len)
int mg_pic32_if_create_conn(struct mg_connection *nc)
void mg_pic32_if_get_conn_addr(struct mg_connection *nc, int remote, union socket_address *sa)
static int fourbit(int ch)
#define C_SNPRINTF_FLAG_ZERO
struct mg_str mg_mk_str_n(const char *s, size_t len) WEAK
#define BASE64_ENCODE_BODY
void mg_pic32_if_sock_set(struct mg_connection *nc, sock_t sock)
#define R3(v, w, x, y, z, i)
int mg_lwip_if_listen_tcp(struct mg_connection *nc, union socket_address *sa)
#define MG_SOCKET_IFACE_VTABLE
int mg_socket_if_listen_tcp(struct mg_connection *nc, union socket_address *sa)
void mg_sl_if_init(struct mg_iface *iface)
static void mg_null_if_remove_conn(struct mg_connection *c)
const struct mg_iface_vtable mg_socket_iface_vtable
const char * inet_ntop(int af, const void *src, char *dst, socklen_t size)
int gettimeofday(struct timeval *tp, void *tzp)
void cs_md5_update(cs_md5_ctx *ctx, const unsigned char *buf, size_t len)
struct mg_iface * mg_find_iface(struct mg_mgr *mgr, const struct mg_iface_vtable *vtable, struct mg_iface *from)
static void mg_handle_send(struct mg_connection *nc)
void cs_log_set_file_level(const char *file_level)
#define SET_ADDR(dst, src)
static void udp_remove_tcpip(void *arg)
static void mg_lwip_if_listen_udp_tcpip(void *arg)
void mg_sl_if_free(struct mg_iface *iface)
MG_INTERNAL void mg_add_conn(struct mg_mgr *mgr, struct mg_connection *c)
int inet_pton(int af, const char *src, void *dst)
int mg_lwip_if_listen_udp(struct mg_connection *nc, union socket_address *sa)
#define _MG_F_FD_CAN_WRITE
static int mg_sl_if_tcp_send(struct mg_connection *nc, const void *buf, size_t len)
MG_INTERNAL struct mg_connection * mg_do_connect(struct mg_connection *nc, int proto, union socket_address *sa)
void mg_ev_handler_empty(struct mg_connection *c, int ev, void *ev_data MG_UD_ARG(void *user_data))
void mg_socket_if_free(struct mg_iface *iface)
void mg_if_can_send_cb(struct mg_connection *nc)
static int mg_sl_if_udp_send(struct mg_connection *nc, const void *buf, size_t len)
static void mg_null_if_connect_tcp(struct mg_connection *c, const union socket_address *sa)
static void xxx_tcpip(void *arg)
void mg_pic32_if_connect_tcp(struct mg_connection *nc, const union socket_address *sa)
static struct mg_str mg_strdup_common(const struct mg_str s, int nul_terminate)
void mg_lwip_mgr_schedule_poll(struct mg_mgr *mgr)
void mg_strfree(struct mg_str *s) WEAK
static void mg_lwip_udp_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
void mg_add_to_set(sock_t sock, fd_set *set, sock_t *max_fd)
const struct mg_iface_vtable mg_lwip_iface_vtable
void fprint_str(FILE *fp, const char *str)
struct mg_connection * mg_connect_opt(struct mg_mgr *mgr, const char *address, MG_CB(mg_event_handler_t callback, void *user_data), struct mg_connect_opts opts)
int mg_lwip_if_tcp_send(struct mg_connection *nc, const void *buf, size_t len)
void mg_destroy_conn(struct mg_connection *conn, int destroy_if)
void mg_lwip_if_connect_tcp(struct mg_connection *nc, const union socket_address *sa)
time_t mg_sl_if_poll(struct mg_iface *iface, int timeout_ms)
MG_INTERNAL int mg_get_errno(void)
int mg_socket_if_create_conn(struct mg_connection *nc)
int cs_log_print_prefix(enum cs_log_level level, const char *fname, int line)
static int mg_is_error(void)
static uint32_t blk0(union char64long16 *block, int i)
MG_INTERNAL void mg_call(struct mg_connection *nc, mg_event_handler_t ev_handler, void *user_data, int ev, void *ev_data)
static int mg_recv_udp(struct mg_connection *nc, char *buf, size_t len)
#define C_SNPRINTF_APPEND_CHAR(ch)
void mg_socket_if_destroy_conn(struct mg_connection *nc)
void mg_run_in_task(void(*cb)(struct mg_mgr *mgr, void *arg), void *cb_arg)
static int mg_null_if_listen_udp(struct mg_connection *c, union socket_address *sa)
void mg_pic32_if_remove_conn(struct mg_connection *nc)
#define R4(v, w, x, y, z, i)
#define MG_NULL_IFACE_VTABLE
void mg_mgr_handle_conn(struct mg_connection *nc, int fd_flags, double now)
time_t mg_socket_if_poll(struct mg_iface *iface, int timeout_ms)
struct mg_connection * mg_add_sock(struct mg_mgr *s, sock_t sock, MG_CB(mg_event_handler_t callback, void *user_data))
MG_INTERNAL void mg_timer(struct mg_connection *c, double now)
void tcp_recved_tcpip(void *arg)
struct mg_str mg_url_encode_opt(const struct mg_str src, const struct mg_str safe, unsigned int flags)
const char * mg_strchr(const struct mg_str s, int c)
struct mg_str mg_mk_str_n(const char *s, size_t len)
#define MG_URL_ENCODE_F_UPPERCASE_HEX
#define MG_URL_ENCODE_F_SPACE_AS_PLUS
static size_t mg_http_parse_chunk(char *buf, size_t len, char **chunk_data, size_t *chunk_len)
mg_http_multipart_stream_state
@ MPS_WAITING_FOR_BOUNDARY
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)
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 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 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)
static void mg_resolve_async_eh(struct mg_connection *nc, int ev, void *data)
static const struct @23 mg_static_builtin_mime_types[]
#define MIME_ENTRY(_ext, _type)
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_http_construct_etag(char *buf, size_t buf_len, const cs_stat_t *st)
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 mg_event_handler_t mg_http_get_endpoint_handler(struct mg_connection *nc, struct mg_str *uri_path)
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_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_print_dir_entry(struct mg_connection *nc, const char *file_name, cs_stat_t *stp)
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 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 void mg_escape(const char *src, char *dst, size_t dst_len)
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 mg_get_month_index(const char *s)
static uint32_t mg_ws_random_mask(void)
static void mg_http_free_proto_data_file(struct mg_http_proto_data_file *d)
static int mg_get_ip_address_of_nameserver(char *name, size_t name_len)
static int mg_is_dav_request(const struct mg_str *s)
static unsigned char * mg_parse_dns_resource_record(unsigned char *data, unsigned char *end, struct mg_dns_resource_record *rr, int reply)
static void mg_mgr_handle_ctl_sock(struct mg_mgr *mgr)
static char * mg_addenv(struct mg_cgi_env_block *block, const char *fmt,...)
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)
static void mg_handle_incoming_websocket_frame(struct mg_connection *nc, struct websocket_message *wsm)
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)
static void mg_addenv2(struct mg_cgi_env_block *blk, const char *name)
static void mg_send_ws_header(struct mg_connection *nc, int op, size_t len, struct ws_mask_ctx *ctx)
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)
static pid_t mg_start_process(const char *interp, const char *cmd, const char *env, const char *envp[], const char *dir, sock_t sock)
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)
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)
static size_t mg_get_line_len(const char *buf, size_t buf_len)
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)
void mg_http_handler(struct mg_connection *nc, int ev, void *ev_data)
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)
static void mg_handle_move(struct mg_connection *c, const struct mg_serve_http_opts *opts, const char *path, struct http_message *hm)
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)
static void mg_gmt_time_string(char *buf, size_t buf_len, time_t *t)
static void mg_http_send_options(struct mg_connection *nc)
#define MG_DEFAULT_NAMESERVER
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)
static int mg_num_leap_years(int year)
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
const char * vars[MG_MAX_CGI_ENVIR_VARS]
struct mg_connection * nc
char buf[MG_CGI_ENVIRONMENT_SIZE]
struct mg_ev_mgr_lwip_signal sig_queue[MG_SIG_QUEUE_LEN]
struct mg_connection * nc
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
time_t(* poll)(struct mg_iface *iface, int timeout_ms)
void(* destroy_conn)(struct mg_connection *nc)
int(* listen_tcp)(struct mg_connection *nc, union socket_address *sa)
void(* remove_conn)(struct mg_connection *nc)
void(* connect_tcp)(struct mg_connection *nc, const union socket_address *sa)
void(* free)(struct mg_iface *iface)
void(* init)(struct mg_iface *iface)
void(* get_conn_addr)(struct mg_connection *nc, int remote, union socket_address *sa)
void(* connect_udp)(struct mg_connection *nc)
int(* udp_send)(struct mg_connection *nc, const void *buf, size_t len)
int(* create_conn)(struct mg_connection *nc)
int(* listen_udp)(struct mg_connection *nc, union socket_address *sa)
void(* sock_set)(struct mg_connection *nc, sock_t sock)
int(* udp_recv)(struct mg_connection *nc, void *buf, size_t len, union socket_address *sa, size_t *sa_len)
int(* tcp_send)(struct mg_connection *nc, const void *buf, size_t len)
int(* tcp_recv)(struct mg_connection *nc, void *buf, size_t len)
void(* add_conn)(struct mg_connection *nc)
const struct mg_iface_vtable * vtable
struct mg_connection * lc
union mg_lwip_conn_state::@24 pcb
struct mg_connection * nc
const union socket_address * sa
struct mg_connection * nc
struct mg_connection * nc
union socket_address * sa
struct mg_connection * nc
struct mg_iface ** ifaces
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)