Fix #2612 - struct mg_str::ptr -> buf

This commit is contained in:
Sergey Lyubka committed 2024-04-16 22:42:45 +01:00
1 parent e93d18d200
commit e0e599ffa1
61 files changed
+684 -686

No files matched your search

@@ -37,8 +37,8 @@ static void simple_http_listener(struct mg_connection *c, int ev, void *ev_data)
// Content-Length header automatically. In the response, we show
// the requested URI and HTTP body:
mg_http_reply(c, 200, "", "{%m:%m,%m:%m}\n", // See mg_snprintf doc
MG_ESC("uri"), mg_print_esc, hm->uri.len, hm->uri.ptr,
MG_ESC("body"), mg_print_esc, hm->body.len, hm->body.ptr);
MG_ESC("uri"), mg_print_esc, hm->uri.len, hm->uri.buf,
MG_ESC("body"), mg_print_esc, hm->body.len, hm->body.buf);
} else {
// For all other URIs, serve some static content
mg_http_reply(c, 200, "", "<html>millis: %lu</html>", millis());
+2 -2
View File
@@ -26,8 +26,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
// Received MQTT message
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
MG_INFO(("%lu RECEIVED %.*s <- %.*s", c->id, (int) mm->data.len,
mm->data.ptr, (int) mm->topic.len, mm->topic.ptr));
exec_command(mm->data.ptr, mm->data.len);
mm->data.buf, (int) mm->topic.len, mm->topic.buf));
exec_command(mm->data.buf, mm->data.len);
} else if (ev == MG_EV_CLOSE) {
MG_INFO(("%lu CLOSED", c->id));
mqtt_connection = NULL;
+2 -2
View File
@@ -210,7 +210,7 @@ static void handle_firmware_upload(struct mg_connection *c,
mg_http_reply(c, 500, "", "offset and total not set\n");
} else if (ofs == 0 && mg_ota_begin((size_t) tot) == false) {
mg_http_reply(c, 500, "", "mg_ota_begin(%ld) failed\n", tot);
} else if (data.len > 0 && mg_ota_write(data.ptr, data.len) == false) {
} else if (data.len > 0 && mg_ota_write(data.buf, data.len) == false) {
mg_http_reply(c, 500, "", "mg_ota_write(%lu) @%ld failed\n", data.len, ofs);
mg_ota_end();
} else if (data.len == 0 && mg_ota_end() == false) {
@@ -313,7 +313,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
mg_http_serve_dir(c, ev_data, &opts);
}
MG_DEBUG(("%lu %.*s %.*s -> %.*s", c->id, (int) hm->method.len,
hm->method.ptr, (int) hm->uri.len, hm->uri.ptr, (int) 3,
hm->method.buf, (int) hm->uri.len, hm->uri.buf, (int) 3,
&c->send.buf[9]));
}
}
+3 -3
View File
@@ -10,7 +10,7 @@ struct mg_str config_read(void) {
}
void config_write(struct mg_str config) {
mg_file_write(&mg_fs_posix, FS_ROOT "/config.json", config.ptr, config.len);
mg_file_write(&mg_fs_posix, FS_ROOT "/config.json", config.buf, config.len);
}
void app_main(void) {
@@ -22,13 +22,13 @@ void app_main(void) {
// Try to connect to wifi by using saved WiFi credentials
struct mg_str json = mg_file_read(&mg_fs_posix, WIFI_FILE);
if (json.ptr != NULL) {
if (json.buf != NULL) {
char *ssid = mg_json_get_str(json, "$.ssid");
char *pass = mg_json_get_str(json, "$.pass");
while (!wifi_init(ssid, pass)) (void) 0;
free(ssid);
free(pass);
free((void *) json.ptr);
free((void *) json.buf);
} else {
// If WiFi is not configured, run CLI until configured
MG_INFO(("WiFi is not configured, running CLI. Press enter"));
@@ -23,10 +23,10 @@ static void cb(struct mg_connection *c, int ev, void *ev_data) {
static void cb2(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_CONNECT) {
struct mg_str s = mg_url_host(CLIENT_URL);
mg_printf(c, "GET / HTTP/1.0\r\nHost: %.*s\r\n\r\n", (int) s.len, s.ptr);
mg_printf(c, "GET / HTTP/1.0\r\nHost: %.*s\r\n\r\n", (int) s.len, s.buf);
} else if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = ev_data; // Print HTTP response
MG_INFO(("Fetched:\n%.*s", (int) hm->message.len, hm->message.ptr));
MG_INFO(("Fetched:\n%.*s", (int) hm->message.len, hm->message.buf));
c->is_draining = 1;
}
}
+2 -2
View File
@@ -38,7 +38,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
"Host: %.*s\r\n"
"Content-Type: octet-stream\r\n"
"Content-Length: %d\r\n",
mg_url_uri(s_url), (int) host.len, host.ptr, fsize);
mg_url_uri(s_url), (int) host.len, host.buf, fsize);
mg_http_bauth(c, s_user, s_pass); // Add Basic auth header
mg_printf(c, "%s", "\r\n"); // End HTTP headers
} else if (ev == MG_EV_WRITE && c->send.len < MG_IO_SIZE) {
@@ -53,7 +53,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
MG_DEBUG(("MSG"));
// Response is received. Print it
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
printf("%.*s", (int) hm->body.len, hm->body.ptr);
printf("%.*s", (int) hm->body.len, hm->body.buf);
c->is_draining = 1; // Tell mongoose to close this connection
mg_fs_close(fd);
*(bool *) c->fn_data = true; // Tell event loop to stop
+1 -1
View File
@@ -58,7 +58,7 @@ static void handle_uploads(struct mg_connection *c, int ev, void *ev_data) {
} else {
char fpath[MG_PATH_MAX];
snprintf(fpath, MG_PATH_MAX, "%s%c", s_upld_dir, MG_DIRSEP);
strncat(fpath, hm->uri.ptr + 8, hm->uri.len - 8);
strncat(fpath, hm->uri.buf + 8, hm->uri.len - 8);
if (!mg_path_is_sane(fpath)) {
mg_http_reply(c, 400, "", "Invalid path\n");
c->is_draining = 1; // Tell mongoose to close this connection
+3 -3
View File
@@ -20,14 +20,14 @@ static void cb(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
MG_INFO(("New request to: [%.*s], body size: %lu", (int) hm->uri.len,
hm->uri.ptr, (unsigned long) hm->body.len));
hm->uri.buf, (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) {
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));
(int) part.name.len, part.name.buf, (int) part.filename.len,
part.filename.buf, (unsigned long) part.body.len));
}
mg_http_reply(c, 200, "", "Thank you!");
} else {
+1 -1
View File
@@ -28,7 +28,7 @@ static void handle_uploads(struct mg_connection *c, int ev, void *ev_data) {
if (mg_match(hm->uri, mg_str("/upload/*"), NULL)) {
char path[MG_PATH_MAX];
mg_snprintf(path, sizeof(path), "%s/%.*s", UPLOAD_DIR, hm->uri.len - 8,
hm->uri.ptr + 8);
hm->uri.buf + 8);
us->expected = hm->body.len; // Store number of bytes we expect
mg_iobuf_del(&c->recv, 0, hm->head.len); // Delete HTTP headers
c->pfn = NULL; // Silence HTTP protocol handler, we'll use MG_EV_READ
+2 -2
View File
@@ -43,12 +43,12 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
"Content-Length: %d\r\n"
"\r\n",
s_post_data ? "POST" : "GET", mg_url_uri(s_url), (int) host.len,
host.ptr, content_length);
host.buf, content_length);
mg_send(c, s_post_data, content_length);
} else if (ev == MG_EV_HTTP_MSG) {
// Response is received. Print it
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
printf("%.*s", (int) hm->message.len, hm->message.ptr);
printf("%.*s", (int) hm->message.len, hm->message.buf);
c->is_draining = 1; // Tell mongoose to close this connection
*(bool *) c->fn_data = true; // Tell event loop to stop
} else if (ev == MG_EV_ERROR) {
+5 -5
View File
@@ -15,7 +15,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
static bool connected;
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
printf("%.*s", (int) hm->message.len, hm->message.ptr);
printf("%.*s", (int) hm->message.len, hm->message.buf);
exit(EXIT_SUCCESS);
} else if (ev == MG_EV_CONNECT) {
// Proxy TCP connection established. Send CONNECT request
@@ -29,8 +29,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
// c->is_hexdumping = 1;
mg_printf(c, "CONNECT %.*s:%hu HTTP/1.1\r\nHost: %.*s:%hu\r\n\r\n",
(int) host.len, host.ptr, mg_url_port(url), (int) host.len,
host.ptr, mg_url_port(url));
(int) host.len, host.buf, mg_url_port(url), (int) host.len,
host.buf, mg_url_port(url));
} else if (!connected && ev == MG_EV_READ) {
struct mg_http_message hm;
int n = mg_http_parse((char *) c->recv.buf, c->recv.len, &hm);
@@ -39,14 +39,14 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
// CONNECT response - tunnel is established
connected = true;
MG_DEBUG(
("Connected to proxy, status: %.*s", (int) hm.uri.len, hm.uri.ptr));
("Connected to proxy, status: %.*s", (int) hm.uri.len, hm.uri.buf));
mg_iobuf_del(&c->recv, 0, n);
// Send request to the target server
mg_printf(c,
"GET %s HTTP/1.0\r\n"
"Host: %.*s\r\n"
"\r\n",
mg_url_uri(url), (int) host.len, host.ptr);
mg_url_uri(url), (int) host.len, host.buf);
}
}
}
+1 -1
View File
@@ -83,7 +83,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
mg_http_printf_chunk(c, ""); // Don't forget the last empty chunk
} else if (mg_http_match_uri(hm, "/api/f2/*")) {
mg_http_reply(c, 200, "", "{\"result\": \"%.*s\"}\n", (int) hm->uri.len,
hm->uri.ptr);
hm->uri.buf);
} else {
struct mg_http_serve_opts opts = {.root_dir = s_root_dir};
mg_http_serve_dir(c, ev_data, &opts);
+6 -6
View File
@@ -23,17 +23,17 @@ static void forward_request(struct mg_http_message *hm,
size_t i, max = sizeof(hm->headers) / sizeof(hm->headers[0]);
struct mg_str host = mg_url_host(s_backend_url);
mg_printf(c, "%.*s\r\n",
(int) (hm->proto.ptr + hm->proto.len - hm->message.ptr),
hm->message.ptr);
(int) (hm->proto.buf + hm->proto.len - hm->message.buf),
hm->message.buf);
for (i = 0; i < max && hm->headers[i].name.len > 0; i++) {
struct mg_str *k = &hm->headers[i].name, *v = &hm->headers[i].value;
if (mg_strcmp(*k, mg_str("Host")) == 0) v = &host;
mg_printf(c, "%.*s: %.*s\r\n", (int) k->len, k->ptr, (int) v->len, v->ptr);
mg_printf(c, "%.*s: %.*s\r\n", (int) k->len, k->buf, (int) v->len, v->buf);
}
mg_send(c, "\r\n", 2);
mg_send(c, hm->body.ptr, hm->body.len);
MG_DEBUG(("FORWARDING: %.*s %.*s", (int) hm->method.len, hm->method.ptr,
(int) hm->uri.len, hm->uri.ptr));
mg_send(c, hm->body.buf, hm->body.len);
MG_DEBUG(("FORWARDING: %.*s %.*s", (int) hm->method.len, hm->method.buf,
(int) hm->uri.len, hm->uri.buf));
}
static void fn2(struct mg_connection *c, int ev, void *ev_data) {
+6 -6
View File
@@ -33,12 +33,12 @@ static void cb(struct mg_connection *c, int ev, void *ev_data) {
while ((pos = mg_http_next_multipart(hm->body, pos, &part)) > 0) {
char path[MG_PATH_MAX];
MG_INFO(("Chunk name: [%.*s] filename: [%.*s] length: %lu bytes",
part.name.len, part.name.ptr, part.filename.len,
part.filename.ptr, part.body.len));
part.name.len, part.name.buf, part.filename.len,
part.filename.buf, part.body.len));
mg_snprintf(path, sizeof(path), "%s/%.*s", s_upload_dir,
part.filename.len, part.filename.ptr);
part.filename.len, part.filename.buf);
if (mg_path_is_sane(path)) {
mg_file_write(&mg_fs_posix, path, part.body.ptr, part.body.len);
mg_file_write(&mg_fs_posix, path, part.body.buf, part.body.len);
total_bytes += part.body.len;
num_files++;
} else {
@@ -57,8 +57,8 @@ static void cb(struct mg_connection *c, int ev, void *ev_data) {
}
// Log request
MG_INFO(("%.*s %.*s %lu -> %.*s %lu", hm->method.len, hm->method.ptr,
hm->uri.len, hm->uri.ptr, hm->body.len, 3, c->send.buf + 9,
MG_INFO(("%.*s %.*s %lu -> %.*s %lu", hm->method.len, hm->method.buf,
hm->uri.len, hm->uri.buf, hm->body.len, 3, c->send.buf + 9,
c->send.len));
}
}
+1 -1
View File
@@ -30,7 +30,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
"Connection: close\r\n"
"Host: %.*s\r\n"
"\r\n",
mg_url_uri(s_url), (int) host.len, host.ptr);
mg_url_uri(s_url), (int) host.len, host.buf);
} else if (ev == MG_EV_READ) {
// c->data[0] holds a flag, whether we have parsed the request already
if (c->data[0] == 0) {
+1 -1
View File
@@ -48,7 +48,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
const char *headers = "content-type: text/json\r\n";
long start = getparam(hm, "$.start");
long version = getparam(hm, "$.version");
MG_DEBUG(("%.*s", (int) hm->body.len, hm->body.ptr));
MG_DEBUG(("%.*s", (int) hm->body.len, hm->body.buf));
if (version > 0 && version != s_version) {
// Version mismatch: s_data has changed while client fetches it
// Tell client to restart
@@ -48,20 +48,20 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
sub_opts.topic = subt;
sub_opts.qos = s_qos;
mg_mqtt_sub(c, &sub_opts);
MG_INFO(("%lu SUBSCRIBED to %.*s", c->id, (int) subt.len, subt.ptr));
MG_INFO(("%lu SUBSCRIBED to %.*s", c->id, (int) subt.len, subt.buf));
struct mg_mqtt_opts pub_opts;
memset(&pub_opts, 0, sizeof(pub_opts));
pub_opts.topic = pubt;
pub_opts.message = data;
pub_opts.qos = s_qos, pub_opts.retain = false;
mg_mqtt_pub(c, &pub_opts);
MG_INFO(("%lu PUBLISHED %.*s -> %.*s", c->id, (int) data.len, data.ptr,
(int) pubt.len, pubt.ptr));
MG_INFO(("%lu PUBLISHED %.*s -> %.*s", c->id, (int) data.len, data.buf,
(int) pubt.len, pubt.buf));
} 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;
MG_INFO(("%lu RECEIVED %.*s <- %.*s", c->id, (int) mm->data.len,
mm->data.ptr, (int) mm->topic.len, mm->topic.ptr));
mm->data.buf, (int) mm->topic.len, mm->topic.buf));
} else if (ev == MG_EV_CLOSE) {
MG_INFO(("%lu CLOSED", c->id));
s_conn = NULL; // Mark that we're closed
+2 -2
View File
@@ -46,7 +46,7 @@ static void fn2(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
MG_DEBUG(("Got response (%d) %.*s...", (int) hm->message.len, 12,
hm->message.ptr));
hm->message.buf));
c->is_draining = 1;
} else if (ev == MG_EV_CONNECT) {
mg_printf(c, "GET %s HTTP/1.1\r\n\r\n", mg_url_uri((char *) c->fn_data));
@@ -72,7 +72,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
}
} else {
mg_http_reply(c, 200, NULL, "%.*s\r\n", (int) hm->message.len,
hm->message.ptr);
hm->message.buf);
}
}
(void) ev_data;
+3 -3
View File
@@ -15,14 +15,14 @@ static void signal_handler(int sig_num) {
bool web_load_settings(void *buf, size_t len) {
bool ok = false;
struct mg_str data = mg_file_read(&mg_fs_posix, CONFIG_FILE);
if (data.ptr == NULL) {
if (data.buf == NULL) {
MG_ERROR(("Error reading %s", CONFIG_FILE));
} else if (data.len != len) {
MG_ERROR(("%s size != %lu", CONFIG_FILE, len));
} else {
memcpy(buf, data.ptr, len);
memcpy(buf, data.buf, len);
}
free((void *) data.ptr);
free((void *) data.buf);
return ok;
}
+3 -3
View File
@@ -138,7 +138,7 @@ static struct mg_connection *start_modbus_request(struct mg_mgr *mgr,
uint16_t reg = (uint16_t) mg_json_get_long(json, "$.reg", 1);
uint8_t func = (uint8_t) mg_json_get_long(json, "$.func", 0);
uint16_t nregs = (uint16_t) mg_json_get_long(json, "$.nregs", 1);
MG_INFO(("%lu REQUEST: %.*s", cid, json.len, json.ptr));
MG_INFO(("%lu REQUEST: %.*s", cid, json.len, json.buf));
if (func == 0) {
MG_ERROR(("Set func to a valid modbus function code"));
} else if ((c = mg_connect(mgr, url, mfn, NULL)) == NULL) {
@@ -275,8 +275,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
#endif
mg_http_serve_dir(c, ev_data, &opts);
}
MG_DEBUG(("%lu %.*s %.*s", c->id, (int) hm->method.len, hm->method.ptr,
(int) hm->uri.len, hm->uri.ptr));
MG_DEBUG(("%lu %.*s %.*s", c->id, (int) hm->method.len, hm->method.buf,
(int) hm->uri.len, hm->uri.buf));
} else if (ev == MG_EV_POLL) {
if (cd->expiration_time > 0 && cd->expiration_time < mg_millis()) {
cd->expiration_time = 0;
+2 -2
View File
@@ -64,8 +64,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
} 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;
MG_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.buf,
(int) mm->data.len, mm->data.buf));
} else if (ev == MG_EV_POLL && c->data[0] == 'X') {
static unsigned long prev_second;
unsigned long now_second = (*(unsigned long *) ev_data) / 1000;
+4 -4
View File
@@ -47,20 +47,20 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
sub_opts.topic = subt;
sub_opts.qos = s_qos;
mg_mqtt_sub(c, &sub_opts);
MG_INFO(("%lu SUBSCRIBED to %.*s", c->id, (int) subt.len, subt.ptr));
MG_INFO(("%lu SUBSCRIBED to %.*s", c->id, (int) subt.len, subt.buf));
struct mg_mqtt_opts pub_opts;
memset(&pub_opts, 0, sizeof(pub_opts));
pub_opts.topic = pubt;
pub_opts.message = data;
pub_opts.qos = s_qos, pub_opts.retain = false;
mg_mqtt_pub(c, &pub_opts);
MG_INFO(("%lu PUBLISHED %.*s -> %.*s", c->id, (int) data.len, data.ptr,
(int) pubt.len, pubt.ptr));
MG_INFO(("%lu PUBLISHED %.*s -> %.*s", c->id, (int) data.len, data.buf,
(int) pubt.len, pubt.buf));
} 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;
MG_INFO(("%lu RECEIVED %.*s <- %.*s", c->id, (int) mm->data.len,
mm->data.ptr, (int) mm->topic.len, mm->topic.ptr));
mm->data.buf, (int) mm->topic.len, mm->topic.buf));
} else if (ev == MG_EV_CLOSE) {
MG_INFO(("%lu CLOSED", c->id));
s_conn = NULL; // Mark that we're closed
+7 -7
View File
@@ -41,9 +41,9 @@ static void set_device_id(void) {
}
#elif defined(__linux__)
struct mg_str id = mg_file_read(&mg_fs_posix, "/etc/machine-id");
if (id.ptr != NULL) {
mg_snprintf(buf, sizeof(buf), "%s", id.ptr);
free((void *) id.ptr);
if (id.buf != NULL) {
mg_snprintf(buf, sizeof(buf), "%s", id.buf);
free((void *) id.buf);
}
#endif
@@ -128,7 +128,7 @@ static void subscribe(struct mg_connection *c) {
sub_opts.topic = subt;
sub_opts.qos = s_qos;
mg_mqtt_sub(c, &sub_opts);
MG_INFO(("%lu SUBSCRIBED to %.*s", c->id, (int) subt.len, subt.ptr));
MG_INFO(("%lu SUBSCRIBED to %.*s", c->id, (int) subt.len, subt.buf));
free(rx_topic);
}
@@ -228,10 +228,10 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
struct mg_iobuf io = {0, 0, 0, 512};
struct mg_rpc_req r = {&s_rpc, NULL, mg_pfn_iobuf,
&io, NULL, {mm->data.ptr, mm->data.len}};
&io, NULL, {mm->data.buf, mm->data.len}};
size_t clipped_len = mm->data.len > 512 ? 512 : mm->data.len;
MG_INFO(("%lu RECEIVED %.*s <- %.*s", c->id, clipped_len, mm->data.ptr,
mm->topic.len, mm->topic.ptr));
MG_INFO(("%lu RECEIVED %.*s <- %.*s", c->id, clipped_len, mm->data.buf,
mm->topic.len, mm->topic.buf));
mg_rpc_process(&r);
if (io.buf) {
publish_response(c, (char *) io.buf, io.len);
+8 -8
View File
@@ -44,7 +44,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
uint8_t version = c->is_mqtt5 ? 5 : 4;
MG_INFO(("GOT %d bytes WS msg", (int) wm->data.len));
while ((mg_mqtt_parse((uint8_t *) wm->data.ptr, wm->data.len, version,
while ((mg_mqtt_parse((uint8_t *) wm->data.buf, wm->data.len, version,
&mm)) == MQTT_OK) {
switch (mm.cmd) {
case MQTT_CMD_CONNACK:
@@ -59,15 +59,15 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
sub_opts.qos = 1;
mg_mqtt_sub(c, &sub_opts);
len = mg_ws_wrap(c, c->send.len - len, WEBSOCKET_OP_BINARY);
MG_INFO(("SUBSCRIBED to %.*s", (int) topic.len, topic.ptr));
MG_INFO(("SUBSCRIBED to %.*s", (int) topic.len, topic.buf));
struct mg_mqtt_opts pub_opts;
memset(&pub_opts, 0, sizeof(pub_opts));
pub_opts.topic = topic;
pub_opts.message = data;
pub_opts.qos = 1, pub_opts.retain = false;
mg_mqtt_pub(c, &pub_opts);
MG_INFO(("PUBLISHED %.*s -> %.*s", (int) data.len, data.ptr,
(int) topic.len, topic.ptr));
MG_INFO(("PUBLISHED %.*s -> %.*s", (int) data.len, data.buf,
(int) topic.len, topic.buf));
len = mg_ws_wrap(c, c->send.len - len, WEBSOCKET_OP_BINARY);
} else {
MG_ERROR(("%lu MQTT auth failed, code %d", c->id, mm.ack));
@@ -76,14 +76,14 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
break;
case MQTT_CMD_PUBLISH: {
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));
mm.topic.buf, (int) mm.data.len, mm.data.buf));
MG_INFO(("RECEIVED %.*s <- %.*s", (int) mm.data.len, mm.data.buf,
(int) mm.topic.len, mm.topic.buf));
c->is_draining = 1;
break;
}
}
wm->data.ptr += mm.dgram.len;
wm->data.buf += mm.dgram.len;
wm->data.len -= mm.dgram.len;
}
}
+8 -8
View File
@@ -29,12 +29,12 @@ static void signal_handler(int signo) {
static size_t mg_mqtt_next_topic(struct mg_mqtt_message *msg,
struct mg_str *topic, uint8_t *qos,
size_t pos) {
unsigned char *buf = (unsigned char *) msg->dgram.ptr + pos;
unsigned char *buf = (unsigned char *) msg->dgram.buf + pos;
size_t new_pos;
if (pos >= msg->dgram.len) return 0;
topic->len = (size_t) (((unsigned) buf[0]) << 8 | buf[1]);
topic->ptr = (char *) buf + 2;
topic->buf = (char *) buf + 2;
new_pos = pos + 2 + topic->len + (qos == NULL ? 0 : 1);
if ((size_t) new_pos > msg->dgram.len) return 0;
if (qos != NULL) *qos = buf[2 + topic->len];
@@ -62,8 +62,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
// Client connects
if (mm->dgram.len < 9) {
mg_error(c, "Malformed MQTT frame");
} else if (mm->dgram.ptr[8] != 4) {
mg_error(c, "Unsupported MQTT version %d", mm->dgram.ptr[8]);
} else if (mm->dgram.buf[8] != 4) {
mg_error(c, "Unsupported MQTT version %d", mm->dgram.buf[8]);
} else {
uint8_t response[] = {0, 0};
mg_mqtt_send_header(c, MQTT_CMD_CONNACK, 0, sizeof(response));
@@ -84,10 +84,10 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
sub->qos = qos;
LIST_ADD_HEAD(struct sub, &s_subs, sub);
MG_INFO(
("SUB %p [%.*s]", c->fd, (int) sub->topic.len, sub->topic.ptr));
("SUB %p [%.*s]", c->fd, (int) sub->topic.len, sub->topic.buf));
// Change '+' to '*' for topic matching using mg_match
for (size_t i = 0; i < sub->topic.len; i++) {
if (sub->topic.ptr[i] == '+') ((char *) sub->topic.ptr)[i] = '*';
if (sub->topic.buf[i] == '+') ((char *) sub->topic.buf)[i] = '*';
}
resp[num_topics++] = qos;
}
@@ -100,7 +100,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
case MQTT_CMD_PUBLISH: {
// Client published message. Push to all subscribed channels
MG_INFO(("PUB %p [%.*s] -> [%.*s]", c->fd, (int) mm->data.len,
mm->data.ptr, (int) mm->topic.len, mm->topic.ptr));
mm->data.buf, (int) mm->topic.len, mm->topic.buf));
for (struct sub *sub = s_subs; sub != NULL; sub = sub->next) {
if (mg_match(mm->topic, sub->topic, NULL)) {
struct mg_mqtt_opts pub_opts;
@@ -127,7 +127,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
for (struct sub *next, *sub = s_subs; sub != NULL; sub = next) {
next = sub->next;
if (c != sub->c) continue;
MG_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.buf));
LIST_DELETE(struct sub, &s_subs, sub);
}
}
+2 -2
View File
@@ -63,7 +63,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
} else if (ev == MG_EV_WS_MSG) {
// Got websocket frame. Received data is wm->data. Echo it back!
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
mg_ws_send(c, wm->data.buf, wm->data.len, WEBSOCKET_OP_TEXT);
mg_iobuf_del(&c->recv, 0, c->recv.len);
} else if (ev == MG_EV_WAKEUP) {
struct mg_str *data = (struct mg_str *) ev_data;
@@ -72,7 +72,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
for (struct mg_connection *wc = c->mgr->conns; wc != NULL; wc = wc->next) {
// Send only to marked connections
if (wc->data[0] == 'W')
mg_ws_send(wc, data->ptr, data->len, WEBSOCKET_OP_TEXT);
mg_ws_send(wc, data->buf, data->len, WEBSOCKET_OP_TEXT);
}
}
}
+2 -2
View File
@@ -33,7 +33,7 @@ static void *thread_function(void *param) {
struct thread_data *p = (struct thread_data *) param;
sleep(2); // Simulate long execution
mg_wakeup(p->mgr, p->conn_id, "hi!", 3); // Respond to parent
free((void *) p->message.ptr); // Free all resources that were
free((void *) p->message.buf); // Free all resources that were
free(p); // passed to us
return NULL;
}
@@ -56,7 +56,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
}
} else if (ev == MG_EV_WAKEUP) {
struct mg_str *data = (struct mg_str *) ev_data;
mg_http_reply(c, 200, "", "Result: %.*s\n", data->len, data->ptr);
mg_http_reply(c, 200, "", "Result: %.*s\n", data->len, data->buf);
}
}
+1 -1
View File
@@ -25,7 +25,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
} else if (ev == MG_EV_WS_MSG) {
// Got websocket frame. Received data is wm->data. Echo it back!
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
mg_ws_send(c, wm->data.buf, wm->data.len, WEBSOCKET_OP_TEXT);
mg_iobuf_del(&c->recv, 0, c->recv.len);
}
}
+8 -8
View File
@@ -59,7 +59,7 @@ struct mg_str config_read(void) {
}
void config_write(struct mg_str config) {
mg_file_write(&mg_fs_posix, "config.json", config.ptr, config.len);
mg_file_write(&mg_fs_posix, "config.json", config.buf, config.len);
}
#endif
@@ -72,7 +72,7 @@ static void ws_fn(struct mg_connection *c, int ev, void *ev_data) {
c->data[0] = 'W'; // When WS handhake is done, mark us as WS client
} else if (ev == MG_EV_WS_MSG) {
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
uart_write(wm->data.ptr, wm->data.len); // Send to UART
uart_write(wm->data.buf, wm->data.len); // Send to UART
c->recv.len = 0; // Discard received data
} else if (ev == MG_EV_CLOSE) {
if (c->is_listening) s_state.websocket.c = NULL;
@@ -97,7 +97,7 @@ static void tcp_fn(struct mg_connection *c, int ev, void *ev_data) {
static struct mg_str mqtt_topic(const char *name, const char *dflt) {
struct mg_str qs = mg_str(strchr(s_state.mqtt.url, '?'));
struct mg_str v = mg_http_var(qs, mg_str(name));
return v.ptr == NULL ? mg_str(dflt) : v;
return v.buf == NULL ? mg_str(dflt) : v;
}
// Event handler for MQTT connection
@@ -113,7 +113,7 @@ static void mq_fn(struct mg_connection *c, int ev, void *ev_data) {
mg_mqtt_sub(c, &sub_opts); // Subscribe to RX topic
} else if (ev == MG_EV_MQTT_MSG) {
struct mg_mqtt_message *mm = ev_data; // MQTT message
uart_write(mm->data.ptr, mm->data.len); // Send to UART
uart_write(mm->data.buf, mm->data.len); // Send to UART
} else if (ev == MG_EV_CLOSE) {
s_state.mqtt.c = NULL;
}
@@ -163,7 +163,7 @@ static void update_string(struct mg_str json, const char *path, char **value) {
}
static void config_apply(struct mg_str s) {
MG_INFO(("Applying config: %.*s", (int) s.len, s.ptr));
MG_INFO(("Applying config: %.*s", (int) s.len, s.buf));
bool b;
if (mg_json_get_bool(s, "$.tcp.enable", &b)) s_state.tcp.enable = b;
@@ -187,9 +187,9 @@ static void config_apply(struct mg_str s) {
// HTTP request handler function
void uart_bridge_fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN && c->is_listening) {
struct mg_str config = config_read();
if (config.ptr != NULL) config_apply(config);
free((char *) config.ptr);
struct mg_str config = mg_file_read(&mg_fs_posix, "config.json");
if (config.buf != NULL) config_apply(config);
free(config.buf);
s_state.tcp.url = strdup(DEFAULT_TCP);
s_state.websocket.url = strdup(DEFAULT_WEBSOCKET);
s_state.mqtt.url = strdup(DEFAULT_MQTT);
+2 -2
View File
@@ -22,8 +22,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
struct mg_str *k = &hm.headers[i].name, *v = &hm.headers[i].value;
if ((mg_vcasecmp(k, "SERVER") == 0) ||
(mg_vcasecmp(k, "LOCATION") == 0)) {
printf("\t%.*s -> %.*s\n", (int) k->len, k->ptr, (int) v->len,
v->ptr);
printf("\t%.*s -> %.*s\n", (int) k->len, k->buf, (int) v->len,
v->buf);
}
}
printf("\n");
+3 -3
View File
@@ -36,15 +36,15 @@ static void broadcast_mjpeg_frame(struct mg_mgr *mgr) {
struct mg_connection *c;
for (c = mgr->conns; c != NULL; c = c->next) {
if (c->data[0] != 'S') continue; // Skip non-stream connections
if (data.ptr == NULL) continue; // Skip on file read error
if (data.buf == NULL) continue; // Skip on file read error
mg_printf(c,
"--foo\r\nContent-Type: image/jpeg\r\n"
"Content-Length: %lu\r\n\r\n",
data.len);
mg_send(c, data.ptr, data.len);
mg_send(c, data.buf, data.len);
mg_send(c, "\r\n", 2);
}
free((void *) data.ptr);
free((void *) data.buf);
}
static void timer_callback(void *arg) {
+1 -1
View File
@@ -22,7 +22,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
} else if (ev == MG_EV_WS_MSG) {
// When we get echo response, print it
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
printf("GOT ECHO REPLY: [%.*s]\n", (int) wm->data.len, wm->data.ptr);
printf("GOT ECHO REPLY: [%.*s]\n", (int) wm->data.len, wm->data.buf);
}
if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE || ev == MG_EV_WS_MSG) {
+1 -1
View File
@@ -32,7 +32,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
} else if (ev == MG_EV_WS_MSG) {
// Got websocket frame. Received data is wm->data. Echo it back!
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
mg_ws_send(c, wm->data.buf, wm->data.len, WEBSOCKET_OP_TEXT);
}
}
+1 -1
View File
@@ -312,7 +312,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
mg_http_serve_dir(c, ev_data, &opts);
}
MG_DEBUG(("%lu %.*s %.*s -> %.*s", c->id, (int) hm->method.len,
hm->method.ptr, (int) hm->uri.len, hm->uri.ptr, (int) 3,
hm->method.buf, (int) hm->uri.len, hm->uri.buf, (int) 3,
&c->send.buf[9]));
}
}
+2 -2
View File
@@ -42,12 +42,12 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
"Content-Length: %d\r\n"
"\r\n",
s_post_data ? "POST" : "GET", mg_url_uri(s_url), (int) host.len,
host.ptr, content_length);
host.buf, content_length);
mg_send(c, s_post_data, content_length);
} else if (ev == MG_EV_HTTP_MSG) {
// Response is received. Print it
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
MG_INFO(("%.*s", (int) hm->message.len, hm->message.ptr));
MG_INFO(("%.*s", (int) hm->message.len, hm->message.buf));
c->is_draining = 1; // Tell mongoose to close this connection
*(bool *) c->fn_data = true; // Tell event loop to stop
} else if (ev == MG_EV_ERROR) {
+2 -2
View File
@@ -17,8 +17,8 @@ static void wcb(struct mg_connection *c, int ev, void *ev_data) {
mg_tls_init(c, &opts);
} else if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = ev_data;
MG_INFO(("%.*s %.*s %ld", (int) hm->method.len, hm->method.ptr,
(int) hm->uri.len, hm->uri.ptr, (long) hm->body.len));
MG_INFO(("%.*s %.*s %ld", (int) hm->method.len, hm->method.buf,
(int) hm->uri.len, hm->uri.buf, (long) hm->body.len));
if (mg_http_match_uri(hm, "/api/#")) { // REST API requests
// Print some statistics about currently established connections
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
+2 -2
View File
@@ -40,8 +40,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
} 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;
MG_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", mm->topic.len, mm->topic.buf,
mm->data.len, mm->data.buf));
} else if (ev == MG_EV_POLL && c->data[0] == 'X') {
static unsigned long prev_second;
unsigned long now_second = (*(unsigned long *) ev_data) / 1000;
+2 -2
View File
@@ -32,8 +32,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
} else if (ev == MG_EV_WS_MSG) {
// Got websocket frame. Received data is wm->data. Echo it back!
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
MG_INFO(("Got wm: %p, data: %p, %d = %*.s", wm, wm->data.ptr, wm->data.len, wm->data.len, wm->data.ptr));
mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
MG_INFO(("Got wm: %p, data: %p, %d = %*.s", wm, wm->data.buf, wm->data.len, wm->data.len, wm->data.buf));
mg_ws_send(c, wm->data.buf, wm->data.len, WEBSOCKET_OP_TEXT);
}
}