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

This commit is contained in:
Sergey Lyubka 2024-03-15 07:42:24 +00:00
parent e93d18d200
commit e0e599ffa1
61 changed files with 684 additions and 686 deletions

View File

@ -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());

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;

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]));
}
}

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"));

View File

@ -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;
}
}

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

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

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 {

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

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) {

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);
}
}
}

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);

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) {

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));
}
}

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) {

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

View File

@ -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

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;

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;
}

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;

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;

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

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);

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;
}
}

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);
}
}

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);
}
}
}

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);
}
}

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);
}
}

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);

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");

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) {

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) {

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);
}
}

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]));
}
}

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) {

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");

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;

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);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -200,7 +200,7 @@ extern "C" {
#define calloc(a, b) mg_calloc(a, b)
#define free(a) vPortFree(a)
#define malloc(a) pvPortMalloc(a)
#define strdup(s) ((char *) mg_strdup(mg_str(s)).ptr)
#define strdup(s) ((char *) mg_strdup(mg_str(s)).buf)
// Re-route calloc/free to the FreeRTOS's functions, don't use stdlib
static inline void *mg_calloc(size_t cnt, size_t size) {
@ -302,7 +302,7 @@ extern uint32_t rt_time_get(void);
#include "cmsis_os2.h" // keep this include
#endif
#define strdup(s) ((char *) mg_strdup(mg_str(s)).ptr)
#define strdup(s) ((char *) mg_strdup(mg_str(s)).buf)
#if defined(__ARMCC_VERSION)
#define mode_t size_t
@ -536,7 +536,7 @@ typedef int socklen_t;
#define MG_PUTCHAR(x) printk("%c", x)
#ifndef strdup
#define strdup(s) ((char *) mg_strdup(mg_str(s)).ptr)
#define strdup(s) ((char *) mg_strdup(mg_str(s)).buf)
#endif
#define strerror(x) zsock_gai_strerror(x)
@ -861,14 +861,12 @@ struct timeval {
// Describes an arbitrary chunk of memory
struct mg_str {
const char *ptr; // Pointer to string data
size_t len; // String len
char *buf; // String data
size_t len; // String length
};
#define MG_C_STR(a) \
{ (a), sizeof(a) - 1 }
// Using macro to avoid shadowing C++ struct constructor, see #1298
#define mg_str(s) mg_str_s(s)

View File

@ -32,7 +32,7 @@
#define calloc(a, b) mg_calloc(a, b)
#define free(a) vPortFree(a)
#define malloc(a) pvPortMalloc(a)
#define strdup(s) ((char *) mg_strdup(mg_str(s)).ptr)
#define strdup(s) ((char *) mg_strdup(mg_str(s)).buf)
// Re-route calloc/free to the FreeRTOS's functions, don't use stdlib
static inline void *mg_calloc(size_t cnt, size_t size) {

View File

@ -22,7 +22,7 @@ extern uint32_t rt_time_get(void);
#include "cmsis_os2.h" // keep this include
#endif
#define strdup(s) ((char *) mg_strdup(mg_str(s)).ptr)
#define strdup(s) ((char *) mg_strdup(mg_str(s)).buf)
#if defined(__ARMCC_VERSION)
#define mode_t size_t

View File

@ -20,7 +20,7 @@
#define MG_PUTCHAR(x) printk("%c", x)
#ifndef strdup
#define strdup(s) ((char *) mg_strdup(mg_str(s)).ptr)
#define strdup(s) ((char *) mg_strdup(mg_str(s)).buf)
#endif
#define strerror(x) zsock_gai_strerror(x)

View File

@ -206,9 +206,9 @@ static bool mg_dns_send(struct mg_connection *c, const struct mg_str *name,
pkt.header.flags = mg_htons(0x100);
pkt.header.num_questions = mg_htons(1);
for (i = n = 0; i < sizeof(pkt.data) - 5; i++) {
if (name->ptr[i] == '.' || i >= name->len) {
if (name->buf[i] == '.' || i >= name->len) {
pkt.data[n] = (uint8_t) (i - n);
memcpy(&pkt.data[n + 1], name->ptr + n, i - n);
memcpy(&pkt.data[n + 1], name->buf + n, i - n);
n = i + 1;
}
if (i >= name->len) break;
@ -246,7 +246,7 @@ static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
d->c = c;
c->is_resolving = 1;
MG_VERBOSE(("%lu resolving %.*s @ %s, txnid %hu", c->id, (int) name->len,
name->ptr, dnsc->url, d->txnid));
name->buf, dnsc->url, d->txnid));
if (!mg_dns_send(dnsc->c, name, d->txnid, ipv6)) {
mg_error(dnsc->c, "DNS send");
}

View File

@ -27,15 +27,15 @@ struct mg_str mg_file_read(struct mg_fs *fs, const char *path) {
void *fp;
fs->st(path, &result.len, NULL);
if ((fp = fs->op(path, MG_FS_READ)) != NULL) {
result.ptr = (char *) calloc(1, result.len + 1);
if (result.ptr != NULL &&
fs->rd(fp, (void *) result.ptr, result.len) != result.len) {
free((void *) result.ptr);
result.ptr = NULL;
result.buf = (char *) calloc(1, result.len + 1);
if (result.buf != NULL &&
fs->rd(fp, (void *) result.buf, result.len) != result.len) {
free((void *) result.buf);
result.buf = NULL;
}
fs->cl(fp);
}
if (result.ptr == NULL) result.len = 0;
if (result.buf == NULL) result.len = 0;
return result;
}
@ -77,10 +77,10 @@ bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...) {
// ...
static void mg_fs_ls_fn(const char *filename, void *param) {
struct mg_str *s = (struct mg_str *) param;
if (s->ptr[0] == '\0') {
mg_snprintf((char *) s->ptr, s->len, "%s", filename);
} else if (strcmp(s->ptr, filename) == 0) {
((char *) s->ptr)[0] = '\0'; // Fetch next file
if (s->buf[0] == '\0') {
mg_snprintf((char *) s->buf, s->len, "%s", filename);
} else if (strcmp(s->buf, filename) == 0) {
((char *) s->buf)[0] = '\0'; // Fetch next file
}
}

View File

@ -14,17 +14,17 @@
bool mg_to_size_t(struct mg_str str, size_t *val);
bool mg_to_size_t(struct mg_str str, size_t *val) {
size_t i = 0, max = (size_t) -1, max2 = max / 10, result = 0, ndigits = 0;
while (i < str.len && (str.ptr[i] == ' ' || str.ptr[i] == '\t')) i++;
if (i < str.len && str.ptr[i] == '-') return false;
while (i < str.len && str.ptr[i] >= '0' && str.ptr[i] <= '9') {
size_t digit = (size_t) (str.ptr[i] - '0');
while (i < str.len && (str.buf[i] == ' ' || str.buf[i] == '\t')) i++;
if (i < str.len && str.buf[i] == '-') return false;
while (i < str.len && str.buf[i] >= '0' && str.buf[i] <= '9') {
size_t digit = (size_t) (str.buf[i] - '0');
if (result > max2) return false; // Overflow
result *= 10;
if (result > max - digit) return false; // Overflow
result += digit;
i++, ndigits++;
}
while (i < str.len && (str.ptr[i] == ' ' || str.ptr[i] == '\t')) i++;
while (i < str.len && (str.buf[i] == ' ' || str.buf[i] == '\t')) i++;
if (ndigits == 0) return false; // #2322: Content-Length = 1 * DIGIT
if (i != str.len) return false; // Ditto
*val = (size_t) result;
@ -49,7 +49,7 @@ bool mg_to_size_t(struct mg_str str, size_t *val) {
size_t mg_http_next_multipart(struct mg_str body, size_t ofs,
struct mg_http_part *part) {
struct mg_str cd = mg_str_n("Content-Disposition", 19);
const char *s = body.ptr;
const char *s = body.buf;
size_t b = ofs, h1, h2, b1, b2, max = body.len;
// Init part params
@ -68,7 +68,7 @@ size_t mg_http_next_multipart(struct mg_str body, size_t ofs,
if (h2 + 2 >= max) return 0;
// 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) {
mg_ncasecmp(&s[h1], cd.buf, cd.len) == 0) {
struct mg_str v = mg_str_n(&s[h1 + cd.len + 2], h2 - (h1 + cd.len + 2));
part->name = mg_http_get_header_var(v, mg_str_n("name", 4));
part->filename = mg_http_get_header_var(v, mg_str_n("filename", 8));
@ -96,12 +96,12 @@ void mg_http_bauth(struct mg_connection *c, const char *user,
char *buf = (char *) &c->send.buf[c->send.len];
memcpy(buf, "Authorization: Basic ", 21); // DON'T use mg_send!
for (i = 0; i < u.len; i++) {
n = mg_base64_update(((unsigned char *) u.ptr)[i], buf + 21, n);
n = mg_base64_update(((unsigned char *) u.buf)[i], buf + 21, n);
}
if (p.len > 0) {
n = mg_base64_update(':', buf + 21, n);
for (i = 0; i < p.len; i++) {
n = mg_base64_update(((unsigned char *) p.ptr)[i], buf + 21, n);
n = mg_base64_update(((unsigned char *) p.buf)[i], buf + 21, n);
}
}
n = mg_base64_final(buf + 21, n);
@ -116,7 +116,7 @@ struct mg_str mg_http_var(struct mg_str buf, struct mg_str name) {
struct mg_str entry, k, v, result = mg_str_n(NULL, 0);
while (mg_span(buf, &entry, &buf, '&')) {
if (mg_span(entry, &k, &v, '=') && name.len == k.len &&
mg_ncasecmp(name.ptr, k.ptr, k.len) == 0) {
mg_ncasecmp(name.buf, k.buf, k.len) == 0) {
result = v;
break;
}
@ -132,14 +132,14 @@ int mg_http_get_var(const struct mg_str *buf, const char *name, char *dst,
}
if (dst == NULL || dst_len == 0) {
len = -2; // Bad destination
} else if (buf->ptr == NULL || name == NULL || buf->len == 0) {
} else if (buf->buf == NULL || name == NULL || buf->len == 0) {
len = -1; // Bad source
} else {
struct mg_str v = mg_http_var(*buf, mg_str(name));
if (v.ptr == NULL) {
if (v.buf == NULL) {
len = -4; // Name does not exist
} else {
len = mg_url_decode(v.ptr, v.len, dst, dst_len, 1);
len = mg_url_decode(v.buf, v.len, dst, dst_len, 1);
if (len < 0) len = -3; // Failed to decode
}
}
@ -191,7 +191,7 @@ struct mg_str *mg_http_get_header(struct mg_http_message *h, const char *name) {
size_t i, n = strlen(name), max = sizeof(h->headers) / sizeof(h->headers[0]);
for (i = 0; i < max && h->headers[i].name.len > 0; i++) {
struct mg_str *k = &h->headers[i].name, *v = &h->headers[i].value;
if (n == k->len && mg_ncasecmp(k->ptr, name, n) == 0) return v;
if (n == k->len && mg_ncasecmp(k->buf, name, n) == 0) return v;
}
return NULL;
}
@ -215,7 +215,7 @@ static size_t clen(const char *s, const char *end) {
// Skip until the newline. Return advanced `s`, or NULL on error
static const char *skiptorn(const char *s, const char *end, struct mg_str *v) {
v->ptr = s;
v->buf = (char *) s;
while (s < end && s[0] != '\n' && s[0] != '\r') s++, v->len++; // To newline
if (s >= end || (s[0] == '\r' && s[1] != '\n')) return NULL; // Stray \r
if (s < end && s[0] == '\r') s++; // Skip \r
@ -230,7 +230,7 @@ static bool mg_http_parse_headers(const char *s, const char *end,
struct mg_str k = {NULL, 0}, v = {NULL, 0};
if (s >= end) return false;
if (s[0] == '\n' || (s[0] == '\r' && s[1] == '\n')) break;
k.ptr = s;
k.buf = (char *) s;
while (s < end && s[0] != ':' && (n = clen(s, end)) > 0) s += n, k.len += n;
if (k.len == 0) return false; // Empty name
if (s >= end || clen(s, end) == 0) return false; // Invalid UTF-8
@ -238,8 +238,8 @@ static bool mg_http_parse_headers(const char *s, const char *end,
// if (clen(s, end) == 0) return false; // Invalid UTF-8
while (s < end && s[0] == ' ') s++; // Skip spaces
if ((s = skiptorn(s, end, &v)) == NULL) return false;
while (v.len > 0 && v.ptr[v.len - 1] == ' ') v.len--; // Trim spaces
// MG_INFO(("--HH [%.*s] [%.*s]", (int) k.len, k.ptr, (int) v.len, v.ptr));
while (v.len > 0 && v.buf[v.len - 1] == ' ') v.len--; // Trim spaces
// MG_INFO(("--HH [%.*s] [%.*s]", (int) k.len, k.buf, (int) v.len, v.buf));
h[i].name = k, h[i].value = v; // Success. Assign values
}
return true;
@ -248,31 +248,31 @@ static bool mg_http_parse_headers(const char *s, const char *end,
int mg_http_parse(const char *s, size_t len, struct mg_http_message *hm) {
int is_response, req_len = mg_http_get_request_len((unsigned char *) s, len);
const char *end = s == NULL ? NULL : s + req_len, *qs; // Cannot add to NULL
struct mg_str *cl;
const struct mg_str *cl;
size_t n;
memset(hm, 0, sizeof(*hm));
if (req_len <= 0) return req_len;
hm->message.ptr = hm->head.ptr = s;
hm->body.ptr = end;
hm->message.buf = hm->head.buf = (char *) s;
hm->body.buf = (char *) end;
hm->head.len = (size_t) req_len;
hm->message.len = hm->body.len = (size_t) -1; // Set body length to infinite
// Parse request line
hm->method.ptr = s;
hm->method.buf = (char *) s;
while (s < end && (n = clen(s, end)) > 0) s += n, hm->method.len += n;
while (s < end && s[0] == ' ') s++; // Skip spaces
hm->uri.ptr = s;
hm->uri.buf = (char *) s;
while (s < end && (n = clen(s, end)) > 0) s += n, hm->uri.len += n;
while (s < end && s[0] == ' ') s++; // Skip spaces
if ((s = skiptorn(s, end, &hm->proto)) == NULL) return false;
// If URI contains '?' character, setup query string
if ((qs = (const char *) memchr(hm->uri.ptr, '?', hm->uri.len)) != NULL) {
hm->query.ptr = qs + 1;
hm->query.len = (size_t) (&hm->uri.ptr[hm->uri.len] - (qs + 1));
hm->uri.len = (size_t) (qs - hm->uri.ptr);
if ((qs = (const char *) memchr(hm->uri.buf, '?', hm->uri.len)) != NULL) {
hm->query.buf = (char *) qs + 1;
hm->query.len = (size_t) (&hm->uri.buf[hm->uri.len] - (qs + 1));
hm->uri.len = (size_t) (qs - hm->uri.buf);
}
// Sanity check. Allow protocol/reason to be empty
@ -299,7 +299,7 @@ int mg_http_parse(const char *s, size_t len, struct mg_http_message *hm) {
//
// So, if it is HTTP request, and Content-Length is not set,
// and method is not (PUT or POST) then reset body length to zero.
is_response = mg_ncasecmp(hm->method.ptr, "HTTP/", 5) == 0;
is_response = mg_ncasecmp(hm->method.buf, "HTTP/", 5) == 0;
if (hm->body.len == (size_t) ~0 && !is_response &&
mg_vcasecmp(&hm->method, "PUT") != 0 &&
mg_vcasecmp(&hm->method, "POST") != 0) {
@ -471,6 +471,7 @@ static void static_cb(struct mg_connection *c, int ev, void *ev_data) {
// Known mime types. Keep it outside guess_content_type() function, since
// some environments don't like it defined there.
// clang-format off
#define MG_C_STR(a) { (char *) (a), sizeof(a) - 1 }
static struct mg_str s_known_types[] = {
MG_C_STR("html"), MG_C_STR("text/html; charset=utf-8"),
MG_C_STR("htm"), MG_C_STR("text/html; charset=utf-8"),
@ -511,8 +512,8 @@ static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
size_t i = 0;
// Shrink path to its extension only
while (i < path.len && path.ptr[path.len - i - 1] != '.') i++;
path.ptr += path.len - i;
while (i < path.len && path.buf[path.len - i - 1] != '.') i++;
path.buf += path.len - i;
path.len = i;
// Process user-provided mime type overrides, if any
@ -521,7 +522,7 @@ static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
}
// Process built-in mime types
for (i = 0; s_known_types[i].ptr != NULL; i += 2) {
for (i = 0; s_known_types[i].buf != NULL; i += 2) {
if (mg_strcmp(path, s_known_types[i]) == 0) return s_known_types[i + 1];
}
@ -531,8 +532,8 @@ static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
static int getrange(struct mg_str *s, size_t *a, size_t *b) {
size_t i, numparsed = 0;
for (i = 0; i + 6 < s->len; i++) {
struct mg_str k, v = mg_str_n(s->ptr + i + 6, s->len - i - 6);
if (memcmp(&s->ptr[i], "bytes=", 6) != 0) continue;
struct mg_str k, v = mg_str_n(s->buf + i + 6, s->len - i - 6);
if (memcmp(&s->buf[i], "bytes=", 6) != 0) continue;
if (mg_span(v, &k, &v, '-')) {
if (mg_to_size_t(k, a)) numparsed++;
if (v.len > 0 && mg_to_size_t(v, b)) numparsed++;
@ -615,7 +616,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
"Etag: %s\r\n"
"Content-Length: %llu\r\n"
"%s%s%s\r\n",
status, mg_http_status_code_str(status), (int) mime.len, mime.ptr,
status, mg_http_status_code_str(status), (int) mime.len, mime.buf,
etag, (uint64_t) cl, gzip ? "Content-Encoding: gzip\r\n" : "",
range, opts->extra_headers ? opts->extra_headers : "");
if (mg_vcasecmp(&hm->method, "HEAD") == 0) {
@ -710,7 +711,7 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm,
struct printdirentrydata d = {c, hm, opts, dir};
char tmp[10], buf[MG_PATH_MAX];
size_t off, n;
int len = mg_url_decode(hm->uri.ptr, hm->uri.len, buf, sizeof(buf), 0);
int len = mg_url_decode(hm->uri.buf, hm->uri.len, buf, sizeof(buf), 0);
struct mg_str uri = len > 0 ? mg_str_n(buf, (size_t) len) : hm->uri;
mg_printf(c,
@ -731,8 +732,8 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm,
"<tr><td colspan=\"3\"><hr></td></tr>"
"</thead>"
"<tbody id=\"tb\">\n",
(int) uri.len, uri.ptr, sort_js_code, sort_js_code2, (int) uri.len,
uri.ptr);
(int) uri.len, uri.buf, sort_js_code, sort_js_code2, (int) uri.len,
uri.buf);
mg_printf(c, "%s",
" <tr><td><a href=\"..\">..</a></td>"
"<td name=-1></td><td name=-1>[DIR]</td></tr>\n");
@ -755,7 +756,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm,
char *path, size_t path_size) {
int flags, tmp;
// Append URI to the root_dir, and sanitize it
size_t n = mg_snprintf(path, path_size, "%.*s", (int) dir.len, dir.ptr);
size_t n = mg_snprintf(path, path_size, "%.*s", (int) dir.len, dir.buf);
if (n + 2 >= path_size) {
mg_http_reply(c, 400, "", "Exceeded path size");
return -1;
@ -764,7 +765,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm,
// Terminate root dir with slash
if (n > 0 && path[n - 1] != '/') path[n++] = '/', path[n] = '\0';
if (url.len < hm->uri.len) {
mg_url_decode(hm->uri.ptr + url.len, hm->uri.len - url.len, path + n,
mg_url_decode(hm->uri.buf + url.len, hm->uri.len - url.len, path + n,
path_size - n, 0);
}
path[path_size - 1] = '\0'; // Double-check
@ -775,18 +776,18 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm,
n = strlen(path);
while (n > 1 && path[n - 1] == '/') path[--n] = 0; // Trim trailing slashes
flags = mg_vcmp(&hm->uri, "/") == 0 ? MG_FS_DIR : fs->st(path, NULL, NULL);
MG_VERBOSE(("%lu %.*s -> %s %d", c->id, (int) hm->uri.len, hm->uri.ptr, path,
MG_VERBOSE(("%lu %.*s -> %s %d", c->id, (int) hm->uri.len, hm->uri.buf, path,
flags));
if (flags == 0) {
// Do nothing - let's caller decide
} else if ((flags & MG_FS_DIR) && hm->uri.len > 0 &&
hm->uri.ptr[hm->uri.len - 1] != '/') {
hm->uri.buf[hm->uri.len - 1] != '/') {
mg_printf(c,
"HTTP/1.1 301 Moved\r\n"
"Location: %.*s/\r\n"
"Content-Length: 0\r\n"
"\r\n",
(int) hm->uri.len, hm->uri.ptr);
(int) hm->uri.len, hm->uri.buf);
c->is_resp = 0;
flags = -1;
} else if (flags & MG_FS_DIR) {
@ -818,7 +819,7 @@ static int uri_to_path(struct mg_connection *c, struct mg_http_message *hm,
if (!mg_span(part, &k, &v, '=')) k = part, v = mg_str_n(NULL, 0);
if (v.len == 0) v = k, k = mg_str("/"), u = k, p = v;
if (hm->uri.len < k.len) continue;
if (mg_strcmp(k, mg_str_n(hm->uri.ptr, k.len)) != 0) continue;
if (mg_strcmp(k, mg_str_n(hm->uri.buf, k.len)) != 0) continue;
u = k, p = v;
}
return uri_to_path2(c, hm, fs, u, p, path, path_size);
@ -872,41 +873,41 @@ void mg_http_creds(struct mg_http_message *hm, char *user, size_t userlen,
char *pass, size_t passlen) {
struct mg_str *v = mg_http_get_header(hm, "Authorization");
user[0] = pass[0] = '\0';
if (v != NULL && v->len > 6 && memcmp(v->ptr, "Basic ", 6) == 0) {
if (v != NULL && v->len > 6 && memcmp(v->buf, "Basic ", 6) == 0) {
char buf[256];
size_t n = mg_base64_decode(v->ptr + 6, v->len - 6, buf, sizeof(buf));
size_t n = mg_base64_decode(v->buf + 6, v->len - 6, buf, sizeof(buf));
const char *p = (const char *) memchr(buf, ':', n > 0 ? n : 0);
if (p != NULL) {
mg_snprintf(user, userlen, "%.*s", p - buf, buf);
mg_snprintf(pass, passlen, "%.*s", n - (size_t) (p - buf) - 1, p + 1);
}
} else if (v != NULL && v->len > 7 && memcmp(v->ptr, "Bearer ", 7) == 0) {
mg_snprintf(pass, passlen, "%.*s", (int) v->len - 7, v->ptr + 7);
} else if (v != NULL && v->len > 7 && memcmp(v->buf, "Bearer ", 7) == 0) {
mg_snprintf(pass, passlen, "%.*s", (int) v->len - 7, v->buf + 7);
} else if ((v = mg_http_get_header(hm, "Cookie")) != NULL) {
struct mg_str t = mg_http_get_header_var(*v, mg_str_n("access_token", 12));
if (t.len > 0) mg_snprintf(pass, passlen, "%.*s", (int) t.len, t.ptr);
if (t.len > 0) mg_snprintf(pass, passlen, "%.*s", (int) t.len, t.buf);
} else {
mg_http_get_var(&hm->query, "access_token", pass, passlen);
}
}
static struct mg_str stripquotes(struct mg_str s) {
return s.len > 1 && s.ptr[0] == '"' && s.ptr[s.len - 1] == '"'
? mg_str_n(s.ptr + 1, s.len - 2)
return s.len > 1 && s.buf[0] == '"' && s.buf[s.len - 1] == '"'
? mg_str_n(s.buf + 1, s.len - 2)
: s;
}
struct mg_str mg_http_get_header_var(struct mg_str s, struct mg_str v) {
size_t i;
for (i = 0; v.len > 0 && i + v.len + 2 < s.len; i++) {
if (s.ptr[i + v.len] == '=' && memcmp(&s.ptr[i], v.ptr, v.len) == 0) {
const char *p = &s.ptr[i + v.len + 1], *b = p, *x = &s.ptr[s.len];
if (s.buf[i + v.len] == '=' && memcmp(&s.buf[i], v.buf, v.len) == 0) {
const char *p = &s.buf[i + v.len + 1], *b = p, *x = &s.buf[s.len];
int q = p < x && *p == '"' ? 1 : 0;
while (p < x &&
(q ? p == b || *p != '"' : *p != ';' && *p != ' ' && *p != ','))
p++;
// MG_INFO(("[%.*s] [%.*s] [%.*s]", (int) s.len, s.ptr, (int) v.len,
// v.ptr, (int) (p - b), b));
// MG_INFO(("[%.*s] [%.*s] [%.*s]", (int) s.len, s.buf, (int) v.len,
// v.buf, (int) (p - b), b));
return stripquotes(mg_str_n(b, (size_t) (p - b + q)));
}
}
@ -953,7 +954,7 @@ long mg_http_upload(struct mg_connection *c, struct mg_http_message *hm,
mg_http_reply(c, 400, "", "open(%s): %d", path, errno);
res = -6;
} else {
res = offset + (long) fs->wr(fd->fd, hm->body.ptr, hm->body.len);
res = offset + (long) fs->wr(fd->fd, hm->body.buf, hm->body.len);
mg_fs_close(fd);
mg_http_reply(c, 200, "", "%ld", res);
}
@ -962,7 +963,7 @@ long mg_http_upload(struct mg_connection *c, struct mg_http_message *hm,
}
int mg_http_status(const struct mg_http_message *hm) {
return atoi(hm->uri.ptr);
return atoi(hm->uri.buf);
}
static bool is_hex_digit(int c) {
@ -1007,7 +1008,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
mg_call(c, MG_EV_HTTP_HDRS, &hm); // Got all HTTP headers
if (ev == MG_EV_CLOSE) { // If client did not set Content-Length
hm.message.len = c->recv.len - ofs; // and closes now, deliver MSG
hm.body.len = hm.message.len - (size_t) (hm.body.ptr - hm.message.ptr);
hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf);
}
if ((te = mg_http_get_header(&hm, "Transfer-Encoding")) != NULL) {
if (mg_vcasecmp(te, "chunked") == 0) {
@ -1019,7 +1020,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
} else if (mg_http_get_header(&hm, "Content-length") == NULL) {
// #2593: HTTP packets must contain either Transfer-Encoding or
// Content-length
bool is_response = mg_ncasecmp(hm.method.ptr, "HTTP/", 5) == 0;
bool is_response = mg_ncasecmp(hm.method.buf, "HTTP/", 5) == 0;
bool require_content_len = false;
if (!is_response && (mg_vcasecmp(&hm.method, "POST") == 0 ||
mg_vcasecmp(&hm.method, "PUT") == 0)) {

View File

@ -79,52 +79,52 @@ size_t mg_json_next(struct mg_str obj, size_t ofs, struct mg_str *key,
struct mg_str *val) {
if (ofs >= obj.len) {
ofs = 0; // Out of boundaries, stop scanning
} else if (obj.len < 2 || (*obj.ptr != '{' && *obj.ptr != '[')) {
} else if (obj.len < 2 || (*obj.buf != '{' && *obj.buf != '[')) {
ofs = 0; // Not an array or object, stop
} else {
struct mg_str sub = mg_str_n(obj.ptr + ofs, obj.len - ofs);
if (ofs == 0) ofs++, sub.ptr++, sub.len--;
if (*obj.ptr == '[') { // Iterate over an array
struct mg_str sub = mg_str_n(obj.buf + ofs, obj.len - ofs);
if (ofs == 0) ofs++, sub.buf++, sub.len--;
if (*obj.buf == '[') { // Iterate over an array
int n = 0, o = mg_json_get(sub, "$", &n);
if (n < 0 || o < 0 || (size_t) (o + n) > sub.len) {
ofs = 0; // Error parsing key, stop scanning
} else {
if (key) *key = mg_str_n(NULL, 0);
if (val) *val = mg_str_n(sub.ptr + o, (size_t) n);
ofs = (size_t) (&sub.ptr[o + n] - obj.ptr);
if (val) *val = mg_str_n(sub.buf + o, (size_t) n);
ofs = (size_t) (&sub.buf[o + n] - obj.buf);
}
} else { // Iterate over an object
int n = 0, o = mg_json_get(sub, "$", &n);
if (n < 0 || o < 0 || (size_t) (o + n) > sub.len) {
ofs = 0; // Error parsing key, stop scanning
} else {
if (key) *key = mg_str_n(sub.ptr + o, (size_t) n);
sub.ptr += o + n, sub.len -= (size_t) (o + n);
while (sub.len > 0 && *sub.ptr != ':') sub.len--, sub.ptr++;
if (sub.len > 0 && *sub.ptr == ':') sub.len--, sub.ptr++;
if (key) *key = mg_str_n(sub.buf + o, (size_t) n);
sub.buf += o + n, sub.len -= (size_t) (o + n);
while (sub.len > 0 && *sub.buf != ':') sub.len--, sub.buf++;
if (sub.len > 0 && *sub.buf == ':') sub.len--, sub.buf++;
n = 0, o = mg_json_get(sub, "$", &n);
if (n < 0 || o < 0 || (size_t) (o + n) > sub.len) {
ofs = 0; // Error parsing value, stop scanning
} else {
if (val) *val = mg_str_n(sub.ptr + o, (size_t) n);
ofs = (size_t) (&sub.ptr[o + n] - obj.ptr);
if (val) *val = mg_str_n(sub.buf + o, (size_t) n);
ofs = (size_t) (&sub.buf[o + n] - obj.buf);
}
}
}
// MG_INFO(("SUB ofs %u %.*s", ofs, sub.len, sub.ptr));
// MG_INFO(("SUB ofs %u %.*s", ofs, sub.len, sub.buf));
while (ofs && ofs < obj.len &&
(obj.ptr[ofs] == ' ' || obj.ptr[ofs] == '\t' ||
obj.ptr[ofs] == '\n' || obj.ptr[ofs] == '\r')) {
(obj.buf[ofs] == ' ' || obj.buf[ofs] == '\t' ||
obj.buf[ofs] == '\n' || obj.buf[ofs] == '\r')) {
ofs++;
}
if (ofs && ofs < obj.len && obj.ptr[ofs] == ',') ofs++;
if (ofs && ofs < obj.len && obj.buf[ofs] == ',') ofs++;
if (ofs > obj.len) ofs = 0;
}
return ofs;
}
int mg_json_get(struct mg_str json, const char *path, int *toklen) {
const char *s = json.ptr;
const char *s = json.buf;
int len = (int) json.len;
enum { S_VALUE, S_KEY, S_COLON, S_COMMA_OR_EOO } expecting = S_VALUE;
unsigned char nesting[MG_JSON_MAX_DEPTH];
@ -268,15 +268,15 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
struct mg_str mg_json_get_tok(struct mg_str json, const char *path) {
int len = 0, ofs = mg_json_get(json, path, &len);
return mg_str_n(ofs < 0 ? NULL : json.ptr + ofs,
return mg_str_n(ofs < 0 ? NULL : json.buf + ofs,
(size_t) (len < 0 ? 0 : len));
}
bool mg_json_get_num(struct mg_str json, const char *path, double *v) {
int n, toklen, found = 0;
if ((n = mg_json_get(json, path, &toklen)) >= 0 &&
(json.ptr[n] == '-' || (json.ptr[n] >= '0' && json.ptr[n] <= '9'))) {
if (v != NULL) *v = mg_atod(json.ptr + n, toklen, NULL);
(json.buf[n] == '-' || (json.buf[n] >= '0' && json.buf[n] <= '9'))) {
if (v != NULL) *v = mg_atod(json.buf + n, toklen, NULL);
found = 1;
}
return found;
@ -284,8 +284,8 @@ bool mg_json_get_num(struct mg_str json, const char *path, double *v) {
bool mg_json_get_bool(struct mg_str json, const char *path, bool *v) {
int found = 0, off = mg_json_get(json, path, NULL);
if (off >= 0 && (json.ptr[off] == 't' || json.ptr[off] == 'f')) {
if (v != NULL) *v = json.ptr[off] == 't';
if (off >= 0 && (json.buf[off] == 't' || json.buf[off] == 'f')) {
if (v != NULL) *v = json.buf[off] == 't';
found = 1;
}
return found;
@ -294,21 +294,21 @@ bool mg_json_get_bool(struct mg_str json, const char *path, bool *v) {
bool mg_json_unescape(struct mg_str s, char *to, size_t n) {
size_t i, j;
for (i = 0, j = 0; i < s.len && j < n; i++, j++) {
if (s.ptr[i] == '\\' && i + 5 < s.len && s.ptr[i + 1] == 'u') {
if (s.buf[i] == '\\' && i + 5 < s.len && s.buf[i + 1] == 'u') {
// \uXXXX escape. We could process a simple one-byte chars
// \u00xx from the ASCII range. More complex chars would require
// dragging in a UTF8 library, which is too much for us
if (s.ptr[i + 2] != '0' || s.ptr[i + 3] != '0') return false; // Give up
((unsigned char *) to)[j] = (unsigned char) mg_unhexn(s.ptr + i + 4, 2);
if (s.buf[i + 2] != '0' || s.buf[i + 3] != '0') return false; // Give up
((unsigned char *) to)[j] = (unsigned char) mg_unhexn(s.buf + i + 4, 2);
i += 5;
} else if (s.ptr[i] == '\\' && i + 1 < s.len) {
char c = json_esc(s.ptr[i + 1], 0);
} else if (s.buf[i] == '\\' && i + 1 < s.len) {
char c = json_esc(s.buf[i + 1], 0);
if (c == 0) return false;
to[j] = c;
i++;
} else {
to[j] = s.ptr[i];
to[j] = s.buf[i];
}
}
if (j >= n) return false;
@ -319,9 +319,9 @@ bool mg_json_unescape(struct mg_str s, char *to, size_t n) {
char *mg_json_get_str(struct mg_str json, const char *path) {
char *result = NULL;
int len = 0, off = mg_json_get(json, path, &len);
if (off >= 0 && len > 1 && json.ptr[off] == '"') {
if (off >= 0 && len > 1 && json.buf[off] == '"') {
if ((result = (char *) calloc(1, (size_t) len)) != NULL &&
!mg_json_unescape(mg_str_n(json.ptr + off + 1, (size_t) (len - 2)),
!mg_json_unescape(mg_str_n(json.buf + off + 1, (size_t) (len - 2)),
result, (size_t) len)) {
free(result);
result = NULL;
@ -333,9 +333,9 @@ char *mg_json_get_str(struct mg_str json, const char *path) {
char *mg_json_get_b64(struct mg_str json, const char *path, int *slen) {
char *result = NULL;
int len = 0, off = mg_json_get(json, path, &len);
if (off >= 0 && json.ptr[off] == '"' && len > 1 &&
if (off >= 0 && json.buf[off] == '"' && len > 1 &&
(result = (char *) calloc(1, (size_t) len)) != NULL) {
size_t k = mg_base64_decode(json.ptr + off + 1, (size_t) (len - 2), result,
size_t k = mg_base64_decode(json.buf + off + 1, (size_t) (len - 2), result,
(size_t) len);
if (slen != NULL) *slen = (int) k;
}
@ -345,9 +345,9 @@ char *mg_json_get_b64(struct mg_str json, const char *path, int *slen) {
char *mg_json_get_hex(struct mg_str json, const char *path, int *slen) {
char *result = NULL;
int len = 0, off = mg_json_get(json, path, &len);
if (off >= 0 && json.ptr[off] == '"' && len > 1 &&
if (off >= 0 && json.buf[off] == '"' && len > 1 &&
(result = (char *) calloc(1, (size_t) len / 2)) != NULL) {
mg_unhex(json.ptr + off + 1, (size_t) (len - 2), (uint8_t *) result);
mg_unhex(json.buf + off + 1, (size_t) (len - 2), (uint8_t *) result);
result[len / 2 - 1] = '\0';
if (slen != NULL) *slen = len / 2 - 1;
}

View File

@ -170,9 +170,9 @@ static void mg_send_mqtt_properties(struct mg_connection *c,
switch (mqtt_prop_type_by_id(props[i].id)) {
case MQTT_PROP_TYPE_STRING_PAIR:
mg_send_u16(c, mg_htons((uint16_t) props[i].key.len));
mg_send(c, props[i].key.ptr, props[i].key.len);
mg_send(c, props[i].key.buf, props[i].key.len);
mg_send_u16(c, mg_htons((uint16_t) props[i].val.len));
mg_send(c, props[i].val.ptr, props[i].val.len);
mg_send(c, props[i].val.buf, props[i].val.len);
break;
case MQTT_PROP_TYPE_BYTE:
mg_send(c, &props[i].iv, sizeof(uint8_t));
@ -185,11 +185,11 @@ static void mg_send_mqtt_properties(struct mg_connection *c,
break;
case MQTT_PROP_TYPE_STRING:
mg_send_u16(c, mg_htons((uint16_t) props[i].val.len));
mg_send(c, props[i].val.ptr, props[i].val.len);
mg_send(c, props[i].val.buf, props[i].val.len);
break;
case MQTT_PROP_TYPE_BINARY_DATA:
mg_send_u16(c, mg_htons((uint16_t) props[i].val.len));
mg_send(c, props[i].val.ptr, props[i].val.len);
mg_send(c, props[i].val.buf, props[i].val.len);
break;
case MQTT_PROP_TYPE_VARIABLE_INT:
len = encode_varint(buf_v, props[i].iv);
@ -201,8 +201,8 @@ static void mg_send_mqtt_properties(struct mg_connection *c,
size_t mg_mqtt_next_prop(struct mg_mqtt_message *msg, struct mg_mqtt_prop *prop,
size_t ofs) {
uint8_t *i = (uint8_t *) msg->dgram.ptr + msg->props_start + ofs;
uint8_t *end = (uint8_t *) msg->dgram.ptr + msg->dgram.len;
uint8_t *i = (uint8_t *) msg->dgram.buf + msg->props_start + ofs;
uint8_t *end = (uint8_t *) msg->dgram.buf + msg->dgram.len;
size_t new_pos = ofs, len;
prop->id = i[0];
@ -213,10 +213,10 @@ size_t mg_mqtt_next_prop(struct mg_mqtt_message *msg, struct mg_mqtt_prop *prop,
switch (mqtt_prop_type_by_id(prop->id)) {
case MQTT_PROP_TYPE_STRING_PAIR:
prop->key.len = (uint16_t) ((((uint16_t) i[0]) << 8) | i[1]);
prop->key.ptr = (char *) i + 2;
prop->key.buf = (char *) i + 2;
i += 2 + prop->key.len;
prop->val.len = (uint16_t) ((((uint16_t) i[0]) << 8) | i[1]);
prop->val.ptr = (char *) i + 2;
prop->val.buf = (char *) i + 2;
new_pos += 2 * sizeof(uint16_t) + prop->val.len + prop->key.len;
break;
case MQTT_PROP_TYPE_BYTE:
@ -234,12 +234,12 @@ size_t mg_mqtt_next_prop(struct mg_mqtt_message *msg, struct mg_mqtt_prop *prop,
break;
case MQTT_PROP_TYPE_STRING:
prop->val.len = (uint16_t) ((((uint16_t) i[0]) << 8) | i[1]);
prop->val.ptr = (char *) i + 2;
prop->val.buf = (char *) i + 2;
new_pos += 2 + prop->val.len;
break;
case MQTT_PROP_TYPE_BINARY_DATA:
prop->val.len = (uint16_t) ((((uint16_t) i[0]) << 8) | i[1]);
prop->val.ptr = (char *) i + 2;
prop->val.buf = (char *) i + 2;
new_pos += 2 + prop->val.len;
break;
case MQTT_PROP_TYPE_VARIABLE_INT:
@ -298,24 +298,24 @@ void mg_mqtt_login(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
if (c->is_mqtt5) mg_send_mqtt_properties(c, opts->props, opts->num_props);
mg_send_u16(c, mg_htons((uint16_t) cid.len));
mg_send(c, cid.ptr, cid.len);
mg_send(c, cid.buf, cid.len);
if (hdr[7] & MQTT_HAS_WILL) {
if (c->is_mqtt5)
mg_send_mqtt_properties(c, opts->will_props, opts->num_will_props);
mg_send_u16(c, mg_htons((uint16_t) opts->topic.len));
mg_send(c, opts->topic.ptr, opts->topic.len);
mg_send(c, opts->topic.buf, opts->topic.len);
mg_send_u16(c, mg_htons((uint16_t) opts->message.len));
mg_send(c, opts->message.ptr, opts->message.len);
mg_send(c, opts->message.buf, opts->message.len);
}
if (opts->user.len > 0) {
mg_send_u16(c, mg_htons((uint16_t) opts->user.len));
mg_send(c, opts->user.ptr, opts->user.len);
mg_send(c, opts->user.buf, opts->user.len);
}
if (opts->pass.len > 0) {
mg_send_u16(c, mg_htons((uint16_t) opts->pass.len));
mg_send(c, opts->pass.ptr, opts->pass.len);
mg_send(c, opts->pass.buf, opts->pass.len);
}
}
@ -323,14 +323,14 @@ void mg_mqtt_pub(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
uint8_t flags = (uint8_t) (((opts->qos & 3) << 1) | (opts->retain ? 1 : 0));
size_t len = 2 + opts->topic.len + opts->message.len;
MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) opts->topic.len,
(char *) opts->topic.ptr, (int) opts->message.len,
(char *) opts->message.ptr));
(char *) opts->topic.buf, (int) opts->message.len,
(char *) opts->message.buf));
if (opts->qos > 0) len += 2;
if (c->is_mqtt5) len += get_props_size(opts->props, opts->num_props);
mg_mqtt_send_header(c, MQTT_CMD_PUBLISH, flags, (uint32_t) len);
mg_send_u16(c, mg_htons((uint16_t) opts->topic.len));
mg_send(c, opts->topic.ptr, opts->topic.len);
mg_send(c, opts->topic.buf, opts->topic.len);
if (opts->qos > 0) {
if (++c->mgr->mqtt_id == 0) ++c->mgr->mqtt_id;
mg_send_u16(c, mg_htons(c->mgr->mqtt_id));
@ -338,7 +338,7 @@ void mg_mqtt_pub(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
if (c->is_mqtt5) mg_send_mqtt_properties(c, opts->props, opts->num_props);
if (opts->message.len > 0) mg_send(c, opts->message.ptr, opts->message.len);
if (opts->message.len > 0) mg_send(c, opts->message.buf, opts->message.len);
}
void mg_mqtt_sub(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
@ -352,7 +352,7 @@ void mg_mqtt_sub(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
if (c->is_mqtt5) mg_send_mqtt_properties(c, opts->props, opts->num_props);
mg_send_u16(c, mg_htons((uint16_t) opts->topic.len));
mg_send(c, opts->topic.ptr, opts->topic.len);
mg_send(c, opts->topic.buf, opts->topic.len);
mg_send(c, &qos_, sizeof(qos_));
}
@ -362,7 +362,7 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
uint32_t n = 0, len_len = 0;
memset(m, 0, sizeof(*m));
m->dgram.ptr = (char *) buf;
m->dgram.buf = (char *) buf;
if (len < 2) return MQTT_INCOMPLETE;
m->cmd = (uint8_t) (buf[0] >> 4);
m->qos = (buf[0] >> 1) & 3;
@ -400,7 +400,7 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
case MQTT_CMD_PUBLISH: {
if (p + 2 > end) return MQTT_MALFORMED;
m->topic.len = (uint16_t) ((((uint16_t) p[0]) << 8) | p[1]);
m->topic.ptr = (char *) p + 2;
m->topic.buf = (char *) p + 2;
p += 2 + m->topic.len;
if (p > end) return MQTT_MALFORMED;
if (m->qos > 0) {
@ -417,7 +417,7 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
p += len_len + m->props_size;
}
if (p > end) return MQTT_MALFORMED;
m->data.ptr = (char *) p;
m->data.buf = (char *) p;
m->data.len = (size_t) (end - p);
break;
}
@ -439,7 +439,7 @@ static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data) {
break;
} else if (rc == MQTT_OK) {
MG_VERBOSE(("%lu MQTT CMD %d len %d [%.*s]", c->id, mm.cmd,
(int) mm.dgram.len, (int) mm.data.len, mm.data.ptr));
(int) mm.dgram.len, (int) mm.data.len, mm.data.buf));
switch (mm.cmd) {
case MQTT_CMD_CONNACK:
mg_call(c, MG_EV_MQTT_OPEN, &mm.ack);
@ -452,7 +452,7 @@ static void mqtt_cb(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));*/
mm.topic.buf, (int) mm.data.len, mm.data.buf));*/
if (mm.qos > 0) {
uint16_t id = mg_ntohs(mm.id);
uint32_t remaining_len = sizeof(id);

View File

@ -41,18 +41,18 @@ static bool mg_aton4(struct mg_str str, struct mg_addr *addr) {
uint8_t data[4] = {0, 0, 0, 0};
size_t i, num_dots = 0;
for (i = 0; i < str.len; i++) {
if (str.ptr[i] >= '0' && str.ptr[i] <= '9') {
int octet = data[num_dots] * 10 + (str.ptr[i] - '0');
if (str.buf[i] >= '0' && str.buf[i] <= '9') {
int octet = data[num_dots] * 10 + (str.buf[i] - '0');
if (octet > 255) return false;
data[num_dots] = (uint8_t) octet;
} else if (str.ptr[i] == '.') {
if (num_dots >= 3 || i == 0 || str.ptr[i - 1] == '.') return false;
} else if (str.buf[i] == '.') {
if (num_dots >= 3 || i == 0 || str.buf[i - 1] == '.') return false;
num_dots++;
} else {
return false;
}
}
if (num_dots != 3 || str.ptr[i - 1] == '.') return false;
if (num_dots != 3 || str.buf[i - 1] == '.') return false;
memcpy(&addr->ip, data, sizeof(data));
addr->is_ip6 = false;
return true;
@ -62,12 +62,12 @@ static bool mg_v4mapped(struct mg_str str, struct mg_addr *addr) {
int i;
uint32_t ipv4;
if (str.len < 14) return false;
if (str.ptr[0] != ':' || str.ptr[1] != ':' || str.ptr[6] != ':') return false;
if (str.buf[0] != ':' || str.buf[1] != ':' || str.buf[6] != ':') return false;
for (i = 2; i < 6; i++) {
if (str.ptr[i] != 'f' && str.ptr[i] != 'F') return false;
if (str.buf[i] != 'f' && str.buf[i] != 'F') return false;
}
// struct mg_str s = mg_str_n(&str.ptr[7], str.len - 7);
if (!mg_aton4(mg_str_n(&str.ptr[7], str.len - 7), addr)) return false;
// struct mg_str s = mg_str_n(&str.buf[7], str.len - 7);
if (!mg_aton4(mg_str_n(&str.buf[7], str.len - 7), addr)) return false;
memcpy(&ipv4, addr->ip, sizeof(ipv4));
memset(addr->ip, 0, sizeof(addr->ip));
addr->ip[10] = addr->ip[11] = 255;
@ -79,33 +79,33 @@ static bool mg_v4mapped(struct mg_str str, struct mg_addr *addr) {
static bool mg_aton6(struct mg_str str, struct mg_addr *addr) {
size_t i, j = 0, n = 0, dc = 42;
addr->scope_id = 0;
if (str.len > 2 && str.ptr[0] == '[') str.ptr++, str.len -= 2;
if (str.len > 2 && str.buf[0] == '[') str.buf++, str.len -= 2;
if (mg_v4mapped(str, addr)) return true;
for (i = 0; i < str.len; i++) {
if ((str.ptr[i] >= '0' && str.ptr[i] <= '9') ||
(str.ptr[i] >= 'a' && str.ptr[i] <= 'f') ||
(str.ptr[i] >= 'A' && str.ptr[i] <= 'F')) {
if ((str.buf[i] >= '0' && str.buf[i] <= '9') ||
(str.buf[i] >= 'a' && str.buf[i] <= 'f') ||
(str.buf[i] >= 'A' && str.buf[i] <= 'F')) {
unsigned long val;
if (i > j + 3) return false;
// MG_DEBUG(("%lu %lu [%.*s]", i, j, (int) (i - j + 1), &str.ptr[j]));
val = mg_unhexn(&str.ptr[j], i - j + 1);
// MG_DEBUG(("%lu %lu [%.*s]", i, j, (int) (i - j + 1), &str.buf[j]));
val = mg_unhexn(&str.buf[j], i - j + 1);
addr->ip[n] = (uint8_t) ((val >> 8) & 255);
addr->ip[n + 1] = (uint8_t) (val & 255);
} else if (str.ptr[i] == ':') {
} else if (str.buf[i] == ':') {
j = i + 1;
if (i > 0 && str.ptr[i - 1] == ':') {
if (i > 0 && str.buf[i - 1] == ':') {
dc = n; // Double colon
if (i > 1 && str.ptr[i - 2] == ':') return false;
if (i > 1 && str.buf[i - 2] == ':') return false;
} else if (i > 0) {
n += 2;
}
if (n > 14) return false;
addr->ip[n] = addr->ip[n + 1] = 0; // For trailing ::
} else if (str.ptr[i] == '%') { // Scope ID
} else if (str.buf[i] == '%') { // Scope ID
for (i = i + 1; i < str.len; i++) {
if (str.ptr[i] < '0' || str.ptr[i] > '9') return false;
if (str.buf[i] < '0' || str.buf[i] > '9') return false;
addr->scope_id = (uint8_t) (addr->scope_id * 10);
addr->scope_id = (uint8_t) (addr->scope_id + (str.ptr[i] - '0'));
addr->scope_id = (uint8_t) (addr->scope_id + (str.buf[i] - '0'));
}
} else {
return false;
@ -122,7 +122,7 @@ static bool mg_aton6(struct mg_str str, struct mg_addr *addr) {
}
bool mg_aton(struct mg_str str, struct mg_addr *addr) {
// MG_INFO(("[%.*s]", (int) str.len, str.ptr));
// MG_INFO(("[%.*s]", (int) str.len, str.buf));
return mg_atone(str, addr) || mg_atonl(str, addr) || mg_aton4(str, addr) ||
mg_aton6(str, addr);
}

View File

@ -142,7 +142,7 @@ static void send_syn(struct mg_connection *c);
static void mkpay(struct pkt *pkt, void *p) {
pkt->pay =
mg_str_n((char *) p, (size_t) (&pkt->raw.ptr[pkt->raw.len] - (char *) p));
mg_str_n((char *) p, (size_t) (&pkt->raw.buf[pkt->raw.len] - (char *) p));
}
static uint32_t csumup(uint32_t sum, const void *buf, size_t len) {
@ -176,13 +176,13 @@ static void settmout(struct mg_connection *c, uint8_t type) {
}
static size_t ether_output(struct mg_tcpip_if *ifp, size_t len) {
size_t n = ifp->driver->tx(ifp->tx.ptr, len, ifp);
size_t n = ifp->driver->tx(ifp->tx.buf, len, ifp);
if (n == len) ifp->nsent++;
return n;
}
static void arp_ask(struct mg_tcpip_if *ifp, uint32_t ip) {
struct eth *eth = (struct eth *) ifp->tx.ptr;
struct eth *eth = (struct eth *) ifp->tx.buf;
struct arp *arp = (struct arp *) (eth + 1);
memset(eth->dst, 255, sizeof(eth->dst));
memcpy(eth->src, ifp->mac, sizeof(eth->src));
@ -212,7 +212,7 @@ static void onstatechange(struct mg_tcpip_if *ifp) {
static struct ip *tx_ip(struct mg_tcpip_if *ifp, uint8_t *mac_dst,
uint8_t proto, uint32_t ip_src, uint32_t ip_dst,
size_t plen) {
struct eth *eth = (struct eth *) ifp->tx.ptr;
struct eth *eth = (struct eth *) ifp->tx.buf;
struct ip *ip = (struct ip *) (eth + 1);
memcpy(eth->dst, mac_dst, sizeof(eth->dst));
memcpy(eth->src, ifp->mac, sizeof(eth->src)); // Use our MAC
@ -325,7 +325,7 @@ static void rx_arp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
// ARP request. Make a response, then send
// MG_DEBUG(("ARP op %d %M: %M", mg_ntohs(pkt->arp->op), mg_print_ip4,
// &pkt->arp->spa, mg_print_ip4, &pkt->arp->tpa));
struct eth *eth = (struct eth *) ifp->tx.ptr;
struct eth *eth = (struct eth *) ifp->tx.buf;
struct arp *arp = (struct arp *) (eth + 1);
memcpy(eth->dst, pkt->eth->src, sizeof(eth->dst));
memcpy(eth->src, ifp->mac, sizeof(eth->src));
@ -369,7 +369,7 @@ static void rx_icmp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
sizeof(struct icmp) + plen);
struct icmp *icmp = (struct icmp *) (ip + 1);
memset(icmp, 0, sizeof(*icmp)); // Set csum to 0
memcpy(icmp + 1, pkt->pay.ptr, plen); // Copy RX payload to TX
memcpy(icmp + 1, pkt->pay.buf, plen); // Copy RX payload to TX
icmp->csum = ipcsum(icmp, sizeof(*icmp) + plen);
ether_output(ifp, hlen + plen);
}
@ -380,7 +380,7 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
uint8_t msgtype = 0, state = ifp->state;
// perform size check first, then access fields
uint8_t *p = pkt->dhcp->options,
*end = (uint8_t *) &pkt->raw.ptr[pkt->raw.len];
*end = (uint8_t *) &pkt->raw.buf[pkt->raw.len];
if (end < (uint8_t *) (pkt->dhcp + 1)) return;
if (memcmp(&pkt->dhcp->xid, ifp->mac + 2, sizeof(pkt->dhcp->xid))) return;
while (p + 1 < end && p[0] != 255) { // Parse options RFC-1533 #9
@ -428,7 +428,7 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
// Simple DHCP server that assigns a next IP address: ifp->ip + 1
static void rx_dhcp_server(struct mg_tcpip_if *ifp, struct pkt *pkt) {
uint8_t op = 0, *p = pkt->dhcp->options,
*end = (uint8_t *) &pkt->raw.ptr[pkt->raw.len];
*end = (uint8_t *) &pkt->raw.buf[pkt->raw.len];
if (end < (uint8_t *) (pkt->dhcp + 1)) return;
// struct dhcp *req = pkt->dhcp;
struct dhcp res = {2, 1, 6, 0, 0, 0, 0, 0, 0, 0, 0, {0}, 0, {0}};
@ -480,7 +480,7 @@ static void rx_udp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
!mg_iobuf_resize(&c->recv, c->recv.len + pkt->pay.len)) {
mg_error(c, "oom");
} else {
memcpy(&c->recv.buf[c->recv.len], pkt->pay.ptr, pkt->pay.len);
memcpy(&c->recv.buf[c->recv.len], pkt->pay.buf, pkt->pay.len);
c->recv.len += pkt->pay.len;
mg_call(c, MG_EV_READ, &pkt->pay.len);
}
@ -523,8 +523,8 @@ static size_t tx_tcp(struct mg_tcpip_if *ifp, uint8_t *dst_mac, uint32_t dst_ip,
MG_VERBOSE(("TCP %M:%hu -> %M:%hu fl %x len %u", mg_print_ip4, &ip->src,
mg_ntohs(tcp->sport), mg_print_ip4, &ip->dst,
mg_ntohs(tcp->dport), tcp->flags, len));
// mg_hexdump(ifp->tx.ptr, PDIFF(ifp->tx.ptr, tcp + 1) + len);
return ether_output(ifp, PDIFF(ifp->tx.ptr, tcp + 1) + len);
// mg_hexdump(ifp->tx.buf, PDIFF(ifp->tx.buf, tcp + 1) + len);
return ether_output(ifp, PDIFF(ifp->tx.buf, tcp + 1) + len);
}
static size_t tx_tcp_pkt(struct mg_tcpip_if *ifp, struct pkt *pkt,
@ -670,7 +670,7 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
// therefore we copy that encrypted data to the c->rtls iobuffer instead,
// and then call mg_tls_recv() to decrypt it. NOTE: mg_tls_recv() will
// call back mg_io_recv() which grabs raw data from c->rtls
memcpy(&io->buf[io->len], pkt->pay.ptr, pkt->pay.len);
memcpy(&io->buf[io->len], pkt->pay.buf, pkt->pay.len);
io->len += pkt->pay.len;
MG_VERBOSE(("%lu SEQ %x -> %x", c->id, mg_htonl(pkt->tcp->seq), s->ack));
@ -725,7 +725,7 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
settmout(c, MIP_TTYPE_KEEPALIVE);
mg_call(c, MG_EV_CONNECT, NULL); // Let user know
} else if (c != NULL && c->is_connecting && pkt->tcp->flags != TH_ACK) {
// mg_hexdump(pkt->raw.ptr, pkt->raw.len);
// mg_hexdump(pkt->raw.buf, pkt->raw.len);
tx_tcp_pkt(ifp, pkt, TH_RST | TH_ACK, pkt->tcp->ack, NULL, 0);
} else if (c != NULL && pkt->tcp->flags & TH_RST) {
mg_error(c, "peer RST"); // RFC-1122 4.2.2.13
@ -734,7 +734,7 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
MG_DEBUG(("%lu %d %M:%hu -> %M:%hu", c->id, (int) pkt->raw.len,
mg_print_ip4, &pkt->ip->src, mg_ntohs(pkt->tcp->sport),
mg_print_ip4, &pkt->ip->dst, mg_ntohs(pkt->tcp->dport)));
mg_hexdump(pkt->pay.ptr, pkt->pay.len);
mg_hexdump(pkt->pay.buf, pkt->pay.len);
#endif
s->tmiss = 0; // Reset missed keep-alive counter
if (s->ttype == MIP_TTYPE_KEEPALIVE) // Advance keep-alive timer
@ -823,7 +823,7 @@ static void rx_ip6(struct mg_tcpip_if *ifp, struct pkt *pkt) {
static void mg_tcpip_rx(struct mg_tcpip_if *ifp, void *buf, size_t len) {
struct pkt pkt;
memset(&pkt, 0, sizeof(pkt));
pkt.raw.ptr = (char *) buf;
pkt.raw.buf = (char *) buf;
pkt.raw.len = len;
pkt.eth = (struct eth *) buf;
// mg_hexdump(buf, len > 16 ? 16: len);
@ -985,7 +985,7 @@ void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) {
MG_ERROR(("driver init failed"));
} else {
size_t framesize = 1540;
ifp->tx.ptr = (char *) calloc(1, framesize), ifp->tx.len = framesize;
ifp->tx.buf = (char *) calloc(1, framesize), ifp->tx.len = framesize;
if (ifp->recv_queue.size == 0)
ifp->recv_queue.size = ifp->driver->rx ? framesize : 8192;
ifp->recv_queue.buf = (char *) calloc(1, ifp->recv_queue.size);
@ -999,13 +999,13 @@ void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) {
mg_random(&ifp->eport, sizeof(ifp->eport)); // Random from 0 to 65535
ifp->eport |= MG_EPHEMERAL_PORT_BASE; // Random from
// MG_EPHEMERAL_PORT_BASE to 65535
if (ifp->tx.ptr == NULL || ifp->recv_queue.buf == NULL) MG_ERROR(("OOM"));
if (ifp->tx.buf == NULL || ifp->recv_queue.buf == NULL) MG_ERROR(("OOM"));
}
}
void mg_tcpip_free(struct mg_tcpip_if *ifp) {
free(ifp->recv_queue.buf);
free((char *) ifp->tx.ptr);
free(ifp->tx.buf);
}
static void send_syn(struct mg_connection *c) {

View File

@ -15,7 +15,7 @@ void mg_rpc_del(struct mg_rpc **head, void (*fn)(struct mg_rpc_req *)) {
while ((r = *head) != NULL) {
if (r->fn == fn || fn == NULL) {
*head = r->next;
free((void *) r->method.ptr);
free((void *) r->method.buf);
free(r);
} else {
head = &(*head)->next;
@ -30,21 +30,21 @@ static void mg_rpc_call(struct mg_rpc_req *r, struct mg_str method) {
r->rpc = h;
h->fn(r);
} else {
mg_rpc_err(r, -32601, "\"%.*s not found\"", (int) method.len, method.ptr);
mg_rpc_err(r, -32601, "\"%.*s not found\"", (int) method.len, method.buf);
}
}
void mg_rpc_process(struct mg_rpc_req *r) {
int len, off = mg_json_get(r->frame, "$.method", &len);
if (off > 0 && r->frame.ptr[off] == '"') {
struct mg_str method = mg_str_n(&r->frame.ptr[off + 1], (size_t) len - 2);
if (off > 0 && r->frame.buf[off] == '"') {
struct mg_str method = mg_str_n(&r->frame.buf[off + 1], (size_t) len - 2);
mg_rpc_call(r, method);
} else if ((off = mg_json_get(r->frame, "$.result", &len)) > 0 ||
(off = mg_json_get(r->frame, "$.error", &len)) > 0) {
mg_rpc_call(r, mg_str("")); // JSON response! call "" method handler
} else {
mg_rpc_err(r, -32700, "%m", mg_print_esc, (int) r->frame.len,
r->frame.ptr); // Invalid
r->frame.buf); // Invalid
}
}
@ -52,7 +52,7 @@ void mg_rpc_vok(struct mg_rpc_req *r, const char *fmt, va_list *ap) {
int len, off = mg_json_get(r->frame, "$.id", &len);
if (off > 0) {
mg_xprintf(r->pfn, r->pfn_data, "{%m:%.*s,%m:", mg_print_esc, 0, "id", len,
&r->frame.ptr[off], mg_print_esc, 0, "result");
&r->frame.buf[off], mg_print_esc, 0, "result");
mg_vxprintf(r->pfn, r->pfn_data, fmt == NULL ? "null" : fmt, ap);
mg_xprintf(r->pfn, r->pfn_data, "}");
}
@ -70,7 +70,7 @@ void mg_rpc_verr(struct mg_rpc_req *r, int code, const char *fmt, va_list *ap) {
mg_xprintf(r->pfn, r->pfn_data, "{");
if (off > 0) {
mg_xprintf(r->pfn, r->pfn_data, "%m:%.*s,", mg_print_esc, 0, "id", len,
&r->frame.ptr[off]);
&r->frame.buf[off]);
}
mg_xprintf(r->pfn, r->pfn_data, "%m:{%m:%d,%m:", mg_print_esc, 0, "error",
mg_print_esc, 0, "code", code, mg_print_esc, 0, "message");
@ -91,7 +91,7 @@ static size_t print_methods(mg_pfn_t pfn, void *pfn_data, va_list *ap) {
for (h = *head; h != NULL; h = h->next) {
if (h->method.len == 0) continue; // Ignore response handler
len += mg_xprintf(pfn, pfn_data, "%s%m", h == *head ? "" : ",",
mg_print_esc, (int) h->method.len, h->method.ptr);
mg_print_esc, (int) h->method.len, h->method.buf);
}
return len;
}

View File

@ -1,12 +1,12 @@
#include "str.h"
struct mg_str mg_str_s(const char *s) {
struct mg_str str = {s, s == NULL ? 0 : strlen(s)};
struct mg_str str = {(char *) s, s == NULL ? 0 : strlen(s)};
return str;
}
struct mg_str mg_str_n(const char *s, size_t n) {
struct mg_str str = {s, n};
struct mg_str str = {(char *) s, n};
return str;
}
@ -30,26 +30,26 @@ int mg_casecmp(const char *s1, const char *s2) {
int mg_vcmp(const struct mg_str *s1, const char *s2) {
size_t n2 = strlen(s2), n1 = s1->len;
int r = strncmp(s1->ptr, s2, (n1 < n2) ? n1 : n2);
int r = strncmp(s1->buf, s2, (n1 < n2) ? n1 : n2);
if (r == 0) return (int) (n1 - n2);
return r;
}
int mg_vcasecmp(const struct mg_str *str1, const char *str2) {
size_t n2 = strlen(str2), n1 = str1->len;
int r = mg_ncasecmp(str1->ptr, str2, (n1 < n2) ? n1 : n2);
int r = mg_ncasecmp(str1->buf, str2, (n1 < n2) ? n1 : n2);
if (r == 0) return (int) (n1 - n2);
return r;
}
struct mg_str mg_strdup(const struct mg_str s) {
struct mg_str r = {NULL, 0};
if (s.len > 0 && s.ptr != NULL) {
if (s.len > 0 && s.buf != NULL) {
char *sc = (char *) calloc(1, s.len + 1);
if (sc != NULL) {
memcpy(sc, s.ptr, s.len);
memcpy(sc, s.buf, s.len);
sc[s.len] = '\0';
r.ptr = sc;
r.buf = sc;
r.len = s.len;
}
}
@ -59,8 +59,8 @@ struct mg_str mg_strdup(const struct mg_str s) {
int mg_strcmp(const struct mg_str str1, const struct mg_str str2) {
size_t i = 0;
while (i < str1.len && i < str2.len) {
int c1 = str1.ptr[i];
int c2 = str2.ptr[i];
int c1 = str1.buf[i];
int c2 = str2.buf[i];
if (c1 < c2) return -1;
if (c1 > c2) return 1;
i++;
@ -74,10 +74,10 @@ const char *mg_strstr(const struct mg_str haystack,
const struct mg_str needle) {
size_t i;
if (needle.len > haystack.len) return NULL;
if (needle.len == 0) return haystack.ptr;
if (needle.len == 0) return haystack.buf;
for (i = 0; i <= haystack.len - needle.len; i++) {
if (memcmp(haystack.ptr + i, needle.ptr, needle.len) == 0) {
return haystack.ptr + i;
if (memcmp(haystack.buf + i, needle.buf, needle.len) == 0) {
return haystack.buf + i;
}
}
return NULL;
@ -88,39 +88,39 @@ static bool is_space(int c) {
}
struct mg_str mg_strstrip(struct mg_str s) {
while (s.len > 0 && is_space((int) *s.ptr)) s.ptr++, s.len--;
while (s.len > 0 && is_space((int) *(s.ptr + s.len - 1))) s.len--;
while (s.len > 0 && is_space((int) *s.buf)) s.buf++, s.len--;
while (s.len > 0 && is_space((int) *(s.buf + s.len - 1))) s.len--;
return s;
}
bool mg_match(struct mg_str s, struct mg_str p, struct mg_str *caps) {
size_t i = 0, j = 0, ni = 0, nj = 0;
if (caps) caps->ptr = NULL, caps->len = 0;
if (caps) caps->buf = NULL, caps->len = 0;
while (i < p.len || j < s.len) {
if (i < p.len && j < s.len && (p.ptr[i] == '?' || s.ptr[j] == p.ptr[i])) {
if (i < p.len && j < s.len && (p.buf[i] == '?' || s.buf[j] == p.buf[i])) {
if (caps == NULL) {
} else if (p.ptr[i] == '?') {
caps->ptr = &s.ptr[j], caps->len = 1; // Finalize `?` cap
caps++, caps->ptr = NULL, caps->len = 0; // Init next cap
} else if (caps->ptr != NULL && caps->len == 0) {
caps->len = (size_t) (&s.ptr[j] - caps->ptr); // Finalize current cap
caps++, caps->len = 0, caps->ptr = NULL; // Init next cap
} else if (p.buf[i] == '?') {
caps->buf = &s.buf[j], caps->len = 1; // Finalize `?` cap
caps++, caps->buf = NULL, caps->len = 0; // Init next cap
} else if (caps->buf != NULL && caps->len == 0) {
caps->len = (size_t) (&s.buf[j] - caps->buf); // Finalize current cap
caps++, caps->len = 0, caps->buf = NULL; // Init next cap
}
i++, j++;
} else if (i < p.len && (p.ptr[i] == '*' || p.ptr[i] == '#')) {
if (caps && !caps->ptr) caps->len = 0, caps->ptr = &s.ptr[j]; // Init cap
} else if (i < p.len && (p.buf[i] == '*' || p.buf[i] == '#')) {
if (caps && !caps->buf) caps->len = 0, caps->buf = &s.buf[j]; // Init cap
ni = i++, nj = j + 1;
} else if (nj > 0 && nj <= s.len && (p.ptr[ni] == '#' || s.ptr[j] != '/')) {
} else if (nj > 0 && nj <= s.len && (p.buf[ni] == '#' || s.buf[j] != '/')) {
i = ni, j = nj;
if (caps && caps->ptr == NULL && caps->len == 0) {
if (caps && caps->buf == NULL && caps->len == 0) {
caps--, caps->len = 0; // Restart previous cap
}
} else {
return false;
}
}
if (caps && caps->ptr && caps->len == 0) {
caps->len = (size_t) (&s.ptr[j] - caps->ptr);
if (caps && caps->buf && caps->len == 0) {
caps->len = (size_t) (&s.buf[j] - caps->buf);
}
return true;
}
@ -130,14 +130,14 @@ bool mg_globmatch(const char *s1, size_t n1, const char *s2, size_t n2) {
}
bool mg_span(struct mg_str s, struct mg_str *a, struct mg_str *b, char sep) {
if (s.len == 0 || s.ptr == NULL) {
if (s.len == 0 || s.buf == NULL) {
return false; // Empty string, nothing to span - fail
} else {
size_t len = 0;
while (len < s.len && s.ptr[len] != sep) len++; // Find separator
if (a) *a = mg_str_n(s.ptr, len); // Init a
if (b) *b = mg_str_n(s.ptr + len, s.len - len); // Init b
if (b && len < s.len) b->ptr++, b->len--; // Skip separator
while (len < s.len && s.buf[len] != sep) len++; // Find separator
if (a) *a = mg_str_n(s.buf, len); // Init a
if (b) *b = mg_str_n(s.buf + len, s.len - len); // Init b
if (b && len < s.len) b->buf++, b->len--; // Skip separator
return true;
}
}

View File

@ -2,14 +2,12 @@
#include "arch.h"
// Describes an arbitrary chunk of memory
struct mg_str {
const char *ptr; // Pointer to string data
size_t len; // String len
char *buf; // String data
size_t len; // String length
};
#define MG_C_STR(a) \
{ (a), sizeof(a) - 1 }
// Using macro to avoid shadowing C++ struct constructor, see #1298
#define mg_str(s) mg_str_s(s)

View File

@ -627,7 +627,7 @@ static void mg_tls_server_send_cert(struct mg_connection *c) {
cert[9] = (uint8_t) (((n) >> 8) & 255U);
cert[10] = (uint8_t) (n & 255U);
// bytes 11+ are certificate in DER format
memmove(cert + 11, tls->server_cert_der.ptr, n);
memmove(cert + 11, tls->server_cert_der.buf, n);
cert[11 + n] = cert[12 + n] = 0; // certificate extensions (none)
mg_sha256_update(&tls->sha256, cert, 13 + n);
mg_tls_encrypt(c, cert, 13 + n, MG_TLS_HANDSHAKE);
@ -1244,7 +1244,7 @@ static int mg_parse_pem(const struct mg_str pem, const struct mg_str label,
return -1;
}
for (c = caps[2].ptr; c < caps[2].ptr + caps[2].len; c++) {
for (c = caps[2].buf; c < caps[2].buf + caps[2].len; c++) {
if (*c == ' ' || *c == '\n' || *c == '\r' || *c == '\t') {
continue;
}
@ -1255,7 +1255,7 @@ static int mg_parse_pem(const struct mg_str pem, const struct mg_str label,
free(s);
return -1;
}
der->ptr = s;
der->buf = s;
der->len = m;
return 0;
}
@ -1283,12 +1283,12 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
if (opts->name.len >= sizeof(tls->hostname) - 1) {
mg_error(c, "hostname too long");
}
strncpy((char *) tls->hostname, opts->name.ptr, sizeof(tls->hostname) - 1);
strncpy((char *) tls->hostname, opts->name.buf, sizeof(tls->hostname) - 1);
tls->hostname[opts->name.len] = 0;
}
if (c->is_client) {
tls->server_cert_der.ptr = NULL;
tls->server_cert_der.buf = NULL;
return;
}
@ -1300,7 +1300,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
}
// parse PEM or DER EC key
if (opts->key.ptr == NULL) {
if (opts->key.buf == NULL) {
mg_error(c, "certificate provided without a private key");
return;
}
@ -1312,15 +1312,15 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
}
// expect ASN.1 SEQUENCE=[INTEGER=1, BITSTRING of 32 bytes, ...]
// 30 nn 02 01 01 04 20 [key] ...
if (key.ptr[0] != 0x30 || (key.ptr[1] & 0x80) != 0) {
if (key.buf[0] != 0x30 || (key.buf[1] & 0x80) != 0) {
MG_ERROR(("EC private key: ASN.1 bad sequence"));
return;
}
if (memcmp(key.ptr + 2, "\x02\x01\x01\x04\x20", 5) != 0) {
if (memcmp(key.buf + 2, "\x02\x01\x01\x04\x20", 5) != 0) {
MG_ERROR(("EC private key: ASN.1 bad data"));
}
memmove(tls->server_key, key.ptr + 7, 32);
free((void *) key.ptr);
memmove(tls->server_key, key.buf + 7, 32);
free((void *) key.buf);
} else if (mg_parse_pem(opts->key, mg_str_s("PRIVATE KEY"), &key) == 0) {
mg_error(c, "PKCS8 private key format is not supported");
} else {
@ -1332,7 +1332,7 @@ void mg_tls_free(struct mg_connection *c) {
struct tls_data *tls = (struct tls_data *) c->tls;
if (tls != NULL) {
mg_iobuf_free(&tls->send);
free((void *) tls->server_cert_der.ptr);
free((void *) tls->server_cert_der.buf);
}
free(c->tls);
c->tls = NULL;

View File

@ -17,9 +17,9 @@ static int mg_mbed_rng(void *ctx, unsigned char *buf, size_t len) {
static bool mg_load_cert(struct mg_str str, mbedtls_x509_crt *p) {
int rc;
if (str.ptr == NULL || str.ptr[0] == '\0' || str.ptr[0] == '*') return true;
if (str.ptr[0] == '-') str.len++; // PEM, include trailing NUL
if ((rc = mbedtls_x509_crt_parse(p, (uint8_t *) str.ptr, str.len)) != 0) {
if (str.buf == NULL || str.buf[0] == '\0' || str.buf[0] == '*') return true;
if (str.buf[0] == '-') str.len++; // PEM, include trailing NUL
if ((rc = mbedtls_x509_crt_parse(p, (uint8_t *) str.buf, str.len)) != 0) {
MG_ERROR(("cert err %#x", -rc));
return false;
}
@ -28,9 +28,9 @@ static bool mg_load_cert(struct mg_str str, mbedtls_x509_crt *p) {
static bool mg_load_key(struct mg_str str, mbedtls_pk_context *p) {
int rc;
if (str.ptr == NULL || str.ptr[0] == '\0' || str.ptr[0] == '*') return true;
if (str.ptr[0] == '-') str.len++; // PEM, include trailing NUL
if ((rc = mbedtls_pk_parse_key(p, (uint8_t *) str.ptr, str.len, NULL,
if (str.buf == NULL || str.buf[0] == '\0' || str.buf[0] == '*') return true;
if (str.buf[0] == '-') str.len++; // PEM, include trailing NUL
if ((rc = mbedtls_pk_parse_key(p, (uint8_t *) str.buf, str.len, NULL,
0 MG_MBEDTLS_RNG_GET)) != 0) {
MG_ERROR(("key err %#x", -rc));
return false;
@ -128,8 +128,8 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
} else {
if (mg_load_cert(opts->ca, &tls->ca) == false) goto fail;
mbedtls_ssl_conf_ca_chain(&tls->conf, &tls->ca, NULL);
if (c->is_client && opts->name.ptr != NULL && opts->name.ptr[0] != '\0') {
char *host = mg_mprintf("%.*s", opts->name.len, opts->name.ptr);
if (c->is_client && opts->name.buf != NULL && opts->name.buf[0] != '\0') {
char *host = mg_mprintf("%.*s", opts->name.len, opts->name.buf);
mbedtls_ssl_set_hostname(&tls->ssl, host);
MG_DEBUG(("%lu hostname verification: %s", c->id, host));
free(host);

View File

@ -27,7 +27,7 @@ static int mg_tls_err(struct mg_connection *c, struct mg_tls *tls, int res) {
}
static STACK_OF(X509_INFO) * load_ca_certs(struct mg_str ca) {
BIO *bio = BIO_new_mem_buf(ca.ptr, (int) ca.len);
BIO *bio = BIO_new_mem_buf(ca.buf, (int) ca.len);
STACK_OF(X509_INFO) *certs =
bio ? PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL) : NULL;
if (bio) BIO_free(bio);
@ -45,16 +45,16 @@ static bool add_ca_certs(SSL_CTX *ctx, STACK_OF(X509_INFO) * certs) {
}
static EVP_PKEY *load_key(struct mg_str s) {
BIO *bio = BIO_new_mem_buf(s.ptr, (int) (long) s.len);
BIO *bio = BIO_new_mem_buf(s.buf, (int) (long) s.len);
EVP_PKEY *key = bio ? PEM_read_bio_PrivateKey(bio, NULL, 0, NULL) : NULL;
if (bio) BIO_free(bio);
return key;
}
static X509 *load_cert(struct mg_str s) {
BIO *bio = BIO_new_mem_buf(s.ptr, (int) (long) s.len);
BIO *bio = BIO_new_mem_buf(s.buf, (int) (long) s.len);
X509 *cert = bio == NULL ? NULL
: s.ptr[0] == '-'
: s.buf[0] == '-'
? PEM_read_bio_X509(bio, NULL, NULL, NULL) // PEM
: d2i_X509_bio(bio, NULL); // DER
if (bio) BIO_free(bio);
@ -128,7 +128,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
SSL_set_options(tls->ssl, SSL_OP_CIPHER_SERVER_PREFERENCE);
#endif
if (opts->ca.ptr != NULL && opts->ca.ptr[0] != '\0') {
if (opts->ca.buf != NULL && opts->ca.buf[0] != '\0') {
SSL_set_verify(tls->ssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
NULL);
STACK_OF(X509_INFO) *certs = load_ca_certs(opts->ca);
@ -139,7 +139,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
goto fail;
}
}
if (opts->cert.ptr != NULL && opts->cert.ptr[0] != '\0') {
if (opts->cert.buf != NULL && opts->cert.buf[0] != '\0') {
X509 *cert = load_cert(opts->cert);
rc = cert == NULL ? 0 : SSL_use_certificate(tls->ssl, cert);
X509_free(cert);
@ -148,7 +148,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
goto fail;
}
}
if (opts->key.ptr != NULL && opts->key.ptr[0] != '\0') {
if (opts->key.buf != NULL && opts->key.buf[0] != '\0') {
EVP_PKEY *key = load_key(opts->key);
rc = key == NULL ? 0 : SSL_use_PrivateKey(tls->ssl, key);
EVP_PKEY_free(key);
@ -164,7 +164,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
#endif
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
if (opts->name.len > 0) {
char *s = mg_mprintf("%.*s", (int) opts->name.len, opts->name.ptr);
char *s = mg_mprintf("%.*s", (int) opts->name.len, opts->name.buf);
SSL_set1_host(tls->ssl, s);
SSL_set_tlsext_host_name(tls->ssl, s);
free(s);

View File

@ -98,9 +98,9 @@ int mg_check_ip_acl(struct mg_str acl, struct mg_addr *remote_ip) {
memcpy((void *) &remote_ip4, remote_ip->ip, sizeof(remote_ip4));
while (mg_span(acl, &entry, &acl, ',')) {
uint32_t net, mask;
if (entry.ptr[0] != '+' && entry.ptr[0] != '-') return -1;
if (parse_net(&entry.ptr[1], &net, &mask) == 0) return -2;
if ((mg_ntohl(remote_ip4) & mask) == net) allowed = entry.ptr[0];
if (entry.buf[0] != '+' && entry.buf[0] != '-') return -1;
if (parse_net(&entry.buf[1], &net, &mask) == 0) return -2;
if ((mg_ntohl(remote_ip4) & mask) == net) allowed = entry.buf[0];
}
}
return allowed == '+';

View File

@ -40,7 +40,7 @@ static void ws_handshake(struct mg_connection *c, const struct mg_str *wskey,
mg_sha1_ctx sha_ctx;
mg_sha1_init(&sha_ctx);
mg_sha1_update(&sha_ctx, (unsigned char *) wskey->ptr, wskey->len);
mg_sha1_update(&sha_ctx, (unsigned char *) wskey->buf, wskey->len);
mg_sha1_update(&sha_ctx, (unsigned char *) magic, 36);
mg_sha1_final(sha, &sha_ctx);
mg_base64_encode(sha, sizeof(sha), (char *) b64_sha, sizeof(b64_sha));
@ -53,7 +53,7 @@ static void ws_handshake(struct mg_connection *c, const struct mg_str *wskey,
if (fmt != NULL) mg_vxprintf(mg_pfn_iobuf, &c->send, fmt, ap);
if (wsproto != NULL) {
mg_printf(c, "Sec-WebSocket-Protocol: %.*s\r\n", (int) wsproto->len,
wsproto->ptr);
wsproto->buf);
}
mg_send(c, "\r\n", 2);
}
@ -177,7 +177,7 @@ static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data) {
size_t len = msg.header_len + msg.data_len;
uint8_t final = msg.flags & 128, op = msg.flags & 15;
// MG_VERBOSE ("fin %d op %d len %d [%.*s]", final, op,
// (int) m.data.len, (int) m.data.len, m.data.ptr));
// (int) m.data.len, (int) m.data.len, m.data.buf));
switch (op) {
case WEBSOCKET_OP_CONTINUE:
mg_call(c, MG_EV_WS_CTL, &m);
@ -198,7 +198,7 @@ static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data) {
MG_DEBUG(("%lu WS CLOSE", c->id));
mg_call(c, MG_EV_WS_CTL, &m);
// Echo the payload of the received CLOSE message back to the sender
mg_ws_send(c, m.data.ptr, m.data.len, WEBSOCKET_OP_CLOSE);
mg_ws_send(c, m.data.buf, m.data.len, WEBSOCKET_OP_CLOSE);
c->is_draining = 1;
break;
default:
@ -248,7 +248,7 @@ struct mg_connection *mg_ws_connect(struct mg_mgr *mgr, const char *url,
"Connection: Upgrade\r\n"
"Sec-WebSocket-Version: 13\r\n"
"Sec-WebSocket-Key: %s\r\n",
mg_url_uri(url), (int) host.len, host.ptr, key);
mg_url_uri(url), (int) host.len, host.buf, key);
if (fmt != NULL) {
va_list ap;
va_start(ap, fmt);

View File

@ -77,7 +77,7 @@ static void f_http_fetch_query(struct mg_connection *c, int ev, void *ev_data) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
http_responses_received++;
if (!http_response_allocated) {
http_response = (char *) mg_strdup(hm->message).ptr;
http_response = (char *) mg_strdup(hm->message).buf;
http_response_allocated = 1;
}
if (http_responses_received > 0) {
@ -174,9 +174,9 @@ static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data) {
mg_mqtt_pub(c, &pub_opts);
} else if (ev == MG_EV_MQTT_MSG) {
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
if (mm->topic.len != strlen(s_topic) || strcmp(mm->topic.ptr, s_topic))
if (mm->topic.len != strlen(s_topic) || strcmp(mm->topic.buf, s_topic))
ASSERT(0);
if (mm->data.len != 2 || strcmp(mm->data.ptr, "hi")) ASSERT(0);
if (mm->data.len != 2 || strcmp(mm->data.buf, "hi")) ASSERT(0);
mg_mqtt_disconnect(c, NULL);
*(bool *) c->fn_data = true;
} else if (ev == MG_EV_CLOSE) {

View File

@ -26,7 +26,7 @@ static void test_statechange(void) {
memset(&iface, 0, sizeof(iface));
iface.ip = mg_htonl(0x01020304);
iface.state = MG_TCPIP_STATE_READY;
iface.tx.ptr = tx, iface.tx.len = sizeof(tx);
iface.tx.buf = tx, iface.tx.len = sizeof(tx);
iface.driver = &mg_tcpip_driver_mock;
onstatechange(&iface);
}

View File

@ -128,8 +128,8 @@ static void test_http_get_var(void) {
}
static int vcmp(struct mg_str s1, const char *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)));
// MG_INFO(("->%.*s<->%s<- %d %d %d", (int) s1.len, s1.buf, s2,
//(int) s1.len, strncmp(s1.buf, s2, s1.len), mg_vcmp(&s1, s2)));
return mg_vcmp(&s1, s2) == 0;
}
@ -367,9 +367,9 @@ static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data) {
} else if (ev == MG_EV_MQTT_MSG) {
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
snprintf(test_data->topic, test_data->topicsize, "%.*s",
(int) mm->topic.len, mm->topic.ptr);
(int) mm->topic.len, mm->topic.buf);
snprintf(buf + 1, test_data->msgsize - 2, "%.*s", (int) mm->data.len,
mm->data.ptr);
mm->data.buf);
if (mm->cmd == MQTT_CMD_PUBLISH && c->is_mqtt5) {
size_t pos = 0;
@ -383,21 +383,21 @@ static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data) {
ASSERT((pos = mg_mqtt_next_prop(mm, &prop, pos)) > 0);
ASSERT(prop.id == MQTT_PROP_CONTENT_TYPE);
ASSERT(strncmp(prop.val.ptr, "test_content_val_2", prop.val.len) == 0 &&
ASSERT(strncmp(prop.val.buf, "test_content_val_2", prop.val.len) == 0 &&
prop.val.len == strlen("test_content_val_2"));
ASSERT((pos = mg_mqtt_next_prop(mm, &prop, pos)) > 0);
ASSERT(prop.id == MQTT_PROP_USER_PROPERTY);
ASSERT(strncmp(prop.key.ptr, "test_key_1", prop.key.len) == 0 &&
ASSERT(strncmp(prop.key.buf, "test_key_1", prop.key.len) == 0 &&
prop.key.len == strlen("test_key_1"));
ASSERT(strncmp(prop.val.ptr, "test_value_1", prop.val.len) == 0 &&
ASSERT(strncmp(prop.val.buf, "test_value_1", prop.val.len) == 0 &&
prop.val.len == strlen("test_value_1"));
ASSERT((pos = mg_mqtt_next_prop(mm, &prop, pos)) > 0);
ASSERT(prop.id == MQTT_PROP_USER_PROPERTY);
ASSERT(strncmp(prop.key.ptr, "test_key_2", prop.key.len) == 0 &&
ASSERT(strncmp(prop.key.buf, "test_key_2", prop.key.len) == 0 &&
prop.key.len == strlen("test_key_2"));
ASSERT(strncmp(prop.val.ptr, "test_value_2", prop.val.len) == 0 &&
ASSERT(strncmp(prop.val.buf, "test_value_2", prop.val.len) == 0 &&
prop.val.len == strlen("test_value_2"));
ASSERT((pos = mg_mqtt_next_prop(mm, &prop, pos)) == 0);
@ -449,12 +449,12 @@ static void test_mqtt_base(void) {
static void check_mqtt_message(struct mg_mqtt_opts *opts,
struct mqtt_data *data, bool enforce) {
if (opts->topic.len != strlen(data->topic) ||
strcmp(opts->topic.ptr, data->topic)) {
strcmp(opts->topic.buf, data->topic)) {
MG_INFO(("TOPIC[%s]", data->topic));
if (enforce) ASSERT(0);
}
if (*data->msg != 'X' || opts->message.len != (strlen(&data->msg[1])) ||
strcmp(opts->message.ptr, &data->msg[1])) {
strcmp(opts->message.buf, &data->msg[1])) {
MG_INFO(("MSG[%s]", data->msg));
if (enforce) ASSERT(0);
}
@ -625,14 +625,14 @@ static void eh1(struct mg_connection *c, int ev, void *ev_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;
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));
MG_INFO(("[%.*s %.*s] message len %d", (int) hm->method.len, hm->method.buf,
(int) hm->uri.len, hm->uri.buf, (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);
mg_http_reply(c, 200, "", "uri: %.*s", hm->uri.len - 5, hm->uri.buf + 5);
} else if (mg_http_match_uri(hm, "/ws")) {
mg_ws_upgrade(c, hm, NULL);
} else if (mg_http_match_uri(hm, "/body")) {
mg_http_reply(c, 200, "", "%.*s", (int) hm->body.len, hm->body.ptr);
mg_http_reply(c, 200, "", "%.*s", (int) hm->body.len, hm->body.buf);
} else if (mg_http_match_uri(hm, "/bar")) {
mg_http_reply(c, 404, "", "not found");
} else if (mg_http_match_uri(hm, "/no_reason")) {
@ -674,7 +674,7 @@ static void eh1(struct mg_connection *c, int ev, void *ev_data) {
mg_ws_send(c, "opened", 6, WEBSOCKET_OP_BINARY);
} else if (ev == MG_EV_WS_MSG) {
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_BINARY);
mg_ws_send(c, wm->data.buf, wm->data.len, WEBSOCKET_OP_BINARY);
}
}
@ -688,8 +688,8 @@ static void fcb(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;
snprintf(fd->buf, FETCH_BUF_SIZE, "%.*s", (int) hm->message.len,
hm->message.ptr);
fd->code = atoi(hm->uri.ptr);
hm->message.buf);
fd->code = atoi(hm->uri.buf);
fd->closed = 1;
c->is_closing = 1;
(void) c;
@ -742,7 +742,7 @@ static int cmpbody(const char *buf, const char *str) {
struct mg_http_message hm = gethm(buf);
size_t len = strlen(buf);
// mg_http_parse(buf, len, &hm);
if (hm.body.len > len) hm.body.len = len - (size_t) (hm.body.ptr - buf);
if (hm.body.len > len) hm.body.len = len - (size_t) (hm.body.buf - buf);
return mg_strcmp(hm.body, s);
}
@ -788,7 +788,7 @@ static void ew2(struct mg_connection *c, int ev, void *ev_data) {
size_t ok = 1, i;
ASSERT(wm->data.len == size);
for (i = 0; i < size; i++) {
if (wm->data.ptr[i] != 'A') ok = 0;
if (wm->data.buf[i] != 'A') ok = 0;
}
ASSERT(ok == 1);
*(int *) c->fn_data = 1;
@ -967,8 +967,8 @@ static void test_http_server(void) {
{
struct mg_str data = mg_file_read(&mg_fs_posix, "./test/data/ca.pem");
ASSERT(fetch(&mgr, buf, url, "GET /ca.pem HTTP/1.0\r\n\n") == 200);
ASSERT(cmpbody(buf, data.ptr) == 0);
free((void *) data.ptr);
ASSERT(cmpbody(buf, data.buf) == 0);
free((void *) data.buf);
}
{
@ -1029,7 +1029,7 @@ static void test_http_server(void) {
struct mg_str s;
remove("uploaded.txt");
s = mg_file_read(&mg_fs_posix, "uploaded.txt");
ASSERT(s.ptr == NULL);
ASSERT(s.buf == NULL);
ASSERT(fetch(&mgr, buf, url,
"POST /upload HTTP/1.0\n"
"Content-Length: 1\n\nx") == 400);
@ -1043,9 +1043,9 @@ static void test_http_server(void) {
"Content-Length: 6\r\n"
"\r\n\nworld") == 200);
s = mg_file_read(&mg_fs_posix, "uploaded.txt");
ASSERT(s.ptr != NULL);
ASSERT(strcmp(s.ptr, "hello\nworld") == 0);
free((void *) s.ptr);
ASSERT(s.buf != NULL);
ASSERT(strcmp(s.buf, "hello\nworld") == 0);
free((void *) s.buf);
remove("uploaded.txt");
}
@ -1054,13 +1054,13 @@ static void test_http_server(void) {
struct mg_str s;
remove("uploaded.txt");
s = mg_file_read(&mg_fs_posix, "uploaded.txt");
ASSERT(s.ptr == NULL);
ASSERT(s.buf == NULL);
ASSERT(fetch(&mgr, buf, url,
"POST /upload?file=../uploaded.txt HTTP/1.0\r\n"
"Content-Length: 5\r\n"
"\r\nhello") == 400);
s = mg_file_read(&mg_fs_posix, "uploaded.txt");
ASSERT(s.ptr == NULL);
ASSERT(s.buf == NULL);
}
// HEAD request
@ -1111,8 +1111,8 @@ static void test_http_server(void) {
static void h4(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(("[%.*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.buf,
(int) hm->uri.len, hm->uri.buf, (int) hm->message.len));
if (mg_http_match_uri(hm, "/a/#")) {
struct mg_http_serve_opts opts;
memset(&opts, 0, sizeof(opts));
@ -1189,12 +1189,12 @@ static void test_tls(void) {
// MG_INFO(("%s", buf));
ASSERT(cmpbody(buf, "hello\n") == 0);
// POST a larger file, make sure we drain TLS buffers and read all, #2619
ASSERT(data.ptr != NULL && data.len > 0);
ASSERT(data.buf != NULL && data.len > 0);
ASSERT(fetch(&mgr, buf, url,
"POST /foo/bar HTTP/1.0\n"
"Content-Length: %lu\n\n"
"%s",
data.len, data.ptr) == 200);
data.len, data.buf) == 200);
mg_mgr_free(&mgr);
ASSERT(mgr.conns == NULL);
#endif
@ -1210,7 +1210,7 @@ static void f3(struct mg_connection *c, int ev, void *ev_data) {
c->rem.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;
// MG_INFO(("-->[%.*s]", (int) hm->message.len, hm->message.ptr));
// MG_INFO(("-->[%.*s]", (int) hm->message.len, hm->message.buf));
// ASSERT(mg_vcmp(&hm->method, "HTTP/1.1") == 0);
// ASSERT(mg_vcmp(&hm->uri, "301") == 0);
*ok = mg_http_status(hm);
@ -1316,7 +1316,7 @@ static void test_host_validation(void) {
static void f4(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_printf(c, "HTTP/1.0 200 OK\n\n%.*s/%s", (int) hm->uri.len, hm->uri.ptr,
mg_printf(c, "HTTP/1.0 200 OK\n\n%.*s/%s", (int) hm->uri.len, hm->uri.buf,
"abcdef");
strcat((char *) c->fn_data, "m");
c->is_draining = 1;
@ -1340,7 +1340,7 @@ static void f4c(struct mg_connection *c, int ev, void *ev_data) {
static void f41(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_printf(c, "HTTP/1.0 200 OK\n\n%.*s/%s", (int) hm->uri.len, hm->uri.ptr,
mg_printf(c, "HTTP/1.0 200 OK\n\n%.*s/%s", (int) hm->uri.len, hm->uri.buf,
"abcdef");
}
}
@ -1379,7 +1379,7 @@ static void test_http_no_content_length(void) {
static void f5(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_http_reply(c, 200, "", "%.*s", (int) hm->uri.len, hm->uri.ptr);
mg_http_reply(c, 200, "", "%.*s", (int) hm->uri.len, hm->uri.buf);
(*(int *) c->fn_data)++;
}
}
@ -1436,7 +1436,7 @@ static void test_http_parse(void) {
ASSERT(mg_vcmp(&req.headers[0].name, "Foo") == 0);
ASSERT(mg_vcmp(&req.headers[0].value, "bar") == 0);
ASSERT(req.headers[1].name.len == 0);
ASSERT(req.headers[1].name.ptr == NULL);
ASSERT(req.headers[1].name.buf == NULL);
ASSERT(req.query.len == 0);
ASSERT(req.message.len == len);
ASSERT(req.body.len == 0);
@ -1851,7 +1851,7 @@ static void test_str(void) {
{
struct mg_str s = mg_strdup(mg_str("a"));
ASSERT(mg_strcmp(s, mg_str("a")) == 0);
free((void *) s.ptr);
free((void *) s.buf);
}
{
@ -1859,7 +1859,7 @@ static void test_str(void) {
struct mg_str a = mg_str("hello"), b = mg_str("a"), c = mg_str(NULL);
ASSERT((s = mg_strstr(a, b)) == NULL);
ASSERT((s = mg_strstr(a, c)) != NULL);
ASSERT(s == a.ptr);
ASSERT(s == a.buf);
}
ASSERT(mg_strcmp(mg_str(""), mg_str(NULL)) == 0);
@ -2203,9 +2203,9 @@ static void test_util(void) {
ASSERT(mg_file_printf(&mg_fs_posix, "data.txt", "%s", "hi") == true);
// if (system("ls -l") != 0) (void) 0;
data = mg_file_read(&mg_fs_posix, "data.txt");
ASSERT(data.ptr != NULL);
ASSERT(strcmp(data.ptr, "hi") == 0);
free((void *) data.ptr);
ASSERT(data.buf != NULL);
ASSERT(strcmp(data.buf, "hi") == 0);
free((void *) data.buf);
remove("data.txt");
ASSERT(mg_aton(mg_str("0"), &a) == false);
ASSERT(mg_aton(mg_str("0.0.0."), &a) == false);
@ -2342,10 +2342,10 @@ static void us(struct mg_connection *c, int ev, void *ev_data) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
if (ev == MG_EV_HTTP_MSG && mg_http_match_uri(hm, "/upload")) {
MG_DEBUG(("Got all %lu bytes!", (unsigned long) hm->body.len));
MG_DEBUG(("Query string: [%.*s]", (int) hm->query.len, hm->query.ptr));
// MG_DEBUG(("Body:\n%.*s", (int) hm->body.len, hm->body.ptr));
MG_DEBUG(("Query string: [%.*s]", (int) hm->query.len, hm->query.buf));
// MG_DEBUG(("Body:\n%.*s", (int) hm->body.len, hm->body.buf));
mg_http_reply(c, 200, "", "ok (%d %.*s)\n", (int) hm->body.len,
(int) hm->body.len, hm->body.ptr);
(int) hm->body.len, hm->body.buf);
} else if (ev == MG_EV_HTTP_MSG) {
mg_http_reply(c, 200, "", "ok\n");
}
@ -2364,7 +2364,7 @@ static void uc(struct mg_connection *c, int ev, void *ev_data) {
mg_http_printf_chunk(c, "");
} else if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
// MG_INFO(("---> [%s] [%.*s]", *s, hm->body.len, hm->body.ptr));
// MG_INFO(("---> [%s] [%.*s]", *s, hm->body.len, hm->body.buf));
ASSERT(mg_strcmp(hm->body, mg_str(*s)) == 0);
*s = NULL;
}
@ -2433,8 +2433,8 @@ static void eh4(struct mg_connection *c, int ev, void *ev_data) {
uint32_t *crc = (uint32_t *) c->fn_data;
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);
MG_INFO(("%lu M [%.*s]", c->id, (int) hm->body.len, hm->body.ptr));
*crc = mg_crc32(*crc, hm->body.buf, hm->body.len);
MG_INFO(("%lu M [%.*s]", c->id, (int) hm->body.len, hm->body.buf));
}
}
@ -2594,12 +2594,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);
// MG_INFO(("--> [%.*s]", (int) part.body.len, part.body.ptr));
// MG_INFO(("--> [%.*s]", (int) part.body.len, part.body.buf));
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);
// MG_INFO(("--> [%.*s]", (int) part.filename.len, part.filename.ptr));
// MG_INFO(("--> [%.*s]", (int) part.filename.len, part.filename.buf));
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);
@ -2628,14 +2628,14 @@ static void test_packed(void) {
// fetch(&mgr, buf, url, "GET /Makefile HTTP/1.0\n\n");
// printf("---> %s\n", buf);
ASSERT(fetch(&mgr, buf, url, "GET /Makefile HTTP/1.0\n\n") == 200);
ASSERT(cmpbody(buf, data.ptr) == 0);
free((void *) data.ptr);
ASSERT(cmpbody(buf, data.buf) == 0);
free((void *) data.buf);
// Load file deeper in the FS tree directly
data = mg_file_read(&mg_fs_posix, "src/ssi.h");
ASSERT(fetch(&mgr, buf, url, "GET /src/ssi.h HTTP/1.0\n\n") == 200);
ASSERT(cmpbody(buf, data.ptr) == 0);
free((void *) data.ptr);
ASSERT(cmpbody(buf, data.buf) == 0);
free((void *) data.buf);
// List root dir
ASSERT(fetch(&mgr, buf, url, "GET / HTTP/1.0\n\n") == 200);
@ -2735,7 +2735,7 @@ static void w2(struct mg_connection *c, int ev, void *ev_data) {
uint8_t op = n == 0 ? WEBSOCKET_OP_TEXT : WEBSOCKET_OP_CONTINUE;
mg_ws_send(c, ":->", 3, WEBSOCKET_OP_PING);
ofs = c->send.len;
mg_ws_send(c, &msg.ptr[n], 1, op);
mg_ws_send(c, &msg.buf[n], 1, op);
if (n < msg.len - 1) c->send.buf[ofs] = op; // Clear FIN flag
c->fn_data = (void *) (n + 1); // Point to the next char
} else {
@ -2744,7 +2744,7 @@ static void w2(struct mg_connection *c, int ev, void *ev_data) {
} else if (ev == MG_EV_WS_MSG) {
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
MG_INFO(("Got WS, %lu", wm->data.len));
// mg_hexdump(wm->data.ptr, wm->data.len);
// mg_hexdump(wm->data.buf, wm->data.len);
if (wm->data.len == 9) {
ASSERT(mg_strcmp(wm->data, mg_str("hi there!")) == 0);
} else if (wm->data.len == 10) {
@ -2821,15 +2821,15 @@ static void test_get_header_var(void) {
static void json_scan(struct mg_str json, int depth) {
int i, n = 0, o = mg_json_get(json, "$", &n);
for (i = 0; i < depth; i++) printf(" ");
printf("%.*s\n", n, json.ptr + o);
if (json.ptr[o] == '{' || json.ptr[o] == '[') { // Iterate over elems
struct mg_str key, val, sub = mg_str_n(json.ptr + o, (size_t) n);
printf("%.*s\n", n, json.buf + o);
if (json.buf[o] == '{' || json.buf[o] == '[') { // Iterate over elems
struct mg_str key, val, sub = mg_str_n(json.buf + o, (size_t) n);
size_t ofs = 0;
while ((ofs = mg_json_next(sub, ofs, &key, &val)) > 0) {
for (i = 0; i < depth; i++) printf(" ");
printf("KEY: %.*s VAL: %.*s\n", (int) key.len, key.ptr, (int) val.len,
val.ptr);
if (*val.ptr == '[' || *val.ptr == '{') json_scan(val, depth + 1);
printf("KEY: %.*s VAL: %.*s\n", (int) key.len, key.buf, (int) val.len,
val.buf);
if (*val.buf == '[' || *val.buf == '{') json_scan(val, depth + 1);
}
}
}
@ -3020,7 +3020,7 @@ static void test_json(void) {
json = mg_str("[1,true,{\"a\":[3],\"b\":42}]");
json_scan(json, 0);
{
struct mg_str k, v, sub = mg_str_n(json.ptr + 8, json.len - 8);
struct mg_str k, v, sub = mg_str_n(json.buf + 8, json.len - 8);
const char *a = "\"a\"", *b = "\"b\"";
ASSERT(mg_json_next(sub, 0, &k, &v) == 9);
ASSERT(mg_vcmp(&k, a) == 0);
@ -3041,7 +3041,7 @@ static void test_json(void) {
static void resp_rpc(struct mg_rpc_req *r) {
int len = 0, off = mg_json_get(r->frame, "$.result", &len);
mg_xprintf(r->pfn, r->pfn_data, "%.*s", len, &r->frame.ptr[off]);
mg_xprintf(r->pfn, r->pfn_data, "%.*s", len, &r->frame.buf[off]);
}
static void test_rpc(void) {