src: Rewrite strifind

This commit is contained in:
Tatsuhiro Tsujikawa 2016-03-25 00:27:59 +09:00
parent 1699aef609
commit 79968c6374
6 changed files with 28 additions and 15 deletions

View File

@ -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) ||

View File

@ -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"));
}

View File

@ -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) {

View File

@ -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 <typename InputIt> void inp_strlower(InputIt first, InputIt last) {
std::transform(first, last, first, lowcase);

View File

@ -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

View File

@ -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