diff --git a/src/shrpx.cc b/src/shrpx.cc index a53da086..19516c81 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -1088,7 +1088,8 @@ int main(int argc, char **argv) } if(!get_config()->npn_list) { - parse_config_npn_list(DEFAULT_NPN_LIST); + mod_config()->npn_list = parse_config_str_list(&mod_config()->npn_list_len, + DEFAULT_NPN_LIST); } if(!get_config()->subcerts.empty()) { diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index 902d6949..b60e02ac 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -221,15 +221,13 @@ void set_config_str(char **destp, const char *val) *destp = strdup(val); } -int parse_config_npn_list(const char *s) +char** parse_config_str_list(size_t *outlen, const char *s) { - delete [] mod_config()->npn_list; size_t len = 1; for(const char *first = s, *p = nullptr; (p = strchr(first, ',')); ++len, first = p + 1); auto list = new char*[len]; - auto t = strdup(s); - auto first = t; + auto first = strdup(s); len = 0; for(;;) { auto p = strchr(first, ','); @@ -241,9 +239,8 @@ int parse_config_npn_list(const char *s) first = p + 1; } list[len++] = first; - mod_config()->npn_list = list; - mod_config()->npn_list_len = len; - return 0; + *outlen = len; + return list; } int parse_config(const char *opt, const char *optarg) @@ -453,7 +450,9 @@ int parse_config(const char *opt, const char *optarg) } else if(util::strieq(opt, SHRPX_OPT_WRITE_BURST)) { mod_config()->write_burst = strtoul(optarg, nullptr, 10); } else if(util::strieq(opt, SHRPX_OPT_NPN_LIST)) { - parse_config_npn_list(optarg); + delete [] mod_config()->npn_list; + mod_config()->npn_list = parse_config_str_list(&mod_config()->npn_list_len, + optarg); } else if(util::strieq(opt, SHRPX_OPT_VERIFY_CLIENT)) { mod_config()->verify_client = util::strieq(optarg, "yes"); } else if(util::strieq(opt, SHRPX_OPT_VERIFY_CLIENT_CACERT)) { diff --git a/src/shrpx_config.h b/src/shrpx_config.h index 8d112e7b..6534a0db 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -226,13 +226,16 @@ int load_config(const char *filename); // Read passwd from |filename| std::string read_passwd_from_file(const char *filename); -// Parses NPN protocol strings in |s| and stores the protocols list in -// mod_config()->npn_list and assigns the number of elements in -// mod_config()->npn_list_len. The |s| must be comma delimited list of -// protocol strings. The strings must be delimited by a single command -// and any white spaces around it are treated as a part of protocol -// strings. This function always succeeds and returns 0. -int parse_config_npn_list(const char *s); +// Parses comma delimited strings in |s| and returns the array of +// pointers, each element points to the each substring in |s|. The +// number of elements are stored in |*outlen|. The |s| must be comma +// delimited list of strings. The strings must be delimited by a +// single comma and any white spaces around it are treated as a part +// of protocol strings. This function may modify |s| and the caller +// must leave it as is after this call. This function allocates memory +// to store the parsed strings and it is caller's responsibility to +// deallocate the memory. +char** parse_config_str_list(size_t *outlen, const char *s); // Copies NULL-terminated string |val| to |*destp|. If |*destp| is not // NULL, it is freed before copying.