3#line 1 "mongoose/src/mg_internal.h"
10#ifndef CS_MONGOOSE_SRC_INTERNAL_H_
11#define CS_MONGOOSE_SRC_INTERNAL_H_
16#define MBUF_REALLOC MG_REALLOC
20#define MBUF_FREE MG_FREE
23#define MG_SET_PTRPTR(_ptr, _v) \
25 if (_ptr) *(_ptr) = _v; \
29#define MG_INTERNAL static
41#ifndef MG_CTL_MSG_MESSAGE_SIZE
42#define MG_CTL_MSG_MESSAGE_SIZE 8192
51 int *proto,
char *host,
size_t host_len);
74#define MG_MQTT_ERROR_INCOMPLETE_MSG -1
75#define MG_MQTT_ERROR_MALFORMED_MSG -2
81extern void *(*test_malloc)(
size_t size);
82extern void *(*test_calloc)(
size_t count,
size_t size);
85#define MIN(a, b) ((a) < (b) ? (a) : (b))
106#if MG_ENABLE_FILESYSTEM
114#if MG_ENABLE_HTTP_CGI
116 const struct mg_str *path_info,
122#if MG_ENABLE_HTTP_SSI
128#if MG_ENABLE_HTTP_WEBDAV
145#if MG_ENABLE_HTTP_WEBSOCKET
164#ifdef MG_MODULE_LINES
165#line 1 "common/mg_mem.h"
184#ifndef CS_COMMON_MG_MEM_H_
185#define CS_COMMON_MG_MEM_H_
192#define MG_MALLOC malloc
196#define MG_CALLOC calloc
200#define MG_REALLOC realloc
212#ifdef MG_MODULE_LINES
213#line 1 "common/cs_base64.c"
232#ifndef EXCLUDE_COMMON
242#define NUM_UPPERCASES ('Z' - 'A' + 1)
243#define NUM_LETTERS (NUM_UPPERCASES * 2)
244#define NUM_DIGITS ('9' - '0' + 1)
289 const unsigned char *src = (
const unsigned char *)
str;
291 for (
i = 0;
i < len;
i++) {
311#define BASE64_ENCODE_BODY \
312 static const char *b64 = \
313 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; \
316 for (i = j = 0; i < src_len; i += 3) { \
318 b = i + 1 >= src_len ? 0 : src[i + 1]; \
319 c = i + 2 >= src_len ? 0 : src[i + 2]; \
321 BASE64_OUT(b64[a >> 2]); \
322 BASE64_OUT(b64[((a & 3) << 4) | (b >> 4)]); \
323 if (i + 1 < src_len) { \
324 BASE64_OUT(b64[(b & 15) << 2 | (c >> 6)]); \
326 if (i + 2 < src_len) { \
327 BASE64_OUT(b64[c & 63]); \
331 while (j % 4 != 0) { \
336#define BASE64_OUT(ch) \
341#define BASE64_FLUSH() \
354#define BASE64_OUT(ch) \
356 fprintf(f, "%c", (ch)); \
360#define BASE64_FLUSH()
373 static const unsigned char tab[128] = {
407 return tab[
ch & 127];
411 unsigned char a, b,
c,
d;
414 while (len >= 4 && (a =
from_b64(s[0])) != 255 &&
419 if (a == 200 || b == 200)
break;
432#ifdef MG_MODULE_LINES
433#line 1 "common/cs_dbg.h"
452#ifndef CS_COMMON_CS_DBG_H_
453#define CS_COMMON_CS_DBG_H_
461#ifndef CS_ENABLE_DEBUG
462#define CS_ENABLE_DEBUG 0
465#ifndef CS_LOG_PREFIX_LEN
466#define CS_LOG_PREFIX_LEN 24
469#ifndef CS_LOG_ENABLE_TS_DIFF
470#define CS_LOG_ENABLE_TS_DIFF 0
547 if (cs_log_print_prefix(l, __FILE__, __LINE__)) { \
554#define LOG(l, x) ((void) l)
563#define DBG(x) LOG(LL_VERBOSE_DEBUG, x)
583#ifdef MG_MODULE_LINES
584#line 1 "common/cs_dbg.c"
626#if CS_LOG_ENABLE_TS_DIFF
646 size_t fl = 0, ll = 0,
pl = 0;
653 const char c = *(p - 1);
654 if (
c ==
'/' ||
c ==
'\\')
break;
659 ll = (
ln < 10000 ? (
ln < 1000 ? (
ln < 100 ? (
ln < 10 ? 1 : 2) : 3) : 4) : 5);
667 *(--
q) =
'0' + (
ln % 10);
688#if CS_LOG_ENABLE_TS_DIFF
725#if CS_LOG_ENABLE_TS_DIFF && CS_ENABLE_STDIO
729#ifdef MG_MODULE_LINES
730#line 1 "common/cs_dirent.h"
749#ifndef CS_COMMON_CS_DIRENT_H_
750#define CS_COMMON_CS_DIRENT_H_
760#ifdef CS_DEFINE_DIRENT
783#ifdef MG_MODULE_LINES
784#line 1 "common/cs_dirent.c"
803#ifndef EXCLUDE_COMMON
836 dir->result.d_name[0] =
'\0';
852 result =
FindClose(dir->handle) ? 0 : -1;
867 memset(&dir->result, 0,
sizeof(dir->result));
869 result = &dir->result;
871 result->d_name,
sizeof(result->d_name),
NULL,
894#ifdef MG_MODULE_LINES
895#line 1 "common/cs_time.c"
921#if !(defined(__ARMCC_VERSION) || defined(__ICCARM__)) && \
922 !defined(__TI_COMPILER_VERSION__) && \
923 (!defined(CS_PLATFORM) || CS_PLATFORM != CS_P_NXP_LPC)
936 now = (
double)
tv.tv_sec + (((
double)
tv.tv_usec) / 1000000.0);
960 static const int month_day[12] = {0, 31, 59, 90, 120, 151,
961 181, 212, 243, 273, 304, 334};
965 int year =
tm->tm_year +
tm->tm_mon / 12;
992#ifdef MG_MODULE_LINES
993#line 1 "common/cs_endian.h"
1012#ifndef CS_COMMON_CS_ENDIAN_H_
1013#define CS_COMMON_CS_ENDIAN_H_
1024#if !defined(BYTE_ORDER) && defined(__BYTE_ORDER)
1025#define BYTE_ORDER __BYTE_ORDER
1026#ifndef LITTLE_ENDIAN
1027#define LITTLE_ENDIAN __LITTLE_ENDIAN
1030#define BIG_ENDIAN __LITTLE_ENDIAN
1039#ifdef MG_MODULE_LINES
1040#line 1 "common/cs_md5.c"
1062#if !defined(EXCLUDE_COMMON)
1069#if BYTE_ORDER == BIG_ENDIAN
1072 ((
unsigned) buf[1] << 8 | buf[0]);
1082#define F1(x, y, z) (z ^ (x & (y ^ z)))
1083#define F2(x, y, z) F1(z, x, y)
1084#define F3(x, y, z) (x ^ y ^ z)
1085#define F4(x, y, z) (y ^ (x | ~z))
1087#define MD5STEP(f, w, x, y, z, data, s) \
1088 (w += f(x, y, z) + data, w = w << s | w >> (32 - s), w += x)
1095 ctx->
buf[0] = 0x67452301;
1096 ctx->
buf[1] = 0xefcdab89;
1097 ctx->
buf[2] = 0x98badcfe;
1098 ctx->
buf[3] = 0x10325476;
1193 t = (t >> 3) & 0x3f;
1196 unsigned char *p = (
unsigned char *) ctx->
in + t;
1242 a[14] = ctx->
bits[0];
1243 a[15] = ctx->
bits[1];
1248 memset((
char *) ctx, 0,
sizeof(*ctx));
1253#ifdef MG_MODULE_LINES
1254#line 1 "common/cs_sha1.c"
1261#if !CS_DISABLE_SHA1 && !defined(EXCLUDE_COMMON)
1271 unsigned char c[64];
1275#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
1279#if BYTE_ORDER == LITTLE_ENDIAN
1295 (block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] ^ \
1296 block->l[(i + 2) & 15] ^ block->l[i & 15], \
1298#define R0(v, w, x, y, z, i) \
1299 z += ((w & (x ^ y)) ^ y) + blk0(block, i) + 0x5A827999 + rol(v, 5); \
1301#define R1(v, w, x, y, z, i) \
1302 z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
1304#define R2(v, w, x, y, z, i) \
1305 z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
1307#define R3(v, w, x, y, z, i) \
1308 z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
1310#define R4(v, w, x, y, z, i) \
1311 z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
1324 R0(a, b,
c,
d,
e, 0);
1325 R0(
e, a, b,
c,
d, 1);
1326 R0(
d,
e, a, b,
c, 2);
1327 R0(
c,
d,
e, a, b, 3);
1328 R0(b,
c,
d,
e, a, 4);
1329 R0(a, b,
c,
d,
e, 5);
1330 R0(
e, a, b,
c,
d, 6);
1331 R0(
d,
e, a, b,
c, 7);
1332 R0(
c,
d,
e, a, b, 8);
1333 R0(b,
c,
d,
e, a, 9);
1334 R0(a, b,
c,
d,
e, 10);
1335 R0(
e, a, b,
c,
d, 11);
1336 R0(
d,
e, a, b,
c, 12);
1337 R0(
c,
d,
e, a, b, 13);
1338 R0(b,
c,
d,
e, a, 14);
1339 R0(a, b,
c,
d,
e, 15);
1340 R1(
e, a, b,
c,
d, 16);
1341 R1(
d,
e, a, b,
c, 17);
1342 R1(
c,
d,
e, a, b, 18);
1343 R1(b,
c,
d,
e, a, 19);
1344 R2(a, b,
c,
d,
e, 20);
1345 R2(
e, a, b,
c,
d, 21);
1346 R2(
d,
e, a, b,
c, 22);
1347 R2(
c,
d,
e, a, b, 23);
1348 R2(b,
c,
d,
e, a, 24);
1349 R2(a, b,
c,
d,
e, 25);
1350 R2(
e, a, b,
c,
d, 26);
1351 R2(
d,
e, a, b,
c, 27);
1352 R2(
c,
d,
e, a, b, 28);
1353 R2(b,
c,
d,
e, a, 29);
1354 R2(a, b,
c,
d,
e, 30);
1355 R2(
e, a, b,
c,
d, 31);
1356 R2(
d,
e, a, b,
c, 32);
1357 R2(
c,
d,
e, a, b, 33);
1358 R2(b,
c,
d,
e, a, 34);
1359 R2(a, b,
c,
d,
e, 35);
1360 R2(
e, a, b,
c,
d, 36);
1361 R2(
d,
e, a, b,
c, 37);
1362 R2(
c,
d,
e, a, b, 38);
1363 R2(b,
c,
d,
e, a, 39);
1364 R3(a, b,
c,
d,
e, 40);
1365 R3(
e, a, b,
c,
d, 41);
1366 R3(
d,
e, a, b,
c, 42);
1367 R3(
c,
d,
e, a, b, 43);
1368 R3(b,
c,
d,
e, a, 44);
1369 R3(a, b,
c,
d,
e, 45);
1370 R3(
e, a, b,
c,
d, 46);
1371 R3(
d,
e, a, b,
c, 47);
1372 R3(
c,
d,
e, a, b, 48);
1373 R3(b,
c,
d,
e, a, 49);
1374 R3(a, b,
c,
d,
e, 50);
1375 R3(
e, a, b,
c,
d, 51);
1376 R3(
d,
e, a, b,
c, 52);
1377 R3(
c,
d,
e, a, b, 53);
1378 R3(b,
c,
d,
e, a, 54);
1379 R3(a, b,
c,
d,
e, 55);
1380 R3(
e, a, b,
c,
d, 56);
1381 R3(
d,
e, a, b,
c, 57);
1382 R3(
c,
d,
e, a, b, 58);
1383 R3(b,
c,
d,
e, a, 59);
1384 R4(a, b,
c,
d,
e, 60);
1385 R4(
e, a, b,
c,
d, 61);
1386 R4(
d,
e, a, b,
c, 62);
1387 R4(
c,
d,
e, a, b, 63);
1388 R4(b,
c,
d,
e, a, 64);
1389 R4(a, b,
c,
d,
e, 65);
1390 R4(
e, a, b,
c,
d, 66);
1391 R4(
d,
e, a, b,
c, 67);
1392 R4(
c,
d,
e, a, b, 68);
1393 R4(b,
c,
d,
e, a, 69);
1394 R4(a, b,
c,
d,
e, 70);
1395 R4(
e, a, b,
c,
d, 71);
1396 R4(
d,
e, a, b,
c, 72);
1397 R4(
c,
d,
e, a, b, 73);
1398 R4(b,
c,
d,
e, a, 74);
1399 R4(a, b,
c,
d,
e, 75);
1400 R4(
e, a, b,
c,
d, 76);
1401 R4(
d,
e, a, b,
c, 77);
1402 R4(
c,
d,
e, a, b, 78);
1403 R4(b,
c,
d,
e, a, 79);
1412 a = b =
c =
d =
e = 0;
1421 context->
state[0] = 0x67452301;
1422 context->
state[1] = 0xEFCDAB89;
1423 context->
state[2] = 0x98BADCFE;
1424 context->
state[3] = 0x10325476;
1425 context->
state[4] = 0xC3D2E1F0;
1434 if ((context->
count[0] += len << 3) <
j) context->
count[1]++;
1435 context->
count[1] += (len >> 29);
1437 if ((
j + len) > 63) {
1440 for (;
i + 63 < len;
i += 64) {
1453 for (
i = 0;
i < 8;
i++) {
1455 ((3 - (
i & 3)) * 8)) &
1460 while ((context->
count[0] & 504) != 448) {
1465 for (
i = 0;
i < 20;
i++) {
1467 (
unsigned char) ((context->
state[
i >> 2] >> ((3 - (
i & 3)) * 8)) & 255);
1469 memset(context,
'\0',
sizeof(*context));
1475 unsigned char out[20]) {
1492 for (
i = 0;
i <
sizeof(
buf1);
i++) {
1509#ifdef MG_MODULE_LINES
1510#line 1 "common/mbuf.c"
1529#ifndef EXCLUDE_COMMON
1536#define MBUF_REALLOC realloc
1540#define MBUF_FREE free
1587 if (~(
size_t) 0 - (
size_t) a->
buf < len)
return 0;
1663#ifdef MG_MODULE_LINES
1664#line 1 "common/mg_str.c"
1695 struct mg_str ret = {s, 0};
1754 for (
i = 0;
i < s.
len;
i++) {
1755 if (s.
p[
i] ==
c)
return &s.
p[
i];
1768 if (
i <
str1.len)
return 1;
1769 if (
i <
str2.len)
return -1;
1789 char *
sp = (
char *) s->
p;
1827#ifdef MG_MODULE_LINES
1828#line 1 "common/str_util.c"
1847#ifndef EXCLUDE_COMMON
1853#ifndef C_DISABLE_BUILTIN_SNPRINTF
1854#define C_DISABLE_BUILTIN_SNPRINTF 0
1862 for (; l <
maxlen && s[l] !=
'\0'; l++) {
1867#define C_SNPRINTF_APPEND_CHAR(ch) \
1869 if (i < (int) buf_size) buf[i] = ch; \
1873#define C_SNPRINTF_FLAG_ZERO 1
1875#if C_DISABLE_BUILTIN_SNPRINTF
1884 int i = 0,
k = 0,
neg = 0;
1926 while ((
ch = *fmt++) !=
'\0') {
1946 while (*fmt >=
'0' && *fmt <=
'9') {
1963 while (*fmt >=
'0' && *fmt <=
'9') {
1994 const char *s =
va_arg(
ap,
const char *);
1997 for (
j = 0;
j <
pad;
j++) {
2008 }
else if (
ch ==
'c') {
2014 }
else if (
ch ==
'd' &&
len_mod ==
'l') {
2018 }
else if (
ch ==
'd' &&
len_mod ==
'z') {
2022 }
else if (
ch ==
'd' &&
len_mod ==
'q') {
2025 }
else if ((
ch ==
'x' ||
ch ==
'u') &&
len_mod == 0) {
2028 }
else if ((
ch ==
'x' ||
ch ==
'u') &&
len_mod ==
'l') {
2031 }
else if ((
ch ==
'x' ||
ch ==
'u') &&
len_mod ==
'z') {
2034 }
else if (
ch ==
'p') {
2053 buf[
i < (
int) buf_size ?
i : (
int) buf_size - 1] =
'\0';
2060int c_snprintf(
char *buf,
size_t buf_size,
const char *fmt, ...)
WEAK;
2075 strncpy(buf, path,
sizeof(buf));
2076 buf[
sizeof(buf) - 1] =
'\0';
2080 while (
p > buf &&
p[-1] !=
':' && (
p[0] ==
'\\' ||
p[0] ==
'/')) *
p-- =
'\0';
2121char *
strdup(
const char *src) {
2133 static const char *
hex =
"0123456789abcdef";
2135 for (;
len--;
p++) {
2137 *
to++ =
hex[
p[0] & 0x0f];
2143 if (
ch >=
'0' &&
ch <=
'9') {
2145 }
else if (
ch >=
'a' &&
ch <=
'f') {
2146 return ch -
'a' + 10;
2147 }
else if (
ch >=
'A' &&
ch <=
'F') {
2148 return ch -
'A' + 10;
2157 for (
i = 0;
i <
len;
i += 2) {
2168 while (*s &&
isspace((
unsigned char) *s)) s++;
2173 while (
isdigit((
unsigned char) *s)) {
2175 result += (*s -
'0');
2178 return result *
neg;
2183 return tolower(*(
const unsigned char *) s);
2192 }
while (
diff == 0 &&
s1[-1] !=
'\0' && --
len > 0);
2247 }
else if (
len >= (
int) size) {
2273 if (
list.len == 0) {
2310 size_t res = 0,
len = 0,
i = 0,
j = 0;
2322 for (;
i < pattern.
len &&
j <
str.len;
i++,
j++) {
2323 if (pattern.
p[
i] ==
'?') {
2325 }
else if (pattern.
p[
i] ==
'*') {
2327 if (
i < pattern.
len && pattern.
p[
i] ==
'*') {
2334 if (
i == pattern.
len || (pattern.
p[
i] ==
'$' &&
i == pattern.
len - 1))
2340 }
while (
res == 0 &&
len != 0 &&
len-- > 0);
2347 if (
i < pattern.
len && pattern.
p[
i] ==
'$') {
2348 return j ==
str.len ?
str.len : 0;
2350 return i == pattern.
len ?
j : 0;
2362#ifdef MG_MODULE_LINES
2363#line 1 "mongoose/src/mg_net.c"
2389#define MG_MAX_HOST_LEN 200
2391#ifndef MG_TCP_IO_SIZE
2392#define MG_TCP_IO_SIZE 1460
2394#ifndef MG_UDP_IO_SIZE
2395#define MG_UDP_IO_SIZE 1460
2398#define MG_COPY_COMMON_CONNECTION_OPTIONS(dst, src) \
2399 memcpy(dst, src, sizeof(*dst));
2402#define _MG_ALLOWED_CONNECT_FLAGS_MASK \
2403 (MG_F_USER_1 | MG_F_USER_2 | MG_F_USER_3 | MG_F_USER_4 | MG_F_USER_5 | \
2404 MG_F_USER_6 | MG_F_WEBSOCKET_NO_DEFRAG | MG_F_ENABLE_BROADCAST)
2406#define _MG_CALLBACK_MODIFIABLE_FLAGS_MASK \
2407 (MG_F_USER_1 | MG_F_USER_2 | MG_F_USER_3 | MG_F_USER_4 | MG_F_USER_5 | \
2408 MG_F_USER_6 | MG_F_WEBSOCKET_NO_DEFRAG | MG_F_SEND_AND_CLOSE | \
2409 MG_F_CLOSE_IMMEDIATELY | MG_F_IS_WEBSOCKET | MG_F_DELETE_CHUNK)
2412#define intptr_t long
2416 DBG((
"%p %p", mgr,
c));
2446 DBG((
"%p %s ev=%d ev_data=%p flags=0x%lx rmbl=%d smbl=%d", nc,
2451#if !defined(NO_LIBC) && MG_ENABLE_HEXDUMP
2468 DBG((
"%p after %s flags=0x%lx rmbl=%d smbl=%d", nc,
2472#if !MG_ENABLE_CALLBACK_USERDATA
2478 if (
c->ev_timer_time > 0 &&
now >=
c->ev_timer_time) {
2480 c->ev_timer_time = 0;
2542 memset(conn, 0,
sizeof(*conn));
2578 memset(m, 0,
sizeof(*m));
2579#if MG_ENABLE_BROADCAST
2589#elif defined(__unix__)
2597 if (
opts.num_ifaces == 0) {
2607 for (
i = 0;
i <
opts.num_ifaces;
i++) {
2615 DBG((
"=================================="));
2616 DBG((
"init mgr=%p", m));
2632 if (m ==
NULL)
return;
2636#if MG_ENABLE_BROADCAST
2693#if MG_ENABLE_SYNC_RESOLVER
2696#if MG_ENABLE_GETADDRINFO
2725int mg_resolve(
const char *host,
char *buf,
size_t n) {
2788 int *proto,
char *host,
size_t host_len) {
2789 unsigned int a, b,
c,
d, port = 0;
2812 if (
sscanf(
str,
"%u.%u.%u.%u:%u%n", &a, &b, &
c, &
d, &port, &len) == 5) {
2814 sa->
sin.sin_addr.s_addr =
2818 }
else if (
sscanf(
str,
"[%99[^]]]:%u%n",
buf, &port, &len) == 2 &&
2824#if MG_ENABLE_ASYNC_RESOLVER
2826 sscanf(
str,
"%[^ :]:%u%n", host, &port, &len) == 2) {
2839#if MG_ENABLE_SYNC_RESOLVER
2848 }
else if (
sscanf(
str,
":%u%n", &port, &len) == 1 ||
2849 sscanf(
str,
"%u%n", &port, &len) == 1) {
2861 return port < 0xffffUL && (
ch ==
'\0' ||
ch ==
',' ||
isspace(
ch)) ? len : -1;
2978 DBG((
"%p <- %d bytes (SSL)", nc,
n));
2996 DBG((
"%p <- %d bytes", nc,
n));
3001#if !defined(NO_LIBC) && MG_ENABLE_HEXDUMP
3076#if !defined(NO_LIBC) && MG_ENABLE_HEXDUMP
3108 DBG((
"%p -> %d bytes (SSL)", nc,
n));
3131 DBG((
"%p -> %d bytes", nc,
n));
3134#if !defined(NO_LIBC) && MG_ENABLE_HEXDUMP
3173 (
"%p %s://%s:%hu -> %d", nc, (nc->
flags &
MG_F_UDP ?
"udp" :
"tcp"),
3189#if MG_ENABLE_ASYNC_RESOLVER
3269#if MG_ENABLE_CALLBACK_USERDATA
3277 (
"%p %s %s,%s,%s", nc, address, (
opts.ssl_cert ?
opts.ssl_cert :
"-"),
3278 (
opts.ssl_key ?
opts.ssl_key :
"-"),
3279 (
opts.ssl_ca_cert ?
opts.ssl_ca_cert :
"-")));
3290 memset(¶ms, 0,
sizeof(params));
3291 params.cert =
opts.ssl_cert;
3292 params.key =
opts.ssl_key;
3293 params.ca_cert =
opts.ssl_ca_cert;
3294 params.cipher_suites =
opts.ssl_cipher_suites;
3295 params.psk_identity =
opts.ssl_psk_identity;
3296 params.psk_key =
opts.ssl_psk_key;
3298 if (
opts.ssl_server_name !=
NULL) {
3299 if (
strcmp(
opts.ssl_server_name,
"*") != 0) {
3300 params.server_name =
opts.ssl_server_name;
3302 }
else if (
rc == 0) {
3303 params.server_name = host;
3316#if MG_ENABLE_ASYNC_RESOLVER
3325 o.nameserver =
opts.nameserver;
3364#if MG_ENABLE_CALLBACK_USERDATA
3390 DBG((
"%p %s %s,%s,%s", nc, address, (
opts.ssl_cert ?
opts.ssl_cert :
"-"),
3391 (
opts.ssl_key ?
opts.ssl_key :
"-"),
3392 (
opts.ssl_ca_cert ?
opts.ssl_ca_cert :
"-")));
3402 memset(¶ms, 0,
sizeof(params));
3403 params.cert =
opts.ssl_cert;
3404 params.key =
opts.ssl_key;
3405 params.ca_cert =
opts.ssl_ca_cert;
3406 params.cipher_suites =
opts.ssl_cipher_suites;
3422 DBG((
"Failed to open listener: %d",
rc));
3436#if MG_ENABLE_BROADCAST
3463 return n >= 0 &&
n <= 255;
3467 int n, a, b,
c,
d,
slash = 32, len = 0;
3492 if ((
flag !=
'+' &&
flag !=
'-') ||
3497 if (
net == (remote_ip &
mask)) {
3502 DBG((
"%08x %c", (
unsigned int) remote_ip,
allowed));
3513 double result =
c->ev_timer_time;
3514 c->ev_timer_time = timestamp;
3521 (
unsigned long) timestamp));
3543#if MG_ENABLE_CALLBACK_USERDATA
3566#ifdef MG_MODULE_LINES
3567#line 1 "mongoose/src/mg_net_if_socket.h"
3574#ifndef CS_MONGOOSE_SRC_NET_IF_SOCKET_H_
3575#define CS_MONGOOSE_SRC_NET_IF_SOCKET_H_
3583#ifndef MG_ENABLE_NET_IF_SOCKET
3584#define MG_ENABLE_NET_IF_SOCKET MG_NET_IF == MG_NET_IF_SOCKET
3594#ifdef MG_MODULE_LINES
3595#line 1 "mongoose/src/mg_net_if_socks.h"
3602#ifndef CS_MONGOOSE_SRC_NET_IF_SOCKS_H_
3603#define CS_MONGOOSE_SRC_NET_IF_SOCKS_H_
3619#ifdef MG_MODULE_LINES
3620#line 1 "mongoose/src/mg_net_if.c"
3675#ifdef MG_MODULE_LINES
3676#line 1 "mongoose/src/mg_net_if_null.c"
3804#define MG_NULL_IFACE_VTABLE \
3806 mg_null_if_init, mg_null_if_free, mg_null_if_add_conn, \
3807 mg_null_if_remove_conn, mg_null_if_poll, mg_null_if_listen_tcp, \
3808 mg_null_if_listen_udp, mg_null_if_connect_tcp, mg_null_if_connect_udp, \
3809 mg_null_if_tcp_send, mg_null_if_udp_send, mg_null_if_tcp_recv, \
3810 mg_null_if_udp_recv, mg_null_if_create_conn, mg_null_if_destroy_conn, \
3811 mg_null_if_sock_set, mg_null_if_get_conn_addr, \
3816#if MG_NET_IF == MG_NET_IF_NULL
3819#ifdef MG_MODULE_LINES
3820#line 1 "mongoose/src/mg_net_if_socket.c"
3827#if MG_ENABLE_NET_IF_SOCKET
3838 unsigned long on = 1;
3866#if !defined(MG_ESP8266)
3991 (
sa->sa.sa_family ==
AF_INET) ?
sizeof(
sa->sin) :
sizeof(
sa->sin6);
4005#if !defined(_WIN32) || !defined(SO_EXCLUSIVEADDRUSE)
4034#define _MG_F_FD_CAN_READ 1
4035#define _MG_F_FD_CAN_WRITE 1 << 1
4036#define _MG_F_FD_ERROR 1 << 2
4042 DBG((
"%p fd=%d fd_flags=%d nc_flags=0x%lx rmbl=%d smbl=%d", nc, nc->
sock,
4052#if !defined(MG_ESP8266)
4070 }
else if (nc->
err != 0) {
4095 DBG((
"%p after fd=%d nc_flags=0x%lx rmbl=%d smbl=%d", nc, nc->
sock,
4100#if MG_ENABLE_BROADCAST
4106 DBG((
"read %d from ctl socket", len));
4129#if MG_ENABLE_BROADCAST
4175#if MG_ENABLE_BROADCAST
4201 DBG((
"new sock is still larger than FD_SETSIZE, disregard"));
4252#if MG_ENABLE_BROADCAST
4284#if MG_ENABLE_BROADCAST
4315 sa.sin.sin_addr.s_addr =
htonl(0x7f000001);
4319 }
else if (
bind(sock, &
sa.sa, len) != 0) {
4327 connect(
sp[0], &
sa.sa, len) != 0 ||
4328 connect(sock, &
sa2.sa, len) != 0)) {
4330 sock, &
sa, len))) ==
4376#define MG_SOCKET_IFACE_VTABLE \
4378 mg_socket_if_init, \
4379 mg_socket_if_free, \
4380 mg_socket_if_add_conn, \
4381 mg_socket_if_remove_conn, \
4382 mg_socket_if_poll, \
4383 mg_socket_if_listen_tcp, \
4384 mg_socket_if_listen_udp, \
4385 mg_socket_if_connect_tcp, \
4386 mg_socket_if_connect_udp, \
4387 mg_socket_if_tcp_send, \
4388 mg_socket_if_udp_send, \
4389 mg_socket_if_tcp_recv, \
4390 mg_socket_if_udp_recv, \
4391 mg_socket_if_create_conn, \
4392 mg_socket_if_destroy_conn, \
4393 mg_socket_if_sock_set, \
4394 mg_socket_if_get_conn_addr, \
4399#if MG_NET_IF == MG_NET_IF_SOCKET
4404#ifdef MG_MODULE_LINES
4405#line 1 "mongoose/src/mg_net_if_socks.c"
4424 d->c->user_data =
NULL;
4429 d->s->user_data =
NULL;
4446 if (
d ==
NULL)
return;
4453 LOG(
LL_DEBUG, (
"Sent handshake to %s",
d->proxy_addr));
4466 if (
c->recv_mbuf.len < 2)
return;
4476 memcpy(buf + 4, &
d->c->sa.sin.sin_addr, 4);
4477 memcpy(buf + 8, &
d->c->sa.sin.sin_port, 2);
4484 if (
c->recv_mbuf.len < 10)
return;
4486 LOG(
LL_ERROR, (
"Socks connection error: %d",
c->recv_mbuf.buf[1]));
4506 d->s->user_data =
d;
4533 if (
d->s ==
NULL)
return -1;
4535 DBG((
"%p -> %d -> %p",
c,
res,
d->s));
4549 if (
d->s ==
NULL)
return -1;
4550 if (len >
d->s->recv_mbuf.len) len =
d->s->recv_mbuf.len;
4552 memcpy(buf,
d->s->recv_mbuf.buf, len);
4555 DBG((
"%p <- %d <- %p",
c, (
int) len,
d->s));
4575 c->iface->vtable->free(
c->iface);
4644#ifdef MG_MODULE_LINES
4645#line 1 "mongoose/src/mg_ssl_if_openssl.c"
4652#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_OPENSSL
4655#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4658#include <openssl/ssl.h>
4660#include <openssl/tls1.h>
4679 nc->ssl_if_data = ctx;
4681 ctx->ssl_ctx =
lc_ctx->ssl_ctx;
4701 DBG((
"%p %s,%s,%s", nc, (params->cert ? params->cert :
""),
4702 (params->
key ? params->
key :
""),
4708 nc->ssl_if_data = ctx;
4714 if (ctx->ssl_ctx ==
NULL) {
4724#ifdef MG_SSL_OPENSSL_NO_COMPRESSION
4727#ifdef MG_SSL_OPENSSL_CIPHER_SERVER_PREFERENCE
4734 if (params->cert !=
NULL &&
4740 if (params->ca_cert !=
NULL &&
4764 if (params->server_name !=
NULL) {
4783 DBG((
"%p %p SSL error: %d %d", nc, ctx->ssl_ctx,
res, err));
4803 int n =
SSL_read(ctx->ssl, buf, buf_size);
4804 DBG((
"%p %d -> %d", nc, (
int) buf_size,
n));
4813 DBG((
"%p %d -> %d", nc, (
int) len,
n));
4820 if (ctx ==
NULL)
return;
4826 if (ctx ==
NULL)
return;
4827 nc->ssl_if_data =
NULL;
4831 memset(ctx, 0,
sizeof(*ctx));
4840#if defined(MG_SSL_CRYPTO_MODERN)
4841 "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:"
4842 "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:"
4843 "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:"
4844 "ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:"
4845 "ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:"
4846 "ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:"
4847 "DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:"
4848 "DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:"
4849 "!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK"
4850#elif defined(MG_SSL_CRYPTO_OLD)
4851 "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:"
4852 "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:"
4853 "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:"
4854 "ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:"
4855 "ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:"
4856 "ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:"
4857 "DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:"
4858 "DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:"
4859 "ECDHE-ECDSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:"
4860 "AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:DES-CBC3-SHA:"
4861 "HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:"
4862 "!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA"
4864 "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:"
4865 "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:"
4866 "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:"
4867 "ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:"
4868 "ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:"
4869 "ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:"
4870 "DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:"
4871 "DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:"
4872 "AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:"
4873 "DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:"
4874 "!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA"
4882#if !MG_DISABLE_PFS && !defined(KR_VERSION)
4885-----BEGIN DH PARAMETERS-----\n\
4886MIIBCAKCAQEAlvbgD/qh9znWIlGFcV0zdltD7rq8FeShIqIhkQ0C7hYFThrBvF2E\n\
4887Z9bmgaP+sfQwGpVlv9mtaWjvERbu6mEG7JTkgmVUJrUt/wiRzwTaCXBqZkdUO8Tq\n\
4888+E6VOEQAilstG90ikN1Tfo+K6+X68XkRUIlgawBTKuvKVwBhuvlqTGerOtnXWnrt\n\
4889ym//hd3cd5PBYGBix0i7oR4xdghvfR2WLVu0LgdThTBb6XP7gLd19cQ1JuBtAajZ\n\
4890wMuPn7qlUkEFDIkAZy59/Hue/H2Q2vU/JsvVhHWCQBL4F1ofEAt50il6ZxR1QfFK\n\
48919VGKDC4oOgm9DlxwwBoC2FjqmvQlqVV3kwIBAg==\n\
4892-----END DH PARAMETERS-----\n";
4921#if !MG_DISABLE_PFS && !defined(KR_VERSION)
4945#if OPENSSL_VERSION_NUMBER > 0x10002000L
4967 size_t key_len = ctx->psk.len - ctx->identity_len - 1;
4970 DBG((
"identity too long"));
4974 DBG((
"key too long"));
4986 unsigned char key[32];
4998 if (
hc >=
'0' &&
hc <=
'9') {
5000 }
else if (
hc >=
'a' &&
hc <=
'f') {
5034 memset(¶ms, 0,
sizeof(params));
5044#ifdef MG_MODULE_LINES
5045#line 1 "mongoose/src/mg_ssl_if_mbedtls.c"
5052#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_MBEDTLS
5054#include <mbedtls/debug.h>
5055#include <mbedtls/ecp.h>
5056#include <mbedtls/net.h>
5057#include <mbedtls/platform.h>
5058#include <mbedtls/ssl.h>
5059#include <mbedtls/ssl_internal.h>
5060#include <mbedtls/x509_crt.h>
5061#include <mbedtls/version.h>
5110 nc->ssl_if_data = ctx;
5120 const char *
cert,
const char *
key,
5126#ifdef MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
5137 DBG((
"%p %s,%s,%s", nc, (params->cert ? params->cert :
""),
5138 (params->
key ? params->
key :
""),
5145 nc->ssl_if_data = ctx;
5163 if (params->cert !=
NULL &&
5168 if (params->ca_cert !=
NULL &&
5179#ifdef MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
5194 if (params->server_name !=
NULL &&
5200#ifdef MG_SSL_IF_MBEDTLS_MAX_FRAG_LEN
5227 if (
n > 0)
return n;
5235 if (
n > 0)
return n;
5248 LOG(
LL_DEBUG, (
"%p TLS connection closed by peer", nc));
5252 LOG(
LL_ERROR, (
"%p mbedTLS error: -0x%04x", nc, -ret));
5271#ifdef MBEDTLS_X509_CA_CHAIN_ON_DISK
5272 if (
ctx->conf->ca_chain_file !=
NULL) {
5274 ctx->conf->ca_chain_file =
NULL;
5287 if (ctx->ssl->p_bio ==
NULL) {
5293#ifdef MG_SSL_IF_MBEDTLS_FREE_CERTS
5300 ctx->ssl->session->peer_cert =
NULL;
5303 if (ctx->conf->key_cert !=
NULL) {
5306 ctx->conf->key_cert =
NULL;
5318 DBG((
"%p %d -> %d", nc, (
int) len,
n));
5333 if (ctx->saved_len > 0 && ctx->saved_len < l) l = ctx->saved_len;
5335 DBG((
"%p %d,%d,%d -> %d", nc, (
int) len, (
int) ctx->saved_len, (
int) l,
n));
5338 ctx->saved_len = len;
5349 if (ctx ==
NULL)
return;
5355 if (ctx ==
NULL)
return;
5356 nc->ssl_if_data =
NULL;
5357 if (ctx->ssl !=
NULL) {
5362 if (ctx->conf !=
NULL) {
5367 memset(ctx, 0,
sizeof(*ctx));
5379#ifdef MBEDTLS_X509_CA_CHAIN_ON_DISK
5393 const char *
cert,
const char *
key,
5419#if CS_PLATFORM != CS_P_ESP8266
5476 DBG((
"%s -> %04x",
tmp,
id));
5478 mbuf_append(&ctx->cipher_suites, &
id,
sizeof(
id));
5484 mbuf_append(&ctx->cipher_suites, &
id,
sizeof(
id));
5487 (
const int *) ctx->cipher_suites.buf);
5494#ifdef MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED
5498 unsigned char key[32];
5510 if (
hc >=
'0' &&
hc <=
'9') {
5512 }
else if (
hc >=
'a' &&
hc <=
'f') {
5537 memset(¶ms, 0,
sizeof(params));
5547#ifdef MG_SSL_MBED_DUMMY_RANDOM
5550 while (len--) *buf++ =
rand();
5556#ifdef MG_MODULE_LINES
5557#line 1 "mongoose/src/mg_uri.c"
5577 for (; *p <
end; (*p)++) {
5578 for (
q =
seps; *
q !=
'\0';
q++) {
5579 if (**p == *
q)
break;
5581 if (*
q !=
'\0')
break;
5583 res->len = (*p) -
res->p;
5584 if (*p <
end) (*p)++;
5589 unsigned int *port,
struct mg_str *path,
struct mg_str *query,
5593 unsigned int rport = 0;
5603 const char *
p = uri.
p, *
end =
p + uri.
len;
5618 for (;
p <
end;
p++) {
5622 }
else if (*
p ==
'/') {
5646 for (;
p <
end;
p++) {
5647 if (*
p ==
'@' || *
p ==
'[' || *
p ==
'/') {
5651 if (
p ==
end || *
p ==
'/' || *
p ==
'[') {
5666 if (!
found)
return -1;
5668 for (;
p <
end;
p++) {
5669 if (*
p ==
':' || *
p ==
'/')
break;
5677 }
else if (*
p ==
'/') {
5685 for (;
p <
end;
p++) {
5697 if (
p <
end && *(
p - 1) ==
'?') {
5707 if (host != 0) *host =
rhost;
5708 if (port != 0) *port =
rport;
5709 if (path != 0) *path =
rpath;
5710 if (query != 0) *query =
rquery;
5718 const char *s = in->
p, *
se = s + in->
len;
5719 char *
cp = (
char *)
out->p, *
d;
5721 if (in->
len == 0 || *s !=
'/') {
5729 const char *next = s;
5736 if (
d >
cp + 1 && *(
d - 1) ==
'/')
d--;
5737 while (
d >
cp && *(
d - 1) !=
'/')
d--;
5744 if (
d ==
cp) *
d++ =
'/';
5752 const struct mg_str *host,
unsigned int port,
5770 if (host !=
NULL && host->
len > 0) {
5780 if (path !=
NULL && path->
len > 0) {
5785 free((
void *)
npath.p);
5789 free((
void *)
npath.p);
5797 if (query !=
NULL && query->
len > 0) {
5820#ifdef MG_MODULE_LINES
5821#line 1 "mongoose/src/mg_http.c"
5853 ab->user_buf_size = buf_size;
5861 if (
ab->len <
ab->user_buf_size) {
5863 ab->user_buf[
ab->len++] =
c;
5872 if (
ab->len > 0 &&
ab->m.len == 0) {
5877 ab->len =
ab->m.len;
5894 return ab->len >
ab->user_buf_size;
5908 return ab->user_buf;
5926#if MG_ENABLE_HTTP_CGI
5943#if MG_ENABLE_CALLBACK_USERDATA
5983#if MG_ENABLE_FILESYSTEM
5986#if MG_ENABLE_HTTP_CGI
5989#if MG_ENABLE_HTTP_STREAMING_MULTIPART
5992#if MG_ENABLE_HTTP_WEBSOCKET
6011 if (
c->proto_data ==
NULL) {
6019#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6029#if MG_ENABLE_FILESYSTEM
6032 if (
d->fp !=
NULL) {
6056 if (
rpd->linked_conn !=
NULL) {
6063 if (
pd->reverse_proxy_data.linked_conn !=
NULL) {
6065 pd->reverse_proxy_data.linked_conn =
NULL;
6073#if MG_ENABLE_FILESYSTEM
6076#if MG_ENABLE_HTTP_CGI
6079#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6087#if MG_ENABLE_FILESYSTEM
6089#define MIME_ENTRY(_ext, _type) \
6090 { _ext, sizeof(_ext) - 1, _type }
6091static const struct {
6102 MIME_ENTRY(
"js",
"application/x-javascript"),
6110 MIME_ENTRY(
"torrent",
"application/x-bittorrent"),
6124 MIME_ENTRY(
"exe",
"application/octet-stream"),
6125 MIME_ENTRY(
"zip",
"application/x-zip-compressed"),
6130 MIME_ENTRY(
"arj",
"application/x-arj-compressed"),
6131 MIME_ENTRY(
"rar",
"application/x-rar-compressed"),
6134 MIME_ENTRY(
"swf",
"application/x-shockwave-flash"),
6185 const unsigned char *buf = (
unsigned char *) s;
6189 if (!
isprint(buf[
i]) && buf[
i] !=
'\r' && buf[
i] !=
'\n' && buf[
i] < 128) {
6191 }
else if (buf[
i] ==
'\n' &&
i + 1 <
buf_len && buf[
i + 1] ==
'\n') {
6193 }
else if (buf[
i] ==
'\n' &&
i + 2 <
buf_len && buf[
i + 1] ==
'\r' &&
6194 buf[
i + 2] ==
'\n') {
6211 while (v->len > 0 && v->p[v->len - 1] ==
' ') {
6220 if (
k->len != 0 && v->len == 0) {
6224 if (
k->len == 0 || v->len == 0) {
6226 k->len = v->len = 0;
6242 const char *
end, *
qs;
6245 if (
len <= 0)
return len;
6249 hm->body.p = s +
len;
6250 hm->message.len =
hm->body.len = (
size_t) ~0;
6254 while (s <
end &&
isspace(*(
unsigned char *) s)) s++;
6261 if (
hm->uri.p <=
hm->method.p ||
hm->proto.p <=
hm->uri.p)
return -1;
6265 hm->query_string.p =
qs + 1;
6266 hm->query_string.len = &
hm->uri.p[
hm->uri.len] - (
qs + 1);
6267 hm->uri.len =
qs -
hm->uri.p;
6271 if (
end - s < 4 || s[3] !=
' ')
return -1;
6273 if (
hm->resp_code < 100 ||
hm->resp_code >= 600)
return -1;
6295 if (
hm->body.len == (
size_t) ~0 &&
is_req &&
6299 hm->message.len =
len;
6308 for (
i = 0;
hm->header_names[
i].len > 0;
i++) {
6309 struct mg_str *h = &
hm->header_names[
i], *v = &
hm->header_values[
i];
6317#if MG_ENABLE_FILESYSTEM
6338 DBG((
"%p sent %d (total %d)", nc, (
int)
n, (
int)
pd->file.sent));
6343 if (
pd->file.sent >=
pd->file.cl) {
6344 LOG(
LL_DEBUG, (
"%p done, %d bytes", nc, (
int)
pd->file.sent));
6356 if (
n == 0 ||
pd->file.sent >=
pd->file.cl) {
6361#if MG_ENABLE_HTTP_CGI
6362 else if (
pd->cgi.cgi_nc !=
NULL) {
6364 if (
pd->cgi.cgi_nc !=
NULL) {
6381 unsigned char *s = (
unsigned char *)
buf;
6388 n += (s[
i] >=
'0' && s[
i] <=
'9') ? s[
i] -
'0' :
tolower(s[
i]) -
'a' + 10;
6397 if (
i == 0 ||
i + 2 >
len || s[
i] !=
'\r' || s[
i + 1] !=
'\n') {
6410 if (
i == 0 ||
i + 2 >
len || s[
i] !=
'\r' || s[
i + 1] !=
'\n') {
6423 body_len = (
size_t)
pd->chunk.body_len;
6424 assert(
blen >= body_len);
6432 body_len += data_len;
6433 hm->body.len = body_len;
6435 if (data_len == 0) {
6448 pd->chunk.body_len = body_len;
6456 memset(buf, 0, body_len);
6460 pd->chunk.body_len = 0;
6487 while (ep !=
NULL) {
6502#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6551#if MG_ENABLE_HTTP_WEBSOCKET
6555#if MG_ENABLE_HTTP_CGI
6557 if (
pd->cgi.cgi_nc !=
NULL) {
6558 pd->cgi.cgi_nc->user_data =
NULL;
6562#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6563 if (
pd->mp_stream.boundary !=
NULL) {
6571 mp.var_name =
pd->mp_stream.var_name;
6572 mp.file_name =
pd->mp_stream.file_name;
6573 mg_call(nc, (
pd->endpoint_handler ?
pd->endpoint_handler : nc->handler),
6577 mg_call(nc, (
pd->endpoint_handler ?
pd->endpoint_handler : nc->handler),
6588 hm->message.len =
io->len;
6589 hm->body.len =
io->buf +
io->len -
hm->body.p;
6594 if (
pd->endpoint_handler !=
NULL &&
pd->endpoint_handler != nc->
handler) {
6599#if MG_ENABLE_FILESYSTEM
6600 if (
pd->file.fp !=
NULL) {
6607#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6608 if (
pd->mp_stream.boundary !=
NULL &&
6613 }
else if (
pd->mp_stream.data_avail) {
6634#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6636 s->
len >= 9 &&
strncmp(s->
p,
"multipart", 9) == 0) {
6646 DBG((
"invalid request"));
6651#if MG_ENABLE_HTTP_WEBSOCKET
6654 DBG((
"%p WebSocket upgrade code %d", nc,
hm->resp_code));
6655 if (
hm->resp_code == 101 &&
6686#if MG_ENABLE_CALLBACK_USERDATA
6704 else if (
hm->message.len >
pd->rcvd) {
6708 LOG(
LL_ERROR, (
"%p recv buffer (%lu bytes) exceeds the limit "
6709 "%lu bytes, and not drained, closing",
6721 DBG((
"%p %s %.*s %.*s", nc,
addr, (
int)
hm->method.len,
hm->method.p,
6722 (
int)
hm->uri.len,
hm->uri.p));
6727 pd->rcvd -=
hm->message.len;
6728#if MG_ENABLE_FILESYSTEM
6735#if MG_ENABLE_HTTP_CGI
6757 while (len <
buf_len && buf[len] !=
'\n') len++;
6758 return len ==
buf_len ? 0 : len + 1;
6761#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6779 if (
ct->len < 9 ||
strncmp(
ct->p,
"multipart", 9) != 0) {
6785 if (boundary_len == 0) {
6791 DBG((
"invalid request"));
6797 if (
pd->mp_stream.boundary !=
NULL) {
6806 pd->mp_stream.boundary =
strdup(boundary);
6807 pd->mp_stream.boundary_len =
strlen(boundary);
6808 pd->mp_stream.var_name =
pd->mp_stream.file_name =
NULL;
6824#define CONTENT_DISPOSITION "Content-Disposition: "
6833 mp.var_name =
pd->mp_stream.var_name;
6834 mp.file_name =
pd->mp_stream.file_name;
6835 mp.user_data =
pd->mp_stream.user_data;
6837 mp.data.len = data_len;
6838 mp.num_data_consumed = data_len;
6840 pd->mp_stream.user_data =
mp.user_data;
6841 pd->mp_stream.data_avail = (
mp.num_data_consumed != data_len);
6842 return mp.num_data_consumed;
6849 MG_FREE((
void *)
pd->mp_stream.file_name);
6850 pd->mp_stream.file_name =
NULL;
6851 MG_FREE((
void *)
pd->mp_stream.var_name);
6852 pd->mp_stream.var_name =
NULL;
6861 const char *boundary;
6862 struct mbuf *
io = &
c->recv_mbuf;
6865 if (
pd->mp_stream.boundary ==
NULL) {
6867 DBG((
"Invalid request: boundary not initialized"));
6871 if ((
int)
io->len <
pd->mp_stream.boundary_len + 2) {
6876 if (boundary !=
NULL) {
6877 const char *
boundary_end = (boundary +
pd->mp_stream.boundary_len);
6901 struct mbuf *
io = &
c->recv_mbuf;
6912 while (data_size > 0 &&
6937 if (
pd->mp_stream.processing_part != 0) {
6947 MG_FREE((
void *)
pd->mp_stream.file_name);
6949 MG_FREE((
void *)
pd->mp_stream.var_name);
6954 pd->mp_stream.processing_part++;
6971 struct mbuf *
io = &
c->recv_mbuf;
6973 const char *boundary;
6974 if ((
int)
io->len <
pd->mp_stream.boundary_len + 6 ) {
6979 if (boundary ==
NULL) {
6980 int data_len = (
io->len - (
pd->mp_stream.boundary_len + 6));
6987 }
else if (boundary !=
NULL) {
6988 size_t data_len = ((
size_t)(boundary -
io->buf) - 4);
7007 switch (
pd->mp_stream.state) {
7056 switch (status_code) {
7058 return "Partial Content";
7064 return "Bad Request";
7066 return "Unauthorized";
7072 return "Requested Range Not Satisfiable";
7074 return "I'm a teapot";
7076 return "Internal Server Error";
7078 return "Bad Gateway";
7080 return "Service Unavailable";
7082#if MG_ENABLE_EXTRA_ERRORS_DESC
7086 return "Switching Protocols";
7088 return "Processing";
7096 return "Non-Authoritative Information";
7098 return "No Content";
7100 return "Reset Content";
7102 return "Multi-Status";
7104 return "Already Reported";
7108 return "Multiple Choices";
7112 return "Not Modified";
7116 return "Switch Proxy";
7118 return "Temporary Redirect";
7120 return "Permanent Redirect";
7122 return "Payment Required";
7124 return "Method Not Allowed";
7126 return "Not Acceptable";
7128 return "Proxy Authentication Required";
7130 return "Request Timeout";
7136 return "Length Required";
7138 return "Precondition Failed";
7140 return "Payload Too Large";
7142 return "URI Too Long";
7144 return "Unsupported Media Type";
7146 return "Expectation Failed";
7148 return "Unprocessable Entity";
7152 return "Failed Dependency";
7154 return "Upgrade Required";
7156 return "Precondition Required";
7158 return "Too Many Requests";
7160 return "Request Header Fields Too Large";
7162 return "Unavailable For Legal Reasons";
7164 return "Not Implemented";
7166 return "Gateway Timeout";
7168 return "HTTP Version Not Supported";
7170 return "Variant Also Negotiates";
7172 return "Insufficient Storage";
7174 return "Loop Detected";
7176 return "Not Extended";
7178 return "Network Authentication Required";
7187 const struct mg_str extra_headers) {
7188 mg_printf(nc,
"HTTP/1.1 %d %s\r\n", status_code,
7190#ifndef MG_HIDE_SERVER_INFO
7193 if (extra_headers.
len > 0) {
7194 mg_printf(nc,
"%.*s\r\n", (
int) extra_headers.
len, extra_headers.
p);
7199 const char *extra_headers) {
7205 const struct mg_str extra_headers) {
7208 "<p>Moved <a href='%.*s'>here</a>.\r\n",
7212 "Location: %.*s\r\n"
7213 "Content-Type: text/html\r\n"
7214 "Content-Length: %d\r\n"
7215 "Cache-Control: no-cache\r\n"
7218 extra_headers.
p, (extra_headers.
len > 0 ?
"\r\n" :
""));
7229 mg_printf(
c,
"%s",
"Transfer-Encoding: chunked\r\n");
7241 "Content-Type: text/plain\r\nConnection: close");
7246#if MG_ENABLE_FILESYSTEM
7270 if (p ==
NULL)
return 0;
7272 p[header->
len] =
'\0';
7280 const struct mg_str extra_headers) {
7302 int n, status_code = 200;
7313 if (
r1 >
r2 ||
r2 >= cl) {
7317 "Content-Range: bytes */%" INT64_FMT "\r\n",
7325#if _FILE_OFFSET_BITS == 64 || _POSIX_C_SOURCE >= 200112L || \
7326 _XOPEN_SOURCE >= 600
7334#if !MG_DISABLE_HTTP_KEEP_ALIVE
7340 pd->file.keepalive = (
mg_vcmp(&
hm->proto,
"HTTP/1.1") == 0);
7358 "Last-Modified: %s\r\n"
7359 "Accept-Ranges: bytes\r\n"
7360 "Content-Type: %.*s\r\n"
7361 "Connection: %s\r\n"
7364 "%sEtag: %s\r\n\r\n",
7366 (
pd->file.keepalive ?
"keep-alive" :
"close"), (
size_t) cl, range,
7378#if MG_ENABLE_HTTP_SSI
7393#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
7396 if (src[
i] ==
'%') {
7398 isxdigit(*(
const unsigned char *) (src +
i + 2))) {
7399 a =
tolower(*(
const unsigned char *) (src +
i + 1));
7400 b =
tolower(*(
const unsigned char *) (src +
i + 2));
7420 const char *
p, *
e, *s;
7439 e = buf->
p + buf->
len;
7443 for (
p = buf->
p;
p + name_len <
e;
p++) {
7444 if ((
p == buf->
p ||
p[-1] ==
'&') &&
p[name_len] ==
'=' &&
7447 s = (
const char *)
memchr(
p,
'&', (
size_t)(
e -
p));
7465 char chunk_size[50];
7468 n =
snprintf(chunk_size,
sizeof(chunk_size),
"%lX\r\n", (
unsigned long)
len);
7488 if (buf !=
mem && buf !=
NULL) {
7504 for (
i =
j = 0;
i <
len;
i++) {
7505 if (buf[
i] ==
'<' || buf[
i] ==
'>') {
7507 mg_send(nc, buf[
i] ==
'<' ?
"<" :
">", 4);
7515 if (buf !=
mem && buf !=
NULL) {
7529 if ((s ==
hdr->p || s[-1] ==
ch || s[-1] ==
ch1 || s[-1] ==
';') &&
7534 if (s !=
NULL && &s[
n + 1] <
end) {
7536 if (*s ==
'"' || *s ==
'\'') {
7541 if (
ch !=
' ' &&
p[0] ==
'\\' &&
p[1] ==
ch)
p++;
7545 if (
ch !=
' ' && *
p !=
ch) {
7561 if (*buf !=
NULL && buf_size > 0) *buf[0] =
'\0';
7570 return ab.len > 0 ?
ab.len - 1 : 0;
7617#if MG_ENABLE_FILESYSTEM
7621 const char *
p1 =
opts->per_directory_auth_file;
7622 const char *
p2 =
opts->hidden_file_pattern;
7635#if !MG_DISABLE_HTTP_DIGEST_AUTH
7653void cs_md5(
char buf[33], ...) {
7654 unsigned char hash[16];
7674 const char *nonce,
size_t nonce_len,
const char *nc,
7677 static const char colon[] =
":";
7678 static const size_t one = 1;
7681 cs_md5(
resp,
ha1,
ha1_len,
colon,
one, nonce,
nonce_len,
colon,
one, nc,
7687 const char *method,
const char *uri,
7688 const char *auth_domain,
const char *user,
7689 const char *
passwd,
const char *nonce) {
7690 static const char colon[] =
":", qop[] =
"auth";
7691 static const size_t one = 1;
7692 char ha1[33],
resp[33], cnonce[40];
7700 sizeof(qop) - 1,
resp);
7702 "Authorization: Digest username=\"%s\","
7703 "realm=\"%s\",uri=\"%s\",qop=%s,nc=1,cnonce=%s,"
7704 "nonce=%s,response=%s\r\n",
7705 user, auth_domain, uri, qop, cnonce, nonce,
resp);
7753 hm->uri.len + (
hm->query_string.len ?
hm->query_string.len + 1 : 0)),
7789 nonce.
p, nonce.
len, nc.
p, nc.
len, cnonce.
p, cnonce.
len,
7849#if MG_ENABLE_DIRECTORY_LISTING
7852 while (*src !=
'\0' &&
n + 5 <
dst_len) {
7853 unsigned char ch = *(
unsigned char *) src++;
7872 snprintf(size,
sizeof(size),
"%s",
"[DIRECTORY]");
7880 }
else if (
fsize < 0x100000) {
7881 snprintf(size,
sizeof(size),
"%.1fk", (
double)
fsize / 1024.0);
7882 }
else if (
fsize < 0x40000000) {
7883 snprintf(size,
sizeof(size),
"%.1fM", (
double)
fsize / 1048576);
7885 snprintf(size,
sizeof(size),
"%.1fG", (
double)
fsize / 1073741824);
7892 "<tr><td><a href=\"%s%s\">%s%s</a></td>"
7893 "<td>%s</td><td name=%" INT64_FMT ">%s</td></tr>\n",
7896 free((
void *)
href.p);
7915 snprintf(path,
sizeof(path),
"%s/%s", dir,
dp->d_name);
7917 func(nc, (
const char *)
dp->d_name, &
st);
7930 "<script>function srt(tb, sc, so, d) {"
7931 "var tr = Array.prototype.slice.call(tb.rows, 0),"
7932 "tr = tr.sort(function (a, b) { var c1 = a.cells[sc], c2 = b.cells[sc],"
7933 "n1 = c1.getAttribute('name'), n2 = c2.getAttribute('name'), "
7934 "t1 = a.cells[2].getAttribute('name'), "
7935 "t2 = b.cells[2].getAttribute('name'); "
7936 "return so * (t1 < 0 && t2 >= 0 ? -1 : t2 < 0 && t1 >= 0 ? 1 : "
7937 "n1 ? parseInt(n2) - parseInt(n1) : "
7938 "c1.textContent.trim().localeCompare(c2.textContent.trim())); });";
7940 "for (var i = 0; i < tr.length; i++) tb.appendChild(tr[i]); "
7941 "if (!d) window.location.hash = ('sc=' + sc + '&so=' + so); "
7943 "window.onload = function() {"
7944 "var tb = document.getElementById('tb');"
7945 "var m = /sc=([012]).so=(1|-1)/.exec(window.location.hash) || [0, 2, 1];"
7946 "var sc = m[1], so = m[2]; document.onclick = function(ev) { "
7947 "var c = ev.target.rel; if (c) {if (c == sc) so *= -1; srt(tb, c, so); "
7948 "sc = c; ev.preventDefault();}};"
7949 "srt(tb, sc, so, true);"
7954 mg_printf(nc,
"%s: %s\r\n%s: %s\r\n\r\n",
"Transfer-Encoding",
"chunked",
7955 "Content-Type",
"text/html; charset=utf-8");
7959 "<html><head><title>Index of %.*s</title>%s%s"
7960 "<style>th,td {text-align: left; padding-right: 1em; "
7961 "font-family: monospace; }</style></head>\n"
7962 "<body><h1>Index of %.*s</h1>\n<table cellpadding=0><thead>"
7963 "<tr><th><a href=# rel=0>Name</a></th><th>"
7964 "<a href=# rel=1>Modified</a</th>"
7965 "<th><a href=# rel=2>Size</a></th></tr>"
7966 "<tr><td colspan=3><hr></td></tr>\n"
7970 (
int)
hm->uri.len,
hm->uri.p);
7973 "</tbody><tr><td colspan=3><hr></td></tr>\n"
7975 "<address>%s</address>\n"
8022#if MG_ENABLE_HTTP_URL_REWRITES
8036 mg_printf(
c,
"Content-Length: 0\r\nLocation: %.*s%.*s\r\n\r\n",
8037 (
int) b.len, b.p, (
int) (
hm->proto.p -
hm->uri.p - 1),
8051 if (
pd ==
NULL ||
pd->reverse_proxy_data.linked_conn ==
NULL) {
8052 DBG((
"%p: upstream closed", nc));
8064 mg_send(
pd->reverse_proxy_data.linked_conn,
hm->message.p,
8074#if MG_ENABLE_CALLBACK_USERDATA
8089 opts.error_string = &error;
8097 LOG(
LL_DEBUG, (
"Proxying %.*s to %s (rule: %.*s)", (
int)
hm->uri.len,
8111 mg_printf(
be,
"%.*s %.*s HTTP/1.1\r\n", (
int)
hm->method.len,
hm->method.p,
8112 (
int) path.
len, path.
p);
8114 mg_printf(
be,
"Host: %.*s\r\n", (
int) host.len, host.p);
8180#if MG_ENABLE_HTTP_URL_REWRITES
8189 if (a.len > 1 && a.p[0] ==
'@') {
8191 if (
hh !=
NULL &&
hh->len == a.len - 1 &&
8217#if MG_ENABLE_HTTP_WEBDAV
8249 const char *next =
u;
8257 if (*(
u - 1) ==
'/')
u--;
8319 (
"'%.*s' -> '%s' + '%.*s'", (
int)
hm->uri.len,
hm->uri.p,
8325 static const char *
month_names[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
8326 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"};
8342 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
8381 "HTTP/1.1 401 Unauthorized\r\n"
8382 "WWW-Authenticate: Digest qop=\"auth\", "
8383 "realm=\"%s\", nonce=\"%lx\"\r\n"
8384 "Content-Length: 0\r\n\r\n",
8392 "Allow: GET, POST, HEAD, CONNECT, OPTIONS"
8394 ", MKCOL, PUT, DELETE, PROPFIND, MOVE\r\nDAV: 1,2"
8405 const struct mg_str *path_info,
8408 int exists, is_directory,
is_cgi;
8409#if MG_ENABLE_HTTP_WEBDAV
8418 is_directory = exists &&
S_ISDIR(
st.st_mode);
8428 (
"%p %.*s [%s] exists=%d is_dir=%d is_dav=%d is_cgi=%d index=%s", nc,
8429 (
int)
hm->method.len,
hm->method.p, path, exists, is_directory,
is_dav,
8432 if (is_directory &&
hm->uri.p[
hm->uri.len - 1] !=
'/' && !
is_dav) {
8434 "HTTP/1.1 301 Moved\r\nLocation: %.*s/\r\n"
8435 "Content-Length: 0\r\n\r\n",
8436 (
int)
hm->uri.len,
hm->uri.p);
8457 opts->per_directory_auth_file,
8462#if MG_ENABLE_HTTP_CGI
8467 }
else if ((!exists ||
8471#if MG_ENABLE_HTTP_WEBDAV
8472 }
else if (!
mg_vcmp(&
hm->method,
"PROPFIND")) {
8474#if !MG_DISABLE_DAV_AUTH
8485 }
else if (!
mg_vcmp(&
hm->method,
"MKCOL")) {
8487 }
else if (!
mg_vcmp(&
hm->method,
"DELETE")) {
8489 }
else if (!
mg_vcmp(&
hm->method,
"PUT")) {
8491 }
else if (!
mg_vcmp(&
hm->method,
"MOVE")) {
8493#if MG_ENABLE_FAKE_DAVLOCK
8494 }
else if (!
mg_vcmp(&
hm->method,
"LOCK")) {
8498 }
else if (!
mg_vcmp(&
hm->method,
"OPTIONS")) {
8501#if MG_ENABLE_DIRECTORY_LISTING
8502 if (
strcmp(
opts->enable_directory_listing,
"yes") == 0) {
8531#if MG_ENABLE_HTTP_URL_REWRITES
8542 opts.document_root =
".";
8544 if (
opts.per_directory_auth_file ==
NULL) {
8545 opts.per_directory_auth_file =
".htpasswd";
8547 if (
opts.enable_directory_listing ==
NULL) {
8548 opts.enable_directory_listing =
"yes";
8550 if (
opts.cgi_file_pattern ==
NULL) {
8551 opts.cgi_file_pattern =
"**.cgi$|**.php$";
8554 opts.ssi_pattern =
"**.shtml$|**.shtm$";
8557 opts.index_files =
"index.html,index.htm,index.shtml,index.cgi,index.php";
8574 if (
mg_vcmp(&
hm->proto,
"HTTP/1.1") != 0 ||
8583#if MG_ENABLE_HTTP_STREAMING_MULTIPART
8595 LOG(
LL_ERROR, (
"%p Not allowed to upload %s", nc,
mp->file_name));
8597 "HTTP/1.1 403 Not Allowed\r\n"
8598 "Content-Type: text/plain\r\n"
8599 "Connection: close\r\n\r\n"
8600 "Not allowed to upload %s\r\n",
8612 fus->lfn[
lfn.len] =
'\0';
8615 (
"%p Receiving file %s -> %s", nc,
mp->file_name,
fus->lfn));
8619 "HTTP/1.1 500 Internal Server Error\r\n"
8620 "Content-Type: text/plain\r\n"
8621 "Connection: close\r\n\r\n");
8628 mp->user_data = (
void *)
fus;
8646 "HTTP/1.1 413 Payload Too Large\r\n"
8647 "Content-Type: text/plain\r\n"
8648 "Connection: close\r\n\r\n");
8649 mg_printf(nc,
"Failed to write to %s: no space left; wrote %d\r\n",
8650 fus->lfn, (
int)
fus->num_recd);
8653 "HTTP/1.1 500 Internal Server Error\r\n"
8654 "Content-Type: text/plain\r\n"
8655 "Connection: close\r\n\r\n");
8656 mg_printf(nc,
"Failed to write to %s: %d, wrote %d",
mp->file_name,
8667 fus->num_recd +=
mp->data.len;
8668 LOG(
LL_DEBUG, (
"%p rec'd %d bytes, %d total", nc, (
int)
mp->data.len,
8669 (
int)
fus->num_recd));
8678 if (
mp->status >= 0 &&
fus->fp !=
NULL) {
8679 LOG(
LL_DEBUG, (
"%p Uploaded %s (%s), %d bytes", nc,
mp->file_name,
8680 fus->lfn, (
int)
fus->num_recd));
8697 "HTTP/1.1 200 OK\r\n"
8698 "Content-Type: text/plain\r\n"
8699 "Connection: close\r\n\r\n"
8706#if MG_ENABLE_CALLBACK_USERDATA
8733 if (query.len > 0) path->
len += query.len + 1;
8760 opts.ssl_ca_cert =
"*";
8799 if (extra_headers ==
NULL) extra_headers =
"";
8800 if (path.len == 0) path =
mg_mk_str(
"/");
8805 (
post_data[0] ==
'\0' ?
"GET" :
"POST"), (
int) path.len, path.p,
8815 const char *
url,
const char *extra_headers,
const char *
post_data) {
8826 static const char cd[] =
"Content-Disposition: ";
8827 size_t hl,
bl,
n, ll, pos,
cdl =
sizeof(cd) - 1;
8833 if (buf[0] !=
'-' || buf[1] !=
'-' || buf[2] ==
'\n')
return 0;
8843 header.
p = buf +
n +
cdl;
8844 header.
len = ll - (
cdl + 2);
8868 if (buf[pos] ==
'-' && !
strncmp(buf, &buf[pos],
bl - 2)) {
8869 if (data_len !=
NULL) *data_len = (pos - 2) -
hl;
8885 if (nc ==
NULL)
return;
8896#if MG_ENABLE_CALLBACK_USERDATA
8916#if MG_ENABLE_FILESYSTEM && !MG_DISABLE_HTTP_DIGEST_AUTH
8924#if MG_ENABLE_CALLBACK_USERDATA
8925 user_data = ep->user_data;
8938#if MG_ENABLE_CALLBACK_USERDATA
8945#ifdef MG_MODULE_LINES
8946#line 1 "mongoose/src/mg_http_cgi.c"
8957#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_CGI
8959#ifndef MG_MAX_CGI_ENVIR_VARS
8960#define MG_MAX_CGI_ENVIR_VARS 64
8963#ifndef MG_ENV_EXPORT_TO_CGI
8964#define MG_ENV_EXPORT_TO_CGI "MONGOOSE_CGI"
8967#define MG_F_HTTP_CGI_PARSE_HEADERS MG_F_USER_1
9002 int n, sent,
stop = 0;
9007 (
n =
recv(
tp->s, buf,
sizeof(buf), 0)) > 0) {
9009 for (sent = 0; !
stop && sent <
n; sent +=
k) {
9013 DBG((
"%s",
"FORWARED EVERYTHING TO CGI"));
9021 int k = 0,
stop = 0;
9026 for (sent = 0; !
stop && sent <
n; sent +=
k) {
9028 (
k =
send(
tp->s, buf + sent,
n - sent, 0)) <= 0)
9032 DBG((
"%s",
"EOF FROM CGI"));
9041 void *(*func)(
void *)) {
9058 const char *
env,
const char *
envp[],
9059 const char *dir,
sock_t sock) {
9083 buf[0] = buf[1] =
'\0';
9085 buf[
sizeof(buf) - 1] =
'\0';
9086 if (buf[0] ==
'#' && buf[1] ==
'!') {
9096 snprintf(buf,
sizeof(buf),
"%s/%s", dir, cmd);
9125 DBG((
"CGI command: [%ls] -> %p",
wcmd,
pi.hProcess));
9129 return (
pi.hProcess !=
NULL);
9133 const char *
env,
const char *
envp[],
9134 const char *dir,
sock_t sock) {
9164 "Status: 500\r\n\r\n"
9165 "500 Server Error: %s%s%s: %s",
9213 const struct mg_str *path_info,
9224 blk->len =
blk->nvars = 0;
9245 hm->query_string.len == 0 ?
"" :
"?", (
int)
hm->query_string.
len,
9246 hm->query_string.
p);
9264 if (path_info !=
NULL && path_info->
len > 0) {
9267 mg_addenv(
blk,
"PATH_TRANSLATED=%.*s", (
int) path_info->
len, path_info->
p);
9281 if (
hm->query_string.len > 0) {
9283 hm->query_string.p);
9310 for (
i = 0;
hm->header_names[
i].len > 0;
i++) {
9312 hm->header_names[
i].p, (
int)
hm->header_values[
i].len,
9313 hm->header_values[
i].p);
9316 for (; *
p !=
'=' && *
p !=
'\0';
p++) {
9317 if (*
p ==
'-') *
p =
'_';
9323 blk->buf[
blk->len++] =
'\0';
9328#if !MG_ENABLE_CALLBACK_USERDATA
9359 if (
len == 0)
break;
9368 mg_printf(nc,
"%s",
"HTTP/1.1 302 Moved\r\n");
9372 mg_printf(nc,
"%s",
"HTTP/1.1 200 OK\r\n");
9382 DBG((
"%p CLOSE", cgi_nc));
9390 const struct mg_str *path_info,
9406 snprintf(dir,
sizeof(dir),
"%s",
".");
9431 cgi_pd->cgi.cgi_nc = cgi_nc;
9432#if !MG_ENABLE_CALLBACK_USERDATA
9437 if (
hm->body.len > 0) {
9452 if (
d ==
NULL)
return;
9453 if (
d->cgi_nc !=
NULL) {
9455 d->cgi_nc->user_data =
NULL;
9461#ifdef MG_MODULE_LINES
9462#line 1 "mongoose/src/mg_http_ssi.c"
9469#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_SSI && MG_ENABLE_FILESYSTEM
9478 while ((
n =
mg_fread(buf, 1,
sizeof(buf),
fp)) > 0) {
9511 mg_printf(nc,
"Bad SSI #include: [%s]", tag);
9516 mg_printf(nc,
"SSI include error: mg_fopen(%s): %s", path,
9530#if MG_ENABLE_HTTP_SSI_EXEC
9535 if (
sscanf(tag,
" \"%[^\"]\"", cmd) != 1) {
9536 mg_printf(nc,
"Bad SSI #exec: [%s]", tag);
9556#if MG_ENABLE_HTTP_SSI_EXEC
9563 mg_printf(nc,
"SSI #include level is too deep (%s)", path);
9575 while (
i > 0 && buf[
i] ==
' ') {
9590 (
void *)
cctx.arg.p);
9592#if MG_ENABLE_HTTP_SSI_EXEC
9600 }
else if (
ch ==
'<') {
9603 mg_send(nc, buf, (
size_t) len);
9606 buf[len++] =
ch & 0xff;
9611 }
else if (len == (
int)
sizeof(buf) - 2) {
9612 mg_printf(nc,
"%s: SSI tag is too large", path);
9615 buf[len++] =
ch & 0xff;
9617 buf[len++] =
ch & 0xff;
9618 if (len == (
int)
sizeof(buf)) {
9619 mg_send(nc, buf, (
size_t) len);
9627 mg_send(nc, buf, (
size_t) len);
9637 DBG((
"%p %s", nc, path));
9647 "Content-Type: %.*s\r\n"
9648 "Connection: close\r\n\r\n",
9657#ifdef MG_MODULE_LINES
9658#line 1 "mongoose/src/mg_http_webdav.c"
9665#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_WEBDAV
9668 static const char *
methods[] = {
9674#if MG_ENABLE_FAKE_DAVLOCK
9708 "<d:href>%s</d:href>"
9711 "<d:resourcetype>%s</d:resourcetype>"
9713 "</d:getcontentlength>"
9714 "<d:getlastmodified>%s</d:getlastmodified>"
9716 "<d:status>HTTP/1.1 200 OK</d:status>"
9727 static const char header[] =
9728 "HTTP/1.1 207 Multi-Status\r\n"
9729 "Connection: close\r\n"
9730 "Content-Type: text/xml; charset=utf-8\r\n\r\n"
9731 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
9732 "<d:multistatus xmlns:d='DAV:'>\n";
9733 static const char footer[] =
"</d:multistatus>\n";
9738 strcmp(
opts->enable_directory_listing,
"yes") != 0) {
9739 mg_printf(nc,
"%s",
"HTTP/1.1 403 Directory Listing Denied\r\n\r\n");
9742 mg_send(nc, header,
sizeof(header) - 1);
9743 snprintf(uri,
sizeof(uri),
"%.*s", (
int)
hm->uri.len,
hm->uri.p);
9753#if MG_ENABLE_FAKE_DAVLOCK
9766 static const char *
reply =
9767 "HTTP/1.1 207 Multi-Status\r\n"
9768 "Connection: close\r\n"
9769 "Content-Type: text/xml; charset=utf-8\r\n\r\n"
9770 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
9771 "<d:multistatus xmlns:d='DAV:'>\n"
9772 "<D:lockdiscovery>\n"
9776 "opaquelocktoken:%s%u"
9780 "</D:lockdiscovery>"
9781 "</d:multistatus>\n";
9789 int status_code = 500;
9790 if (
hm->body.len != (
size_t) ~0 &&
hm->body.len > 0) {
9792 }
else if (!
mg_mkdir(path, 0755)) {
9819 snprintf(path,
sizeof(path),
"%s%c%s", dir,
'/',
dp->d_name);
9841 if (
p !=
NULL &&
p[1] ==
'/' &&
9844 snprintf(buf,
sizeof(buf),
"%s%.*s",
opts->dav_document_root,
9846 if (
rename(path, buf) == 0) {
9866 }
else if (
remove(path) == 0) {
9878 for (s = path + 1; *s !=
'\0'; s++) {
9882 snprintf(buf,
sizeof(buf),
"%.*s", (
int) (s - path), path);
9883 buf[
sizeof(buf) - 1] =
'\0';
9898 int rc, status_code =
mg_stat(path, &
st) == 0 ? 200 : 201;
9902 mg_printf(nc,
"HTTP/1.1 %d OK\r\nContent-Length: 0\r\n\r\n", status_code);
9903 }
else if (
rc == -1) {
9921 mg_printf(nc,
"HTTP/1.1 %d OK\r\nContent-Length: 0\r\n\r\n", status_code);
9929#ifdef MG_MODULE_LINES
9930#line 1 "mongoose/src/mg_http_websocket.c"
9937#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_WEBSOCKET
9941#ifndef MG_WEBSOCKET_PING_INTERVAL_SECONDS
9942#define MG_WEBSOCKET_PING_INTERVAL_SECONDS 5
9945#define FLAGS_MASK_FIN (1 << 7)
9946#define FLAGS_MASK_OP 0x0f
9966 if (
wsm->flags & 0x8) {
9984 if ((
int) len == ~0) {
10001 if (
wsd->reass_len > 0) {
10029 mg_ws_close(nc,
"fragmented control frames are illegal", ~0);
10032 wsd->reass_len > 0) {
10037 mg_ws_close(nc,
"non-continuation in the middle of a fragmented message",
10077 for (
i = 0;
i < data_len;
i++) {
10091 wsd->reass_len = 1 ;
10096 wsd->reass_len +=
wsm.size;
10103 wsm.size =
wsd->reass_len - 1 ;
10105 wsd->reass_len = 0;
10165#if MG_DISABLE_WS_RANDOM_MASK
10168 if (
sizeof(
long) >= 4) {
10170 }
else if (
sizeof(
long) == 2) {
10180 unsigned char header[10];
10185 header[1] = (
unsigned char) len;
10187 }
else if (len < 65535) {
10204 header[1] |= 1 << 7;
10217 if (ctx->
pos == 0)
return;
10226 DBG((
"%p %d %d", nc,
op, (
int) len));
10243 len +=
strv[
i].len;
10260 const char *fmt, ...) {
10271 if (buf !=
mem && buf !=
NULL) {
10298#if MG_ENABLE_CALLBACK_USERDATA
10322 static const char *
magic =
"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
10325 unsigned char sha[20];
10332 "HTTP/1.1 101 Switching Protocols\r\n"
10333 "Upgrade: websocket\r\n"
10334 "Connection: Upgrade\r\n");
10338 mg_printf(nc,
"Sec-WebSocket-Protocol: %.*s\r\n", (
int) s->
len, s->
p);
10346 const char *host,
const char *
protocol,
10347 const char *extra_headers) {
10353 const char *host,
const char *
protocol,
10354 const char *extra_headers,
const char *user,
10355 const char *
pass) {
10362 const struct mg_str path,
10363 const struct mg_str host,
10365 const struct mg_str extra_headers,
10366 const struct mg_str user,
10378 if (user.
len > 0) {
10389 "GET %.*s HTTP/1.1\r\n"
10390 "Upgrade: websocket\r\n"
10391 "Connection: Upgrade\r\n"
10393 "Sec-WebSocket-Version: 13\r\n"
10394 "Sec-WebSocket-Key: %s\r\n",
10395 (
int) path.
len, path.
p, (
int)
auth.len,
10399 if (host.
len > 0) {
10407 if (extra_headers.
len > 0) {
10408 mg_printf(nc,
"%.*s", (
int) extra_headers.
len, extra_headers.
p);
10418 const char *extra_headers) {
10428 const char *extra_headers) {
10444 const char *
url,
const char *
protocol,
const char *extra_headers) {
10451#ifdef MG_MODULE_LINES
10452#line 1 "mongoose/src/mg_util.c"
10465#define MAX(a, b) ((a) > (b) ? (a) : (b))
10477#if MG_ENABLE_FILESYSTEM && !defined(MG_USER_FILE_FUNCTIONS)
10501#if defined(_WIN32) && !defined(WINCE)
10527#if MG_ENABLE_THREADS
10531#elif defined(_WIN32)
10540#if defined(MG_STACK_SIZE) && MG_STACK_SIZE > 1
10547 return (
void *) thread_id;
10554#if defined(_WIN32) && !defined(WINCE)
10556#elif defined(__unix__)
10566 if (buf ==
NULL || len <= 0)
return 0;
10581 addr = (
void *) &sa->
sin6.sin6_addr;
10591#elif defined(_WIN32) || MG_LWIP || (MG_NET_IF == MG_NET_IF_PIC32)
10606 int port =
ntohs(sa->
sin.sin_port);
10631#if MG_ENABLE_HEXDUMP
10634 const unsigned char *p = (
const unsigned char *) buf;
10635 char ascii[17] =
"";
10638 for (
i = 0;
i < len;
i++) {
10648 ascii[
idx] = p[
i] < 0x20 || p[
i] > 0x7e ?
'.' : p[
i];
10666 n = (len < 16 ? len : 16);
10677 char src[60], dst[60];
10678 const char *tag =
NULL;
10696 if (tag ==
NULL)
return;
10698 if (
strcmp(path,
"-") == 0) {
10700 }
else if (
strcmp(path,
"--") == 0) {
10702#if MG_ENABLE_FILESYSTEM
10714 fprintf(
fp,
"%lu %p %s %s %s %d\n", (
unsigned long)
mg_time(), (
void *) nc,
10724 static const int n = 1;
10726 return ((
char *) &
n)[0] == 0;
10751 struct mbuf *buf) {
10761 if (
pass.len > 0) {
10773 :
"0123456789abcdef");
10778 for (
i = 0;
i < src.len;
i++) {
10779 const unsigned char c = *((
const unsigned char *) src.p +
i);
10798#ifdef MG_MODULE_LINES
10799#line 1 "mongoose/src/mg_mqtt.c"
10815 return (
up[0] << 8) +
up[1];
10818static const char *
scanto(
const char *p,
struct mg_str *s) {
10821 return s->
p + s->
len;
10827 const char *p, *
end;
10828 unsigned char lc = 0;
10832 header =
io->
buf[0];
10838 while ((
size_t)(p -
io->buf) <
io->len) {
10839 lc = *((
const unsigned char *) p++);
10840 len += (lc & 0x7f) << 7 *
len_len;
10842 if (!(lc & 0x80))
break;
10847 if (lc & 0x80 || len > (
io->len - (p -
io->buf))) {
10856 p =
scanto(p, &
mm->protocol_name);
10858 mm->protocol_version = *(
uint8_t *) p++;
10884 (
"%d %2x %d proto [%.*s] client_id [%.*s] will_topic [%.*s] "
10885 "will_msg [%.*s] user_name [%.*s] password [%.*s]",
10886 (
int) len, (
int)
mm->connect_flags, (
int)
mm->keep_alive_timer,
10887 (
int)
mm->protocol_name.len,
mm->protocol_name.p,
10888 (
int)
mm->client_id.len,
mm->client_id.p, (
int)
mm->will_topic.len,
10889 mm->will_topic.p, (
int)
mm->will_message.len,
mm->will_message.p,
10890 (
int)
mm->user_name.len,
mm->user_name.p, (
int)
mm->password.len,
10896 mm->connack_ret_code = p[1];
10914 mm->payload.len =
end - p;
10926 mm->payload.len =
end - p;
10962 LOG(
LL_ERROR, (
"%p recv buffer (%lu bytes) exceeds the limit "
10963 "%lu bytes, and not drained, closing",
10985 if (
pd->keep_alive > 0 &&
pd->last_control_time > 0 &&
10986 (
now -
pd->last_control_time) >
pd->keep_alive) {
11003 res.len = (
c - topic->p);
11004 topic->len -= (
res.len + 1);
11005 topic->p += (
res.len + 1);
11015 if (
exp.len == 0)
return 0;
11020 if (
tc.len != 0)
return 0;
11021 if (
exp.len == 0)
break;
11025 if (
tc.len == 0 && topic.
len == 0)
return 0;
11030 return (
exp.len == 0);
11036 return (
tc.len == 0 && topic.
len == 0);
11055 buf[0] = (cmd << 4) | flags;
11059 *
vlen = len % 0x80;
11061 if (len > 0) *
vlen |= 0x80;
11081 if (client_id !=
NULL) {
11098 if (
opts.keep_alive == 0) {
11099 opts.keep_alive = 60;
11115 mg_send(nc,
"\00\04MQTT\04", 7);
11147 pd->keep_alive =
opts.keep_alive;
11204 unsigned char *buf = (
unsigned char *) msg->
payload.
p + pos;
11207 if ((
size_t) pos >= msg->payload.len)
return -1;
11209 topic->len = buf[0] << 8 | buf[1];
11210 topic->p = (
char *) buf + 2;
11211 new_pos = pos + 2 + topic->len + 1;
11212 if ((
size_t)
new_pos > msg->payload.len)
return -1;
11213 *qos = buf[2 + topic->len];
11312#ifdef MG_MODULE_LINES
11313#line 1 "mongoose/src/mg_mqtt_server.c"
11323#if MG_ENABLE_MQTT_BROKER
11329 s->subscriptions =
NULL;
11330 s->num_subscriptions = 0;
11344 for (
i = 0;
i < s->num_subscriptions;
i++) {
11345 MG_FREE((
void *) s->subscriptions[
i].topic);
11358 brk->user_data = user_data;
11405 sizeof(*
ss->subscriptions) * (
ss->num_subscriptions +
num_subs));
11410 ss->subscriptions =
te;
11412 pos < (
int) msg->payload.len &&
11414 ss->num_subscriptions++) {
11415 te = &
ss->subscriptions[
ss->num_subscriptions];
11419 ((
char *)
te->topic)[
topic.len] =
'\0';
11423 if (pos == (
int) msg->payload.len) {
11437 for (
i = 0;
i < s->num_subscriptions;
i++) {
11440 char buf[100], *p = buf;
11509#ifdef MG_MODULE_LINES
11510#line 1 "mongoose/src/mg_dns.c"
11540 if (
rr->rtype == query) {
11550 switch (
rr->rtype) {
11552 if (data_len <
sizeof(
struct in_addr)) {
11555 if (
rr->rdata.p + data_len > msg->
pkt.
p + msg->
pkt.
len) {
11562 if (data_len <
sizeof(
struct in6_addr)) {
11580 memset(&header, 0,
sizeof(header));
11602 size_t pos =
io->len;
11609 if (s -
name > 127) {
11622 }
while (*s !=
'\0');
11625 return io->len - pos;
11631 size_t pos =
io->len;
11655 size_t off =
io->len;
11670 return io->len - pos;
11685 msg->
flags = 0x100;
11705 mg_send(nc, pkt.buf, pkt.len);
11719 if (((
unsigned char *)
data)[0] & 0xc0) {
11730 rr->name.p = (
char *)
name;
11750 data_len = *
data << 8 | *(
data + 1);
11753 rr->rdata.p = (
char *)
data;
11754 rr->rdata.len = data_len;
11762 unsigned char *
data = (
unsigned char *) buf +
sizeof(*header);
11763 unsigned char *
end = (
unsigned char *) buf + len;
11766 memset(msg, 0,
sizeof(*msg));
11770 if (len < (
int)
sizeof(*header))
return -1;
11800 const unsigned char *
data = (
unsigned char *)
name->p;
11801 const unsigned char *
end = (
unsigned char *) msg->
pkt.
p + msg->
pkt.
len;
11867 memset(&msg, 0,
sizeof(msg));
11868 msg.
flags = 0x8081;
11889#ifdef MG_MODULE_LINES
11890#line 1 "mongoose/src/mg_dns_server.c"
11897#if MG_ENABLE_DNS_SERVER
11907 rep.start =
io->len;
11910 msg->
flags |= 0x8080;
11918 size_t sent = r->io->len - r->start;
11926 mg_send(nc, r->io->buf + r->start, r->io->len - r->start);
11927 r->io->len = r->start;
11933 const char *
name,
int rtype,
int ttl,
const void *rdata,
11963#ifdef MG_MODULE_LINES
11964#line 1 "mongoose/src/mg_resolv.c"
11971#if MG_ENABLE_ASYNC_RESOLVER
11976#ifndef MG_DEFAULT_NAMESERVER
11977#define MG_DEFAULT_NAMESERVER "8.8.8.8"
12007 *
key =
L"SYSTEM\\ControlSet001\\Services\\Tcpip\\Parameters\\Interfaces";
12014 for (ret = -1,
i = 0; 1;
i++) {
12023 value[0] !=
'\0') ||
12026 value[0] !=
'\0'))) {
12047#elif MG_ENABLE_FILESYSTEM && defined(MG_RESOLV_CONF_FILE_NAME)
12055 for (ret = -1;
fgets(line,
sizeof(line),
fp) !=
NULL;) {
12056 unsigned int a, b,
c,
d;
12057 if (
sscanf(line,
"nameserver %u.%u.%u.%u", &a, &b, &
c, &
d) == 4) {
12073#if MG_ENABLE_FILESYSTEM && defined(MG_HOSTS_FILE_NAME)
12079 unsigned int a, b,
c,
d;
12087 if (line[0] ==
'#')
continue;
12089 if (
sscanf(line,
"%u.%u.%u.%u%n", &a, &b, &
c, &
d, &len) == 0) {
12093 for (p = line + len;
sscanf(p,
"%s%n",
alias, &len) == 1; p += len) {
12095 usa->
sin.sin_addr.s_addr =
htonl(a << 24 | b << 16 |
c << 8 |
d);
12116#if !MG_ENABLE_CALLBACK_USERDATA
12121 DBG((
"ev=%d user_data=%p",
ev, user_data));
12176#ifdef MG_LOG_DNS_FAILURES
12199 const char *nameserver =
opts.nameserver;
12202 if (nameserver ==
NULL) {
12215 req->
name[
sizeof(req->
name) - 1] =
'\0';
12225 if (nameserver ==
NULL) {
12234 snprintf(nameserver_url,
sizeof(nameserver_url),
"udp://%s:53", nameserver);
12241 dns_nc->user_data = req;
12252 if (nameserver !=
NULL) {
12258#ifdef MG_MODULE_LINES
12259#line 1 "mongoose/src/mg_coap.c"
12284 while (
cm->options !=
NULL) {
12287 cm->options = next;
12301 if (
cm->options ==
NULL) {
12357 if (((
uint8_t) *ptr >> 6) != 1) {
12367 cm->msg_type = ((
uint8_t) *ptr & 0x30) >> 4;
12376 cm->token.len = *ptr & 0x0F;
12377 if (
cm->token.len > 8) {
12389 cm->code_detail = *ptr & 0x1F;
12410 if (
cm->token.len != 0) {
12411 if (ptr +
cm->token.len >
io->buf +
io->len) {
12416 ptr +=
cm->token.len;
12479 if (ptr ==
io->buf +
io->len) {
12549 if (ptr ==
io->buf +
io->len) {
12577 cm->payload.len =
io->len - (ptr -
io->buf);
12578 if (
cm->payload.len != 0) {
12579 cm->payload.p = ptr;
12596 }
else if (
value > 0xFF + 13 &&
value <= 0xFFFF + 269) {
12613 }
else if (
value >= 13 &&
value <= 0xFF + 13) {
12617 }
else if (
value > 0xFF + 13 &&
value <= 0xFFFF + 269) {
12619 *ext =
value - 269;
12634 *ptr =
val & 0x00FF;
12645 if (len ==
sizeof(
uint8_t)) {
12648 }
else if (len ==
sizeof(
uint16_t)) {
12669 if (
cm->token.len > 8) {
12672 if (
cm->code_class > 7) {
12675 if (
cm->code_detail > 31) {
12679 *len +=
cm->token.len;
12680 if (
cm->payload.len != 0) {
12681 *len +=
cm->payload.len + 1;
12696 if ((
opt->next !=
NULL &&
opt->number >
opt->next->number) ||
12697 opt->value.len > 0xFFFF + 269 ||
12701 *len +=
opt->value.len;
12721 prev_io_len =
io->len;
12723 ptr =
io->buf + prev_io_len;
12731 *ptr = (1 << 6) | (
cm->msg_type << 4) | (
uint8_t)(
cm->token.len);
12735 *ptr = (
cm->code_class << 5) | (
cm->code_detail);
12740 if (
cm->token.len != 0) {
12742 ptr +=
cm->token.len;
12762 if (
opt->value.len != 0) {
12764 ptr +=
opt->value.len;
12771 if (
cm->payload.len != 0) {
12774 memcpy(ptr,
cm->payload.p,
cm->payload.len);
12858#ifdef MG_MODULE_LINES
12859#line 1 "mongoose/src/mg_sntp.c"
12872#define SNTP_TIME_OFFSET 2208988800
12874#ifndef SNTP_TIMEOUT
12875#define SNTP_TIMEOUT 10
12878#ifndef SNTP_ATTEMPTS
12879#define SNTP_ATTEMPTS 3
12883 return (
val & 0xFFFFFFFF00000000) >> 32;
12918 buf[0] = (3 << 6) | (4 << 3) | 3;
12940#ifndef MG_SNTP_NO_DELAY_CORRECTION
12949#ifndef MG_SNTP_NO_DELAY_CORRECTION
12974 if ((
hdr & 0x38) >> 3 != 4) {
12985 memset(msg, 0,
sizeof(*msg));
12987 msg->kiss_of_death = (buf[1] == 0);
12991#ifndef MG_SNTP_NO_DELAY_CORRECTION
13002 msg->time = (
double)
tv.tv_sec + (((
double)
tv.tv_usec + delay) / 1000000.0);
13009 struct mbuf *
io = &
c->recv_mbuf;
13017 DBG((
"Invalid SNTP packet received (%d)", (
int)
io->len));
13045 const char *proto =
"", *port =
"", *
tmp;
13087#if !MG_ENABLE_CALLBACK_USERDATA
13088 void *user_data =
c->user_data;
13119 c->user_data =
NULL;
13140 sd->hander = event_handler;
13141#if !MG_ENABLE_CALLBACK_USERDATA
13149#ifdef MG_MODULE_LINES
13150#line 1 "mongoose/src/mg_socks.c"
13172 struct mbuf *r = &
c->recv_mbuf;
13175 }
else if (r->
len > 2 && (
size_t) r->
buf[1] + 2 <= r->
len) {
13179 for (
i = 2;
i < r->
buf[1] + 2;
i++) {
13196 c->user_data =
NULL;
13222 serv->user_data =
c;
13223 c->user_data =
serv;
13236 struct mbuf *r = &
c->recv_mbuf;
13237 unsigned char *p = (
unsigned char *) r->
buf;
13242 if (r->
len < 8)
return;
13260 p[4] << 8 | p[5], p[6] << 8 | p[7], p[8] << 8 | p[9],
13261 p[10] << 8 | p[11], p[12] << 8 | p[13], p[14] << 8 | p[15],
13262 p[16] << 8 | p[17], p[18] << 8 | p[19], p[20] << 8 | p[21]);
13311#ifdef MG_MODULE_LINES
13312#line 1 "common/platforms/cc3200/cc3200_libc.c"
13331#if CS_PLATFORM == CS_P_CC3200
13337#ifndef __TI_COMPILER_VERSION__
13339#include <sys/stat.h>
13340#include <sys/time.h>
13344#include <inc/hw_types.h>
13345#include <inc/hw_memmap.h>
13346#include <driverlib/prcm.h>
13347#include <driverlib/rom.h>
13348#include <driverlib/rom_map.h>
13349#include <driverlib/uart.h>
13350#include <driverlib/utils.h>
13352#define CONSOLE_UART UARTA0_BASE
13354#ifdef __TI_COMPILER_VERSION__
13380#if MG_TI_NO_HOST_INTERFACE
13391 while (*
str !=
'\0') {
13429#ifdef MG_MODULE_LINES
13430#line 1 "common/platforms/msp432/msp432_libc.c"
13449#if CS_PLATFORM == CS_P_MSP432
13451#include <ti/sysbios/BIOS.h>
13452#include <ti/sysbios/knl/Clock.h>
13457 tp->tv_usec = (
ticks % 1000) * 1000;
13462#ifdef MG_MODULE_LINES
13463#line 1 "common/platforms/nrf5/nrf5_libc.c"
13482#if (CS_PLATFORM == CS_P_NRF51 || CS_PLATFORM == CS_P_NRF52) && \
13483 defined(__ARMCC_VERSION)
13491#ifdef MG_MODULE_LINES
13492#line 1 "common/platforms/simplelink/sl_fs_slfs.h"
13511#ifndef CS_COMMON_PLATFORMS_SIMPLELINK_SL_FS_SLFS_H_
13512#define CS_COMMON_PLATFORMS_SIMPLELINK_SL_FS_SLFS_H_
13514#if defined(MG_FS_SLFS)
13517#ifndef __TI_COMPILER_VERSION__
13519#include <sys/stat.h>
13522#define MAX_OPEN_SLFS_FILES 8
13542#ifdef MG_MODULE_LINES
13543#line 1 "common/platforms/simplelink/sl_fs_slfs.c"
13564#if defined(MG_FS_SLFS) || defined(CC3200_FS_SLFS)
13570#if CS_PLATFORM == CS_P_CC3200
13571#include <inc/hw_types.h>
13577#if SL_MAJOR_VERSION_NUM < 2
13581 return (r < 0 ? r :
fh);
13597#ifndef FS_SLFS_MAX_FILE_SIZE
13598#define FS_SLFS_MAX_FILE_SIZE (64 * 1024)
13621 DBG((
"SL error: %d", (
int) r));
13658 int rw = (flags & 3);
13664 (
unsigned int)
foi->flags,
foi->token));
13680 if (
foi->size > 0) {
13689#if SL_MAJOR_VERSION_NUM >= 2
13715 LOG(
LL_DEBUG, (
"sl_FsClose(%d) = %d", (
int)
fi->fh, (
int) r));
13725 if (
fi->pos ==
fi->size)
return 0;
13727 DBG((
"sl_FsRead(%d, %d, %d) = %d", (
int)
fi->fh, (
int)
fi->pos, (
int)
count,
13740 DBG((
"sl_FsWrite(%d, %d, %d) = %d", (
int)
fi->fh, (
int)
fi->pos, (
int)
count,
13772 s->st_size =
fi->size;
13835 foi->flags = flags;
13847#ifdef MG_MODULE_LINES
13848#line 1 "common/platforms/simplelink/sl_fs.c"
13867#if MG_NET_IF == MG_NET_IF_SIMPLELINK && \
13868 (defined(MG_FS_SLFS) || defined(MG_FS_SPIFFS))
13872 return (
e == 0 ? 0 : -1);
13894#if !defined(MG_FS_NO_VFS)
13897#include <stdbool.h>
13901#ifdef __TI_COMPILER_VERSION__
13908#ifdef CC3200_FS_SPIFFS
13916#define NUM_SYS_FDS 3
13917#define SPIFFS_FD_BASE 10
13918#define SLFS_FD_BASE 100
13920#if !defined(MG_UART_CHAR_PUT) && !defined(MG_UART_WRITE)
13921#if CS_PLATFORM == CS_P_CC3200
13922#include <inc/hw_types.h>
13923#include <inc/hw_memmap.h>
13924#include <driverlib/rom.h>
13925#include <driverlib/rom_map.h>
13926#include <driverlib/uart.h>
13927#define MG_UART_CHAR_PUT(fd, c) MAP_UARTCharPut(UARTA0_BASE, c);
13929#define MG_UART_WRITE(fd, buf, len)
13936#ifdef CC3200_FS_SPIFFS
13945#ifdef CC3200_FS_SPIFFS
13958#if MG_TI_NO_HOST_INTERFACE
13959int open(
const char *
pathname,
unsigned flags,
int mode) {
13972#ifdef CC3200_FS_SPIFFS
13978 (
"open(%s, 0x%x) = %d, fname = %s",
pathname, flags, fd,
fname));
14000#ifdef CC3200_FS_SPIFFS
14008#if MG_TI_NO_HOST_INTERFACE
14021#ifdef CC3200_FS_SPIFFS
14032 DBG((
"close(%d) = %d", fd, r));
14036#if MG_TI_NO_HOST_INTERFACE
14049#ifdef CC3200_FS_SPIFFS
14066 memset(s, 0,
sizeof(*s));
14073 memset(s, 0,
sizeof(*s));
14079#ifdef CC3200_FS_SPIFFS
14090 DBG((
"fstat(%d) = %d", fd, r));
14094#if MG_TI_NO_HOST_INTERFACE
14095int read(
int fd,
char *buf,
unsigned count) {
14113#ifdef CC3200_FS_SPIFFS
14124 DBG((
"read(%d, %u) = %d", fd,
count, r));
14128#if MG_TI_NO_HOST_INTERFACE
14129int write(
int fd,
const char *buf,
unsigned count) {
14143#ifdef MG_UART_WRITE
14145#elif defined(MG_UART_CHAR_PUT)
14149 const char c = ((
const char *) buf)[
i];
14158#ifdef CC3200_FS_SPIFFS
14176#if MG_TI_NO_HOST_INTERFACE || defined(_NEWLIB_VERSION)
14185#ifdef CC3200_FS_SPIFFS
14189 DBG((
"rename(%s, %s) = %d",
from,
to, r));
14194#if MG_TI_NO_HOST_INTERFACE
14207#ifdef CC3200_FS_SPIFFS
14215#ifdef CC3200_FS_SPIFFS
14232 DBG((
"readdir(%p) = %p", dir,
res));
14238 DBG((
"closedir(%p) = %d", dir,
res));
14242int rmdir(
const char *path) {
14256#ifdef __TI_COMPILER_VERSION__
14259#pragma diag_suppress 169
14273#ifdef MG_MODULE_LINES
14274#line 1 "common/platforms/simplelink/sl_socket.c"
14293#if MG_NET_IF == MG_NET_IF_SIMPLELINK
14336#ifdef MG_MODULE_LINES
14337#line 1 "common/platforms/simplelink/sl_mg_task.c"
14339#if MG_NET_IF == MG_NET_IF_SIMPLELINK && !defined(MG_SIMPLELINK_NO_OSI)
14343#include <oslib/osi.h>
14354static void mg_task(
void *arg);
14376 switch (msg.
type) {
14378 msg.
cb(&mgr, msg.
arg);
14391#ifdef MG_MODULE_LINES
14392#line 1 "common/platforms/simplelink/sl_net_if.h"
14411#ifndef CS_COMMON_PLATFORMS_SIMPLELINK_SL_NET_IF_H_
14412#define CS_COMMON_PLATFORMS_SIMPLELINK_SL_NET_IF_H_
14420#ifndef MG_ENABLE_NET_IF_SIMPLELINK
14421#define MG_ENABLE_NET_IF_SIMPLELINK MG_NET_IF == MG_NET_IF_SIMPLELINK
14431#ifdef MG_MODULE_LINES
14432#line 1 "common/platforms/simplelink/sl_net_if.c"
14453#if MG_ENABLE_NET_IF_SIMPLELINK
14458#define MG_TCP_RECV_BUFFER_SIZE 1024
14459#define MG_UDP_RECV_BUFFER_SIZE 1500
14467#if SL_MAJOR_VERSION_NUM < 2
14468 opt.NonblockingEnabled = 1;
14470 opt.NonBlockingEnabled = 1;
14482#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_SIMPLELINK
14491#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_SIMPLELINK
14493 if (nc->
err != 0)
goto out;
14497 DBG((
"%p to %s:%d sock %d %d err %d", nc,
inet_ntoa(sa->
sin.sin_addr),
14516 if (sock < 0)
return sock;
14584 DBG((
"%p: failed to accept: %d", lc, sock));
14605 (
sa->sa.sa_family ==
AF_INET) ?
sizeof(
sa->sin) :
sizeof(
sa->sin6);
14607 if (sock < 0)
return sock;
14608#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_SIMPLELINK
14624#define _MG_F_FD_CAN_READ 1
14625#define _MG_F_FD_CAN_WRITE 1 << 1
14626#define _MG_F_FD_ERROR 1 << 2
14629 DBG((
"%p fd=%d fd_flags=%d nc_flags=0x%lx rmbl=%d smbl=%d", nc, nc->
sock,
14643 DBG((
"%p conn res=%d", nc, nc->
err));
14680 DBG((
"%p after fd=%d nc_flags=0x%lx rmbl=%d smbl=%d", nc, nc->
sock, nc->
flags,
14688 DBG((
"%p %d", nc, sock));
14693 DBG((
"%p using sl_Select()", iface->
mgr));
14776 DBG((
"sl_Select @ %ld num_ev=%d of %d, timeout=%d", (
long)
now,
num_ev,
14826 if (
res == 0)
continue;
14838#define MG_SL_IFACE_VTABLE \
14842 mg_sl_if_add_conn, \
14843 mg_sl_if_remove_conn, \
14845 mg_sl_if_listen_tcp, \
14846 mg_sl_if_listen_udp, \
14847 mg_sl_if_connect_tcp, \
14848 mg_sl_if_connect_udp, \
14849 mg_sl_if_tcp_send, \
14850 mg_sl_if_udp_send, \
14851 mg_sl_if_tcp_recv, \
14852 mg_sl_if_udp_recv, \
14853 mg_sl_if_create_conn, \
14854 mg_sl_if_destroy_conn, \
14855 mg_sl_if_sock_set, \
14856 mg_sl_if_get_conn_addr, \
14861#if MG_NET_IF == MG_NET_IF_SIMPLELINK
14866#ifdef MG_MODULE_LINES
14867#line 1 "common/platforms/simplelink/sl_ssl_if.c"
14886#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_SIMPLELINK
14890#ifndef MG_SSL_IF_SIMPLELINK_SLFS_PREFIX
14891#define MG_SSL_IF_SIMPLELINK_SLFS_PREFIX "SL:"
14894#define MG_SSL_IF_SIMPLELINK_SLFS_PREFIX_LEN \
14895 (sizeof(MG_SSL_IF_SIMPLELINK_SLFS_PREFIX) - 1)
14916 nc->ssl_if_data = ctx;
14918 if (params->cert !=
NULL || params->key !=
NULL) {
14919 if (params->cert !=
NULL && params->key !=
NULL) {
14920 ctx->ssl_cert =
strdup(params->cert);
14921 ctx->ssl_key =
strdup(params->key);
14927 if (params->ca_cert !=
NULL &&
strcmp(params->ca_cert,
"*") != 0) {
14928 ctx->ssl_ca_cert =
strdup(params->ca_cert);
14931 if (params->server_name !=
NULL) {
14932 ctx->ssl_server_name =
strdup(params->server_name);
14969 if (ctx ==
NULL)
return;
14970 nc->ssl_if_data =
NULL;
14974 MG_FREE(ctx->ssl_server_name);
14975 memset(ctx, 0,
sizeof(*ctx));
15020#if MG_ENABLE_FILESYSTEM && defined(MG_FS_SLFS)
15032 bool result =
false;
15060 DBG((
"%p ssl ctx: %p", nc, ctx));
15062 if (ctx ==
NULL)
return 0;
15063 DBG((
"%p %s,%s,%s,%s", nc, (ctx->ssl_cert ? ctx->ssl_cert :
"-"),
15067 if (ctx->ssl_cert !=
NULL && ctx->ssl_key !=
NULL) {
15088 if (err != 0)
return err;
15090 if (ctx->ssl_ca_cert !=
NULL) {
15091 if (ctx->ssl_ca_cert[0] !=
'\0') {
15102 if (err != 0)
return err;
15105 if (ctx->ssl_server_name !=
NULL) {
15108 ctx->ssl_server_name,
strlen(ctx->ssl_server_name));
15109 DBG((
"DOMAIN_NAME_VERIFICATION %s -> %d", ctx->ssl_server_name, err));
15120#ifdef MG_MODULE_LINES
15121#line 1 "common/platforms/lwip/mg_lwip_net_if.h"
15140#ifndef CS_COMMON_PLATFORMS_LWIP_MG_NET_IF_LWIP_H_
15141#define CS_COMMON_PLATFORMS_LWIP_MG_NET_IF_LWIP_H_
15143#ifndef MG_ENABLE_NET_IF_LWIP_LOW_LEVEL
15144#define MG_ENABLE_NET_IF_LWIP_LOW_LEVEL MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL
15147#if MG_ENABLE_NET_IF_LWIP_LOW_LEVEL
15188#ifdef MG_MODULE_LINES
15189#line 1 "common/platforms/lwip/mg_lwip_net_if.c"
15208#if MG_ENABLE_NET_IF_LWIP_LOW_LEVEL
15212#include <lwip/init.h>
15213#include <lwip/pbuf.h>
15214#include <lwip/tcp.h>
15215#include <lwip/tcpip.h>
15216#if ((LWIP_VERSION_MAJOR << 8) | LWIP_VERSION_MINOR) >= 0x0105
15217#include <lwip/priv/tcp_priv.h>
15219#include <lwip/tcp_impl.h>
15221#include <lwip/udp.h>
15231#define ip_2_ip4(addr) ipX_2_ip(addr)
15233#define ip_2_ip4(addr) (addr)
15242#define TCP_NEW tcp_new_ip6
15243#define TCP_BIND tcp_bind_ip6
15244#define UDP_BIND udp_bind_ip6
15245#define IPADDR_NTOA(x) ip6addr_ntoa((const ip6_addr_t *)(x))
15246#define SET_ADDR(dst, src) \
15247 memcpy((dst)->sin6.sin6_addr.s6_addr, (src)->ip6.addr, \
15248 sizeof((dst)->sin6.sin6_addr.s6_addr))
15250#define TCP_NEW tcp_new
15251#define TCP_BIND tcp_bind
15252#define UDP_BIND udp_bind
15253#define IPADDR_NTOA ipaddr_ntoa
15254#define SET_ADDR(dst, src) (dst)->sin.sin_addr.s_addr = ip_2_ip4(src)->addr
15258#define tcpip_callback(fn, arg) (fn)(arg)
15268#if defined(RTOS_SDK) || defined(ESP_PLATFORM)
15273#define mgos_unlock()
15278#if LWIP_TCP_KEEPALIVE
15287 tpcb->keep_idle =
idle * 1000;
15288 tpcb->keep_intvl =
interval * 1000;
15289 tpcb->keep_cnt =
count;
15295#elif !defined(MG_NO_LWIP_TCP_KEEPALIVE)
15296#warning LWIP TCP keepalive is disabled. Please consider enabling it.
15302 tpcb->remote_port,
err));
15309#if LWIP_TCP_KEEPALIVE
15318 DBG((
"%p conn error %d", nc,
err));
15335 DBG((
"%p %p %p %p %u %d",
nc,
cs, tpcb, p, (p !=
NULL ? p->tot_len : 0),
15339 if (
cs->rx_chain !=
NULL) {
15352 }
else if (
nc ==
NULL) {
15360 if (p->next !=
NULL) {
15361 struct pbuf *
q = p->next;
15365 if (
cs->rx_chain ==
NULL) {
15387 DBG((
"%p %p %u %p %p", nc, tpcb, num_sent, tpcb->unsent, tpcb->unacked));
15413 cs->pcb.tcp = tpcb;
15421 DBG((
"%p tcp_bind = %d", nc,
cs->
err));
15427 DBG((
"%p tcp_connect %p = %d", nc, tpcb,
cs->
err));
15444#if ((LWIP_VERSION_MAJOR << 8) | LWIP_VERSION_MINOR) >= 0x0105
15462#if ((LWIP_VERSION_MAJOR << 8) | LWIP_VERSION_MINOR) >= 0x0105
15465 sa->sin.sin_addr.s_addr =
addr->addr;
15467 sa->sin.sin_port =
htons(port);
15479 if (
cs->rx_chain ==
NULL) {
15484 if (!
cs->recv_pending) {
15485 cs->recv_pending = 1;
15500 if (
cs->rx_chain !=
NULL) {
15519 DBG((
"%p udp_bind %p = %d", nc, upcb,
cs->
err));
15522 cs->pcb.udp = upcb;
15539 if (
cs->pcb.tcp ==
NULL)
return;
15543 sa.
sin.sin_port =
htons(tpcb->remote_port);
15552 (
"%p conn %p from %s:%u", lc,
newtpcb,
15560#if TCP_LISTEN_BACKLOG
15577#if LWIP_TCP_KEEPALIVE
15609 cs->pcb.tcp = tpcb;
15635 cs->pcb.udp = upcb;
15662 size_t len =
MIN(tpcb->mss,
MIN(ctx->
len, tpcb->snd_buf));
15665 DBG((
"%p no buf avail %u %u %p %p", tpcb, tpcb->snd_buf, tpcb->snd_queuelen,
15666 tpcb->unsent, tpcb->unacked));
15671 unsent = (tpcb->unsent !=
NULL ? tpcb->unsent->len : 0);
15672 unacked = (tpcb->unacked !=
NULL ? tpcb->unacked->len : 0);
15679#if CS_PLATFORM == CS_P_ESP8266
15687 unsent = (tpcb->unsent !=
NULL ? tpcb->unsent->len : 0);
15688 unacked = (tpcb->unacked !=
NULL ? tpcb->unacked->len : 0);
15708 if (tpcb ==
NULL)
return -1;
15709 if (tpcb->snd_buf <= 0)
return 0;
15733#if defined(LWIP_IPV4) && LWIP_IPV4 && defined(LWIP_IPV6) && LWIP_IPV6
15734 ip_addr_t ip = {.u_addr.ip4.addr = nc->
sa.
sin.sin_addr.s_addr, .type = 0};
15739 if (p ==
NULL)
return 0;
15745 return (
cs->err ==
ERR_OK ? (
int) len : -2);
15759#if CS_PLATFORM == CS_P_ESP8266
15784 while (
cs->rx_chain !=
NULL && len > 0) {
15794 if (
cs->rx_offset ==
cs->rx_chain->len) {
15811 if (
cs ==
NULL)
return 0;
15826 if (tpcb !=
NULL) {
15828 DBG((
"%p tcp_close %p", nc, tpcb));
15832 while (
cs->rx_chain !=
NULL) {
15842 if (upcb !=
NULL) {
15843 DBG((
"%p udp_remove %p", nc, upcb));
15854 memset(sa, 0,
sizeof(*sa));
15861 }
else if (upcb !=
NULL) {
15862 sa->
sin.sin_port =
htons(upcb->local_port);
15869 }
else if (tpcb !=
NULL) {
15870 sa->
sin.sin_port =
htons(tpcb->local_port);
15881#define MG_LWIP_IFACE_VTABLE \
15885 mg_lwip_if_add_conn, \
15886 mg_lwip_if_remove_conn, \
15888 mg_lwip_if_listen_tcp, \
15889 mg_lwip_if_listen_udp, \
15890 mg_lwip_if_connect_tcp, \
15891 mg_lwip_if_connect_udp, \
15892 mg_lwip_if_tcp_send, \
15893 mg_lwip_if_udp_send, \
15894 mg_lwip_if_tcp_recv, \
15895 mg_lwip_if_udp_recv, \
15896 mg_lwip_if_create_conn, \
15897 mg_lwip_if_destroy_conn, \
15898 mg_lwip_if_sock_set, \
15899 mg_lwip_if_get_conn_addr, \
15904#if MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL
15909#ifdef MG_MODULE_LINES
15910#line 1 "common/platforms/lwip/mg_lwip_ev_mgr.c"
15929#if MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL
15931#ifndef MG_SIG_QUEUE_LEN
15932#define MG_SIG_QUEUE_LEN 32
15957 md->sig_queue_len++;
15965 while (
md->sig_queue_len > 0) {
15967 int i =
md->start_index;
15968 int sig =
md->sig_queue[
i].sig;
15972 md->sig_queue_len--;
15985 cs->recv_pending = 0;
16021 if (
md->sig_queue[
i].nc == nc) {
16035 DBG((
"begin poll @%u", (
unsigned int) (
now * 1000)));
16045 cs->pcb.tcp->unsent !=
NULL) {
16060 if (
cs->rx_chain !=
NULL) {
16062 }
else if (
cs->draining_rx_chain) {
16072 DBG((
"end poll @%u, %d conns, %d timers (min %u), next in %d ms",
16081#ifdef MG_MODULE_LINES
16082#line 1 "common/platforms/wince/wince_libc.c"
16108 static char buf[10];
16113int open(
const char *filename,
int oflag,
int pmode) {
16158 ft.dwHighDateTime = (
filetime & 0xFFFFFFFF00000000) >> 32;
16170#ifdef MG_MODULE_LINES
16171#line 1 "common/platforms/pic32/pic32_net_if.h"
16190#ifndef CS_COMMON_PLATFORMS_PIC32_NET_IF_H_
16191#define CS_COMMON_PLATFORMS_PIC32_NET_IF_H_
16199#ifndef MG_ENABLE_NET_IF_PIC32
16200#define MG_ENABLE_NET_IF_PIC32 MG_NET_IF == MG_NET_IF_PIC32
16210#ifdef MG_MODULE_LINES
16211#line 1 "common/platforms/pic32/pic32_net_if.c"
16230#if MG_ENABLE_NET_IF_PIC32
16328 sa.sin.sin_port =
htons(
si.remotePort);
16329 sa.sin.sin_addr.s_addr =
si.remoteIPaddress.v4Add.Val;
16341 static char addr[17];
16343 (
int) in.S_un.S_un_b.s_b2, (
int) in.S_un.S_un_b.s_b3,
16344 (
int) in.S_un.S_un_b.s_b4);
16370 if (bytes_written != 0) {
16376 bytes_written) != bytes_written) {
16391 if (bytes_read != 0 &&
16404 if (bytes_read != 0) {
16419 if (bytes_read != 0) {
16490#define MG_PIC32_IFACE_VTABLE \
16492 mg_pic32_if_init, \
16493 mg_pic32_if_free, \
16494 mg_pic32_if_add_conn, \
16495 mg_pic32_if_remove_conn, \
16496 mg_pic32_if_poll, \
16497 mg_pic32_if_listen_tcp, \
16498 mg_pic32_if_listen_udp, \
16499 mg_pic32_if_connect_tcp, \
16500 mg_pic32_if_connect_udp, \
16501 mg_pic32_if_tcp_send, \
16502 mg_pic32_if_udp_send, \
16503 mg_pic32_if_recved, \
16504 mg_pic32_if_create_conn, \
16505 mg_pic32_if_destroy_conn, \
16506 mg_pic32_if_sock_set, \
16507 mg_pic32_if_get_conn_addr, \
16512#if MG_NET_IF == MG_NET_IF_PIC32
16517#ifdef MG_MODULE_LINES
16518#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[]
#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
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)
#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)
MG_INTERNAL int mg_get_errno(void)
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 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 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
const struct mg_iface_vtable mg_null_iface_vtable
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_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)
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)
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)
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_CTL_MSG_MESSAGE_SIZE
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)
static void mg_http_conn_destructor(void *proto_data)
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)