mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-08 16:11:18 +00:00
mg_set_option(): allow NULL as value. Fast success if old_value == new_value.
This commit is contained in:
1 parent
2609a2ab72
commit
8be30350be
1 file changed
+51
-41
+51
-41
@@ -4415,53 +4415,63 @@ const char *mg_set_option(struct mg_server *server, const char *name,
|
|||||||
const char *value) {
|
const char *value) {
|
||||||
int ind = get_option_index(name);
|
int ind = get_option_index(name);
|
||||||
const char *error_msg = NULL;
|
const char *error_msg = NULL;
|
||||||
|
char **v = NULL;
|
||||||
|
|
||||||
if (ind < 0) {
|
if (ind < 0) return "No such option";
|
||||||
error_msg = "No such option";
|
v = &server->config_options[ind];
|
||||||
} else {
|
|
||||||
if (server->config_options[ind] != NULL) {
|
// Return success immediately if setting to the same value
|
||||||
free(server->config_options[ind]);
|
if ((*v == NULL && value == NULL) ||
|
||||||
|
(value != NULL && *v != NULL && !strcmp(value, *v))) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*v != NULL) {
|
||||||
|
free(*v);
|
||||||
|
*v = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value == NULL) return NULL;
|
||||||
|
|
||||||
|
*v = mg_strdup(value);
|
||||||
|
DBG(("%s [%s]", name, *v));
|
||||||
|
|
||||||
|
if (ind == LISTENING_PORT) {
|
||||||
|
int port = ns_bind(&server->ns_server, value);
|
||||||
|
if (port < 0) {
|
||||||
|
error_msg = "Cannot bind to port";
|
||||||
|
} else {
|
||||||
|
ns_sock_to_str(server->ns_server.listening_sock, server->local_ip,
|
||||||
|
sizeof(server->local_ip), 0);
|
||||||
|
if (!strcmp(value, "0")) {
|
||||||
|
char buf[10];
|
||||||
|
mg_snprintf(buf, sizeof(buf), "%d", port);
|
||||||
|
free(*v);
|
||||||
|
*v = mg_strdup(buf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
server->config_options[ind] = mg_strdup(value);
|
|
||||||
DBG(("%s [%s]", name, value));
|
|
||||||
|
|
||||||
if (ind == LISTENING_PORT) {
|
|
||||||
int port = ns_bind(&server->ns_server, value);
|
|
||||||
if (port < 0) {
|
|
||||||
error_msg = "Cannot bind to port";
|
|
||||||
} else {
|
|
||||||
ns_sock_to_str(server->ns_server.listening_sock, server->local_ip,
|
|
||||||
sizeof(server->local_ip), 0);
|
|
||||||
if (!strcmp(value, "0")) {
|
|
||||||
char buf[10];
|
|
||||||
mg_snprintf(buf, sizeof(buf), "%d", port);
|
|
||||||
free(server->config_options[ind]);
|
|
||||||
server->config_options[ind] = mg_strdup(buf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
} else if (ind == RUN_AS_USER) {
|
} else if (ind == RUN_AS_USER) {
|
||||||
struct passwd *pw;
|
struct passwd *pw;
|
||||||
if ((pw = getpwnam(value)) == NULL) {
|
if ((pw = getpwnam(value)) == NULL) {
|
||||||
error_msg = "Unknown user";
|
error_msg = "Unknown user";
|
||||||
} else if (setgid(pw->pw_gid) != 0) {
|
} else if (setgid(pw->pw_gid) != 0) {
|
||||||
error_msg = "setgid() failed";
|
error_msg = "setgid() failed";
|
||||||
} else if (setuid(pw->pw_uid) != 0) {
|
} else if (setuid(pw->pw_uid) != 0) {
|
||||||
error_msg = "setuid() failed";
|
error_msg = "setuid() failed";
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef NS_ENABLE_SSL
|
#ifdef NS_ENABLE_SSL
|
||||||
} else if (ind == SSL_CERTIFICATE) {
|
} else if (ind == SSL_CERTIFICATE) {
|
||||||
int res = ns_set_ssl_cert(&server->ns_server, value);
|
int res = ns_set_ssl_cert(&server->ns_server, value);
|
||||||
if (res == -2) {
|
if (res == -2) {
|
||||||
error_msg = "Cannot load PEM";
|
error_msg = "Cannot load PEM";
|
||||||
} else if (res == -3) {
|
} else if (res == -3) {
|
||||||
error_msg = "SSL not enabled";
|
error_msg = "SSL not enabled";
|
||||||
} else if (res == -1) {
|
} else if (res == -1) {
|
||||||
error_msg = "SSL_CTX_new() failed";
|
error_msg = "SSL_CTX_new() failed";
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
return error_msg;
|
return error_msg;
|
||||||
|
|||||||
Reference in new issue
Block a user