mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-26 22:41:03 +08:00
Fix #1465 - rename LL_* constants, introduce MG_* log macros
This commit is contained in:
parent
da72dafbfb
commit
969b84043c
@ -26,7 +26,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
} else if (ev == MG_EV_READ) {
|
||||
struct mg_dns_rr rr; // Parse first question, offset 12 is header size
|
||||
size_t n = mg_dns_parse_rr(c->recv.buf, c->recv.len, 12, true, &rr);
|
||||
LOG(LL_INFO, ("DNS request parsed, result=%d", (int) n));
|
||||
MG_INFO(("DNS request parsed, result=%d", (int) n));
|
||||
if (n > 0) {
|
||||
char buf[512];
|
||||
struct mg_dns_header *h = (struct mg_dns_header *) buf;
|
||||
|
@ -89,7 +89,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
struct user *u = getuser(hm);
|
||||
// LOG(LL_INFO, ("%p [%.*s] auth %s", c->fd, (int) hm->uri.len, hm->uri.ptr,
|
||||
// MG_INFO(("%p [%.*s] auth %s", c->fd, (int) hm->uri.len, hm->uri.ptr,
|
||||
// u ? u->name : "NULL"));
|
||||
if (u == NULL && mg_http_match_uri(hm, "/api/#")) {
|
||||
// All URIs starting with /api/ must be authenticated
|
||||
|
@ -39,7 +39,7 @@ void app_main(void) {
|
||||
esp_vfs_spiffs_conf_t conf = {
|
||||
.base_path = FS_ROOT, .max_files = 20, .format_if_mount_failed = true};
|
||||
int res = esp_vfs_spiffs_register(&conf);
|
||||
LOG(res == ESP_OK ? LL_INFO : LL_ERROR, ("FS %s, %d", conf.base_path, res));
|
||||
MG_INFO(("FS %s, %d", conf.base_path, res));
|
||||
mg_file_printf(&mg_fs_posix, FS_ROOT "/hello.txt", "%s", "hello from ESP");
|
||||
|
||||
// Setup wifi. This function is implemented in wifi.c
|
||||
@ -52,6 +52,6 @@ void app_main(void) {
|
||||
mg_log_set("3");
|
||||
mg_mgr_init(&mgr);
|
||||
mg_http_listen(&mgr, "http://0.0.0.0:80", cb, &mgr); // Listening server
|
||||
LOG(LL_INFO, ("Starting Mongoose web server v%s", MG_VERSION));
|
||||
MG_INFO(("Starting Mongoose web server v%s", MG_VERSION));
|
||||
for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
|
||||
}
|
||||
|
@ -35,14 +35,14 @@ static void event_handler(void *arg, esp_event_base_t event_base,
|
||||
if (s_retry_num < 3) {
|
||||
esp_wifi_connect();
|
||||
s_retry_num++;
|
||||
LOG(LL_INFO, ("retry to connect to the AP"));
|
||||
MG_INFO(("retry to connect to the AP"));
|
||||
} else {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
}
|
||||
LOG(LL_ERROR, ("connect to the AP fail"));
|
||||
MG_ERROR(("connect to the AP fail"));
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
|
||||
LOG(LL_INFO, ("got ip:" IPSTR, IP2STR(&event->ip_info.ip)));
|
||||
MG_INFO(("got ip:" IPSTR, IP2STR(&event->ip_info.ip)));
|
||||
s_retry_num = 0;
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
@ -81,18 +81,18 @@ void wifi_init(const char *ssid, const char *pass) {
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &c));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
LOG(LL_DEBUG, ("wifi_init_sta finished."));
|
||||
MG_DEBUG(("wifi_init_sta finished."));
|
||||
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
LOG(LL_INFO, ("connected to ap SSID:%s password:%s", ssid, pass));
|
||||
MG_INFO(("connected to ap SSID:%s password:%s", ssid, pass));
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
LOG(LL_ERROR, ("Failed to connect to SSID:%s, password:%s", ssid, pass));
|
||||
MG_ERROR(("Failed to connect to SSID:%s, password:%s", ssid, pass));
|
||||
} else {
|
||||
LOG(LL_ERROR, ("UNEXPECTED EVENT"));
|
||||
MG_ERROR(("UNEXPECTED EVENT"));
|
||||
}
|
||||
|
||||
/* The event will not be processed after unregister */
|
||||
|
@ -26,7 +26,7 @@ static void cb2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
mg_printf(c, "GET / HTTP/1.0\r\nHost: %.*s\r\n\r\n", (int) s.len, s.ptr);
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = ev_data; // Print HTTP response
|
||||
LOG(LL_INFO, ("Fetched:\n%.*s", (int) hm->message.len, hm->message.ptr));
|
||||
MG_INFO(("Fetched:\n%.*s", (int) hm->message.len, hm->message.ptr));
|
||||
c->is_closing = 1;
|
||||
}
|
||||
}
|
||||
@ -38,7 +38,7 @@ static void run_mongoose(void) {
|
||||
mg_mgr_init(&mgr);
|
||||
mg_http_listen(&mgr, SERVER_URL, cb, &mgr); // Listening server
|
||||
mg_http_connect(&mgr, CLIENT_URL, cb2, &mgr); // Example client
|
||||
LOG(LL_INFO, ("Starting Mongoose web server v%s", MG_VERSION));
|
||||
MG_INFO(("Starting Mongoose web server v%s", MG_VERSION));
|
||||
for (;;) mg_mgr_poll(&mgr, 1000);
|
||||
mg_mgr_free(&mgr);
|
||||
}
|
||||
|
@ -33,14 +33,14 @@ static void event_handler(void *arg, esp_event_base_t event_base,
|
||||
if (s_retry_num < 3) {
|
||||
esp_wifi_connect();
|
||||
s_retry_num++;
|
||||
LOG(LL_DEBUG, ("retry to connect to the AP"));
|
||||
MG_DEBUG(("retry to connect to the AP"));
|
||||
} else {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
}
|
||||
LOG(LL_ERROR, ("connect to the AP fail"));
|
||||
MG_ERROR(("connect to the AP fail"));
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
|
||||
LOG(LL_INFO, ("got ip:%s", ip4addr_ntoa(&event->ip_info.ip)));
|
||||
MG_INFO(("got ip:%s", ip4addr_ntoa(&event->ip_info.ip)));
|
||||
s_retry_num = 0;
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
@ -71,11 +71,11 @@ void wifi_init(const char *ssid, const char *pass) {
|
||||
pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
LOG(LL_INFO, ("connected to ap SSID:%s password:%s", ssid, pass));
|
||||
MG_INFO(("connected to ap SSID:%s password:%s", ssid, pass));
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
LOG(LL_ERROR, ("Failed to connect to SSID:%s, password:%s", ssid, pass));
|
||||
MG_ERROR(("Failed to connect to SSID:%s, password:%s", ssid, pass));
|
||||
} else {
|
||||
LOG(LL_ERROR, ("UNEXPECTED EVENT"));
|
||||
MG_ERROR(("UNEXPECTED EVENT"));
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP,
|
||||
|
@ -14,18 +14,18 @@
|
||||
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (ev == MG_EV_HTTP_CHUNK && mg_http_match_uri(hm, "/upload")) {
|
||||
LOG(LL_INFO, ("Got chunk len %lu", (unsigned long) hm->chunk.len));
|
||||
LOG(LL_INFO, ("Query string: [%.*s]", (int) hm->query.len, hm->query.ptr));
|
||||
LOG(LL_INFO, ("Chunk data:\n%.*s", (int) hm->chunk.len, hm->chunk.ptr));
|
||||
MG_INFO(("Got chunk len %lu", (unsigned long) hm->chunk.len));
|
||||
MG_INFO(("Query string: [%.*s]", (int) hm->query.len, hm->query.ptr));
|
||||
MG_INFO(("Chunk data:\n%.*s", (int) hm->chunk.len, hm->chunk.ptr));
|
||||
mg_http_delete_chunk(c, hm);
|
||||
if (hm->chunk.len == 0) {
|
||||
LOG(LL_INFO, ("Last chunk received, sending response"));
|
||||
MG_INFO(("Last chunk received, sending response"));
|
||||
mg_http_reply(c, 200, "", "ok (chunked)\n");
|
||||
}
|
||||
} else if (ev == MG_EV_HTTP_MSG && mg_http_match_uri(hm, "/upload")) {
|
||||
LOG(LL_INFO, ("Got all %lu bytes!", (unsigned long) hm->body.len));
|
||||
LOG(LL_INFO, ("Query string: [%.*s]", (int) hm->query.len, hm->query.ptr));
|
||||
LOG(LL_INFO, ("Body:\n%.*s", (int) hm->body.len, hm->body.ptr));
|
||||
MG_INFO(("Got all %lu bytes!", (unsigned long) hm->body.len));
|
||||
MG_INFO(("Query string: [%.*s]", (int) hm->query.len, hm->query.ptr));
|
||||
MG_INFO(("Body:\n%.*s", (int) hm->body.len, hm->body.ptr));
|
||||
mg_http_reply(c, 200, "", "ok (%lu)\n", (unsigned long) hm->body.len);
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_serve_opts opts = {.root_dir = "web_root"};
|
||||
|
@ -19,16 +19,15 @@
|
||||
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
LOG(LL_INFO, ("New request to: [%.*s], body size: %lu", (int) hm->uri.len,
|
||||
hm->uri.ptr, (unsigned long) hm->body.len));
|
||||
MG_INFO(("New request to: [%.*s], body size: %lu", (int) hm->uri.len,
|
||||
hm->uri.ptr, (unsigned long) hm->body.len));
|
||||
if (mg_http_match_uri(hm, "/upload")) {
|
||||
struct mg_http_part part;
|
||||
size_t ofs = 0;
|
||||
while ((ofs = mg_http_next_multipart(hm->body, ofs, &part)) > 0) {
|
||||
LOG(LL_INFO,
|
||||
("Chunk name: [%.*s] filename: [%.*s] length: %lu bytes",
|
||||
(int) part.name.len, part.name.ptr, (int) part.filename.len,
|
||||
part.filename.ptr, (unsigned long) part.body.len));
|
||||
MG_INFO(("Chunk name: [%.*s] filename: [%.*s] length: %lu bytes",
|
||||
(int) part.name.len, part.name.ptr, (int) part.filename.len,
|
||||
part.filename.ptr, (unsigned long) part.body.len));
|
||||
}
|
||||
mg_http_reply(c, 200, "", "Thank you!");
|
||||
} else {
|
||||
|
@ -37,7 +37,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
struct mg_str host = mg_url_host(url);
|
||||
// CONNECT response - tunnel is established
|
||||
connected = true;
|
||||
LOG(LL_DEBUG,
|
||||
MG_DEBUG(
|
||||
("Connected to proxy, status: %.*s", (int) hm.uri.len, hm.uri.ptr));
|
||||
mg_iobuf_del(&c->recv, 0, n);
|
||||
// Send request to the target server
|
||||
|
@ -28,8 +28,8 @@ static void forward_request(struct mg_http_message *hm,
|
||||
}
|
||||
mg_send(c, "\r\n", 2);
|
||||
mg_send(c, hm->body.ptr, hm->body.len);
|
||||
LOG(LL_DEBUG, ("FORWARDING: %.*s %.*s", (int) hm->method.len, hm->method.ptr,
|
||||
(int) hm->uri.len, hm->uri.ptr));
|
||||
MG_DEBUG(("FORWARDING: %.*s %.*s", (int) hm->method.len, hm->method.ptr,
|
||||
(int) hm->uri.len, hm->uri.ptr));
|
||||
}
|
||||
|
||||
static void fn2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
|
@ -29,9 +29,9 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
mg_http_parse((char *) c->send.buf, c->send.len, &tmp);
|
||||
cl = mg_http_get_header(&tmp, "Content-Length");
|
||||
if (cl == NULL) cl = &unknown;
|
||||
LOG(LL_INFO, ("%.*s %.*s %.*s %.*s", (int) hm->method.len, hm->method.ptr,
|
||||
(int) hm->uri.len, hm->uri.ptr, (int) tmp.uri.len,
|
||||
tmp.uri.ptr, (int) cl->len, cl->ptr));
|
||||
MG_INFO(("%.*s %.*s %.*s %.*s", (int) hm->method.len, hm->method.ptr,
|
||||
(int) hm->uri.len, hm->uri.ptr, (int) tmp.uri.len, tmp.uri.ptr,
|
||||
(int) cl->len, cl->ptr));
|
||||
}
|
||||
(void) fn_data;
|
||||
}
|
||||
@ -86,18 +86,18 @@ int main(int argc, char *argv[]) {
|
||||
mg_log_set(s_debug_level);
|
||||
mg_mgr_init(&mgr);
|
||||
if ((c = mg_http_listen(&mgr, s_listening_address, cb, &mgr)) == NULL) {
|
||||
LOG(LL_ERROR, ("Cannot listen on %s. Use http://ADDR:PORT or :PORT",
|
||||
s_listening_address));
|
||||
MG_ERROR(("Cannot listen on %s. Use http://ADDR:PORT or :PORT",
|
||||
s_listening_address));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (mg_casecmp(s_enable_hexdump, "yes") == 0) c->is_hexdumping = 1;
|
||||
|
||||
// Start infinite event loop
|
||||
LOG(LL_INFO, ("Mongoose version : v%s", MG_VERSION));
|
||||
LOG(LL_INFO, ("Listening on : %s", s_listening_address));
|
||||
LOG(LL_INFO, ("Web root : [%s]", s_root_dir));
|
||||
MG_INFO(("Mongoose version : v%s", MG_VERSION));
|
||||
MG_INFO(("Listening on : %s", s_listening_address));
|
||||
MG_INFO(("Web root : [%s]", s_root_dir));
|
||||
while (s_signo == 0) mg_mgr_poll(&mgr, 1000);
|
||||
mg_mgr_free(&mgr);
|
||||
LOG(LL_INFO, ("Exiting on signal %d", s_signo));
|
||||
MG_INFO(("Exiting on signal %d", s_signo));
|
||||
return 0;
|
||||
}
|
||||
|
@ -38,13 +38,13 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
mg_url_uri(s_url), (int) host.len, host.ptr);
|
||||
} else if (ev == MG_EV_HTTP_CHUNK) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
LOG(LL_INFO, ("%.*s", (int) hm->chunk.len, hm->chunk.ptr));
|
||||
MG_INFO(("%.*s", (int) hm->chunk.len, hm->chunk.ptr));
|
||||
mg_http_delete_chunk(c, hm);
|
||||
if (hm->chunk.len == 0) *(bool *) fn_data = true; // Last chunk
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
// Response is received. Print it
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
LOG(LL_INFO, ("%.*s", (int) hm->body.len, hm->body.ptr));
|
||||
MG_INFO(("%.*s", (int) hm->body.len, hm->body.ptr));
|
||||
c->is_closing = 1; // Tell mongoose to close this connection
|
||||
*(bool *) fn_data = true; // Tell event loop to stop
|
||||
} else if (ev == MG_EV_ERROR) {
|
||||
|
@ -81,7 +81,7 @@ int main(void) {
|
||||
srand(time(NULL));
|
||||
mg_timer_init(&t1, 1000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, NULL);
|
||||
mg_http_listen(&mgr, s_listen_on, fn, NULL);
|
||||
LOG(LL_INFO, ("Listening on %s", s_listen_on));
|
||||
MG_INFO(("Listening on %s", s_listen_on));
|
||||
for (;;) mg_mgr_poll(&mgr, 1000);
|
||||
mg_timer_free(&t1);
|
||||
mg_mgr_free(&mgr);
|
||||
|
@ -49,7 +49,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
char *response = NULL;
|
||||
jsonrpc_process(req.ptr, req.len, mjson_print_dynamic_buf, &response, NULL);
|
||||
mg_ws_send(c, response, strlen(response), WEBSOCKET_OP_TEXT);
|
||||
LOG(LL_INFO, ("[%.*s] -> [%s]", (int) req.len, req.ptr, response));
|
||||
MG_INFO(("[%.*s] -> [%s]", (int) req.len, req.ptr, response));
|
||||
free(response);
|
||||
}
|
||||
(void) fn_data;
|
||||
|
@ -43,7 +43,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
// c->is_hexdumping = 1;
|
||||
} else if (ev == MG_EV_ERROR) {
|
||||
// On error, log error message
|
||||
LOG(LL_ERROR, ("%p %s", c->fd, (char *) ev_data));
|
||||
MG_ERROR(("%p %s", c->fd, (char *) ev_data));
|
||||
} else if (ev == MG_EV_CONNECT) {
|
||||
// Set up 2-way TLS that is required by AWS IoT
|
||||
struct mg_tls_opts opts = {
|
||||
@ -52,28 +52,28 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
} else if (ev == MG_EV_MQTT_OPEN) {
|
||||
// MQTT connect is successful
|
||||
struct mg_str topic = mg_str(s_rx_topic);
|
||||
LOG(LL_INFO, ("Connected to %s", s_url));
|
||||
LOG(LL_INFO, ("Subscribing to %s", s_rx_topic));
|
||||
MG_INFO(("Connected to %s", s_url));
|
||||
MG_INFO(("Subscribing to %s", s_rx_topic));
|
||||
mg_mqtt_sub(c, topic, s_qos);
|
||||
c->label[0] = 'X'; // Set a label that we're logged in
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
// When we receive MQTT message, print it
|
||||
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
||||
LOG(LL_INFO, ("Received on %.*s : %.*s", (int) mm->topic.len, mm->topic.ptr,
|
||||
(int) mm->data.len, mm->data.ptr));
|
||||
MG_INFO(("Received on %.*s : %.*s", (int) mm->topic.len, mm->topic.ptr,
|
||||
(int) mm->data.len, mm->data.ptr));
|
||||
} else if (ev == MG_EV_POLL && c->label[0] == 'X') {
|
||||
static unsigned long prev_second;
|
||||
unsigned long now_second = (*(unsigned long *) ev_data) / 1000;
|
||||
if (now_second != prev_second) {
|
||||
struct mg_str topic = mg_str(s_tx_topic), data = mg_str("{\"a\":123}");
|
||||
LOG(LL_INFO, ("Publishing to %s", s_tx_topic));
|
||||
MG_INFO(("Publishing to %s", s_tx_topic));
|
||||
mg_mqtt_pub(c, topic, data, s_qos, false);
|
||||
prev_second = now_second;
|
||||
}
|
||||
}
|
||||
|
||||
if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE) {
|
||||
LOG(LL_INFO, ("Got event %d, stopping...", ev));
|
||||
MG_INFO(("Got event %d, stopping...", ev));
|
||||
*(bool *) fn_data = true; // Signal that we're done
|
||||
}
|
||||
}
|
||||
@ -83,7 +83,7 @@ int main(void) {
|
||||
struct mg_mqtt_opts opts = {.clean = true};
|
||||
bool done = false;
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
LOG(LL_INFO, ("Connecting to %s", s_url)); // Inform that we're starting
|
||||
MG_INFO(("Connecting to %s", s_url)); // Inform that we're starting
|
||||
mg_mqtt_connect(&mgr, s_url, &opts, fn, &done); // Create client connection
|
||||
while (!done) mg_mgr_poll(&mgr, 1000); // Loop until done
|
||||
mg_mgr_free(&mgr); // Finished, cleanup
|
||||
|
@ -27,11 +27,11 @@ static void signal_handler(int signo) {
|
||||
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_OPEN) {
|
||||
LOG(LL_INFO, ("CREATED"));
|
||||
MG_INFO(("CREATED"));
|
||||
// c->is_hexdumping = 1;
|
||||
} else if (ev == MG_EV_ERROR) {
|
||||
// On error, log error message
|
||||
LOG(LL_ERROR, ("%p %s", c->fd, (char *) ev_data));
|
||||
MG_ERROR(("%p %s", c->fd, (char *) ev_data));
|
||||
} else if (ev == MG_EV_CONNECT) {
|
||||
// If target URL is SSL/TLS, command client connection to use TLS
|
||||
if (mg_url_is_ssl(s_url)) {
|
||||
@ -42,21 +42,21 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
// MQTT connect is successful
|
||||
struct mg_str subt = mg_str(s_sub_topic);
|
||||
struct mg_str pubt = mg_str(s_pub_topic), data = mg_str("hello");
|
||||
LOG(LL_INFO, ("CONNECTED to %s", s_url));
|
||||
MG_INFO(("CONNECTED to %s", s_url));
|
||||
mg_mqtt_sub(c, subt, s_qos);
|
||||
LOG(LL_INFO, ("SUBSCRIBED to %.*s", (int) subt.len, subt.ptr));
|
||||
MG_INFO(("SUBSCRIBED to %.*s", (int) subt.len, subt.ptr));
|
||||
|
||||
mg_mqtt_pub(c, pubt, data, s_qos, false);
|
||||
LOG(LL_INFO, ("PUBSLISHED %.*s -> %.*s", (int) data.len, data.ptr,
|
||||
(int) pubt.len, pubt.ptr));
|
||||
MG_INFO(("PUBSLISHED %.*s -> %.*s", (int) data.len, data.ptr,
|
||||
(int) pubt.len, pubt.ptr));
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
// When we get echo response, print it
|
||||
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
||||
LOG(LL_INFO, ("RECEIVED %.*s <- %.*s", (int) mm->data.len, mm->data.ptr,
|
||||
(int) mm->topic.len, mm->topic.ptr));
|
||||
MG_INFO(("RECEIVED %.*s <- %.*s", (int) mm->data.len, mm->data.ptr,
|
||||
(int) mm->topic.len, mm->topic.ptr));
|
||||
c->is_closing = 1;
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
LOG(LL_INFO, ("CLOSED"));
|
||||
MG_INFO(("CLOSED"));
|
||||
s_conn = NULL; // Mark that we're closed
|
||||
}
|
||||
(void) fn_data;
|
||||
|
@ -18,7 +18,7 @@ static const char *s_topic = "mg/test";
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_ERROR) {
|
||||
// On error, log error message
|
||||
LOG(LL_ERROR, ("%p %s", c->fd, (char *) ev_data));
|
||||
MG_ERROR(("%p %s", c->fd, (char *) ev_data));
|
||||
} else if (ev == MG_EV_CONNECT) {
|
||||
// If target URL is SSL/TLS, command client connection to use TLS
|
||||
if (mg_url_is_ssl(s_url)) {
|
||||
@ -27,7 +27,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
}
|
||||
} else if (ev == MG_EV_WS_OPEN) {
|
||||
// WS connection established. Perform MQTT login
|
||||
LOG(LL_INFO, ("Connected to WS. Logging in to MQTT..."));
|
||||
MG_INFO(("Connected to WS. Logging in to MQTT..."));
|
||||
struct mg_mqtt_opts opts = {.will_qos = 1,
|
||||
.will_topic = mg_str(s_topic),
|
||||
.will_message = mg_str("goodbye")};
|
||||
@ -37,7 +37,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
} else if (ev == MG_EV_WS_MSG) {
|
||||
struct mg_mqtt_message mm;
|
||||
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
|
||||
LOG(LL_INFO, ("GOT %d bytes WS msg", (int) wm->data.len));
|
||||
MG_INFO(("GOT %d bytes WS msg", (int) wm->data.len));
|
||||
while ((mg_mqtt_parse((uint8_t *) wm->data.ptr, wm->data.len, &mm)) == 0) {
|
||||
switch (mm.cmd) {
|
||||
case MQTT_CMD_CONNACK:
|
||||
@ -45,24 +45,24 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (mm.ack == 0) {
|
||||
struct mg_str topic = mg_str(s_topic), data = mg_str("hello");
|
||||
size_t len = c->send.len;
|
||||
LOG(LL_INFO, ("CONNECTED to %s", s_url));
|
||||
MG_INFO(("CONNECTED to %s", s_url));
|
||||
mg_mqtt_sub(c, topic, 1);
|
||||
mg_ws_wrap(c, len, WEBSOCKET_OP_BINARY);
|
||||
LOG(LL_INFO, ("SUBSCRIBED to %.*s", (int) topic.len, topic.ptr));
|
||||
MG_INFO(("SUBSCRIBED to %.*s", (int) topic.len, topic.ptr));
|
||||
mg_mqtt_pub(c, topic, data, 1, false);
|
||||
LOG(LL_INFO, ("PUBSLISHED %.*s -> %.*s", (int) data.len, data.ptr,
|
||||
(int) topic.len, topic.ptr));
|
||||
MG_INFO(("PUBSLISHED %.*s -> %.*s", (int) data.len, data.ptr,
|
||||
(int) topic.len, topic.ptr));
|
||||
len = mg_ws_wrap(c, c->send.len - len, WEBSOCKET_OP_BINARY);
|
||||
} else {
|
||||
LOG(LL_ERROR, ("%lu MQTT auth failed, code %d", c->id, mm.ack));
|
||||
MG_ERROR(("%lu MQTT auth failed, code %d", c->id, mm.ack));
|
||||
c->is_closing = 1;
|
||||
}
|
||||
break;
|
||||
case MQTT_CMD_PUBLISH: {
|
||||
LOG(LL_DEBUG, ("%lu [%.*s] -> [%.*s]", c->id, (int) mm.topic.len,
|
||||
mm.topic.ptr, (int) mm.data.len, mm.data.ptr));
|
||||
LOG(LL_INFO, ("RECEIVED %.*s <- %.*s", (int) mm.data.len, mm.data.ptr,
|
||||
(int) mm.topic.len, mm.topic.ptr));
|
||||
MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) mm.topic.len,
|
||||
mm.topic.ptr, (int) mm.data.len, mm.data.ptr));
|
||||
MG_INFO(("RECEIVED %.*s <- %.*s", (int) mm.data.len, mm.data.ptr,
|
||||
(int) mm.topic.len, mm.topic.ptr));
|
||||
c->is_closing = 1;
|
||||
break;
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ static void signal_handler(int signo) {
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_MQTT_CMD) {
|
||||
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
||||
LOG(LL_DEBUG, ("cmd %d qos %d", mm->cmd, mm->qos));
|
||||
MG_DEBUG(("cmd %d qos %d", mm->cmd, mm->qos));
|
||||
switch (mm->cmd) {
|
||||
case MQTT_CMD_CONNECT: {
|
||||
// Client connects
|
||||
@ -57,7 +57,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
sub->topic = mg_strdup(topic);
|
||||
sub->qos = qos;
|
||||
LIST_ADD_HEAD(struct sub, &s_subs, sub);
|
||||
LOG(LL_INFO,
|
||||
MG_INFO(
|
||||
("SUB %p [%.*s]", c->fd, (int) sub->topic.len, sub->topic.ptr));
|
||||
// Change '+' to '*' for topic matching using mg_match
|
||||
for (size_t i = 0; i < sub->topic.len; i++) {
|
||||
@ -73,8 +73,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
}
|
||||
case MQTT_CMD_PUBLISH: {
|
||||
// Client published message. Push to all subscribed channels
|
||||
LOG(LL_INFO, ("PUB %p [%.*s] -> [%.*s]", c->fd, (int) mm->data.len,
|
||||
mm->data.ptr, (int) mm->topic.len, mm->topic.ptr));
|
||||
MG_INFO(("PUB %p [%.*s] -> [%.*s]", c->fd, (int) mm->data.len,
|
||||
mm->data.ptr, (int) mm->topic.len, mm->topic.ptr));
|
||||
for (struct sub *sub = s_subs; sub != NULL; sub = sub->next) {
|
||||
if (mg_match(mm->topic, sub->topic, NULL)) {
|
||||
mg_mqtt_pub(sub->c, mm->topic, mm->data, 1, false);
|
||||
@ -90,8 +90,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
for (struct sub *next, *sub = s_subs; sub != NULL; sub = next) {
|
||||
next = sub->next;
|
||||
if (c != sub->c) continue;
|
||||
LOG(LL_INFO,
|
||||
("UNSUB %p [%.*s]", c->fd, (int) sub->topic.len, sub->topic.ptr));
|
||||
MG_INFO(("UNSUB %p [%.*s]", c->fd, (int) sub->topic.len, sub->topic.ptr));
|
||||
LIST_DELETE(struct sub, &s_subs, sub);
|
||||
}
|
||||
}
|
||||
@ -103,9 +102,9 @@ int main(void) {
|
||||
signal(SIGINT, signal_handler); // Setup signal handlers - exist event
|
||||
signal(SIGTERM, signal_handler); // manager loop on SIGINT and SIGTERM
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
LOG(LL_INFO, ("Starting on %s", s_listen_on)); // Inform that we're starting
|
||||
mg_mqtt_listen(&mgr, s_listen_on, fn, NULL); // Create MQTT listener
|
||||
while (s_signo == 0) mg_mgr_poll(&mgr, 1000); // Event loop, 1s timeout
|
||||
mg_mgr_free(&mgr); // Cleanup
|
||||
MG_INFO(("Starting on %s", s_listen_on)); // Inform that we're starting
|
||||
mg_mqtt_listen(&mgr, s_listen_on, fn, NULL); // Create MQTT listener
|
||||
while (s_signo == 0) mg_mgr_poll(&mgr, 1000); // Event loop, 1s timeout
|
||||
mg_mgr_free(&mgr); // Cleanup
|
||||
return 0;
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
struct user *u = getuser(hm);
|
||||
// LOG(LL_INFO, ("[%.*s] auth %s", (int) hm->uri.len, hm->uri.ptr,
|
||||
// MG_INFO(("[%.*s] auth %s", (int) hm->uri.len, hm->uri.ptr,
|
||||
// u ? u->name : "NULL"));
|
||||
if (u == NULL && mg_http_match_uri(hm, "/api/#")) {
|
||||
// All URIs starting with /api/ must be authenticated
|
||||
|
@ -20,7 +20,7 @@ static void server(void *args) {
|
||||
struct mg_mgr mgr;
|
||||
mg_log_set(s_debug_level);
|
||||
mg_mgr_init(&mgr);
|
||||
LOG(LL_INFO, ("Starting Mongoose v%s", MG_VERSION)); // Tell the world
|
||||
MG_INFO(("Starting Mongoose v%s", MG_VERSION)); // Tell the world
|
||||
mg_http_listen(&mgr, s_listening_address, cb, &mgr); // Web listener
|
||||
while (args == NULL) mg_mgr_poll(&mgr, 1000); // Infinite event loop
|
||||
mg_mgr_free(&mgr); // Unreachable
|
||||
@ -32,14 +32,14 @@ static void blinker(void *args) {
|
||||
for (;;) {
|
||||
gpio_toggle(pin);
|
||||
vTaskDelay(pdMS_TO_TICKS(ms));
|
||||
LOG(LL_INFO, ("blink %s, RAM: %u", (char *) args, xPortGetFreeHeapSize()));
|
||||
MG_INFO(("blink %s, RAM: %u", (char *) args, xPortGetFreeHeapSize()));
|
||||
}
|
||||
}
|
||||
|
||||
// Start Mongoose server when network is ready
|
||||
void vApplicationIPNetworkEventHook(eIPCallbackEvent_t ev) {
|
||||
static bool mongoose_started = false;
|
||||
LOG(LL_INFO, ("FreeRTOS net event %d, up: %d", ev, eNetworkUp));
|
||||
MG_INFO(("FreeRTOS net event %d, up: %d", ev, eNetworkUp));
|
||||
if (ev == eNetworkUp && mongoose_started == false) {
|
||||
xTaskCreate(server, "server", 8192, NULL, configMAX_PRIORITIES - 1, NULL);
|
||||
mongoose_started = true;
|
||||
@ -49,7 +49,7 @@ void vApplicationIPNetworkEventHook(eIPCallbackEvent_t ev) {
|
||||
static void init_heap(void) {
|
||||
extern uint32_t _end, _estack;
|
||||
uint8_t *ptr = (uint8_t *) ((((uint32_t) &_end) + 7) & ~7U);
|
||||
uint32_t len = (uint32_t)((char *) &_estack - (char *) &_end);
|
||||
uint32_t len = (uint32_t) ((char *) &_estack - (char *) &_end);
|
||||
HeapRegion_t regions[] = {{ptr, len}, {NULL, 0}};
|
||||
vPortDefineHeapRegions(regions);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
static const char *s_ssdp_url = "udp://239.255.255.250:1900";
|
||||
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
LOG(LL_DEBUG, ("%p got event: %d %p %p", c, ev, ev_data, fn_data));
|
||||
MG_DEBUG(("%p got event: %d %p %p", c, ev, ev_data, fn_data));
|
||||
if (ev == MG_EV_OPEN) {
|
||||
c->is_hexdumping = 1;
|
||||
} else if (ev == MG_EV_RESOLVE) {
|
||||
@ -24,7 +24,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
static void tfn(void *param) {
|
||||
struct mg_connection *c = param;
|
||||
if (c == NULL) return;
|
||||
LOG(LL_INFO, ("Sending M-SEARCH"));
|
||||
MG_INFO(("Sending M-SEARCH"));
|
||||
mg_printf(c, "%s",
|
||||
"M-SEARCH * HTTP/1.1\r\n"
|
||||
"HOST: 239.255.255.250:1900\r\n"
|
||||
|
@ -13,7 +13,7 @@ static const char *s_url = "ws://localhost:8000/websocket";
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_ERROR) {
|
||||
// On error, log error message
|
||||
LOG(LL_ERROR, ("%p %s", c->fd, (char *) ev_data));
|
||||
MG_ERROR(("%p %s", c->fd, (char *) ev_data));
|
||||
} else if (ev == MG_EV_WS_OPEN) {
|
||||
// When websocket handshake is successful, send message
|
||||
mg_ws_send(c, "hello", 5, WEBSOCKET_OP_TEXT);
|
||||
|
229
mongoose.c
229
mongoose.c
@ -215,11 +215,11 @@ bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) {
|
||||
|
||||
for (i = 0; i < mg_ntohs(h->num_questions); i++) {
|
||||
if ((n = mg_dns_parse_rr(buf, len, ofs, true, &rr)) == 0) return false;
|
||||
// LOG(LL_INFO, ("Q %zu %zu", ofs, n));
|
||||
// MG_INFO(("Q %zu %zu", ofs, n));
|
||||
ofs += n;
|
||||
}
|
||||
for (i = 0; i < mg_ntohs(h->num_answers); i++) {
|
||||
// LOG(LL_INFO, ("A -- %zu %zu %s", ofs, n, dm->name));
|
||||
// MG_INFO(("A -- %zu %zu %s", ofs, n, dm->name));
|
||||
if ((n = mg_dns_parse_rr(buf, len, ofs, false, &rr)) == 0) return false;
|
||||
mg_dns_parse_name(buf, len, ofs, dm->name, sizeof(dm->name));
|
||||
ofs += n;
|
||||
@ -246,7 +246,7 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
int64_t now = *(int64_t *) ev_data;
|
||||
for (d = s_reqs; d != NULL; d = tmp) {
|
||||
tmp = d->next;
|
||||
// LOG(LL_DEBUG, ("%lu %lu dns poll", d->expire, now));
|
||||
// MG_DEBUG ("%lu %lu dns poll", d->expire, now));
|
||||
if (now > d->expire) mg_error(d->c, "DNS timeout");
|
||||
}
|
||||
} else if (ev == MG_EV_READ) {
|
||||
@ -254,13 +254,13 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
int resolved = 0;
|
||||
if (mg_dns_parse(c->recv.buf, c->recv.len, &dm) == false) {
|
||||
char *s = mg_hexdump(c->recv.buf, c->recv.len);
|
||||
LOG(LL_ERROR, ("Unexpected DNS response:\n%s\n", s));
|
||||
MG_ERROR(("Unexpected DNS response:\n%s\n", s));
|
||||
free(s);
|
||||
} else {
|
||||
LOG(LL_VERBOSE_DEBUG, ("%s %d", dm.name, dm.resolved));
|
||||
MG_VERBOSE(("%s %d", dm.name, dm.resolved));
|
||||
for (d = s_reqs; d != NULL; d = tmp) {
|
||||
tmp = d->next;
|
||||
// LOG(LL_INFO, ("d %p %hu %hu", d, d->txnid, dm.txnid));
|
||||
// MG_INFO(("d %p %hu %hu", d, d->txnid, dm.txnid));
|
||||
if (dm.txnid != d->txnid) continue;
|
||||
if (d->c->is_resolving) {
|
||||
d->c->is_resolving = 0;
|
||||
@ -268,8 +268,8 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
char buf[100];
|
||||
dm.addr.port = d->c->peer.port; // Save port
|
||||
d->c->peer = dm.addr; // Copy resolved address
|
||||
LOG(LL_DEBUG, ("%lu %s resolved to %s", d->c->id, dm.name,
|
||||
mg_ntoa(&d->c->peer, buf, sizeof(buf))));
|
||||
MG_DEBUG(("%lu %s resolved to %s", d->c->id, dm.name,
|
||||
mg_ntoa(&d->c->peer, buf, sizeof(buf))));
|
||||
mg_connect_resolved(d->c);
|
||||
#if MG_ENABLE_IPV6
|
||||
} else if (dm.addr.is_ip6 == false && dm.name[0] != '\0') {
|
||||
@ -280,13 +280,13 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
mg_error(d->c, "%s DNS lookup failed", dm.name);
|
||||
}
|
||||
} else {
|
||||
LOG(LL_ERROR, ("%lu already resolved", d->c->id));
|
||||
MG_ERROR(("%lu already resolved", d->c->id));
|
||||
}
|
||||
mg_dns_free(d);
|
||||
resolved = 1;
|
||||
}
|
||||
}
|
||||
if (!resolved) LOG(LL_ERROR, ("stray DNS reply"));
|
||||
if (!resolved) MG_ERROR(("stray DNS reply"));
|
||||
c->recv.len = 0;
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
for (d = s_reqs; d != NULL; d = tmp) {
|
||||
@ -356,9 +356,9 @@ static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
|
||||
d->expire = mg_millis() + (int64_t) ms;
|
||||
d->c = c;
|
||||
c->is_resolving = 1;
|
||||
LOG(LL_VERBOSE_DEBUG,
|
||||
("%lu resolving %.*s @ %s, txnid %hu", c->id, (int) name->len,
|
||||
name->ptr, mg_ntoa(&dnsc->c->peer, buf, sizeof(buf)), d->txnid));
|
||||
MG_VERBOSE(("%lu resolving %.*s @ %s, txnid %hu", c->id, (int) name->len,
|
||||
name->ptr, mg_ntoa(&dnsc->c->peer, buf, sizeof(buf)),
|
||||
d->txnid));
|
||||
mg_dns_send(dnsc->c, name, d->txnid, ipv6);
|
||||
}
|
||||
}
|
||||
@ -397,7 +397,7 @@ void mg_error(struct mg_connection *c, const char *fmt, ...) {
|
||||
va_start(ap, fmt);
|
||||
mg_vasprintf(&buf, sizeof(mem), fmt, ap);
|
||||
va_end(ap);
|
||||
LOG(LL_ERROR, ("%lu %s", c->id, buf));
|
||||
MG_ERROR(("%lu %s", c->id, buf));
|
||||
c->is_closing = 1; // Set is_closing before sending MG_EV_CALL
|
||||
mg_call(c, MG_EV_ERROR, buf); // Let user handler to override it
|
||||
if (buf != mem) free(buf);
|
||||
@ -753,7 +753,7 @@ struct mg_fs mg_fs_packed = {
|
||||
|
||||
static int p_stat(const char *path, size_t *size, time_t *mtime) {
|
||||
#if !defined(S_ISDIR)
|
||||
LOG(LL_ERROR, ("stat() API is not supported. %p %p %p", path, size, mtime));
|
||||
MG_ERROR(("stat() API is not supported. %p %p %p", path, size, mtime));
|
||||
return 0;
|
||||
#else
|
||||
#if MG_ARCH == MG_ARCH_WIN32
|
||||
@ -1026,7 +1026,7 @@ size_t mg_http_next_multipart(struct mg_str body, size_t ofs,
|
||||
// Skip boundary
|
||||
while (b + 2 < max && s[b] != '\r' && s[b + 1] != '\n') b++;
|
||||
if (b <= ofs || b + 2 >= max) return 0;
|
||||
// LOG(LL_INFO, ("B: %zu %zu [%.*s]", ofs, b - ofs, (int) (b - ofs), s));
|
||||
// MG_INFO(("B: %zu %zu [%.*s]", ofs, b - ofs, (int) (b - ofs), s));
|
||||
|
||||
// Skip headers
|
||||
h1 = h2 = b + 2;
|
||||
@ -1034,7 +1034,7 @@ size_t mg_http_next_multipart(struct mg_str body, size_t ofs,
|
||||
while (h2 + 2 < max && s[h2] != '\r' && s[h2 + 1] != '\n') h2++;
|
||||
if (h2 == h1) break;
|
||||
if (h2 + 2 >= max) return 0;
|
||||
// LOG(LL_INFO, ("Header: [%.*s]", (int) (h2 - h1), &s[h1]));
|
||||
// MG_INFO(("Header: [%.*s]", (int) (h2 - h1), &s[h1]));
|
||||
if (part != NULL && h1 + cd.len + 2 < h2 && s[h1 + cd.len] == ':' &&
|
||||
mg_ncasecmp(&s[h1], cd.ptr, cd.len) == 0) {
|
||||
struct mg_str v = mg_str_n(&s[h1 + cd.len + 2], h2 - (h1 + cd.len + 2));
|
||||
@ -1050,7 +1050,7 @@ size_t mg_http_next_multipart(struct mg_str body, size_t ofs,
|
||||
|
||||
if (b2 + 2 >= max) return 0;
|
||||
if (part != NULL) part->body = mg_str_n(&s[b1], b2 - b1);
|
||||
// LOG(LL_INFO, ("Body: [%.*s]", (int) (b2 - b1), &s[b1]));
|
||||
// MG_INFO(("Body: [%.*s]", (int) (b2 - b1), &s[b1]));
|
||||
return b2 + 2;
|
||||
}
|
||||
|
||||
@ -1076,8 +1076,8 @@ void mg_http_bauth(struct mg_connection *c, const char *user,
|
||||
c->send.len += 21 + (size_t) n + 2;
|
||||
memcpy(&c->send.buf[c->send.len - 2], "\r\n", 2);
|
||||
} else {
|
||||
LOG(LL_ERROR, ("%lu %s cannot resize iobuf %d->%d ", c->id, c->label,
|
||||
(int) c->send.size, (int) need));
|
||||
MG_ERROR(("%lu %s cannot resize iobuf %d->%d ", c->id, c->label,
|
||||
(int) c->send.size, (int) need));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1176,7 +1176,7 @@ static void mg_http_parse_headers(const char *s, const char *end,
|
||||
if (k.len == tmp.len) continue;
|
||||
while (v.len > 0 && v.ptr[v.len - 1] == ' ') v.len--; // Trim spaces
|
||||
if (k.len == 0) break;
|
||||
// LOG(LL_INFO, ("--HH [%.*s] [%.*s] [%.*s]", (int) tmp.len - 1, tmp.ptr,
|
||||
// MG_INFO(("--HH [%.*s] [%.*s] [%.*s]", (int) tmp.len - 1, tmp.ptr,
|
||||
//(int) k.len, k.ptr, (int) v.len, v.ptr));
|
||||
h[i].name = k;
|
||||
h[i].value = v;
|
||||
@ -1448,18 +1448,18 @@ static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
|
||||
|
||||
static int getrange(struct mg_str *s, int64_t *a, int64_t *b) {
|
||||
size_t i, numparsed = 0;
|
||||
// LOG(LL_INFO, ("%.*s", (int) s->len, s->ptr));
|
||||
// MG_INFO(("%.*s", (int) s->len, s->ptr));
|
||||
for (i = 0; i + 6 < s->len; i++) {
|
||||
if (memcmp(&s->ptr[i], "bytes=", 6) == 0) {
|
||||
struct mg_str p = mg_str_n(s->ptr + i + 6, s->len - i - 6);
|
||||
if (p.len > 0 && p.ptr[0] >= '0' && p.ptr[0] <= '9') numparsed++;
|
||||
*a = mg_to64(p);
|
||||
// LOG(LL_INFO, ("PPP [%.*s] %d", (int) p.len, p.ptr, numparsed));
|
||||
// MG_INFO(("PPP [%.*s] %d", (int) p.len, p.ptr, numparsed));
|
||||
while (p.len && p.ptr[0] >= '0' && p.ptr[0] <= '9') p.ptr++, p.len--;
|
||||
if (p.len && p.ptr[0] == '-') p.ptr++, p.len--;
|
||||
*b = mg_to64(p);
|
||||
if (p.len > 0 && p.ptr[0] >= '0' && p.ptr[0] <= '9') numparsed++;
|
||||
// LOG(LL_INFO, ("PPP [%.*s] %d", (int) p.len, p.ptr, numparsed));
|
||||
// MG_INFO(("PPP [%.*s] %d", (int) p.len, p.ptr, numparsed));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -1476,7 +1476,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
|
||||
struct mg_str *inm = NULL;
|
||||
|
||||
if (fd == NULL || fs->st(path, &size, &mtime) == 0) {
|
||||
LOG(LL_DEBUG, ("404 [%s] %p", path, (void *) fd));
|
||||
MG_DEBUG(("404 [%s] %p", path, (void *) fd));
|
||||
mg_http_reply(c, 404, "", "%s", "Not found\n");
|
||||
mg_fs_close(fd);
|
||||
// NOTE: mg_http_etag() call should go first!
|
||||
@ -1543,12 +1543,12 @@ static void printdirentry(const char *name, void *userdata) {
|
||||
char path[MG_PATH_MAX], sz[40], mod[40];
|
||||
int flags, n = 0;
|
||||
|
||||
// LOG(LL_DEBUG, ("[%s] [%s]", d->dir, name));
|
||||
// MG_DEBUG(("[%s] [%s]", d->dir, name));
|
||||
if (mg_snprintf(path, sizeof(path), "%s%c%s", d->dir, '/', name) >
|
||||
sizeof(path)) {
|
||||
LOG(LL_ERROR, ("%s truncated", name));
|
||||
MG_ERROR(("%s truncated", name));
|
||||
} else if ((flags = fs->st(path, &size, &t)) == 0) {
|
||||
LOG(LL_ERROR, ("%lu stat(%s): %d", d->c->id, path, errno));
|
||||
MG_ERROR(("%lu stat(%s): %d", d->c->id, path, errno));
|
||||
} else {
|
||||
const char *slash = flags & MG_FS_DIR ? "/" : "";
|
||||
if (flags & MG_FS_DIR) {
|
||||
@ -1671,8 +1671,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm,
|
||||
path[path_size - 1] = '\0'; // Double-check
|
||||
remove_double_dots(path);
|
||||
n = strlen(path);
|
||||
LOG(LL_VERBOSE_DEBUG,
|
||||
("%lu %.*s -> %s", c->id, (int) hm->uri.len, hm->uri.ptr, path));
|
||||
MG_VERBOSE(("%lu %.*s -> %s", c->id, (int) hm->uri.len, hm->uri.ptr, path));
|
||||
while (n > 0 && path[n - 1] == '/') path[--n] = 0; // Trim trailing slashes
|
||||
flags = fs->st(path, NULL, NULL); // Does it exist?
|
||||
if (flags == 0) {
|
||||
@ -1720,8 +1719,7 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
|
||||
const char *sp = opts->ssi_pattern;
|
||||
int flags = uri_to_path(c, hm, opts, path, sizeof(path));
|
||||
if (flags == 0) return;
|
||||
LOG(LL_VERBOSE_DEBUG,
|
||||
("%.*s %s %d", (int) hm->uri.len, hm->uri.ptr, path, flags));
|
||||
MG_VERBOSE(("%.*s %s %d", (int) hm->uri.len, hm->uri.ptr, path, flags));
|
||||
if (flags & MG_FS_DIR) {
|
||||
listdir(c, hm, opts, path);
|
||||
} else if (sp != NULL && mg_globmatch(sp, strlen(sp), path, strlen(path))) {
|
||||
@ -1789,7 +1787,7 @@ struct mg_str mg_http_get_header_var(struct mg_str s, struct mg_str v) {
|
||||
while (p < x &&
|
||||
(q ? p == b || *p != '"' : *p != ';' && *p != ' ' && *p != ','))
|
||||
p++;
|
||||
// LOG(LL_INFO, ("[%.*s] [%.*s] [%.*s]", (int) s.len, s.ptr, (int) v.len,
|
||||
// MG_INFO(("[%.*s] [%.*s] [%.*s]", (int) s.len, s.ptr, (int) v.len,
|
||||
// v.ptr, (int) (p - b), b));
|
||||
return stripquotes(mg_str_n(b, (size_t) (p - b + q)));
|
||||
}
|
||||
@ -1806,7 +1804,7 @@ static size_t get_chunk_length(const char *buf, size_t len, size_t *ll) {
|
||||
while (i < len && buf[i] != '\r' && i != '\n') i++;
|
||||
n = mg_unhexn((char *) buf, i);
|
||||
while (i < len && (buf[i] == '\r' || i == '\n')) i++;
|
||||
// LOG(LL_INFO, ("len %zu i %zu n %zu ", len, i, n));
|
||||
// MG_INFO(("len %zu i %zu n %zu ", len, i, n));
|
||||
if (ll != NULL) *ll = i + 1;
|
||||
if (i < len && i + n + 2 < len) return i + n + 3;
|
||||
return 0;
|
||||
@ -1821,7 +1819,7 @@ static void walkchunks(struct mg_connection *c, struct mg_http_message *hm,
|
||||
char *buf = (char *) &c->recv.buf[reqlen];
|
||||
size_t memo = c->recv.len;
|
||||
size_t cl = get_chunk_length(&buf[off], memo - reqlen - off, &ll);
|
||||
// LOG(LL_INFO, ("len %zu off %zu cl %zu ll %zu", len, off, cl, ll));
|
||||
// MG_INFO(("len %zu off %zu cl %zu ll %zu", len, off, cl, ll));
|
||||
if (cl == 0) break;
|
||||
hm->chunk = mg_str_n(&buf[off + ll], cl < ll + 2 ? 0 : cl - ll - 2);
|
||||
mg_call(c, MG_EV_HTTP_CHUNK, hm);
|
||||
@ -1840,7 +1838,7 @@ static void walkchunks(struct mg_connection *c, struct mg_http_message *hm,
|
||||
off += cl2;
|
||||
if (cl2 <= 5) break;
|
||||
}
|
||||
// LOG(LL_INFO, ("BL->%d del %d off %d", (int) bl, (int) del, (int) off));
|
||||
// MG_INFO(("BL->%d del %d off %d", (int) bl, (int) del, (int) off));
|
||||
c->recv.len -= off - bl;
|
||||
// Set message length to indicate we've received
|
||||
// everything, to fire MG_EV_HTTP_MSG
|
||||
@ -1885,7 +1883,7 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm,
|
||||
long oft = strtol(offset, NULL, 0);
|
||||
mg_snprintf(path, sizeof(path), "%s%c%s", dir, MG_DIRSEP, name);
|
||||
remove_double_dots(path);
|
||||
LOG(LL_DEBUG, ("%d bytes @ %ld [%s]", (int) hm->body.len, oft, path));
|
||||
MG_DEBUG(("%d bytes @ %ld [%s]", (int) hm->body.len, oft, path));
|
||||
if (oft == 0) fs->rm(path);
|
||||
if ((fd = mg_fs_open(fs, path, MG_FS_WRITE)) == NULL) {
|
||||
mg_http_reply(c, 400, "", "open(%s): %d", path, errno);
|
||||
@ -1915,8 +1913,8 @@ static void http_cb(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
} else if (is_chunked && n > 0) {
|
||||
walkchunks(c, &hm, (size_t) n);
|
||||
}
|
||||
// LOG(LL_INFO,
|
||||
//("---->%d %d\n%.*s", n, is_chunked, (int) c->recv.len, c->recv.buf));
|
||||
// MG_INFO(("---->%d %d\n%.*s", n, is_chunked, (int) c->recv.len,
|
||||
// c->recv.buf));
|
||||
if (n < 0 && ev == MG_EV_READ) {
|
||||
mg_error(c, "HTTP parse:\n%.*s", (int) c->recv.len, c->recv.buf);
|
||||
break;
|
||||
@ -1996,8 +1994,7 @@ int mg_iobuf_resize(struct mg_iobuf *io, size_t new_size) {
|
||||
io->size = new_size;
|
||||
} else {
|
||||
ok = 0;
|
||||
LOG(LL_ERROR,
|
||||
("%lu->%lu", (unsigned long) io->size, (unsigned long) new_size));
|
||||
MG_ERROR(("%lld->%lld", (uint64_t) io->size, (uint64_t) new_size));
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
@ -2055,13 +2052,13 @@ static void (*s_fn)(const void *, size_t, void *) = mg_log_stdout;
|
||||
static void *s_fn_param = NULL;
|
||||
|
||||
void mg_log_set(const char *spec) {
|
||||
LOG(LL_DEBUG, ("Setting log level to %s", spec));
|
||||
MG_DEBUG(("Setting log level to %s", spec));
|
||||
s_spec = spec;
|
||||
}
|
||||
|
||||
bool mg_log_prefix(int level, const char *file, int line, const char *fname) {
|
||||
// static unsigned long seq;
|
||||
int max = LL_INFO;
|
||||
int max = MG_LL_INFO;
|
||||
struct mg_str k, v, s = mg_str(s_spec);
|
||||
const char *p = strrchr(file, '/');
|
||||
|
||||
@ -2402,8 +2399,8 @@ void mg_mqtt_pub(struct mg_connection *c, struct mg_str topic,
|
||||
struct mg_str data, int qos, bool retain) {
|
||||
uint8_t flags = (uint8_t) (((qos & 3) << 1) | (retain ? 1 : 0));
|
||||
uint32_t total_len = 2 + (uint32_t) topic.len + (uint32_t) data.len;
|
||||
LOG(LL_DEBUG, ("%lu [%.*s] -> [%.*s]", c->id, (int) topic.len,
|
||||
(char *) topic.ptr, (int) data.len, (char *) data.ptr));
|
||||
MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) topic.len, (char *) topic.ptr,
|
||||
(int) data.len, (char *) data.ptr));
|
||||
if (qos > 0) total_len += 2;
|
||||
mg_mqtt_send_header(c, MQTT_CMD_PUBLISH, flags, total_len);
|
||||
mg_send_u16(c, mg_htons((uint16_t) topic.len));
|
||||
@ -2525,26 +2522,25 @@ static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
struct mg_mqtt_message mm;
|
||||
int rc = mg_mqtt_parse(c->recv.buf, c->recv.len, &mm);
|
||||
if (rc == MQTT_MALFORMED) {
|
||||
LOG(LL_ERROR, ("%lu MQTT malformed message", c->id));
|
||||
MG_ERROR(("%lu MQTT malformed message", c->id));
|
||||
c->is_closing = 1;
|
||||
break;
|
||||
} else if (rc == MQTT_OK) {
|
||||
LOG(LL_VERBOSE_DEBUG,
|
||||
("%p MQTT CMD %d len %d [%.*s]", c->fd, mm.cmd, (int) mm.dgram.len,
|
||||
(int) mm.data.len, mm.data.ptr));
|
||||
MG_VERBOSE(("%p MQTT CMD %d len %d [%.*s]", c->fd, mm.cmd,
|
||||
(int) mm.dgram.len, (int) mm.data.len, mm.data.ptr));
|
||||
switch (mm.cmd) {
|
||||
case MQTT_CMD_CONNACK:
|
||||
mg_call(c, MG_EV_MQTT_OPEN, &mm.ack);
|
||||
if (mm.ack == 0) {
|
||||
LOG(LL_DEBUG, ("%lu Connected", c->id));
|
||||
MG_DEBUG(("%lu Connected", c->id));
|
||||
} else {
|
||||
LOG(LL_ERROR, ("%lu MQTT auth failed, code %d", c->id, mm.ack));
|
||||
MG_ERROR(("%lu MQTT auth failed, code %d", c->id, mm.ack));
|
||||
c->is_closing = 1;
|
||||
}
|
||||
break;
|
||||
case MQTT_CMD_PUBLISH: {
|
||||
LOG(LL_DEBUG, ("%lu [%.*s] -> [%.*s]", c->id, (int) mm.topic.len,
|
||||
mm.topic.ptr, (int) mm.data.len, mm.data.ptr));
|
||||
MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) mm.topic.len,
|
||||
mm.topic.ptr, (int) mm.data.len, mm.data.ptr));
|
||||
mg_call(c, MG_EV_MQTT_MSG, &mm);
|
||||
break;
|
||||
}
|
||||
@ -2699,8 +2695,7 @@ static bool mg_aton6(struct mg_str str, struct mg_addr *addr) {
|
||||
(str.ptr[i] >= 'A' && str.ptr[i] <= 'F')) {
|
||||
unsigned long val;
|
||||
if (i > j + 3) return false;
|
||||
// LOG(LL_DEBUG, ("%zu %zu [%.*s]", i, j, (int) (i - j + 1),
|
||||
// &str.ptr[j]));
|
||||
// MG_DEBUG(("%zu %zu [%.*s]", i, j, (int) (i - j + 1), &str.ptr[j]));
|
||||
val = mg_unhexn(&str.ptr[j], i - j + 1);
|
||||
addr->ip6[n] = (uint8_t) ((val >> 8) & 255);
|
||||
addr->ip6[n + 1] = (uint8_t) (val & 255);
|
||||
@ -2728,7 +2723,7 @@ static bool mg_aton6(struct mg_str str, struct mg_addr *addr) {
|
||||
}
|
||||
|
||||
bool mg_aton(struct mg_str str, struct mg_addr *addr) {
|
||||
// LOG(LL_INFO, ("[%.*s]", (int) str.len, str.ptr));
|
||||
// MG_INFO(("[%.*s]", (int) str.len, str.ptr));
|
||||
return mg_atone(str, addr) || mg_atonl(str, addr) || mg_aton4(str, addr) ||
|
||||
mg_aton6(str, addr);
|
||||
}
|
||||
@ -2740,7 +2735,7 @@ void mg_mgr_free(struct mg_mgr *mgr) {
|
||||
#if MG_ARCH == MG_ARCH_FREERTOS_TCP
|
||||
FreeRTOS_DeleteSocketSet(mgr->ss);
|
||||
#endif
|
||||
LOG(LL_INFO, ("All connections closed"));
|
||||
MG_DEBUG(("All connections closed"));
|
||||
}
|
||||
|
||||
void mg_mgr_init(struct mg_mgr *mgr) {
|
||||
@ -3040,18 +3035,18 @@ int64_t mg_sntp_parse(const unsigned char *buf, size_t len) {
|
||||
int64_t res = -1;
|
||||
int mode = len > 0 ? buf[0] & 7 : 0;
|
||||
if (len < 48) {
|
||||
LOG(LL_ERROR, ("%s", "corrupt packet"));
|
||||
MG_ERROR(("%s", "corrupt packet"));
|
||||
} else if ((buf[0] & 0x38) >> 3 != 4) {
|
||||
LOG(LL_ERROR, ("%s", "wrong version"));
|
||||
MG_ERROR(("%s", "wrong version"));
|
||||
} else if (mode != 4 && mode != 5) {
|
||||
LOG(LL_ERROR, ("%s", "not a server reply"));
|
||||
MG_ERROR(("%s", "not a server reply"));
|
||||
} else if (buf[1] == 0) {
|
||||
LOG(LL_ERROR, ("%s", "server sent a kiss of death"));
|
||||
MG_ERROR(("%s", "server sent a kiss of death"));
|
||||
} else {
|
||||
uint32_t *data = (uint32_t *) &buf[40];
|
||||
unsigned long seconds = mg_ntohl(data[0]) - SNTP_TIME_OFFSET;
|
||||
unsigned long useconds = mg_ntohl(data[1]);
|
||||
// LOG(LL_DEBUG, ("%lu %lu %lu", time(0), seconds, useconds));
|
||||
// MG_DEBUG(("%lu %lu %lu", time(0), seconds, useconds));
|
||||
res = ((int64_t) seconds) * 1000 + (int64_t) ((useconds / 1000) % 1000);
|
||||
s_sntmp_next = seconds + SNTP_INTERVAL_SEC;
|
||||
}
|
||||
@ -3063,8 +3058,8 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len);
|
||||
if (milliseconds > 0) {
|
||||
mg_call(c, MG_EV_SNTP_TIME, &milliseconds);
|
||||
LOG(LL_DEBUG, ("%u.%u, next at %lu", (unsigned) (milliseconds / 1000),
|
||||
(unsigned) (milliseconds % 1000), s_sntmp_next));
|
||||
MG_DEBUG(("%u.%u, next at %lu", (unsigned) (milliseconds / 1000),
|
||||
(unsigned) (milliseconds % 1000), s_sntmp_next));
|
||||
}
|
||||
c->recv.len = 0; // Clear receive buffer
|
||||
} else if (ev == MG_EV_CONNECT) {
|
||||
@ -3077,13 +3072,13 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
|
||||
void mg_sntp_send(struct mg_connection *c, unsigned long utc) {
|
||||
if (c->is_resolving) {
|
||||
LOG(LL_ERROR, ("%lu wait until resolved", c->id));
|
||||
MG_ERROR(("%lu wait until resolved", c->id));
|
||||
} else if (utc > s_sntmp_next) {
|
||||
uint8_t buf[48] = {0};
|
||||
s_sntmp_next = utc + SNTP_INTERVAL_SEC;
|
||||
buf[0] = (0 << 6) | (4 << 3) | 3;
|
||||
mg_send(c, buf, sizeof(buf));
|
||||
LOG(LL_DEBUG, ("%lu ct %lu, next at %lu", c->id, utc, s_sntmp_next));
|
||||
MG_DEBUG(("%lu ct %lu, next at %lu", c->id, utc, s_sntmp_next));
|
||||
}
|
||||
}
|
||||
|
||||
@ -3200,7 +3195,7 @@ static struct mg_connection *alloc_conn(struct mg_mgr *mgr, bool is_client,
|
||||
}
|
||||
|
||||
static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
|
||||
int log_level = n > 0 ? LL_VERBOSE_DEBUG : LL_DEBUG;
|
||||
int log_level = n > 0 ? MG_LL_VERBOSE : MG_LL_DEBUG;
|
||||
char flags[] = {(char) ('0' + c->is_listening),
|
||||
(char) ('0' + c->is_client),
|
||||
(char) ('0' + c->is_accepted),
|
||||
@ -3216,9 +3211,9 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
|
||||
(char) ('0' + c->is_readable),
|
||||
(char) ('0' + c->is_writable),
|
||||
'\0'};
|
||||
LOG(log_level,
|
||||
("%3lu %s %d:%d %ld err %d (%s)", c->id, flags, (int) c->send.len,
|
||||
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
|
||||
MG_LOG(log_level,
|
||||
("%3lu %s %d:%d %ld err %d (%s)", c->id, flags, (int) c->send.len,
|
||||
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
|
||||
if (n == 0) {
|
||||
// Do nothing
|
||||
} else if (n < 0) {
|
||||
@ -3234,9 +3229,9 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
|
||||
memset(&a, 0, sizeof(a));
|
||||
getsockname(FD(c), &usa.sa, &slen);
|
||||
tomgaddr(&usa, &a, c->peer.is_ip6);
|
||||
LOG(LL_INFO, ("\n-- %lu %s %s %s %s %ld\n%s", c->id,
|
||||
mg_straddr(&a, t1, sizeof(t1)), r ? "<-" : "->",
|
||||
mg_straddr(&c->peer, t2, sizeof(t2)), c->label, n, s));
|
||||
MG_INFO(("\n-- %lu %s %s %s %s %ld\n%s", c->id,
|
||||
mg_straddr(&a, t1, sizeof(t1)), r ? "<-" : "->",
|
||||
mg_straddr(&c->peer, t2, sizeof(t2)), c->label, n, s));
|
||||
free(s);
|
||||
(void) t1, (void) t2; // Silence warnings for MG_ENABLE_LOG=0
|
||||
}
|
||||
@ -3298,7 +3293,7 @@ static SOCKET mg_open_listener(const char *url, struct mg_addr *addr) {
|
||||
memset(addr, 0, sizeof(*addr));
|
||||
addr->port = mg_htons(mg_url_port(url));
|
||||
if (!mg_aton(mg_url_host(url), addr)) {
|
||||
LOG(LL_ERROR, ("invalid listening URL: %s", url));
|
||||
MG_ERROR(("invalid listening URL: %s", url));
|
||||
} else {
|
||||
union usa usa;
|
||||
socklen_t slen = tousa(addr, &usa);
|
||||
@ -3349,7 +3344,7 @@ static SOCKET mg_open_listener(const char *url, struct mg_addr *addr) {
|
||||
}
|
||||
if (fd == INVALID_SOCKET) {
|
||||
if (s_err == 0) s_err = MG_SOCK_ERRNO;
|
||||
LOG(LL_ERROR, ("Failed to listen on %s, errno %d", url, s_err));
|
||||
MG_ERROR(("Failed to listen on %s, errno %d", url, s_err));
|
||||
}
|
||||
|
||||
return fd;
|
||||
@ -3401,7 +3396,7 @@ static void close_conn(struct mg_connection *c) {
|
||||
// Order of operations is important. `MG_EV_CLOSE` event must be fired
|
||||
// before we deallocate received data, see #1331
|
||||
mg_call(c, MG_EV_CLOSE, NULL);
|
||||
LOG(LL_DEBUG, ("%lu closed", c->id));
|
||||
MG_DEBUG(("%lu closed", c->id));
|
||||
if (FD(c) != INVALID_SOCKET) {
|
||||
closesocket(FD(c));
|
||||
#if MG_ARCH == MG_ARCH_FREERTOS_TCP
|
||||
@ -3475,15 +3470,15 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
struct mg_connection *c = NULL;
|
||||
if (url == NULL || url[0] == '\0') {
|
||||
LOG(LL_ERROR, ("null url"));
|
||||
MG_ERROR(("null url"));
|
||||
} else if ((c = alloc_conn(mgr, 1, INVALID_SOCKET)) == NULL) {
|
||||
LOG(LL_ERROR, ("OOM"));
|
||||
MG_ERROR(("OOM"));
|
||||
} else {
|
||||
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
|
||||
c->is_udp = (strncmp(url, "udp:", 4) == 0);
|
||||
c->fn = fn;
|
||||
c->fn_data = fn_data;
|
||||
LOG(LL_DEBUG, ("%lu -> %s", c->id, url));
|
||||
MG_DEBUG(("%lu -> %s", c->id, url));
|
||||
mg_call(c, MG_EV_OPEN, NULL);
|
||||
mg_resolve(c, url);
|
||||
}
|
||||
@ -3502,20 +3497,20 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
|
||||
// That's not an error, just should try later
|
||||
if (MG_SOCK_ERRNO != EAGAIN)
|
||||
#endif
|
||||
LOG(LL_ERROR, ("%lu accept failed, errno %d", lsn->id, MG_SOCK_ERRNO));
|
||||
MG_ERROR(("%lu accept failed, errno %d", lsn->id, MG_SOCK_ERRNO));
|
||||
#if ((MG_ARCH != MG_ARCH_WIN32) && (MG_ARCH != MG_ARCH_FREERTOS_TCP))
|
||||
} else if ((long) fd >= FD_SETSIZE) {
|
||||
LOG(LL_ERROR, ("%ld > %ld", (long) fd, (long) FD_SETSIZE));
|
||||
MG_ERROR(("%ld > %ld", (long) fd, (long) FD_SETSIZE));
|
||||
closesocket(fd);
|
||||
#endif
|
||||
} else if ((c = alloc_conn(mgr, 0, fd)) == NULL) {
|
||||
LOG(LL_ERROR, ("%lu OOM", lsn->id));
|
||||
MG_ERROR(("%lu OOM", lsn->id));
|
||||
closesocket(fd);
|
||||
} else {
|
||||
char buf[40];
|
||||
tomgaddr(&usa, &c->peer, sa_len != sizeof(usa.sin));
|
||||
mg_straddr(&c->peer, buf, sizeof(buf));
|
||||
LOG(LL_DEBUG, ("%lu accepted %s", c->id, buf));
|
||||
MG_DEBUG(("%lu accepted %s", c->id, buf));
|
||||
mg_set_non_blocking_mode(FD(c));
|
||||
setsockopts(c);
|
||||
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
|
||||
@ -3577,13 +3572,13 @@ struct mg_connection *mg_mkpipe(struct mg_mgr *mgr, mg_event_handler_t fn,
|
||||
SOCKET sp[2] = {INVALID_SOCKET, INVALID_SOCKET};
|
||||
struct mg_connection *c = NULL;
|
||||
if (!mg_socketpair(sp, usa)) {
|
||||
LOG(LL_ERROR, ("Cannot create socket pair"));
|
||||
MG_ERROR(("Cannot create socket pair"));
|
||||
} else if ((c = alloc_conn(mgr, false, sp[1])) == NULL) {
|
||||
closesocket(sp[0]);
|
||||
closesocket(sp[1]);
|
||||
LOG(LL_ERROR, ("OOM"));
|
||||
MG_ERROR(("OOM"));
|
||||
} else {
|
||||
LOG(LL_INFO, ("pipe %lu", (unsigned long) sp[0]));
|
||||
MG_DEBUG(("pipe %lu", (unsigned long) sp[0]));
|
||||
tomgaddr(&usa[0], &c->peer, false);
|
||||
c->is_udp = 1;
|
||||
c->pfn = pf1;
|
||||
@ -3603,9 +3598,9 @@ struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url,
|
||||
struct mg_addr addr;
|
||||
SOCKET fd = mg_open_listener(url, &addr);
|
||||
if (fd == INVALID_SOCKET) {
|
||||
LOG(LL_ERROR, ("Failed: %s, errno %d", url, MG_SOCK_ERRNO));
|
||||
MG_ERROR(("Failed: %s, errno %d", url, MG_SOCK_ERRNO));
|
||||
} else if ((c = alloc_conn(mgr, 0, fd)) == NULL) {
|
||||
LOG(LL_ERROR, ("OOM %s", url));
|
||||
MG_ERROR(("OOM %s", url));
|
||||
closesocket(fd);
|
||||
} else {
|
||||
memcpy(&c->peer, &addr, sizeof(struct mg_addr));
|
||||
@ -3616,8 +3611,7 @@ struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url,
|
||||
c->fn = fn;
|
||||
c->fn_data = fn_data;
|
||||
mg_call(c, MG_EV_OPEN, NULL);
|
||||
LOG(LL_DEBUG,
|
||||
("%lu accepting on %s (port %u)", c->id, url, mg_ntohs(c->peer.port)));
|
||||
MG_DEBUG(("%lu %s port %u", c->id, url, mg_ntohs(c->peer.port)));
|
||||
}
|
||||
return c;
|
||||
}
|
||||
@ -3658,7 +3652,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
|
||||
}
|
||||
|
||||
if ((rc = select((int) maxfd + 1, &rset, &wset, NULL, &tv)) < 0) {
|
||||
LOG(LL_DEBUG, ("select: %d %d", rc, MG_SOCK_ERRNO));
|
||||
MG_DEBUG(("select: %d %d", rc, MG_SOCK_ERRNO));
|
||||
FD_ZERO(&rset);
|
||||
FD_ZERO(&wset);
|
||||
}
|
||||
@ -3700,11 +3694,10 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
|
||||
for (c = mgr->conns; c != NULL; c = tmp) {
|
||||
tmp = c->next;
|
||||
mg_call(c, MG_EV_POLL, &now);
|
||||
LOG(LL_VERBOSE_DEBUG,
|
||||
("%lu %c%c %c%c%c%c%c", c->id, c->is_readable ? 'r' : '-',
|
||||
c->is_writable ? 'w' : '-', c->is_tls ? 'T' : 't',
|
||||
c->is_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h',
|
||||
c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c'));
|
||||
MG_VERBOSE(("%lu %c%c %c%c%c%c%c", c->id, c->is_readable ? 'r' : '-',
|
||||
c->is_writable ? 'w' : '-', c->is_tls ? 'T' : 't',
|
||||
c->is_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h',
|
||||
c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c'));
|
||||
if (c->is_resolving || c->is_closing) {
|
||||
// Do nothing
|
||||
} else if (c->is_listening && c->is_udp == 0) {
|
||||
@ -3757,7 +3750,7 @@ static char *mg_ssi(const char *path, const char *root, int depth) {
|
||||
mg_iobuf_add(&b, b.len, data, strlen(data), align);
|
||||
free(data);
|
||||
} else {
|
||||
LOG(LL_ERROR, ("%s: file=%s error or too deep", path, arg));
|
||||
MG_ERROR(("%s: file=%s error or too deep", path, arg));
|
||||
}
|
||||
} else if (sscanf(buf, "<!--#include virtual=\"%[^\"]", arg)) {
|
||||
char tmp[MG_PATH_MAX + BUFSIZ + 10], *data;
|
||||
@ -3767,11 +3760,11 @@ static char *mg_ssi(const char *path, const char *root, int depth) {
|
||||
mg_iobuf_add(&b, b.len, data, strlen(data), align);
|
||||
free(data);
|
||||
} else {
|
||||
LOG(LL_ERROR, ("%s: virtual=%s error or too deep", path, arg));
|
||||
MG_ERROR(("%s: virtual=%s error or too deep", path, arg));
|
||||
}
|
||||
} else {
|
||||
// Unknown SSI tag
|
||||
LOG(LL_INFO, ("Unknown SSI tag: %.*s", (int) len, buf));
|
||||
MG_ERROR(("Unknown SSI tag: %.*s", (int) len, buf));
|
||||
mg_iobuf_add(&b, b.len, buf, len, align);
|
||||
}
|
||||
intag = 0;
|
||||
@ -3785,7 +3778,7 @@ static char *mg_ssi(const char *path, const char *root, int depth) {
|
||||
if (len == 5 && strncmp(buf, "<!--#", 5) != 0) {
|
||||
intag = 0;
|
||||
} else if (len >= sizeof(buf) - 2) {
|
||||
LOG(LL_ERROR, ("%s: SSI tag is too large", path));
|
||||
MG_ERROR(("%s: SSI tag is too large", path));
|
||||
len = 0;
|
||||
}
|
||||
buf[len++] = (char) (ch & 0xff);
|
||||
@ -4316,12 +4309,12 @@ void mg_tls_handshake(struct mg_connection *c) {
|
||||
mbedtls_ssl_set_bio(&tls->ssl, &c->fd, mg_net_send, mg_net_recv, 0);
|
||||
rc = mbedtls_ssl_handshake(&tls->ssl);
|
||||
if (rc == 0) { // Success
|
||||
LOG(LL_DEBUG, ("%lu success", c->id));
|
||||
MG_DEBUG(("%lu success", c->id));
|
||||
c->is_tls_hs = 0;
|
||||
} else if (rc == MBEDTLS_ERR_SSL_WANT_READ ||
|
||||
rc == MBEDTLS_ERR_SSL_WANT_WRITE) { // Still pending
|
||||
LOG(LL_VERBOSE_DEBUG, ("%lu pending, %d%d %d (-%#x)", c->id,
|
||||
c->is_connecting, c->is_tls_hs, rc, -rc));
|
||||
MG_VERBOSE(("%lu pending, %d%d %d (-%#x)", c->id, c->is_connecting,
|
||||
c->is_tls_hs, rc, -rc));
|
||||
} else {
|
||||
mg_error(c, "TLS handshake: -%#x", -rc); // Error
|
||||
}
|
||||
@ -4335,7 +4328,7 @@ static int mbed_rng(void *ctx, unsigned char *buf, size_t len) {
|
||||
|
||||
static void debug_cb(void *c, int lev, const char *s, int n, const char *s2) {
|
||||
n = (int) strlen(s2) - 1;
|
||||
LOG(LL_VERBOSE_DEBUG, ("%p %.*s", ((struct mg_connection *) c)->fd, n, s2));
|
||||
MG_VERBOSE(("%p %.*s", ((struct mg_connection *) c)->fd, n, s2));
|
||||
(void) s;
|
||||
(void) c;
|
||||
(void) lev;
|
||||
@ -4365,7 +4358,7 @@ void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
|
||||
mg_error(c, "TLS OOM");
|
||||
goto fail;
|
||||
}
|
||||
LOG(LL_DEBUG, ("%lu Setting TLS", c->id));
|
||||
MG_DEBUG(("%lu Setting TLS", c->id));
|
||||
mbedtls_ssl_init(&tls->ssl);
|
||||
mbedtls_ssl_config_init(&tls->conf);
|
||||
mbedtls_x509_crt_init(&tls->ca);
|
||||
@ -4509,10 +4502,10 @@ void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
|
||||
SSL_library_init();
|
||||
s_initialised++;
|
||||
}
|
||||
LOG(LL_DEBUG, ("%lu Setting TLS, CA: %s, cert: %s, key: %s", c->id,
|
||||
opts->ca == NULL ? "null" : opts->ca,
|
||||
opts->cert == NULL ? "null" : opts->cert,
|
||||
opts->certkey == NULL ? "null" : opts->certkey));
|
||||
MG_DEBUG(("%lu Setting TLS, CA: %s, cert: %s, key: %s", c->id,
|
||||
opts->ca == NULL ? "null" : opts->ca,
|
||||
opts->cert == NULL ? "null" : opts->cert,
|
||||
opts->certkey == NULL ? "null" : opts->certkey));
|
||||
tls->ctx = c->is_client ? SSL_CTX_new(SSLv23_client_method())
|
||||
: SSL_CTX_new(SSLv23_server_method());
|
||||
if ((tls->ssl = SSL_new(tls->ctx)) == NULL) {
|
||||
@ -4577,7 +4570,7 @@ void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
|
||||
if (c->is_client && c->is_resolving == 0 && c->is_connecting == 0) {
|
||||
mg_tls_handshake(c);
|
||||
}
|
||||
LOG(LL_DEBUG, ("%lu SSL %s OK", c->id, c->is_accepted ? "accept" : "client"));
|
||||
MG_DEBUG(("%lu SSL %s OK", c->id, c->is_accepted ? "accept" : "client"));
|
||||
return;
|
||||
fail:
|
||||
c->is_closing = 1;
|
||||
@ -4590,7 +4583,7 @@ void mg_tls_handshake(struct mg_connection *c) {
|
||||
SSL_set_fd(tls->ssl, (int) (size_t) c->fd);
|
||||
rc = c->is_client ? SSL_connect(tls->ssl) : SSL_accept(tls->ssl);
|
||||
if (rc == 1) {
|
||||
LOG(LL_DEBUG, ("%lu success", c->id));
|
||||
MG_DEBUG(("%lu success", c->id));
|
||||
c->is_tls_hs = 0;
|
||||
} else {
|
||||
int code = mg_tls_err(tls, rc);
|
||||
@ -4948,7 +4941,7 @@ size_t mg_ws_send(struct mg_connection *c, const char *buf, size_t len,
|
||||
uint8_t header[14];
|
||||
size_t header_len = mkhdr(len, op, c->is_client, header);
|
||||
mg_send(c, header, header_len);
|
||||
LOG(LL_VERBOSE_DEBUG, ("WS out: %d [%.*s]", (int) len, (int) len, buf));
|
||||
MG_VERBOSE(("WS out: %d [%.*s]", (int) len, (int) len, buf));
|
||||
mg_send(c, buf, len);
|
||||
mg_ws_mask(c, len);
|
||||
return header_len + len;
|
||||
@ -4967,8 +4960,7 @@ static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
c->is_closing = 1; // Some just, not an HTTP request
|
||||
} else if (n > 0) {
|
||||
if (n < 15 || memcmp(c->recv.buf + 9, "101", 3) != 0) {
|
||||
LOG(LL_ERROR,
|
||||
("%lu WS handshake error: %.*s", c->id, 15, c->recv.buf));
|
||||
MG_ERROR(("%lu WS handshake error: %.*s", c->id, 15, c->recv.buf));
|
||||
c->is_closing = 1;
|
||||
} else {
|
||||
struct mg_http_message hm;
|
||||
@ -4987,14 +4979,14 @@ static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
struct mg_ws_message m = {{s, msg.data_len}, msg.flags};
|
||||
size_t len = msg.header_len + msg.data_len;
|
||||
uint8_t final = msg.flags & 128, op = msg.flags & 15;
|
||||
// LOG(LL_VERBOSE_DEBUG, ("fin %d op %d len %d [%.*s]", final, op,
|
||||
// MG_VERBOSE ("fin %d op %d len %d [%.*s]", final, op,
|
||||
// (int) m.data.len, (int) m.data.len, m.data.ptr));
|
||||
switch (op) {
|
||||
case WEBSOCKET_OP_CONTINUE:
|
||||
mg_call(c, MG_EV_WS_CTL, &m);
|
||||
break;
|
||||
case WEBSOCKET_OP_PING:
|
||||
LOG(LL_DEBUG, ("%s", "WS PONG"));
|
||||
MG_DEBUG(("%s", "WS PONG"));
|
||||
mg_ws_send(c, s, msg.data_len, WEBSOCKET_OP_PONG);
|
||||
mg_call(c, MG_EV_WS_CTL, &m);
|
||||
break;
|
||||
@ -5006,7 +4998,7 @@ static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
if (final) mg_call(c, MG_EV_WS_MSG, &m);
|
||||
break;
|
||||
case WEBSOCKET_OP_CLOSE:
|
||||
LOG(LL_DEBUG, ("%lu Got WS CLOSE", c->id));
|
||||
MG_DEBUG(("%lu Got WS CLOSE", c->id));
|
||||
mg_call(c, MG_EV_WS_CTL, &m);
|
||||
c->is_closing = 1;
|
||||
break;
|
||||
@ -5023,7 +5015,7 @@ static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
len -= msg.header_len;
|
||||
ofs += len;
|
||||
c->pfn_data = (void *) ofs;
|
||||
// LOG(LL_INFO, ("FRAG %d [%.*s]", (int) ofs, (int) ofs, c->recv.buf));
|
||||
// MG_INFO(("FRAG %d [%.*s]", (int) ofs, (int) ofs, c->recv.buf));
|
||||
}
|
||||
// Remove non-fragmented frame
|
||||
if (final && op) mg_iobuf_del(&c->recv, ofs, len);
|
||||
@ -5060,7 +5052,6 @@ struct mg_connection *mg_ws_connect(struct mg_mgr *mgr, const char *url,
|
||||
// mg_url_host(url, host, sizeof(host));
|
||||
mg_random(nonce, sizeof(nonce));
|
||||
mg_base64_encode((unsigned char *) nonce, sizeof(nonce), key);
|
||||
// LOG(LL_DEBUG, "%p [%s]", uri, uri == NULL ? "???" : uri);
|
||||
n2 = mg_asprintf(&buf2, sizeof(mem2),
|
||||
"GET %s HTTP/1.1\r\n"
|
||||
"Upgrade: websocket\r\n"
|
||||
|
17
mongoose.h
17
mongoose.h
@ -566,7 +566,7 @@ int64_t mg_to64(struct mg_str str);
|
||||
|
||||
|
||||
|
||||
enum { LL_NONE, LL_ERROR, LL_INFO, LL_DEBUG, LL_VERBOSE_DEBUG };
|
||||
enum { MG_LL_NONE, MG_LL_ERROR, MG_LL_INFO, MG_LL_DEBUG, MG_LL_VERBOSE };
|
||||
void mg_log(const char *fmt, ...) PRINTF_LIKE(1, 2);
|
||||
bool mg_log_prefix(int ll, const char *file, int line, const char *fname);
|
||||
void mg_log_set(const char *spec);
|
||||
@ -577,21 +577,26 @@ void mg_log_set_callback(void (*fn)(const void *, size_t, void *), void *param);
|
||||
|
||||
#if MG_ENABLE_LOG
|
||||
|
||||
#define LOG(level, args) \
|
||||
#define MG_LOG(level, args) \
|
||||
do { \
|
||||
if (mg_log_prefix((level), __FILE__, __LINE__, __func__)) mg_log args; \
|
||||
} while (0)
|
||||
|
||||
#else
|
||||
|
||||
#define LOG(level, args) \
|
||||
do { \
|
||||
(void) level; \
|
||||
if (0) mg_log args; \
|
||||
#define MG_LOG(level, args) \
|
||||
do { \
|
||||
(void) level; \
|
||||
if (0) mg_log args; \
|
||||
} while (0)
|
||||
|
||||
#endif
|
||||
|
||||
#define MG_ERROR(args) MG_LOG(MG_LL_ERROR, args)
|
||||
#define MG_INFO(args) MG_LOG(MG_LL_INFO, args)
|
||||
#define MG_DEBUG(args) MG_LOG(MG_LL_DEBUG, args)
|
||||
#define MG_VERBOSE(args) MG_LOG(MG_LL_VERBOSE, args)
|
||||
|
||||
|
||||
|
||||
|
||||
|
26
src/dns.c
26
src/dns.c
@ -105,11 +105,11 @@ bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) {
|
||||
|
||||
for (i = 0; i < mg_ntohs(h->num_questions); i++) {
|
||||
if ((n = mg_dns_parse_rr(buf, len, ofs, true, &rr)) == 0) return false;
|
||||
// LOG(LL_INFO, ("Q %zu %zu", ofs, n));
|
||||
// MG_INFO(("Q %zu %zu", ofs, n));
|
||||
ofs += n;
|
||||
}
|
||||
for (i = 0; i < mg_ntohs(h->num_answers); i++) {
|
||||
// LOG(LL_INFO, ("A -- %zu %zu %s", ofs, n, dm->name));
|
||||
// MG_INFO(("A -- %zu %zu %s", ofs, n, dm->name));
|
||||
if ((n = mg_dns_parse_rr(buf, len, ofs, false, &rr)) == 0) return false;
|
||||
mg_dns_parse_name(buf, len, ofs, dm->name, sizeof(dm->name));
|
||||
ofs += n;
|
||||
@ -136,7 +136,7 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
int64_t now = *(int64_t *) ev_data;
|
||||
for (d = s_reqs; d != NULL; d = tmp) {
|
||||
tmp = d->next;
|
||||
// LOG(LL_DEBUG, ("%lu %lu dns poll", d->expire, now));
|
||||
// MG_DEBUG ("%lu %lu dns poll", d->expire, now));
|
||||
if (now > d->expire) mg_error(d->c, "DNS timeout");
|
||||
}
|
||||
} else if (ev == MG_EV_READ) {
|
||||
@ -144,13 +144,13 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
int resolved = 0;
|
||||
if (mg_dns_parse(c->recv.buf, c->recv.len, &dm) == false) {
|
||||
char *s = mg_hexdump(c->recv.buf, c->recv.len);
|
||||
LOG(LL_ERROR, ("Unexpected DNS response:\n%s\n", s));
|
||||
MG_ERROR(("Unexpected DNS response:\n%s\n", s));
|
||||
free(s);
|
||||
} else {
|
||||
LOG(LL_VERBOSE_DEBUG, ("%s %d", dm.name, dm.resolved));
|
||||
MG_VERBOSE(("%s %d", dm.name, dm.resolved));
|
||||
for (d = s_reqs; d != NULL; d = tmp) {
|
||||
tmp = d->next;
|
||||
// LOG(LL_INFO, ("d %p %hu %hu", d, d->txnid, dm.txnid));
|
||||
// MG_INFO(("d %p %hu %hu", d, d->txnid, dm.txnid));
|
||||
if (dm.txnid != d->txnid) continue;
|
||||
if (d->c->is_resolving) {
|
||||
d->c->is_resolving = 0;
|
||||
@ -158,8 +158,8 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
char buf[100];
|
||||
dm.addr.port = d->c->peer.port; // Save port
|
||||
d->c->peer = dm.addr; // Copy resolved address
|
||||
LOG(LL_DEBUG, ("%lu %s resolved to %s", d->c->id, dm.name,
|
||||
mg_ntoa(&d->c->peer, buf, sizeof(buf))));
|
||||
MG_DEBUG(("%lu %s resolved to %s", d->c->id, dm.name,
|
||||
mg_ntoa(&d->c->peer, buf, sizeof(buf))));
|
||||
mg_connect_resolved(d->c);
|
||||
#if MG_ENABLE_IPV6
|
||||
} else if (dm.addr.is_ip6 == false && dm.name[0] != '\0') {
|
||||
@ -170,13 +170,13 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
mg_error(d->c, "%s DNS lookup failed", dm.name);
|
||||
}
|
||||
} else {
|
||||
LOG(LL_ERROR, ("%lu already resolved", d->c->id));
|
||||
MG_ERROR(("%lu already resolved", d->c->id));
|
||||
}
|
||||
mg_dns_free(d);
|
||||
resolved = 1;
|
||||
}
|
||||
}
|
||||
if (!resolved) LOG(LL_ERROR, ("stray DNS reply"));
|
||||
if (!resolved) MG_ERROR(("stray DNS reply"));
|
||||
c->recv.len = 0;
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
for (d = s_reqs; d != NULL; d = tmp) {
|
||||
@ -246,9 +246,9 @@ static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
|
||||
d->expire = mg_millis() + (int64_t) ms;
|
||||
d->c = c;
|
||||
c->is_resolving = 1;
|
||||
LOG(LL_VERBOSE_DEBUG,
|
||||
("%lu resolving %.*s @ %s, txnid %hu", c->id, (int) name->len,
|
||||
name->ptr, mg_ntoa(&dnsc->c->peer, buf, sizeof(buf)), d->txnid));
|
||||
MG_VERBOSE(("%lu resolving %.*s @ %s, txnid %hu", c->id, (int) name->len,
|
||||
name->ptr, mg_ntoa(&dnsc->c->peer, buf, sizeof(buf)),
|
||||
d->txnid));
|
||||
mg_dns_send(dnsc->c, name, d->txnid, ipv6);
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ void mg_error(struct mg_connection *c, const char *fmt, ...) {
|
||||
va_start(ap, fmt);
|
||||
mg_vasprintf(&buf, sizeof(mem), fmt, ap);
|
||||
va_end(ap);
|
||||
LOG(LL_ERROR, ("%lu %s", c->id, buf));
|
||||
MG_ERROR(("%lu %s", c->id, buf));
|
||||
c->is_closing = 1; // Set is_closing before sending MG_EV_CALL
|
||||
mg_call(c, MG_EV_ERROR, buf); // Let user handler to override it
|
||||
if (buf != mem) free(buf);
|
||||
|
@ -12,7 +12,7 @@
|
||||
|
||||
static int p_stat(const char *path, size_t *size, time_t *mtime) {
|
||||
#if !defined(S_ISDIR)
|
||||
LOG(LL_ERROR, ("stat() API is not supported. %p %p %p", path, size, mtime));
|
||||
MG_ERROR(("stat() API is not supported. %p %p %p", path, size, mtime));
|
||||
return 0;
|
||||
#else
|
||||
#if MG_ARCH == MG_ARCH_WIN32
|
||||
|
46
src/http.c
46
src/http.c
@ -32,7 +32,7 @@ size_t mg_http_next_multipart(struct mg_str body, size_t ofs,
|
||||
// Skip boundary
|
||||
while (b + 2 < max && s[b] != '\r' && s[b + 1] != '\n') b++;
|
||||
if (b <= ofs || b + 2 >= max) return 0;
|
||||
// LOG(LL_INFO, ("B: %zu %zu [%.*s]", ofs, b - ofs, (int) (b - ofs), s));
|
||||
// MG_INFO(("B: %zu %zu [%.*s]", ofs, b - ofs, (int) (b - ofs), s));
|
||||
|
||||
// Skip headers
|
||||
h1 = h2 = b + 2;
|
||||
@ -40,7 +40,7 @@ size_t mg_http_next_multipart(struct mg_str body, size_t ofs,
|
||||
while (h2 + 2 < max && s[h2] != '\r' && s[h2 + 1] != '\n') h2++;
|
||||
if (h2 == h1) break;
|
||||
if (h2 + 2 >= max) return 0;
|
||||
// LOG(LL_INFO, ("Header: [%.*s]", (int) (h2 - h1), &s[h1]));
|
||||
// MG_INFO(("Header: [%.*s]", (int) (h2 - h1), &s[h1]));
|
||||
if (part != NULL && h1 + cd.len + 2 < h2 && s[h1 + cd.len] == ':' &&
|
||||
mg_ncasecmp(&s[h1], cd.ptr, cd.len) == 0) {
|
||||
struct mg_str v = mg_str_n(&s[h1 + cd.len + 2], h2 - (h1 + cd.len + 2));
|
||||
@ -56,7 +56,7 @@ size_t mg_http_next_multipart(struct mg_str body, size_t ofs,
|
||||
|
||||
if (b2 + 2 >= max) return 0;
|
||||
if (part != NULL) part->body = mg_str_n(&s[b1], b2 - b1);
|
||||
// LOG(LL_INFO, ("Body: [%.*s]", (int) (b2 - b1), &s[b1]));
|
||||
// MG_INFO(("Body: [%.*s]", (int) (b2 - b1), &s[b1]));
|
||||
return b2 + 2;
|
||||
}
|
||||
|
||||
@ -82,8 +82,8 @@ void mg_http_bauth(struct mg_connection *c, const char *user,
|
||||
c->send.len += 21 + (size_t) n + 2;
|
||||
memcpy(&c->send.buf[c->send.len - 2], "\r\n", 2);
|
||||
} else {
|
||||
LOG(LL_ERROR, ("%lu %s cannot resize iobuf %d->%d ", c->id, c->label,
|
||||
(int) c->send.size, (int) need));
|
||||
MG_ERROR(("%lu %s cannot resize iobuf %d->%d ", c->id, c->label,
|
||||
(int) c->send.size, (int) need));
|
||||
}
|
||||
}
|
||||
|
||||
@ -182,7 +182,7 @@ static void mg_http_parse_headers(const char *s, const char *end,
|
||||
if (k.len == tmp.len) continue;
|
||||
while (v.len > 0 && v.ptr[v.len - 1] == ' ') v.len--; // Trim spaces
|
||||
if (k.len == 0) break;
|
||||
// LOG(LL_INFO, ("--HH [%.*s] [%.*s] [%.*s]", (int) tmp.len - 1, tmp.ptr,
|
||||
// MG_INFO(("--HH [%.*s] [%.*s] [%.*s]", (int) tmp.len - 1, tmp.ptr,
|
||||
//(int) k.len, k.ptr, (int) v.len, v.ptr));
|
||||
h[i].name = k;
|
||||
h[i].value = v;
|
||||
@ -454,18 +454,18 @@ static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
|
||||
|
||||
static int getrange(struct mg_str *s, int64_t *a, int64_t *b) {
|
||||
size_t i, numparsed = 0;
|
||||
// LOG(LL_INFO, ("%.*s", (int) s->len, s->ptr));
|
||||
// MG_INFO(("%.*s", (int) s->len, s->ptr));
|
||||
for (i = 0; i + 6 < s->len; i++) {
|
||||
if (memcmp(&s->ptr[i], "bytes=", 6) == 0) {
|
||||
struct mg_str p = mg_str_n(s->ptr + i + 6, s->len - i - 6);
|
||||
if (p.len > 0 && p.ptr[0] >= '0' && p.ptr[0] <= '9') numparsed++;
|
||||
*a = mg_to64(p);
|
||||
// LOG(LL_INFO, ("PPP [%.*s] %d", (int) p.len, p.ptr, numparsed));
|
||||
// MG_INFO(("PPP [%.*s] %d", (int) p.len, p.ptr, numparsed));
|
||||
while (p.len && p.ptr[0] >= '0' && p.ptr[0] <= '9') p.ptr++, p.len--;
|
||||
if (p.len && p.ptr[0] == '-') p.ptr++, p.len--;
|
||||
*b = mg_to64(p);
|
||||
if (p.len > 0 && p.ptr[0] >= '0' && p.ptr[0] <= '9') numparsed++;
|
||||
// LOG(LL_INFO, ("PPP [%.*s] %d", (int) p.len, p.ptr, numparsed));
|
||||
// MG_INFO(("PPP [%.*s] %d", (int) p.len, p.ptr, numparsed));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -482,7 +482,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
|
||||
struct mg_str *inm = NULL;
|
||||
|
||||
if (fd == NULL || fs->st(path, &size, &mtime) == 0) {
|
||||
LOG(LL_DEBUG, ("404 [%s] %p", path, (void *) fd));
|
||||
MG_DEBUG(("404 [%s] %p", path, (void *) fd));
|
||||
mg_http_reply(c, 404, "", "%s", "Not found\n");
|
||||
mg_fs_close(fd);
|
||||
// NOTE: mg_http_etag() call should go first!
|
||||
@ -549,12 +549,12 @@ static void printdirentry(const char *name, void *userdata) {
|
||||
char path[MG_PATH_MAX], sz[40], mod[40];
|
||||
int flags, n = 0;
|
||||
|
||||
// LOG(LL_DEBUG, ("[%s] [%s]", d->dir, name));
|
||||
// MG_DEBUG(("[%s] [%s]", d->dir, name));
|
||||
if (mg_snprintf(path, sizeof(path), "%s%c%s", d->dir, '/', name) >
|
||||
sizeof(path)) {
|
||||
LOG(LL_ERROR, ("%s truncated", name));
|
||||
MG_ERROR(("%s truncated", name));
|
||||
} else if ((flags = fs->st(path, &size, &t)) == 0) {
|
||||
LOG(LL_ERROR, ("%lu stat(%s): %d", d->c->id, path, errno));
|
||||
MG_ERROR(("%lu stat(%s): %d", d->c->id, path, errno));
|
||||
} else {
|
||||
const char *slash = flags & MG_FS_DIR ? "/" : "";
|
||||
if (flags & MG_FS_DIR) {
|
||||
@ -677,8 +677,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm,
|
||||
path[path_size - 1] = '\0'; // Double-check
|
||||
remove_double_dots(path);
|
||||
n = strlen(path);
|
||||
LOG(LL_VERBOSE_DEBUG,
|
||||
("%lu %.*s -> %s", c->id, (int) hm->uri.len, hm->uri.ptr, path));
|
||||
MG_VERBOSE(("%lu %.*s -> %s", c->id, (int) hm->uri.len, hm->uri.ptr, path));
|
||||
while (n > 0 && path[n - 1] == '/') path[--n] = 0; // Trim trailing slashes
|
||||
flags = fs->st(path, NULL, NULL); // Does it exist?
|
||||
if (flags == 0) {
|
||||
@ -726,8 +725,7 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
|
||||
const char *sp = opts->ssi_pattern;
|
||||
int flags = uri_to_path(c, hm, opts, path, sizeof(path));
|
||||
if (flags == 0) return;
|
||||
LOG(LL_VERBOSE_DEBUG,
|
||||
("%.*s %s %d", (int) hm->uri.len, hm->uri.ptr, path, flags));
|
||||
MG_VERBOSE(("%.*s %s %d", (int) hm->uri.len, hm->uri.ptr, path, flags));
|
||||
if (flags & MG_FS_DIR) {
|
||||
listdir(c, hm, opts, path);
|
||||
} else if (sp != NULL && mg_globmatch(sp, strlen(sp), path, strlen(path))) {
|
||||
@ -795,7 +793,7 @@ struct mg_str mg_http_get_header_var(struct mg_str s, struct mg_str v) {
|
||||
while (p < x &&
|
||||
(q ? p == b || *p != '"' : *p != ';' && *p != ' ' && *p != ','))
|
||||
p++;
|
||||
// LOG(LL_INFO, ("[%.*s] [%.*s] [%.*s]", (int) s.len, s.ptr, (int) v.len,
|
||||
// MG_INFO(("[%.*s] [%.*s] [%.*s]", (int) s.len, s.ptr, (int) v.len,
|
||||
// v.ptr, (int) (p - b), b));
|
||||
return stripquotes(mg_str_n(b, (size_t) (p - b + q)));
|
||||
}
|
||||
@ -812,7 +810,7 @@ static size_t get_chunk_length(const char *buf, size_t len, size_t *ll) {
|
||||
while (i < len && buf[i] != '\r' && i != '\n') i++;
|
||||
n = mg_unhexn((char *) buf, i);
|
||||
while (i < len && (buf[i] == '\r' || i == '\n')) i++;
|
||||
// LOG(LL_INFO, ("len %zu i %zu n %zu ", len, i, n));
|
||||
// MG_INFO(("len %zu i %zu n %zu ", len, i, n));
|
||||
if (ll != NULL) *ll = i + 1;
|
||||
if (i < len && i + n + 2 < len) return i + n + 3;
|
||||
return 0;
|
||||
@ -827,7 +825,7 @@ static void walkchunks(struct mg_connection *c, struct mg_http_message *hm,
|
||||
char *buf = (char *) &c->recv.buf[reqlen];
|
||||
size_t memo = c->recv.len;
|
||||
size_t cl = get_chunk_length(&buf[off], memo - reqlen - off, &ll);
|
||||
// LOG(LL_INFO, ("len %zu off %zu cl %zu ll %zu", len, off, cl, ll));
|
||||
// MG_INFO(("len %zu off %zu cl %zu ll %zu", len, off, cl, ll));
|
||||
if (cl == 0) break;
|
||||
hm->chunk = mg_str_n(&buf[off + ll], cl < ll + 2 ? 0 : cl - ll - 2);
|
||||
mg_call(c, MG_EV_HTTP_CHUNK, hm);
|
||||
@ -846,7 +844,7 @@ static void walkchunks(struct mg_connection *c, struct mg_http_message *hm,
|
||||
off += cl2;
|
||||
if (cl2 <= 5) break;
|
||||
}
|
||||
// LOG(LL_INFO, ("BL->%d del %d off %d", (int) bl, (int) del, (int) off));
|
||||
// MG_INFO(("BL->%d del %d off %d", (int) bl, (int) del, (int) off));
|
||||
c->recv.len -= off - bl;
|
||||
// Set message length to indicate we've received
|
||||
// everything, to fire MG_EV_HTTP_MSG
|
||||
@ -891,7 +889,7 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm,
|
||||
long oft = strtol(offset, NULL, 0);
|
||||
mg_snprintf(path, sizeof(path), "%s%c%s", dir, MG_DIRSEP, name);
|
||||
remove_double_dots(path);
|
||||
LOG(LL_DEBUG, ("%d bytes @ %ld [%s]", (int) hm->body.len, oft, path));
|
||||
MG_DEBUG(("%d bytes @ %ld [%s]", (int) hm->body.len, oft, path));
|
||||
if (oft == 0) fs->rm(path);
|
||||
if ((fd = mg_fs_open(fs, path, MG_FS_WRITE)) == NULL) {
|
||||
mg_http_reply(c, 400, "", "open(%s): %d", path, errno);
|
||||
@ -921,8 +919,8 @@ static void http_cb(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
} else if (is_chunked && n > 0) {
|
||||
walkchunks(c, &hm, (size_t) n);
|
||||
}
|
||||
// LOG(LL_INFO,
|
||||
//("---->%d %d\n%.*s", n, is_chunked, (int) c->recv.len, c->recv.buf));
|
||||
// MG_INFO(("---->%d %d\n%.*s", n, is_chunked, (int) c->recv.len,
|
||||
// c->recv.buf));
|
||||
if (n < 0 && ev == MG_EV_READ) {
|
||||
mg_error(c, "HTTP parse:\n%.*s", (int) c->recv.len, c->recv.buf);
|
||||
break;
|
||||
|
@ -31,8 +31,7 @@ int mg_iobuf_resize(struct mg_iobuf *io, size_t new_size) {
|
||||
io->size = new_size;
|
||||
} else {
|
||||
ok = 0;
|
||||
LOG(LL_ERROR,
|
||||
("%lu->%lu", (unsigned long) io->size, (unsigned long) new_size));
|
||||
MG_ERROR(("%lld->%lld", (uint64_t) io->size, (uint64_t) new_size));
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
|
@ -14,13 +14,13 @@ static void (*s_fn)(const void *, size_t, void *) = mg_log_stdout;
|
||||
static void *s_fn_param = NULL;
|
||||
|
||||
void mg_log_set(const char *spec) {
|
||||
LOG(LL_DEBUG, ("Setting log level to %s", spec));
|
||||
MG_DEBUG(("Setting log level to %s", spec));
|
||||
s_spec = spec;
|
||||
}
|
||||
|
||||
bool mg_log_prefix(int level, const char *file, int line, const char *fname) {
|
||||
// static unsigned long seq;
|
||||
int max = LL_INFO;
|
||||
int max = MG_LL_INFO;
|
||||
struct mg_str k, v, s = mg_str(s_spec);
|
||||
const char *p = strrchr(file, '/');
|
||||
|
||||
|
17
src/log.h
17
src/log.h
@ -3,7 +3,7 @@
|
||||
#include "arch.h"
|
||||
#include "config.h"
|
||||
|
||||
enum { LL_NONE, LL_ERROR, LL_INFO, LL_DEBUG, LL_VERBOSE_DEBUG };
|
||||
enum { MG_LL_NONE, MG_LL_ERROR, MG_LL_INFO, MG_LL_DEBUG, MG_LL_VERBOSE };
|
||||
void mg_log(const char *fmt, ...) PRINTF_LIKE(1, 2);
|
||||
bool mg_log_prefix(int ll, const char *file, int line, const char *fname);
|
||||
void mg_log_set(const char *spec);
|
||||
@ -14,17 +14,22 @@ void mg_log_set_callback(void (*fn)(const void *, size_t, void *), void *param);
|
||||
|
||||
#if MG_ENABLE_LOG
|
||||
|
||||
#define LOG(level, args) \
|
||||
#define MG_LOG(level, args) \
|
||||
do { \
|
||||
if (mg_log_prefix((level), __FILE__, __LINE__, __func__)) mg_log args; \
|
||||
} while (0)
|
||||
|
||||
#else
|
||||
|
||||
#define LOG(level, args) \
|
||||
do { \
|
||||
(void) level; \
|
||||
if (0) mg_log args; \
|
||||
#define MG_LOG(level, args) \
|
||||
do { \
|
||||
(void) level; \
|
||||
if (0) mg_log args; \
|
||||
} while (0)
|
||||
|
||||
#endif
|
||||
|
||||
#define MG_ERROR(args) MG_LOG(MG_LL_ERROR, args)
|
||||
#define MG_INFO(args) MG_LOG(MG_LL_INFO, args)
|
||||
#define MG_DEBUG(args) MG_LOG(MG_LL_DEBUG, args)
|
||||
#define MG_VERBOSE(args) MG_LOG(MG_LL_VERBOSE, args)
|
||||
|
19
src/mqtt.c
19
src/mqtt.c
@ -88,8 +88,8 @@ void mg_mqtt_pub(struct mg_connection *c, struct mg_str topic,
|
||||
struct mg_str data, int qos, bool retain) {
|
||||
uint8_t flags = (uint8_t) (((qos & 3) << 1) | (retain ? 1 : 0));
|
||||
uint32_t total_len = 2 + (uint32_t) topic.len + (uint32_t) data.len;
|
||||
LOG(LL_DEBUG, ("%lu [%.*s] -> [%.*s]", c->id, (int) topic.len,
|
||||
(char *) topic.ptr, (int) data.len, (char *) data.ptr));
|
||||
MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) topic.len, (char *) topic.ptr,
|
||||
(int) data.len, (char *) data.ptr));
|
||||
if (qos > 0) total_len += 2;
|
||||
mg_mqtt_send_header(c, MQTT_CMD_PUBLISH, flags, total_len);
|
||||
mg_send_u16(c, mg_htons((uint16_t) topic.len));
|
||||
@ -211,26 +211,25 @@ static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
struct mg_mqtt_message mm;
|
||||
int rc = mg_mqtt_parse(c->recv.buf, c->recv.len, &mm);
|
||||
if (rc == MQTT_MALFORMED) {
|
||||
LOG(LL_ERROR, ("%lu MQTT malformed message", c->id));
|
||||
MG_ERROR(("%lu MQTT malformed message", c->id));
|
||||
c->is_closing = 1;
|
||||
break;
|
||||
} else if (rc == MQTT_OK) {
|
||||
LOG(LL_VERBOSE_DEBUG,
|
||||
("%p MQTT CMD %d len %d [%.*s]", c->fd, mm.cmd, (int) mm.dgram.len,
|
||||
(int) mm.data.len, mm.data.ptr));
|
||||
MG_VERBOSE(("%p MQTT CMD %d len %d [%.*s]", c->fd, mm.cmd,
|
||||
(int) mm.dgram.len, (int) mm.data.len, mm.data.ptr));
|
||||
switch (mm.cmd) {
|
||||
case MQTT_CMD_CONNACK:
|
||||
mg_call(c, MG_EV_MQTT_OPEN, &mm.ack);
|
||||
if (mm.ack == 0) {
|
||||
LOG(LL_DEBUG, ("%lu Connected", c->id));
|
||||
MG_DEBUG(("%lu Connected", c->id));
|
||||
} else {
|
||||
LOG(LL_ERROR, ("%lu MQTT auth failed, code %d", c->id, mm.ack));
|
||||
MG_ERROR(("%lu MQTT auth failed, code %d", c->id, mm.ack));
|
||||
c->is_closing = 1;
|
||||
}
|
||||
break;
|
||||
case MQTT_CMD_PUBLISH: {
|
||||
LOG(LL_DEBUG, ("%lu [%.*s] -> [%.*s]", c->id, (int) mm.topic.len,
|
||||
mm.topic.ptr, (int) mm.data.len, mm.data.ptr));
|
||||
MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) mm.topic.len,
|
||||
mm.topic.ptr, (int) mm.data.len, mm.data.ptr));
|
||||
mg_call(c, MG_EV_MQTT_MSG, &mm);
|
||||
break;
|
||||
}
|
||||
|
@ -102,8 +102,7 @@ static bool mg_aton6(struct mg_str str, struct mg_addr *addr) {
|
||||
(str.ptr[i] >= 'A' && str.ptr[i] <= 'F')) {
|
||||
unsigned long val;
|
||||
if (i > j + 3) return false;
|
||||
// LOG(LL_DEBUG, ("%zu %zu [%.*s]", i, j, (int) (i - j + 1),
|
||||
// &str.ptr[j]));
|
||||
// MG_DEBUG(("%zu %zu [%.*s]", i, j, (int) (i - j + 1), &str.ptr[j]));
|
||||
val = mg_unhexn(&str.ptr[j], i - j + 1);
|
||||
addr->ip6[n] = (uint8_t) ((val >> 8) & 255);
|
||||
addr->ip6[n + 1] = (uint8_t) (val & 255);
|
||||
@ -131,7 +130,7 @@ static bool mg_aton6(struct mg_str str, struct mg_addr *addr) {
|
||||
}
|
||||
|
||||
bool mg_aton(struct mg_str str, struct mg_addr *addr) {
|
||||
// LOG(LL_INFO, ("[%.*s]", (int) str.len, str.ptr));
|
||||
// MG_INFO(("[%.*s]", (int) str.len, str.ptr));
|
||||
return mg_atone(str, addr) || mg_atonl(str, addr) || mg_aton4(str, addr) ||
|
||||
mg_aton6(str, addr);
|
||||
}
|
||||
@ -143,7 +142,7 @@ void mg_mgr_free(struct mg_mgr *mgr) {
|
||||
#if MG_ARCH == MG_ARCH_FREERTOS_TCP
|
||||
FreeRTOS_DeleteSocketSet(mgr->ss);
|
||||
#endif
|
||||
LOG(LL_INFO, ("All connections closed"));
|
||||
MG_DEBUG(("All connections closed"));
|
||||
}
|
||||
|
||||
void mg_mgr_init(struct mg_mgr *mgr) {
|
||||
|
18
src/sntp.c
18
src/sntp.c
@ -13,18 +13,18 @@ int64_t mg_sntp_parse(const unsigned char *buf, size_t len) {
|
||||
int64_t res = -1;
|
||||
int mode = len > 0 ? buf[0] & 7 : 0;
|
||||
if (len < 48) {
|
||||
LOG(LL_ERROR, ("%s", "corrupt packet"));
|
||||
MG_ERROR(("%s", "corrupt packet"));
|
||||
} else if ((buf[0] & 0x38) >> 3 != 4) {
|
||||
LOG(LL_ERROR, ("%s", "wrong version"));
|
||||
MG_ERROR(("%s", "wrong version"));
|
||||
} else if (mode != 4 && mode != 5) {
|
||||
LOG(LL_ERROR, ("%s", "not a server reply"));
|
||||
MG_ERROR(("%s", "not a server reply"));
|
||||
} else if (buf[1] == 0) {
|
||||
LOG(LL_ERROR, ("%s", "server sent a kiss of death"));
|
||||
MG_ERROR(("%s", "server sent a kiss of death"));
|
||||
} else {
|
||||
uint32_t *data = (uint32_t *) &buf[40];
|
||||
unsigned long seconds = mg_ntohl(data[0]) - SNTP_TIME_OFFSET;
|
||||
unsigned long useconds = mg_ntohl(data[1]);
|
||||
// LOG(LL_DEBUG, ("%lu %lu %lu", time(0), seconds, useconds));
|
||||
// MG_DEBUG(("%lu %lu %lu", time(0), seconds, useconds));
|
||||
res = ((int64_t) seconds) * 1000 + (int64_t) ((useconds / 1000) % 1000);
|
||||
s_sntmp_next = seconds + SNTP_INTERVAL_SEC;
|
||||
}
|
||||
@ -36,8 +36,8 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len);
|
||||
if (milliseconds > 0) {
|
||||
mg_call(c, MG_EV_SNTP_TIME, &milliseconds);
|
||||
LOG(LL_DEBUG, ("%u.%u, next at %lu", (unsigned) (milliseconds / 1000),
|
||||
(unsigned) (milliseconds % 1000), s_sntmp_next));
|
||||
MG_DEBUG(("%u.%u, next at %lu", (unsigned) (milliseconds / 1000),
|
||||
(unsigned) (milliseconds % 1000), s_sntmp_next));
|
||||
}
|
||||
c->recv.len = 0; // Clear receive buffer
|
||||
} else if (ev == MG_EV_CONNECT) {
|
||||
@ -50,13 +50,13 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
|
||||
void mg_sntp_send(struct mg_connection *c, unsigned long utc) {
|
||||
if (c->is_resolving) {
|
||||
LOG(LL_ERROR, ("%lu wait until resolved", c->id));
|
||||
MG_ERROR(("%lu wait until resolved", c->id));
|
||||
} else if (utc > s_sntmp_next) {
|
||||
uint8_t buf[48] = {0};
|
||||
s_sntmp_next = utc + SNTP_INTERVAL_SEC;
|
||||
buf[0] = (0 << 6) | (4 << 3) | 3;
|
||||
mg_send(c, buf, sizeof(buf));
|
||||
LOG(LL_DEBUG, ("%lu ct %lu, next at %lu", c->id, utc, s_sntmp_next));
|
||||
MG_DEBUG(("%lu ct %lu, next at %lu", c->id, utc, s_sntmp_next));
|
||||
}
|
||||
}
|
||||
|
||||
|
58
src/sock.c
58
src/sock.c
@ -100,7 +100,7 @@ static struct mg_connection *alloc_conn(struct mg_mgr *mgr, bool is_client,
|
||||
}
|
||||
|
||||
static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
|
||||
int log_level = n > 0 ? LL_VERBOSE_DEBUG : LL_DEBUG;
|
||||
int log_level = n > 0 ? MG_LL_VERBOSE : MG_LL_DEBUG;
|
||||
char flags[] = {(char) ('0' + c->is_listening),
|
||||
(char) ('0' + c->is_client),
|
||||
(char) ('0' + c->is_accepted),
|
||||
@ -116,9 +116,9 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
|
||||
(char) ('0' + c->is_readable),
|
||||
(char) ('0' + c->is_writable),
|
||||
'\0'};
|
||||
LOG(log_level,
|
||||
("%3lu %s %d:%d %ld err %d (%s)", c->id, flags, (int) c->send.len,
|
||||
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
|
||||
MG_LOG(log_level,
|
||||
("%3lu %s %d:%d %ld err %d (%s)", c->id, flags, (int) c->send.len,
|
||||
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
|
||||
if (n == 0) {
|
||||
// Do nothing
|
||||
} else if (n < 0) {
|
||||
@ -134,9 +134,9 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
|
||||
memset(&a, 0, sizeof(a));
|
||||
getsockname(FD(c), &usa.sa, &slen);
|
||||
tomgaddr(&usa, &a, c->peer.is_ip6);
|
||||
LOG(LL_INFO, ("\n-- %lu %s %s %s %s %ld\n%s", c->id,
|
||||
mg_straddr(&a, t1, sizeof(t1)), r ? "<-" : "->",
|
||||
mg_straddr(&c->peer, t2, sizeof(t2)), c->label, n, s));
|
||||
MG_INFO(("\n-- %lu %s %s %s %s %ld\n%s", c->id,
|
||||
mg_straddr(&a, t1, sizeof(t1)), r ? "<-" : "->",
|
||||
mg_straddr(&c->peer, t2, sizeof(t2)), c->label, n, s));
|
||||
free(s);
|
||||
(void) t1, (void) t2; // Silence warnings for MG_ENABLE_LOG=0
|
||||
}
|
||||
@ -198,7 +198,7 @@ static SOCKET mg_open_listener(const char *url, struct mg_addr *addr) {
|
||||
memset(addr, 0, sizeof(*addr));
|
||||
addr->port = mg_htons(mg_url_port(url));
|
||||
if (!mg_aton(mg_url_host(url), addr)) {
|
||||
LOG(LL_ERROR, ("invalid listening URL: %s", url));
|
||||
MG_ERROR(("invalid listening URL: %s", url));
|
||||
} else {
|
||||
union usa usa;
|
||||
socklen_t slen = tousa(addr, &usa);
|
||||
@ -249,7 +249,7 @@ static SOCKET mg_open_listener(const char *url, struct mg_addr *addr) {
|
||||
}
|
||||
if (fd == INVALID_SOCKET) {
|
||||
if (s_err == 0) s_err = MG_SOCK_ERRNO;
|
||||
LOG(LL_ERROR, ("Failed to listen on %s, errno %d", url, s_err));
|
||||
MG_ERROR(("Failed to listen on %s, errno %d", url, s_err));
|
||||
}
|
||||
|
||||
return fd;
|
||||
@ -301,7 +301,7 @@ static void close_conn(struct mg_connection *c) {
|
||||
// Order of operations is important. `MG_EV_CLOSE` event must be fired
|
||||
// before we deallocate received data, see #1331
|
||||
mg_call(c, MG_EV_CLOSE, NULL);
|
||||
LOG(LL_DEBUG, ("%lu closed", c->id));
|
||||
MG_DEBUG(("%lu closed", c->id));
|
||||
if (FD(c) != INVALID_SOCKET) {
|
||||
closesocket(FD(c));
|
||||
#if MG_ARCH == MG_ARCH_FREERTOS_TCP
|
||||
@ -375,15 +375,15 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
struct mg_connection *c = NULL;
|
||||
if (url == NULL || url[0] == '\0') {
|
||||
LOG(LL_ERROR, ("null url"));
|
||||
MG_ERROR(("null url"));
|
||||
} else if ((c = alloc_conn(mgr, 1, INVALID_SOCKET)) == NULL) {
|
||||
LOG(LL_ERROR, ("OOM"));
|
||||
MG_ERROR(("OOM"));
|
||||
} else {
|
||||
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
|
||||
c->is_udp = (strncmp(url, "udp:", 4) == 0);
|
||||
c->fn = fn;
|
||||
c->fn_data = fn_data;
|
||||
LOG(LL_DEBUG, ("%lu -> %s", c->id, url));
|
||||
MG_DEBUG(("%lu -> %s", c->id, url));
|
||||
mg_call(c, MG_EV_OPEN, NULL);
|
||||
mg_resolve(c, url);
|
||||
}
|
||||
@ -402,20 +402,20 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
|
||||
// That's not an error, just should try later
|
||||
if (MG_SOCK_ERRNO != EAGAIN)
|
||||
#endif
|
||||
LOG(LL_ERROR, ("%lu accept failed, errno %d", lsn->id, MG_SOCK_ERRNO));
|
||||
MG_ERROR(("%lu accept failed, errno %d", lsn->id, MG_SOCK_ERRNO));
|
||||
#if ((MG_ARCH != MG_ARCH_WIN32) && (MG_ARCH != MG_ARCH_FREERTOS_TCP))
|
||||
} else if ((long) fd >= FD_SETSIZE) {
|
||||
LOG(LL_ERROR, ("%ld > %ld", (long) fd, (long) FD_SETSIZE));
|
||||
MG_ERROR(("%ld > %ld", (long) fd, (long) FD_SETSIZE));
|
||||
closesocket(fd);
|
||||
#endif
|
||||
} else if ((c = alloc_conn(mgr, 0, fd)) == NULL) {
|
||||
LOG(LL_ERROR, ("%lu OOM", lsn->id));
|
||||
MG_ERROR(("%lu OOM", lsn->id));
|
||||
closesocket(fd);
|
||||
} else {
|
||||
char buf[40];
|
||||
tomgaddr(&usa, &c->peer, sa_len != sizeof(usa.sin));
|
||||
mg_straddr(&c->peer, buf, sizeof(buf));
|
||||
LOG(LL_DEBUG, ("%lu accepted %s", c->id, buf));
|
||||
MG_DEBUG(("%lu accepted %s", c->id, buf));
|
||||
mg_set_non_blocking_mode(FD(c));
|
||||
setsockopts(c);
|
||||
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
|
||||
@ -477,13 +477,13 @@ struct mg_connection *mg_mkpipe(struct mg_mgr *mgr, mg_event_handler_t fn,
|
||||
SOCKET sp[2] = {INVALID_SOCKET, INVALID_SOCKET};
|
||||
struct mg_connection *c = NULL;
|
||||
if (!mg_socketpair(sp, usa)) {
|
||||
LOG(LL_ERROR, ("Cannot create socket pair"));
|
||||
MG_ERROR(("Cannot create socket pair"));
|
||||
} else if ((c = alloc_conn(mgr, false, sp[1])) == NULL) {
|
||||
closesocket(sp[0]);
|
||||
closesocket(sp[1]);
|
||||
LOG(LL_ERROR, ("OOM"));
|
||||
MG_ERROR(("OOM"));
|
||||
} else {
|
||||
LOG(LL_INFO, ("pipe %lu", (unsigned long) sp[0]));
|
||||
MG_DEBUG(("pipe %lu", (unsigned long) sp[0]));
|
||||
tomgaddr(&usa[0], &c->peer, false);
|
||||
c->is_udp = 1;
|
||||
c->pfn = pf1;
|
||||
@ -503,9 +503,9 @@ struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url,
|
||||
struct mg_addr addr;
|
||||
SOCKET fd = mg_open_listener(url, &addr);
|
||||
if (fd == INVALID_SOCKET) {
|
||||
LOG(LL_ERROR, ("Failed: %s, errno %d", url, MG_SOCK_ERRNO));
|
||||
MG_ERROR(("Failed: %s, errno %d", url, MG_SOCK_ERRNO));
|
||||
} else if ((c = alloc_conn(mgr, 0, fd)) == NULL) {
|
||||
LOG(LL_ERROR, ("OOM %s", url));
|
||||
MG_ERROR(("OOM %s", url));
|
||||
closesocket(fd);
|
||||
} else {
|
||||
memcpy(&c->peer, &addr, sizeof(struct mg_addr));
|
||||
@ -516,8 +516,7 @@ struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url,
|
||||
c->fn = fn;
|
||||
c->fn_data = fn_data;
|
||||
mg_call(c, MG_EV_OPEN, NULL);
|
||||
LOG(LL_DEBUG,
|
||||
("%lu accepting on %s (port %u)", c->id, url, mg_ntohs(c->peer.port)));
|
||||
MG_DEBUG(("%lu %s port %u", c->id, url, mg_ntohs(c->peer.port)));
|
||||
}
|
||||
return c;
|
||||
}
|
||||
@ -558,7 +557,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
|
||||
}
|
||||
|
||||
if ((rc = select((int) maxfd + 1, &rset, &wset, NULL, &tv)) < 0) {
|
||||
LOG(LL_DEBUG, ("select: %d %d", rc, MG_SOCK_ERRNO));
|
||||
MG_DEBUG(("select: %d %d", rc, MG_SOCK_ERRNO));
|
||||
FD_ZERO(&rset);
|
||||
FD_ZERO(&wset);
|
||||
}
|
||||
@ -600,11 +599,10 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
|
||||
for (c = mgr->conns; c != NULL; c = tmp) {
|
||||
tmp = c->next;
|
||||
mg_call(c, MG_EV_POLL, &now);
|
||||
LOG(LL_VERBOSE_DEBUG,
|
||||
("%lu %c%c %c%c%c%c%c", c->id, c->is_readable ? 'r' : '-',
|
||||
c->is_writable ? 'w' : '-', c->is_tls ? 'T' : 't',
|
||||
c->is_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h',
|
||||
c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c'));
|
||||
MG_VERBOSE(("%lu %c%c %c%c%c%c%c", c->id, c->is_readable ? 'r' : '-',
|
||||
c->is_writable ? 'w' : '-', c->is_tls ? 'T' : 't',
|
||||
c->is_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h',
|
||||
c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c'));
|
||||
if (c->is_resolving || c->is_closing) {
|
||||
// Do nothing
|
||||
} else if (c->is_listening && c->is_udp == 0) {
|
||||
|
@ -27,7 +27,7 @@ static char *mg_ssi(const char *path, const char *root, int depth) {
|
||||
mg_iobuf_add(&b, b.len, data, strlen(data), align);
|
||||
free(data);
|
||||
} else {
|
||||
LOG(LL_ERROR, ("%s: file=%s error or too deep", path, arg));
|
||||
MG_ERROR(("%s: file=%s error or too deep", path, arg));
|
||||
}
|
||||
} else if (sscanf(buf, "<!--#include virtual=\"%[^\"]", arg)) {
|
||||
char tmp[MG_PATH_MAX + BUFSIZ + 10], *data;
|
||||
@ -37,11 +37,11 @@ static char *mg_ssi(const char *path, const char *root, int depth) {
|
||||
mg_iobuf_add(&b, b.len, data, strlen(data), align);
|
||||
free(data);
|
||||
} else {
|
||||
LOG(LL_ERROR, ("%s: virtual=%s error or too deep", path, arg));
|
||||
MG_ERROR(("%s: virtual=%s error or too deep", path, arg));
|
||||
}
|
||||
} else {
|
||||
// Unknown SSI tag
|
||||
LOG(LL_INFO, ("Unknown SSI tag: %.*s", (int) len, buf));
|
||||
MG_ERROR(("Unknown SSI tag: %.*s", (int) len, buf));
|
||||
mg_iobuf_add(&b, b.len, buf, len, align);
|
||||
}
|
||||
intag = 0;
|
||||
@ -55,7 +55,7 @@ static char *mg_ssi(const char *path, const char *root, int depth) {
|
||||
if (len == 5 && strncmp(buf, "<!--#", 5) != 0) {
|
||||
intag = 0;
|
||||
} else if (len >= sizeof(buf) - 2) {
|
||||
LOG(LL_ERROR, ("%s: SSI tag is too large", path));
|
||||
MG_ERROR(("%s: SSI tag is too large", path));
|
||||
len = 0;
|
||||
}
|
||||
buf[len++] = (char) (ch & 0xff);
|
||||
|
@ -51,12 +51,12 @@ void mg_tls_handshake(struct mg_connection *c) {
|
||||
mbedtls_ssl_set_bio(&tls->ssl, &c->fd, mg_net_send, mg_net_recv, 0);
|
||||
rc = mbedtls_ssl_handshake(&tls->ssl);
|
||||
if (rc == 0) { // Success
|
||||
LOG(LL_DEBUG, ("%lu success", c->id));
|
||||
MG_DEBUG(("%lu success", c->id));
|
||||
c->is_tls_hs = 0;
|
||||
} else if (rc == MBEDTLS_ERR_SSL_WANT_READ ||
|
||||
rc == MBEDTLS_ERR_SSL_WANT_WRITE) { // Still pending
|
||||
LOG(LL_VERBOSE_DEBUG, ("%lu pending, %d%d %d (-%#x)", c->id,
|
||||
c->is_connecting, c->is_tls_hs, rc, -rc));
|
||||
MG_VERBOSE(("%lu pending, %d%d %d (-%#x)", c->id, c->is_connecting,
|
||||
c->is_tls_hs, rc, -rc));
|
||||
} else {
|
||||
mg_error(c, "TLS handshake: -%#x", -rc); // Error
|
||||
}
|
||||
@ -70,7 +70,7 @@ static int mbed_rng(void *ctx, unsigned char *buf, size_t len) {
|
||||
|
||||
static void debug_cb(void *c, int lev, const char *s, int n, const char *s2) {
|
||||
n = (int) strlen(s2) - 1;
|
||||
LOG(LL_VERBOSE_DEBUG, ("%p %.*s", ((struct mg_connection *) c)->fd, n, s2));
|
||||
MG_VERBOSE(("%p %.*s", ((struct mg_connection *) c)->fd, n, s2));
|
||||
(void) s;
|
||||
(void) c;
|
||||
(void) lev;
|
||||
@ -100,7 +100,7 @@ void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
|
||||
mg_error(c, "TLS OOM");
|
||||
goto fail;
|
||||
}
|
||||
LOG(LL_DEBUG, ("%lu Setting TLS", c->id));
|
||||
MG_DEBUG(("%lu Setting TLS", c->id));
|
||||
mbedtls_ssl_init(&tls->ssl);
|
||||
mbedtls_ssl_config_init(&tls->conf);
|
||||
mbedtls_x509_crt_init(&tls->ca);
|
||||
|
@ -34,10 +34,10 @@ void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
|
||||
SSL_library_init();
|
||||
s_initialised++;
|
||||
}
|
||||
LOG(LL_DEBUG, ("%lu Setting TLS, CA: %s, cert: %s, key: %s", c->id,
|
||||
opts->ca == NULL ? "null" : opts->ca,
|
||||
opts->cert == NULL ? "null" : opts->cert,
|
||||
opts->certkey == NULL ? "null" : opts->certkey));
|
||||
MG_DEBUG(("%lu Setting TLS, CA: %s, cert: %s, key: %s", c->id,
|
||||
opts->ca == NULL ? "null" : opts->ca,
|
||||
opts->cert == NULL ? "null" : opts->cert,
|
||||
opts->certkey == NULL ? "null" : opts->certkey));
|
||||
tls->ctx = c->is_client ? SSL_CTX_new(SSLv23_client_method())
|
||||
: SSL_CTX_new(SSLv23_server_method());
|
||||
if ((tls->ssl = SSL_new(tls->ctx)) == NULL) {
|
||||
@ -102,7 +102,7 @@ void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
|
||||
if (c->is_client && c->is_resolving == 0 && c->is_connecting == 0) {
|
||||
mg_tls_handshake(c);
|
||||
}
|
||||
LOG(LL_DEBUG, ("%lu SSL %s OK", c->id, c->is_accepted ? "accept" : "client"));
|
||||
MG_DEBUG(("%lu SSL %s OK", c->id, c->is_accepted ? "accept" : "client"));
|
||||
return;
|
||||
fail:
|
||||
c->is_closing = 1;
|
||||
@ -115,7 +115,7 @@ void mg_tls_handshake(struct mg_connection *c) {
|
||||
SSL_set_fd(tls->ssl, (int) (size_t) c->fd);
|
||||
rc = c->is_client ? SSL_connect(tls->ssl) : SSL_accept(tls->ssl);
|
||||
if (rc == 1) {
|
||||
LOG(LL_DEBUG, ("%lu success", c->id));
|
||||
MG_DEBUG(("%lu success", c->id));
|
||||
c->is_tls_hs = 0;
|
||||
} else {
|
||||
int code = mg_tls_err(tls, rc);
|
||||
|
14
src/ws.c
14
src/ws.c
@ -115,7 +115,7 @@ size_t mg_ws_send(struct mg_connection *c, const char *buf, size_t len,
|
||||
uint8_t header[14];
|
||||
size_t header_len = mkhdr(len, op, c->is_client, header);
|
||||
mg_send(c, header, header_len);
|
||||
LOG(LL_VERBOSE_DEBUG, ("WS out: %d [%.*s]", (int) len, (int) len, buf));
|
||||
MG_VERBOSE(("WS out: %d [%.*s]", (int) len, (int) len, buf));
|
||||
mg_send(c, buf, len);
|
||||
mg_ws_mask(c, len);
|
||||
return header_len + len;
|
||||
@ -134,8 +134,7 @@ static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
c->is_closing = 1; // Some just, not an HTTP request
|
||||
} else if (n > 0) {
|
||||
if (n < 15 || memcmp(c->recv.buf + 9, "101", 3) != 0) {
|
||||
LOG(LL_ERROR,
|
||||
("%lu WS handshake error: %.*s", c->id, 15, c->recv.buf));
|
||||
MG_ERROR(("%lu WS handshake error: %.*s", c->id, 15, c->recv.buf));
|
||||
c->is_closing = 1;
|
||||
} else {
|
||||
struct mg_http_message hm;
|
||||
@ -154,14 +153,14 @@ static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
struct mg_ws_message m = {{s, msg.data_len}, msg.flags};
|
||||
size_t len = msg.header_len + msg.data_len;
|
||||
uint8_t final = msg.flags & 128, op = msg.flags & 15;
|
||||
// LOG(LL_VERBOSE_DEBUG, ("fin %d op %d len %d [%.*s]", final, op,
|
||||
// MG_VERBOSE ("fin %d op %d len %d [%.*s]", final, op,
|
||||
// (int) m.data.len, (int) m.data.len, m.data.ptr));
|
||||
switch (op) {
|
||||
case WEBSOCKET_OP_CONTINUE:
|
||||
mg_call(c, MG_EV_WS_CTL, &m);
|
||||
break;
|
||||
case WEBSOCKET_OP_PING:
|
||||
LOG(LL_DEBUG, ("%s", "WS PONG"));
|
||||
MG_DEBUG(("%s", "WS PONG"));
|
||||
mg_ws_send(c, s, msg.data_len, WEBSOCKET_OP_PONG);
|
||||
mg_call(c, MG_EV_WS_CTL, &m);
|
||||
break;
|
||||
@ -173,7 +172,7 @@ static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
if (final) mg_call(c, MG_EV_WS_MSG, &m);
|
||||
break;
|
||||
case WEBSOCKET_OP_CLOSE:
|
||||
LOG(LL_DEBUG, ("%lu Got WS CLOSE", c->id));
|
||||
MG_DEBUG(("%lu Got WS CLOSE", c->id));
|
||||
mg_call(c, MG_EV_WS_CTL, &m);
|
||||
c->is_closing = 1;
|
||||
break;
|
||||
@ -190,7 +189,7 @@ static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
len -= msg.header_len;
|
||||
ofs += len;
|
||||
c->pfn_data = (void *) ofs;
|
||||
// LOG(LL_INFO, ("FRAG %d [%.*s]", (int) ofs, (int) ofs, c->recv.buf));
|
||||
// MG_INFO(("FRAG %d [%.*s]", (int) ofs, (int) ofs, c->recv.buf));
|
||||
}
|
||||
// Remove non-fragmented frame
|
||||
if (final && op) mg_iobuf_del(&c->recv, ofs, len);
|
||||
@ -227,7 +226,6 @@ struct mg_connection *mg_ws_connect(struct mg_mgr *mgr, const char *url,
|
||||
// mg_url_host(url, host, sizeof(host));
|
||||
mg_random(nonce, sizeof(nonce));
|
||||
mg_base64_encode((unsigned char *) nonce, sizeof(nonce), key);
|
||||
// LOG(LL_DEBUG, "%p [%s]", uri, uri == NULL ? "???" : uri);
|
||||
n2 = mg_asprintf(&buf2, sizeof(mem2),
|
||||
"GET %s HTTP/1.1\r\n"
|
||||
"Upgrade: websocket\r\n"
|
||||
|
@ -152,7 +152,7 @@ static void test_http_get_var(void) {
|
||||
}
|
||||
|
||||
static int vcmp(struct mg_str s1, const char *s2) {
|
||||
// LOG(LL_INFO, ("->%.*s<->%s<- %d %d %d", (int) s1.len, s1.ptr, s2,
|
||||
// MG_INFO(("->%.*s<->%s<- %d %d %d", (int) s1.len, s1.ptr, s2,
|
||||
//(int) s1.len, strncmp(s1.ptr, s2, s1.len), mg_vcmp(&s1, s2)));
|
||||
return mg_vcmp(&s1, s2) == 0;
|
||||
}
|
||||
@ -369,7 +369,7 @@ static void test_mqtt(void) {
|
||||
mg_mqtt_sub(c, topic, 1);
|
||||
mg_mqtt_pub(c, topic, data, 1, false);
|
||||
for (i = 0; i < 300 && buf[1] == 0; i++) mg_mgr_poll(&mgr, 10);
|
||||
// LOG(LL_INFO, ("[%s]", buf));
|
||||
// MG_INFO(("[%s]", buf));
|
||||
ASSERT(strcmp(buf, "Xx/f12/hi") == 0);
|
||||
|
||||
// Set params
|
||||
@ -399,9 +399,8 @@ static void eh1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_ACCEPT && topts != NULL) mg_tls_init(c, topts);
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
LOG(LL_INFO,
|
||||
("[%.*s %.*s] message len %d", (int) hm->method.len, hm->method.ptr,
|
||||
(int) hm->uri.len, hm->uri.ptr, (int) hm->message.len));
|
||||
MG_INFO(("[%.*s %.*s] message len %d", (int) hm->method.len, hm->method.ptr,
|
||||
(int) hm->uri.len, hm->uri.ptr, (int) hm->message.len));
|
||||
if (mg_http_match_uri(hm, "/foo/*")) {
|
||||
mg_http_reply(c, 200, "", "uri: %.*s", hm->uri.len - 5, hm->uri.ptr + 5);
|
||||
} else if (mg_http_match_uri(hm, "/ws")) {
|
||||
@ -549,7 +548,7 @@ static void test_ws(void) {
|
||||
ASSERT(mg_http_listen(&mgr, url, eh1, NULL) != NULL);
|
||||
mg_ws_connect(&mgr, url, wcb, &done, "%s", "Sec-WebSocket-Protocol: meh\r\n");
|
||||
for (i = 0; i < 30; i++) mg_mgr_poll(&mgr, 1);
|
||||
// LOG(LL_INFO, ("--> %d", done));
|
||||
// MG_INFO(("--> %d", done));
|
||||
ASSERT(done == 112);
|
||||
|
||||
// Test that non-WS requests fail
|
||||
@ -628,7 +627,7 @@ static void test_http_server(void) {
|
||||
}
|
||||
|
||||
ASSERT(fetch(&mgr, buf, url, "GET /foo/1 HTTP/1.0\r\n\n") == 200);
|
||||
// LOG(LL_INFO, ("%d %.*s", (int) hm.len, (int) hm.len, hm.buf));
|
||||
// MG_INFO(("%d %.*s", (int) hm.len, (int) hm.len, hm.buf));
|
||||
ASSERT(cmpbody(buf, "uri: 1") == 0);
|
||||
|
||||
ASSERT(fetch(&mgr, buf, url, "%s",
|
||||
@ -785,7 +784,7 @@ static void test_tls(void) {
|
||||
c = mg_http_listen(&mgr, url, eh1, (void *) &opts);
|
||||
ASSERT(c != NULL);
|
||||
ASSERT(fetch(&mgr, buf, url, "GET /a.txt HTTP/1.0\n\n") == 200);
|
||||
// LOG(LL_INFO, ("%s", buf));
|
||||
// MG_INFO(("%s", buf));
|
||||
ASSERT(cmpbody(buf, "hello\n") == 0);
|
||||
mg_mgr_free(&mgr);
|
||||
ASSERT(mgr.conns == NULL);
|
||||
@ -794,14 +793,14 @@ static void test_tls(void) {
|
||||
|
||||
static void f3(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
int *ok = (int *) fn_data;
|
||||
// LOG(LL_INFO, ("%d", ev));
|
||||
// MG_INFO(("%d", ev));
|
||||
if (ev == MG_EV_CONNECT) {
|
||||
// c->is_hexdumping = 1;
|
||||
mg_printf(c, "GET / HTTP/1.0\r\nHost: %s\r\n\r\n",
|
||||
c->peer.is_ip6 ? "ipv6.google.com" : "cesanta.com");
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
// LOG(LL_INFO, ("-->[%.*s]", (int) hm->message.len, hm->message.ptr));
|
||||
// MG_INFO(("-->[%.*s]", (int) hm->message.len, hm->message.ptr));
|
||||
// ASSERT(mg_vcmp(&hm->method, "HTTP/1.1") == 0);
|
||||
// ASSERT(mg_vcmp(&hm->uri, "301") == 0);
|
||||
*ok = atoi(hm->uri.ptr);
|
||||
@ -888,7 +887,7 @@ static void test_http_pipeline(void) {
|
||||
c = mg_http_connect(&mgr, url, NULL, NULL);
|
||||
mg_printf(c, "POST / HTTP/1.0\nContent-Length: 5\n\n12345GET / HTTP/1.0\n\n");
|
||||
for (i = 0; i < 20; i++) mg_mgr_poll(&mgr, 1);
|
||||
// LOG(LL_INFO, ("-----> [%d]", ok));
|
||||
// MG_INFO(("-----> [%d]", ok));
|
||||
ASSERT(ok == 2);
|
||||
mg_mgr_free(&mgr);
|
||||
ASSERT(mgr.conns == NULL);
|
||||
@ -1068,7 +1067,7 @@ static void test_http_range(void) {
|
||||
ASSERT(mg_http_parse(buf, strlen(buf), &hm) > 0);
|
||||
ASSERT(mg_strcmp(hm.uri, mg_str("206")) == 0);
|
||||
ASSERT(mg_strcmp(hm.body, mg_str("is disease.\n")) == 0);
|
||||
// LOG(LL_INFO, ("----%d\n[%s]", (int) hm.body.len, buf));
|
||||
// MG_INFO(("----%d\n[%s]", (int) hm.body.len, buf));
|
||||
|
||||
// Fetch past EOF, must trigger 416 response
|
||||
fetch(&mgr, buf, url, "%s", "GET /range.txt HTTP/1.0\nRange: bytes=999-\n\n");
|
||||
@ -1095,7 +1094,7 @@ static void test_timer(void) {
|
||||
int v1 = 0, v2 = 0, v3 = 0;
|
||||
struct mg_timer t1, t2, t3;
|
||||
|
||||
LOG(LL_INFO, ("g_timers: %p", g_timers));
|
||||
MG_INFO(("g_timers: %p", g_timers));
|
||||
ASSERT(g_timers == NULL);
|
||||
|
||||
mg_timer_init(&t1, 5, MG_TIMER_REPEAT, f1, &v1);
|
||||
@ -1208,8 +1207,8 @@ static bool sn(const char *fmt, ...) {
|
||||
va_end(ap);
|
||||
result = n1 == n2 && n1 == n && strcmp(buf, buf2) == 0;
|
||||
if (!result)
|
||||
LOG(LL_ERROR, ("[%s] -> [%s] != [%s] %d %d %d\n", fmt, buf, buf2, (int) n1,
|
||||
(int) n2, (int) n));
|
||||
MG_ERROR(("[%s] -> [%s] != [%s] %d %d %d\n", fmt, buf, buf2, (int) n1,
|
||||
(int) n2, (int) n));
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -1281,12 +1280,12 @@ static void test_dns_timeout(const char *dns_server_url, const char *errstr) {
|
||||
mg_mgr_init(&mgr);
|
||||
mgr.dns4.url = dns_server_url;
|
||||
mgr.dnstimeout = 10;
|
||||
LOG(LL_DEBUG, ("opening dummy DNS listener..."));
|
||||
MG_DEBUG(("opening dummy DNS listener..."));
|
||||
mg_listen(&mgr, mgr.dns4.url, NULL, NULL); // Just discard our queries
|
||||
mg_http_connect(&mgr, "http://google.com", fn1, buf);
|
||||
for (i = 0; i < 50 && buf[0] == '\0'; i++) mg_mgr_poll(&mgr, 1);
|
||||
mg_mgr_free(&mgr);
|
||||
LOG(LL_DEBUG, ("buf: [%s]", buf));
|
||||
MG_DEBUG(("buf: [%s]", buf));
|
||||
ASSERT(strcmp(buf, errstr) == 0);
|
||||
}
|
||||
|
||||
@ -1451,7 +1450,7 @@ static void eh3(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
mg_printf(c, "GET / HTTP/1.0\n\n");
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
// LOG(LL_INFO, ("----> [%.*s]", (int) hm->body.len, hm->body.ptr));
|
||||
// MG_INFO(("----> [%.*s]", (int) hm->body.len, hm->body.ptr));
|
||||
c->is_closing = 1;
|
||||
*(uint32_t *) fn_data = mg_crc32(0, hm->body.ptr, hm->body.len);
|
||||
}
|
||||
@ -1468,7 +1467,7 @@ static void eh4(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
*crc = mg_crc32(*crc, hm->body.ptr, hm->body.len);
|
||||
// LOG(LL_INFO, ("MSG [%.*s]", (int) hm->body.len, hm->body.ptr));
|
||||
// MG_INFO(("MSG [%.*s]", (int) hm->body.len, hm->body.ptr));
|
||||
c->is_closing = 1;
|
||||
*(uint32_t *) fn_data = *crc;
|
||||
}
|
||||
@ -1483,14 +1482,14 @@ static void eh5(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
} else if (ev == MG_EV_HTTP_CHUNK) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
*crc = mg_crc32(*crc, hm->chunk.ptr, hm->chunk.len);
|
||||
// LOG(LL_INFO, ("CHUNK [%.*s]", (int) hm->chunk.len, hm->chunk.ptr));
|
||||
// MG_INFO(("CHUNK [%.*s]", (int) hm->chunk.len, hm->chunk.ptr));
|
||||
mg_http_delete_chunk(c, hm);
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
*crc = mg_crc32(*crc, hm->chunk.ptr, hm->chunk.len);
|
||||
c->is_closing = 1;
|
||||
*(uint32_t *) fn_data = *crc;
|
||||
// LOG(LL_INFO, ("MSG [%.*s]", (int) hm->body.len, hm->body.ptr));
|
||||
// MG_INFO(("MSG [%.*s]", (int) hm->body.len, hm->body.ptr));
|
||||
}
|
||||
(void) ev_data;
|
||||
}
|
||||
@ -1544,12 +1543,12 @@ static void test_multipart(void) {
|
||||
ASSERT(mg_http_next_multipart(mg_str(""), 0, NULL) == 0);
|
||||
ASSERT((ofs = mg_http_next_multipart(mg_str(s), 0, &part)) > 0);
|
||||
ASSERT(mg_strcmp(part.name, mg_str("val")) == 0);
|
||||
// LOG(LL_INFO, ("--> [%.*s]", (int) part.body.len, part.body.ptr));
|
||||
// MG_INFO(("--> [%.*s]", (int) part.body.len, part.body.ptr));
|
||||
ASSERT(mg_strcmp(part.body, mg_str("abc\r\ndef")) == 0);
|
||||
ASSERT(part.filename.len == 0);
|
||||
ASSERT((ofs = mg_http_next_multipart(mg_str(s), ofs, &part)) > 0);
|
||||
ASSERT(mg_strcmp(part.name, mg_str("foo")) == 0);
|
||||
// LOG(LL_INFO, ("--> [%.*s]", (int) part.filename.len, part.filename.ptr));
|
||||
// MG_INFO(("--> [%.*s]", (int) part.filename.len, part.filename.ptr));
|
||||
ASSERT(mg_strcmp(part.filename, mg_str("a b.txt")) == 0);
|
||||
ASSERT(mg_strcmp(part.body, mg_str("hello world\r\n")) == 0);
|
||||
ASSERT(mg_http_next_multipart(mg_str(s), ofs, &part) == 0);
|
||||
@ -1638,7 +1637,7 @@ static void test_udp(void) {
|
||||
mg_listen(&mgr, url, u1, (void *) &done);
|
||||
mg_connect(&mgr, url, u1, (void *) &done);
|
||||
for (i = 0; i < 5; i++) mg_mgr_poll(&mgr, 1);
|
||||
// LOG(LL_INFO, ("%d", done));
|
||||
// MG_INFO(("%d", done));
|
||||
ASSERT(done == 111);
|
||||
mg_mgr_free(&mgr);
|
||||
ASSERT(mgr.conns == NULL);
|
||||
@ -1658,7 +1657,7 @@ static void test_check_ip_acl(void) {
|
||||
}
|
||||
|
||||
static void w3(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
// LOG(LL_INFO, ("ev %d", ev));
|
||||
// MG_INFO(("ev %d", ev));
|
||||
if (ev == MG_EV_WS_OPEN) {
|
||||
mg_ws_send(c, "hi there!", 9, WEBSOCKET_OP_TEXT);
|
||||
} else if (ev == MG_EV_WS_MSG) {
|
||||
@ -1706,7 +1705,7 @@ static void test_ws_fragmentation(void) {
|
||||
ASSERT(mg_http_listen(&mgr, url, w2, NULL) != NULL);
|
||||
mg_ws_connect(&mgr, url, w3, &done, "%s", "Sec-WebSocket-Protocol: echo\r\n");
|
||||
for (i = 0; i < 25; i++) mg_mgr_poll(&mgr, 1);
|
||||
// LOG(LL_INFO, ("--> %d", done));
|
||||
// MG_INFO(("--> %d", done));
|
||||
ASSERT(done == 11);
|
||||
|
||||
mg_mgr_free(&mgr);
|
||||
@ -1748,7 +1747,7 @@ static void test_get_header_var(void) {
|
||||
struct mg_str header = mg_str("Digest foo=\"bar\", blah,boo=baz, x=\"yy\"");
|
||||
struct mg_str yy = mg_str("yy");
|
||||
// struct mg_str x = mg_http_get_header_var(header, mg_str("x"));
|
||||
// LOG(LL_INFO, ("--> [%d] [%d]", (int) x.len, yy.len));
|
||||
// MG_INFO(("--> [%d] [%d]", (int) x.len, yy.len));
|
||||
ASSERT(mg_strcmp(empty, mg_http_get_header_var(empty, empty)) == 0);
|
||||
ASSERT(mg_strcmp(empty, mg_http_get_header_var(header, empty)) == 0);
|
||||
ASSERT(mg_strcmp(empty, mg_http_get_header_var(header, mg_str("fooo"))) == 0);
|
||||
|
Loading…
x
Reference in New Issue
Block a user