Fix #1519 - make opts structs in API calls const

This commit is contained in:
Sergey Lyubka 2022-04-12 10:04:55 +01:00
parent 5f78d34f4d
commit db941e2627
10 changed files with 32 additions and 30 deletions

View File

@ -1470,7 +1470,8 @@ static int getrange(struct mg_str *s, int64_t *a, int64_t *b) {
}
void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
const char *path, struct mg_http_serve_opts *opts) {
const char *path,
const struct mg_http_serve_opts *opts) {
char etag[64];
struct mg_fs *fs = opts->fs == NULL ? &mg_fs_posix : opts->fs;
struct mg_fd *fd = mg_fs_open(fs, path, MG_FS_READ);
@ -1534,7 +1535,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
struct printdirentrydata {
struct mg_connection *c;
struct mg_http_message *hm;
struct mg_http_serve_opts *opts;
const struct mg_http_serve_opts *opts;
const char *dir;
};
@ -1570,7 +1571,7 @@ static void printdirentry(const char *name, void *userdata) {
}
static void listdir(struct mg_connection *c, struct mg_http_message *hm,
struct mg_http_serve_opts *opts, char *dir) {
const struct mg_http_serve_opts *opts, char *dir) {
static const char *sort_js_code =
"<script>function srt(tb, sc, so, d) {"
"var tr = Array.prototype.slice.call(tb.rows, 0),"
@ -1698,7 +1699,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm,
}
static int uri_to_path(struct mg_connection *c, struct mg_http_message *hm,
struct mg_http_serve_opts *opts, char *path,
const struct mg_http_serve_opts *opts, char *path,
size_t path_size) {
struct mg_fs *fs = opts->fs == NULL ? &mg_fs_posix : opts->fs;
struct mg_str k, v, s = mg_str(opts->root_dir), u = {0, 0}, p = {0, 0};
@ -1712,7 +1713,7 @@ static int uri_to_path(struct mg_connection *c, struct mg_http_message *hm,
}
void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
struct mg_http_serve_opts *opts) {
const struct mg_http_serve_opts *opts) {
char path[MG_PATH_MAX] = "";
const char *sp = opts->ssi_pattern;
int flags = uri_to_path(c, hm, opts, path, sizeof(path));
@ -2341,7 +2342,7 @@ static void mg_send_u16(struct mg_connection *c, uint16_t value) {
mg_send(c, &value, sizeof(value));
}
void mg_mqtt_login(struct mg_connection *c, struct mg_mqtt_opts *opts) {
void mg_mqtt_login(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
char rnd[9], client_id[16];
struct mg_str cid = opts->client_id;
uint32_t total_len = 7 + 1 + 2 + 2;
@ -2568,7 +2569,7 @@ void mg_mqtt_disconnect(struct mg_connection *nc) {
}
struct mg_connection *mg_mqtt_connect(struct mg_mgr *mgr, const char *url,
struct mg_mqtt_opts *opts,
const struct mg_mqtt_opts *opts,
mg_event_handler_t fn, void *fn_data) {
struct mg_connection *c = mg_connect(mgr, url, fn, fn_data);
if (c != NULL) {
@ -4223,7 +4224,7 @@ void mg_timer_poll(uint64_t now_ms) {
#if !MG_ENABLE_MBEDTLS && !MG_ENABLE_OPENSSL && !MG_ENABLE_CUSTOM_TLS
void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
(void) opts;
mg_error(c, "TLS is not enabled");
}
@ -4342,7 +4343,7 @@ static struct mg_str mg_loadfile(struct mg_fs *fs, const char *path) {
return mg_str_n(p, n);
}
void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
struct mg_fs *fs = opts->fs == NULL ? &mg_fs_posix : opts->fs;
struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls));
int rc = 0;
@ -4473,7 +4474,7 @@ static int mg_tls_err(struct mg_tls *tls, int res) {
return err;
}
void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls));
const char *id = "mongoose";
static unsigned char s_initialised = 0;

View File

@ -1003,9 +1003,9 @@ struct mg_connection *mg_http_listen(struct mg_mgr *, const char *url,
struct mg_connection *mg_http_connect(struct mg_mgr *, const char *url,
mg_event_handler_t fn, void *fn_data);
void mg_http_serve_dir(struct mg_connection *, struct mg_http_message *hm,
struct mg_http_serve_opts *opts);
const struct mg_http_serve_opts *);
void mg_http_serve_file(struct mg_connection *, struct mg_http_message *hm,
const char *path, struct mg_http_serve_opts *opts);
const char *path, const struct mg_http_serve_opts *);
void mg_http_reply(struct mg_connection *, int status_code, const char *headers,
const char *body_fmt, ...);
struct mg_str *mg_http_get_header(struct mg_http_message *, const char *name);
@ -1040,7 +1040,7 @@ struct mg_tls_opts {
struct mg_fs *fs; // FS API for reading certificate files
};
void mg_tls_init(struct mg_connection *, struct mg_tls_opts *);
void mg_tls_init(struct mg_connection *, const struct mg_tls_opts *);
void mg_tls_free(struct mg_connection *);
long mg_tls_send(struct mg_connection *, const void *buf, size_t len);
long mg_tls_recv(struct mg_connection *, void *buf, size_t len);
@ -1152,11 +1152,11 @@ struct mg_mqtt_message {
};
struct mg_connection *mg_mqtt_connect(struct mg_mgr *, const char *url,
struct mg_mqtt_opts *opts,
const struct mg_mqtt_opts *opts,
mg_event_handler_t fn, void *fn_data);
struct mg_connection *mg_mqtt_listen(struct mg_mgr *mgr, const char *url,
mg_event_handler_t fn, void *fn_data);
void mg_mqtt_login(struct mg_connection *c, struct mg_mqtt_opts *opts);
void mg_mqtt_login(struct mg_connection *c, const struct mg_mqtt_opts *opts);
void mg_mqtt_pub(struct mg_connection *c, struct mg_str topic,
struct mg_str data, int qos, bool retain);
void mg_mqtt_sub(struct mg_connection *, struct mg_str topic, int qos);

View File

@ -480,7 +480,8 @@ static int getrange(struct mg_str *s, int64_t *a, int64_t *b) {
}
void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
const char *path, struct mg_http_serve_opts *opts) {
const char *path,
const struct mg_http_serve_opts *opts) {
char etag[64];
struct mg_fs *fs = opts->fs == NULL ? &mg_fs_posix : opts->fs;
struct mg_fd *fd = mg_fs_open(fs, path, MG_FS_READ);
@ -544,7 +545,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
struct printdirentrydata {
struct mg_connection *c;
struct mg_http_message *hm;
struct mg_http_serve_opts *opts;
const struct mg_http_serve_opts *opts;
const char *dir;
};
@ -580,7 +581,7 @@ static void printdirentry(const char *name, void *userdata) {
}
static void listdir(struct mg_connection *c, struct mg_http_message *hm,
struct mg_http_serve_opts *opts, char *dir) {
const struct mg_http_serve_opts *opts, char *dir) {
static const char *sort_js_code =
"<script>function srt(tb, sc, so, d) {"
"var tr = Array.prototype.slice.call(tb.rows, 0),"
@ -708,7 +709,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm,
}
static int uri_to_path(struct mg_connection *c, struct mg_http_message *hm,
struct mg_http_serve_opts *opts, char *path,
const struct mg_http_serve_opts *opts, char *path,
size_t path_size) {
struct mg_fs *fs = opts->fs == NULL ? &mg_fs_posix : opts->fs;
struct mg_str k, v, s = mg_str(opts->root_dir), u = {0, 0}, p = {0, 0};
@ -722,7 +723,7 @@ static int uri_to_path(struct mg_connection *c, struct mg_http_message *hm,
}
void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
struct mg_http_serve_opts *opts) {
const struct mg_http_serve_opts *opts) {
char path[MG_PATH_MAX] = "";
const char *sp = opts->ssi_pattern;
int flags = uri_to_path(c, hm, opts, path, sizeof(path));

View File

@ -46,9 +46,9 @@ struct mg_connection *mg_http_listen(struct mg_mgr *, const char *url,
struct mg_connection *mg_http_connect(struct mg_mgr *, const char *url,
mg_event_handler_t fn, void *fn_data);
void mg_http_serve_dir(struct mg_connection *, struct mg_http_message *hm,
struct mg_http_serve_opts *opts);
const struct mg_http_serve_opts *);
void mg_http_serve_file(struct mg_connection *, struct mg_http_message *hm,
const char *path, struct mg_http_serve_opts *opts);
const char *path, const struct mg_http_serve_opts *);
void mg_http_reply(struct mg_connection *, int status_code, const char *headers,
const char *body_fmt, ...);
struct mg_str *mg_http_get_header(struct mg_http_message *, const char *name);

View File

@ -31,7 +31,7 @@ static void mg_send_u16(struct mg_connection *c, uint16_t value) {
mg_send(c, &value, sizeof(value));
}
void mg_mqtt_login(struct mg_connection *c, struct mg_mqtt_opts *opts) {
void mg_mqtt_login(struct mg_connection *c, const struct mg_mqtt_opts *opts) {
char rnd[9], client_id[16];
struct mg_str cid = opts->client_id;
uint32_t total_len = 7 + 1 + 2 + 2;
@ -258,7 +258,7 @@ void mg_mqtt_disconnect(struct mg_connection *nc) {
}
struct mg_connection *mg_mqtt_connect(struct mg_mgr *mgr, const char *url,
struct mg_mqtt_opts *opts,
const struct mg_mqtt_opts *opts,
mg_event_handler_t fn, void *fn_data) {
struct mg_connection *c = mg_connect(mgr, url, fn, fn_data);
if (c != NULL) {

View File

@ -41,11 +41,11 @@ struct mg_mqtt_message {
};
struct mg_connection *mg_mqtt_connect(struct mg_mgr *, const char *url,
struct mg_mqtt_opts *opts,
const struct mg_mqtt_opts *opts,
mg_event_handler_t fn, void *fn_data);
struct mg_connection *mg_mqtt_listen(struct mg_mgr *mgr, const char *url,
mg_event_handler_t fn, void *fn_data);
void mg_mqtt_login(struct mg_connection *c, struct mg_mqtt_opts *opts);
void mg_mqtt_login(struct mg_connection *c, const struct mg_mqtt_opts *opts);
void mg_mqtt_pub(struct mg_connection *c, struct mg_str topic,
struct mg_str data, int qos, bool retain);
void mg_mqtt_sub(struct mg_connection *, struct mg_str topic, int qos);

View File

@ -14,7 +14,7 @@ struct mg_tls_opts {
struct mg_fs *fs; // FS API for reading certificate files
};
void mg_tls_init(struct mg_connection *, struct mg_tls_opts *);
void mg_tls_init(struct mg_connection *, const struct mg_tls_opts *);
void mg_tls_free(struct mg_connection *);
long mg_tls_send(struct mg_connection *, const void *buf, size_t len);
long mg_tls_recv(struct mg_connection *, void *buf, size_t len);

View File

@ -1,7 +1,7 @@
#include "tls.h"
#if !MG_ENABLE_MBEDTLS && !MG_ENABLE_OPENSSL && !MG_ENABLE_CUSTOM_TLS
void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
(void) opts;
mg_error(c, "TLS is not enabled");
}

View File

@ -96,7 +96,7 @@ static struct mg_str mg_loadfile(struct mg_fs *fs, const char *path) {
return mg_str_n(p, n);
}
void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
struct mg_fs *fs = opts->fs == NULL ? &mg_fs_posix : opts->fs;
struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls));
int rc = 0;

View File

@ -19,7 +19,7 @@ static int mg_tls_err(struct mg_tls *tls, int res) {
return err;
}
void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls));
const char *id = "mongoose";
static unsigned char s_initialised = 0;