mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-24 20:21:49 +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
+20
-10
+20
-10
@@ -4415,15 +4415,26 @@ 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;
|
||||||
}
|
}
|
||||||
server->config_options[ind] = mg_strdup(value);
|
|
||||||
DBG(("%s [%s]", name, value));
|
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) {
|
if (ind == LISTENING_PORT) {
|
||||||
int port = ns_bind(&server->ns_server, value);
|
int port = ns_bind(&server->ns_server, value);
|
||||||
@@ -4435,8 +4446,8 @@ const char *mg_set_option(struct mg_server *server, const char *name,
|
|||||||
if (!strcmp(value, "0")) {
|
if (!strcmp(value, "0")) {
|
||||||
char buf[10];
|
char buf[10];
|
||||||
mg_snprintf(buf, sizeof(buf), "%d", port);
|
mg_snprintf(buf, sizeof(buf), "%d", port);
|
||||||
free(server->config_options[ind]);
|
free(*v);
|
||||||
server->config_options[ind] = mg_strdup(buf);
|
*v = mg_strdup(buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
@@ -4462,7 +4473,6 @@ const char *mg_set_option(struct mg_server *server, const char *name,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return error_msg;
|
return error_msg;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user