Commit 13596bde authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

nghttpx: Refactor option handling using StringRef

parent daa1ae3a
......@@ -1956,7 +1956,7 @@ int main(int argc, char **argv) {
break;
case 4:
// npn-list option
config.npn_list = util::parse_config_str_list(optarg);
config.npn_list = util::parse_config_str_list(StringRef{optarg});
break;
case 5:
// rate-period
......@@ -1968,7 +1968,8 @@ int main(int argc, char **argv) {
break;
case 6:
// --h1
config.npn_list = util::parse_config_str_list("http/1.1");
config.npn_list =
util::parse_config_str_list(StringRef::from_lit("http/1.1"));
config.no_tls_proto = Config::PROTO_HTTP1_1;
break;
}
......@@ -1992,7 +1993,8 @@ int main(int argc, char **argv) {
}
if (config.npn_list.empty()) {
config.npn_list = util::parse_config_str_list(DEFAULT_NPN_LIST);
config.npn_list =
util::parse_config_str_list(StringRef::from_lit(DEFAULT_NPN_LIST));
}
// serialize the APLN tokens
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -53,7 +53,10 @@ using namespace nghttp2;
namespace shrpx {
namespace {
const char *SEVERITY_STR[] = {"INFO", "NOTICE", "WARN", "ERROR", "FATAL"};
const StringRef SEVERITY_STR[] = {
StringRef::from_lit("INFO"), StringRef::from_lit("NOTICE"),
StringRef::from_lit("WARN"), StringRef::from_lit("ERROR"),
StringRef::from_lit("FATAL")};
} // namespace
namespace {
......@@ -70,9 +73,9 @@ int Log::severity_thres_ = NOTICE;
void Log::set_severity_level(int severity) { severity_thres_ = severity; }
int Log::set_severity_level_by_name(const char *name) {
int Log::set_severity_level_by_name(const StringRef &name) {
for (size_t i = 0, max = array_size(SEVERITY_STR); i < max; ++i) {
if (strcmp(SEVERITY_STR[i], name) == 0) {
if (name == SEVERITY_STR[i]) {
severity_thres_ = i;
return 0;
}
......@@ -119,10 +122,10 @@ Log::~Log() {
if (errorconf.syslog) {
if (severity_ == NOTICE) {
syslog(severity_to_syslog_level(severity_), "[%s] %s",
SEVERITY_STR[severity_], stream_.str().c_str());
SEVERITY_STR[severity_].c_str(), stream_.str().c_str());
} else {
syslog(severity_to_syslog_level(severity_), "[%s] %s (%s:%d)",
SEVERITY_STR[severity_], stream_.str().c_str(), filename_,
SEVERITY_STR[severity_].c_str(), stream_.str().c_str(), filename_,
linenum_);
}
......@@ -136,16 +139,18 @@ Log::~Log() {
auto &time_local = lgconf->time_local_str;
if (severity_ == NOTICE) {
rv = snprintf(buf, sizeof(buf), "%s PID%d [%s%s%s] %s\n",
time_local.c_str(), get_config()->pid,
tty ? SEVERITY_COLOR[severity_] : "", SEVERITY_STR[severity_],
tty ? "\033[0m" : "", stream_.str().c_str());
rv =
snprintf(buf, sizeof(buf), "%s PID%d [%s%s%s] %s\n", time_local.c_str(),
get_config()->pid, tty ? SEVERITY_COLOR[severity_] : "",
SEVERITY_STR[severity_].c_str(), tty ? "\033[0m" : "",
stream_.str().c_str());
} else {
rv = snprintf(buf, sizeof(buf), "%s PID%d [%s%s%s] %s%s:%d%s %s\n",
time_local.c_str(), get_config()->pid,
tty ? SEVERITY_COLOR[severity_] : "", SEVERITY_STR[severity_],
tty ? "\033[0m" : "", tty ? "\033[1;30m" : "", filename_,
linenum_, tty ? "\033[0m" : "", stream_.str().c_str());
tty ? SEVERITY_COLOR[severity_] : "",
SEVERITY_STR[severity_].c_str(), tty ? "\033[0m" : "",
tty ? "\033[1;30m" : "", filename_, linenum_,
tty ? "\033[0m" : "", stream_.str().c_str());
}
if (rv < 0) {
......
......@@ -97,7 +97,7 @@ public:
return *this;
}
static void set_severity_level(int severity);
static int set_severity_level_by_name(const char *name);
static int set_severity_level_by_name(const StringRef &name);
static bool log_enabled(int severity) { return severity >= severity_thres_; }
private:
......
......@@ -878,34 +878,12 @@ std::vector<StringRef> split_str(const StringRef &s, char delim) {
return list;
}
std::vector<Range<const char *>> split_config_str_list(const char *s,
char delim) {
size_t len = 1;
auto last = s + strlen(s);
for (const char *first = s, *d = nullptr;
(d = std::find(first, last, delim)) != last; ++len, first = d + 1)
;
auto list = std::vector<Range<const char *>>(len);
len = 0;
for (auto first = s;; ++len) {
auto stop = std::find(first, last, delim);
list[len] = {first, stop};
if (stop == last) {
break;
}
first = stop + 1;
}
return list;
}
std::vector<std::string> parse_config_str_list(const char *s, char delim) {
auto ranges = split_config_str_list(s, delim);
std::vector<std::string> parse_config_str_list(const StringRef &s, char delim) {
auto sublist = split_str(s, delim);
auto res = std::vector<std::string>();
res.reserve(ranges.size());
for (const auto &range : ranges) {
res.emplace_back(range.first, range.second);
res.reserve(sublist.size());
for (const auto &s : sublist) {
res.emplace_back(std::begin(s), std::end(s));
}
return res;
}
......@@ -1011,9 +989,16 @@ std::pair<int64_t, size_t> parse_uint_digits(const void *ss, size_t len) {
} // namespace
int64_t parse_uint_with_unit(const char *s) {
return parse_uint_with_unit(reinterpret_cast<const uint8_t *>(s), strlen(s));
}
int64_t parse_uint_with_unit(const StringRef &s) {
return parse_uint_with_unit(s.byte(), s.size());
}
int64_t parse_uint_with_unit(const uint8_t *s, size_t len) {
int64_t n;
size_t i;
auto len = strlen(s);
std::tie(n, i) = parse_uint_digits(s, len);
if (n == -1) {
return -1;
......@@ -1071,10 +1056,19 @@ int64_t parse_uint(const uint8_t *s, size_t len) {
}
double parse_duration_with_unit(const char *s) {
return parse_duration_with_unit(reinterpret_cast<const uint8_t *>(s),
strlen(s));
}
double parse_duration_with_unit(const StringRef &s) {
return parse_duration_with_unit(s.byte(), s.size());
}
double parse_duration_with_unit(const uint8_t *s, size_t len) {
constexpr auto max = std::numeric_limits<int64_t>::max();
int64_t n;
size_t i;
auto len = strlen(s);
std::tie(n, i) = parse_uint_digits(s, len);
if (n == -1) {
goto fail;
......
......@@ -584,19 +584,11 @@ bool select_protocol(const unsigned char **out, unsigned char *outlen,
// HTTP/2 protocol identifier.
std::vector<unsigned char> get_default_alpn();
template <typename T> using Range = std::pair<T, T>;
// Parses delimited strings in |s| and returns the array of substring,
// delimited by |delim|. The any white spaces around substring are
// treated as a part of substring.
std::vector<std::string> parse_config_str_list(const char *s, char delim = ',');
// Parses delimited strings in |s| and returns the array of pointers,
// each element points to the beginning and one beyond last of
// substring in |s|. The delimiter is given by |delim|. The any
// white spaces around substring are treated as a part of substring.
std::vector<Range<const char *>> split_config_str_list(const char *s,
char delim);
std::vector<std::string> parse_config_str_list(const StringRef &s,
char delim = ',');
// Parses delimited strings in |s| and returns Substrings in |s|
// delimited by |delim|. The any white spaces around substring are
......@@ -653,10 +645,14 @@ bool ipv6_numeric_addr(const char *host);
// 1024 and 1024 * 1024 respectively. If there is an error, returns
// -1.
int64_t parse_uint_with_unit(const char *s);
// The following overload does not require |s| is NULL terminated.
int64_t parse_uint_with_unit(const uint8_t *s, size_t len);
int64_t parse_uint_with_unit(const StringRef &s);
// Parses NULL terminated string |s| as unsigned integer and returns
// the parsed integer. If there is an error, returns -1.
int64_t parse_uint(const char *s);
// The following overload does not require |s| is NULL terminated.
int64_t parse_uint(const uint8_t *s, size_t len);
int64_t parse_uint(const std::string &s);
int64_t parse_uint(const StringRef &s);
......@@ -669,6 +665,9 @@ int64_t parse_uint(const StringRef &s);
// unit is second. This function returns
// std::numeric_limits<double>::infinity() if error occurs.
double parse_duration_with_unit(const char *s);
// The following overload does not require |s| is NULL terminated.
double parse_duration_with_unit(const uint8_t *s, size_t len);
double parse_duration_with_unit(const StringRef &s);
// Returns string representation of time duration |t|. If t has
// fractional part (at least more than or equal to 1e-3), |t| is
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment