src: Add util::strieq_l

This commit is contained in:
Tatsuhiro Tsujikawa 2015-02-22 15:32:48 +09:00
parent 997f9233bc
commit e457c9a414
7 changed files with 48 additions and 24 deletions

View File

@ -1087,7 +1087,7 @@ int hd_on_frame_recv_callback(nghttp2_session *session,
auto expect100 =
http2::get_header(stream->hdidx, http2::HD_EXPECT, stream->headers);
if (expect100 && util::strieq("100-continue", expect100->value.c_str())) {
if (expect100 && util::strieq_l("100-continue", expect100->value)) {
hd->submit_non_final_response("100", frame->hd.stream_id);
}

View File

@ -1596,8 +1596,8 @@ void check_response_header(nghttp2_session *session, Request *req) {
for (auto &nv : req->res_nva) {
if ("content-encoding" == nv.name) {
gzip =
util::strieq("gzip", nv.value) || util::strieq("deflate", nv.value);
gzip = util::strieq_l("gzip", nv.value) ||
util::strieq_l("deflate", nv.value);
continue;
}
}
@ -2532,7 +2532,7 @@ int main(int argc, char **argv) {
}
// To test "never index" repr, don't index authorization header
// field unconditionally.
auto no_index = util::strieq("authorization", header);
auto no_index = util::strieq_l("authorization", header);
config.headers.emplace_back(header, value, no_index);
util::inp_strlower(config.headers.back().name);
break;

View File

@ -442,31 +442,31 @@ std::vector<LogFragment> parse_log_format(const char *optarg) {
const char *value = nullptr;
size_t valuelen = 0;
if (util::strieq("$remote_addr", var_start, varlen)) {
if (util::strieq_l("$remote_addr", var_start, varlen)) {
type = SHRPX_LOGF_REMOTE_ADDR;
} else if (util::strieq("$time_local", var_start, varlen)) {
} else if (util::strieq_l("$time_local", var_start, varlen)) {
type = SHRPX_LOGF_TIME_LOCAL;
} else if (util::strieq("$time_iso8601", var_start, varlen)) {
} else if (util::strieq_l("$time_iso8601", var_start, varlen)) {
type = SHRPX_LOGF_TIME_ISO8601;
} else if (util::strieq("$request", var_start, varlen)) {
} else if (util::strieq_l("$request", var_start, varlen)) {
type = SHRPX_LOGF_REQUEST;
} else if (util::strieq("$status", var_start, varlen)) {
} else if (util::strieq_l("$status", var_start, varlen)) {
type = SHRPX_LOGF_STATUS;
} else if (util::strieq("$body_bytes_sent", var_start, varlen)) {
} else if (util::strieq_l("$body_bytes_sent", var_start, varlen)) {
type = SHRPX_LOGF_BODY_BYTES_SENT;
} else if (util::istartsWith(var_start, varlen, "$http_")) {
type = SHRPX_LOGF_HTTP;
value = var_start + sizeof("$http_") - 1;
valuelen = varlen - (sizeof("$http_") - 1);
} else if (util::strieq("$remote_port", var_start, varlen)) {
} else if (util::strieq_l("$remote_port", var_start, varlen)) {
type = SHRPX_LOGF_REMOTE_PORT;
} else if (util::strieq("$server_port", var_start, varlen)) {
} else if (util::strieq_l("$server_port", var_start, varlen)) {
type = SHRPX_LOGF_SERVER_PORT;
} else if (util::strieq("$request_time", var_start, varlen)) {
} else if (util::strieq_l("$request_time", var_start, varlen)) {
type = SHRPX_LOGF_REQUEST_TIME;
} else if (util::strieq("$pid", var_start, varlen)) {
} else if (util::strieq_l("$pid", var_start, varlen)) {
type = SHRPX_LOGF_PID;
} else if (util::strieq("$alpn", var_start, varlen)) {
} else if (util::strieq_l("$alpn", var_start, varlen)) {
type = SHRPX_LOGF_ALPN;
} else {
LOG(WARN) << "Unrecognized log format variable: "

View File

@ -383,7 +383,7 @@ int Http2DownstreamConnection::push_request_headers() {
auto transfer_encoding =
downstream_->get_request_header(http2::HD_TRANSFER_ENCODING);
if (transfer_encoding &&
util::strieq((*transfer_encoding).value.c_str(), "chunked")) {
util::strieq_l("chunked", (*transfer_encoding).value)) {
chunked_encoding = true;
}

View File

@ -381,13 +381,6 @@ bool istartsWith(const char *a, const char *b) {
return !*b;
}
bool strieq(const std::string &a, const std::string &b) {
if (a.size() != b.size()) {
return false;
}
return std::equal(std::begin(a), std::end(a), std::begin(b), CaseCmp());
}
bool strieq(const char *a, const char *b) {
if (!a || !b) {
return false;

View File

@ -301,10 +301,29 @@ template <typename InputIt> bool strieq(const char *a, InputIt b, size_t bn) {
return !*a && b == blast;
}
bool strieq(const std::string &a, const std::string &b);
template <typename InputIt1, typename InputIt2>
bool strieq(InputIt1 a, size_t alen, InputIt2 b, size_t blen) {
if (alen != blen) {
return false;
}
return std::equal(a, a + alen, b, CaseCmp());
}
inline bool strieq(const std::string &a, const std::string &b) {
return strieq(std::begin(a), a.size(), std::begin(b), b.size());
}
bool strieq(const char *a, const char *b);
template <typename InputIt, size_t N>
bool strieq_l(const char (&a)[N], InputIt b, size_t blen) {
return strieq(a, N - 1, b, blen);
}
template <size_t N> bool strieq_l(const char (&a)[N], const std::string &b) {
return strieq(a, N - 1, std::begin(b), b.size());
}
template <typename InputIt> bool streq(const char *a, InputIt b, size_t bn) {
if (!a) {
return false;

View File

@ -84,6 +84,18 @@ void test_util_strieq(void) {
CU_ASSERT(util::strieq("", ""));
CU_ASSERT(!util::strieq("alpha", "AlPhA "));
CU_ASSERT(!util::strieq("", "AlPhA "));
CU_ASSERT(util::strieq_l("alpha", "alpha", 5));
CU_ASSERT(util::strieq_l("alpha", "AlPhA", 5));
CU_ASSERT(util::strieq_l("", static_cast<const char *>(nullptr), 0));
CU_ASSERT(!util::strieq_l("alpha", "AlPhA ", 6));
CU_ASSERT(!util::strieq_l("", "AlPhA ", 6));
CU_ASSERT(util::strieq_l("alpha", "alpha"));
CU_ASSERT(util::strieq_l("alpha", "AlPhA"));
CU_ASSERT(util::strieq_l("", ""));
CU_ASSERT(!util::strieq_l("alpha", "AlPhA "));
CU_ASSERT(!util::strieq_l("", "AlPhA "));
}
void test_util_inp_strlower(void) {