From 79968c637424b2ac8ca384ca4edac289766e4773 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 25 Mar 2016 00:27:59 +0900 Subject: [PATCH] src: Rewrite strifind --- src/shrpx-unittest.cc | 1 + src/shrpx_http2_downstream_connection.cc | 2 +- src/util.cc | 16 +++------------- src/util.h | 4 +++- src/util_test.cc | 19 +++++++++++++++++++ src/util_test.h | 1 + 6 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/shrpx-unittest.cc b/src/shrpx-unittest.cc index 6a015778..df6d6b9c 100644 --- a/src/shrpx-unittest.cc +++ b/src/shrpx-unittest.cc @@ -171,6 +171,7 @@ int main(int argc, char *argv[]) { shrpx::test_util_make_http_hostport) || !CU_add_test(pSuite, "util_make_hostport", shrpx::test_util_make_hostport) || + !CU_add_test(pSuite, "util_strifind", shrpx::test_util_strifind) || !CU_add_test(pSuite, "gzip_inflate", test_nghttp2_gzip_inflate) || !CU_add_test(pSuite, "buffer_write", nghttp2::test_buffer_write) || !CU_add_test(pSuite, "pool_recycle", nghttp2::test_pool_recycle) || diff --git a/src/shrpx_http2_downstream_connection.cc b/src/shrpx_http2_downstream_connection.cc index 73a43a8d..a8d1978b 100644 --- a/src/shrpx_http2_downstream_connection.cc +++ b/src/shrpx_http2_downstream_connection.cc @@ -431,7 +431,7 @@ int Http2DownstreamConnection::push_request_headers() { // HTTP/1 upstream request can contain keyword other than // "trailers". We just forward "trailers". // TODO more strict handling required here. - if (te && util::strifind(te->value.c_str(), "trailers")) { + if (te && util::strifind(te->value, StringRef::from_lit("trailers"))) { nva.push_back(http2::make_nv_ll("te", "trailers")); } diff --git a/src/util.cc b/src/util.cc index 29f52ed8..e22d5f47 100644 --- a/src/util.cc +++ b/src/util.cc @@ -348,19 +348,9 @@ time_t parse_http_date(const StringRef &s) { return nghttp2_timegm_without_yday(&tm); } -bool strifind(const char *a, const char *b) { - if (!a || !b) { - return false; - } - for (size_t i = 0; a[i]; ++i) { - const char *ap = &a[i], *bp = b; - for (; *ap && *bp && lowcase(*ap) == lowcase(*bp); ++ap, ++bp) - ; - if (!*bp) { - return true; - } - } - return false; +bool strifind(const StringRef &a, const StringRef &b) { + return std::search(std::begin(a), std::end(a), std::begin(b), std::end(b), + CaseCmp()) != std::end(a); } char upcase(char c) { diff --git a/src/util.h b/src/util.h index 94bcf0da..52e25aae 100644 --- a/src/util.h +++ b/src/util.h @@ -334,7 +334,9 @@ bool streq_l(const CharT(&a)[N], const StringRef &b) { return streq(a, a + (N - 1), std::begin(b), std::end(b)); } -bool strifind(const char *a, const char *b); +// Returns true if |a| contains |b|. If both |a| and |b| are empty, +// this function returns false. +bool strifind(const StringRef &a, const StringRef &b); template void inp_strlower(InputIt first, InputIt last) { std::transform(first, last, first, lowcase); diff --git a/src/util_test.cc b/src/util_test.cc index 19cb0610..d1ee525b 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -498,4 +498,23 @@ void test_util_make_hostport(void) { util::make_hostport(StringRef::from_lit("::1"), 443)); } +void test_util_strifind(void) { + CU_ASSERT(util::strifind(StringRef::from_lit("gzip, deflate, bzip2"), + StringRef::from_lit("gzip"))); + + CU_ASSERT(util::strifind(StringRef::from_lit("gzip, deflate, bzip2"), + StringRef::from_lit("dEflate"))); + + CU_ASSERT(util::strifind(StringRef::from_lit("gzip, deflate, bzip2"), + StringRef::from_lit("BZIP2"))); + + CU_ASSERT(util::strifind(StringRef::from_lit("nghttp2"), StringRef{})); + + // Be aware this fact + CU_ASSERT(!util::strifind(StringRef{}, StringRef{})); + + CU_ASSERT(!util::strifind(StringRef::from_lit("nghttp2"), + StringRef::from_lit("http1"))); +} + } // namespace shrpx diff --git a/src/util_test.h b/src/util_test.h index b44d5b17..c7d7a8e7 100644 --- a/src/util_test.h +++ b/src/util_test.h @@ -61,6 +61,7 @@ void test_util_get_uint64(void); void test_util_parse_config_str_list(void); void test_util_make_http_hostport(void); void test_util_make_hostport(void); +void test_util_strifind(void); } // namespace shrpx