nghttpx: Supply template version strcopy

This commit is contained in:
Tatsuhiro Tsujikawa 2015-07-14 22:40:33 +09:00
parent 7f7b6d641d
commit 0a6877d091
2 changed files with 25 additions and 32 deletions

View File

@ -59,8 +59,6 @@
#include "template.h"
#include "base64.h"
using namespace nghttp2;
namespace shrpx {
namespace {
@ -227,21 +225,6 @@ std::string read_passwd_from_file(const char *filename) {
return line;
}
std::unique_ptr<char[]> strcopy(const char *val) {
return strcopy(val, strlen(val));
}
std::unique_ptr<char[]> strcopy(const char *val, size_t len) {
auto res = make_unique<char[]>(len + 1);
memcpy(res.get(), val, len);
res[len] = '\0';
return res;
}
std::unique_ptr<char[]> strcopy(const std::string &val) {
return strcopy(val.c_str(), val.size());
}
std::vector<char *> parse_config_str_list(const char *s, char delim) {
size_t len = 1;
for (const char *first = s, *p = nullptr; (p = strchr(first, delim));
@ -394,7 +377,6 @@ std::vector<LogFragment> parse_log_format(const char *optarg) {
auto type = SHRPX_LOGF_NONE;
const char *value = nullptr;
size_t valuelen = 0;
if (util::strieq_l("remote_addr", var_name, var_namelen)) {
type = SHRPX_LOGF_REMOTE_ADDR;
@ -411,7 +393,6 @@ std::vector<LogFragment> parse_log_format(const char *optarg) {
} else if (util::istartsWith(var_name, var_namelen, "http_")) {
type = SHRPX_LOGF_HTTP;
value = var_name + sizeof("http_") - 1;
valuelen = var_namelen - (sizeof("http_") - 1);
} else if (util::strieq_l("remote_port", var_name, var_namelen)) {
type = SHRPX_LOGF_REMOTE_PORT;
} else if (util::strieq_l("server_port", var_name, var_namelen)) {
@ -437,9 +418,8 @@ std::vector<LogFragment> parse_log_format(const char *optarg) {
}
if (literal_start < var_start) {
res.push_back(
make_log_fragment(SHRPX_LOGF_LITERAL,
strcopy(literal_start, var_start - literal_start)));
res.push_back(make_log_fragment(SHRPX_LOGF_LITERAL,
strcopy(literal_start, var_start)));
}
literal_start = p;
@ -449,7 +429,8 @@ std::vector<LogFragment> parse_log_format(const char *optarg) {
continue;
}
res.push_back(make_log_fragment(type, strcopy(value, valuelen)));
res.push_back(
make_log_fragment(type, strcopy(value, var_name + var_namelen)));
auto &v = res.back().value;
for (size_t i = 0; v[i]; ++i) {
if (v[i] == '_') {
@ -459,8 +440,8 @@ std::vector<LogFragment> parse_log_format(const char *optarg) {
}
if (literal_start != eop) {
res.push_back(make_log_fragment(
SHRPX_LOGF_LITERAL, strcopy(literal_start, eop - literal_start)));
res.push_back(
make_log_fragment(SHRPX_LOGF_LITERAL, strcopy(literal_start, eop)));
}
return res;
@ -537,7 +518,7 @@ int parse_config(const char *opt, const char *optarg,
DownstreamAddr addr;
if (util::istartsWith(optarg, SHRPX_UNIX_PATH_PREFIX)) {
auto path = optarg + str_size(SHRPX_UNIX_PATH_PREFIX);
addr.host = strcopy(path, pat_delim - path);
addr.host = strcopy(path, pat_delim);
addr.host_unix = true;
} else {
if (split_host_port(host, sizeof(host), &port, optarg,

View File

@ -50,6 +50,10 @@
#include <nghttp2/nghttp2.h>
#include "template.h"
using namespace nghttp2;
namespace shrpx {
struct LogFragment;
@ -416,15 +420,23 @@ std::pair<std::string, std::string> parse_header(const char *optarg);
std::vector<LogFragment> parse_log_format(const char *optarg);
// Returns a copy of NULL-terminated string |val|.
std::unique_ptr<char[]> strcopy(const char *val);
// Returns a copy of NULL-terminated string [first, last).
template <typename InputIt>
std::unique_ptr<char[]> strcopy(InputIt first, InputIt last) {
auto res = make_unique<char[]>(last - first + 1);
*std::copy(first, last, res.get()) = '\0';
return res;
}
// Returns a copy of string |val| of length |n|. The returned string
// will be NULL-terminated.
std::unique_ptr<char[]> strcopy(const char *val, size_t n);
// Returns a copy of NULL-terminated string |val|.
inline std::unique_ptr<char[]> strcopy(const char *val) {
return strcopy(val, val + strlen(val));
}
// Returns a copy of val.c_str().
std::unique_ptr<char[]> strcopy(const std::string &val);
inline std::unique_ptr<char[]> strcopy(const std::string &val) {
return strcopy(std::begin(val), std::end(val));
}
// Returns string for syslog |facility|.
const char *str_syslog_facility(int facility);