From 9302e3edf4a7246f2ba2966e26aac7cdf011934d Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 20 Feb 2015 23:57:40 +0900 Subject: [PATCH] src: Use util::streq_l to compare against string literal --- genheaderfunc.py | 2 +- src/asio_http2_handler.cc | 10 +++---- src/h2load.cc | 2 +- src/http2.cc | 60 +++++++++++++++++++-------------------- src/shrpx_downstream.cc | 8 +++--- src/util_test.cc | 37 +++++++++++++----------- 6 files changed, 62 insertions(+), 57 deletions(-) diff --git a/genheaderfunc.py b/genheaderfunc.py index d8111d4c..1efe3ccf 100755 --- a/genheaderfunc.py +++ b/genheaderfunc.py @@ -84,7 +84,7 @@ int lookup_token(const uint8_t *name, size_t namelen) { case '{}':'''.format(c) for k in headers: print '''\ - if (util::streq("{}", name, {})) {{ + if (util::streq_l("{}", name, {})) {{ return {}; }}'''.format(k[:-1], size - 1, to_enum_hd(k)) print '''\ diff --git a/src/asio_http2_handler.cc b/src/asio_http2_handler.cc index c08345b5..f491e320 100644 --- a/src/asio_http2_handler.cc +++ b/src/asio_http2_handler.cc @@ -350,25 +350,25 @@ int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame, return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; } - if (util::streq(":method", name, namelen)) { + if (util::streq_l(":method", name, namelen)) { if (!req.method().empty()) { stream_error(session, stream_id, NGHTTP2_PROTOCOL_ERROR); return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; } req.method(std::string(value, value + valuelen)); - } else if (util::streq(":scheme", name, namelen)) { + } else if (util::streq_l(":scheme", name, namelen)) { if (!req.scheme().empty()) { stream_error(session, stream_id, NGHTTP2_PROTOCOL_ERROR); return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; } req.scheme(std::string(value, value + valuelen)); - } else if (util::streq(":authority", name, namelen)) { + } else if (util::streq_l(":authority", name, namelen)) { if (!req.authority().empty()) { stream_error(session, stream_id, NGHTTP2_PROTOCOL_ERROR); return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; } req.authority(std::string(value, value + valuelen)); - } else if (util::streq(":path", name, namelen)) { + } else if (util::streq_l(":path", name, namelen)) { if (!req.path().empty()) { stream_error(session, stream_id, NGHTTP2_PROTOCOL_ERROR); return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; @@ -380,7 +380,7 @@ int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame, return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; } - if (util::streq("host", name, namelen)) { + if (util::streq_l("host", name, namelen)) { req.host(std::string(value, value + valuelen)); } diff --git a/src/h2load.cc b/src/h2load.cc index a66bf16a..cad8439c 100644 --- a/src/h2load.cc +++ b/src/h2load.cc @@ -336,7 +336,7 @@ void Client::on_header(int32_t stream_id, const uint8_t *name, size_t namelen, } auto &stream = (*itr).second; if (stream.status_success == -1 && namelen == 7 && - util::streq(":status", 7, name, namelen)) { + util::streq_l(":status", name, namelen)) { int status = 0; for (size_t i = 0; i < valuelen; ++i) { if ('0' <= value[i] && value[i] <= '9') { diff --git a/src/http2.cc b/src/http2.cc index de14668e..0aeb0370 100644 --- a/src/http2.cc +++ b/src/http2.cc @@ -407,7 +407,7 @@ int lookup_token(const uint8_t *name, size_t namelen) { case 2: switch (name[namelen - 1]) { case 'e': - if (util::streq("t", name, 1)) { + if (util::streq_l("t", name, 1)) { return HD_TE; } break; @@ -416,7 +416,7 @@ int lookup_token(const uint8_t *name, size_t namelen) { case 3: switch (name[namelen - 1]) { case 'a': - if (util::streq("vi", name, 2)) { + if (util::streq_l("vi", name, 2)) { return HD_VIA; } break; @@ -425,12 +425,12 @@ int lookup_token(const uint8_t *name, size_t namelen) { case 4: switch (name[namelen - 1]) { case 'k': - if (util::streq("lin", name, 3)) { + if (util::streq_l("lin", name, 3)) { return HD_LINK; } break; case 't': - if (util::streq("hos", name, 3)) { + if (util::streq_l("hos", name, 3)) { return HD_HOST; } break; @@ -439,12 +439,12 @@ int lookup_token(const uint8_t *name, size_t namelen) { case 5: switch (name[namelen - 1]) { case 'h': - if (util::streq(":pat", name, 4)) { + if (util::streq_l(":pat", name, 4)) { return HD__PATH; } break; case 't': - if (util::streq(":hos", name, 4)) { + if (util::streq_l(":hos", name, 4)) { return HD__HOST; } break; @@ -453,17 +453,17 @@ int lookup_token(const uint8_t *name, size_t namelen) { case 6: switch (name[namelen - 1]) { case 'e': - if (util::streq("cooki", name, 5)) { + if (util::streq_l("cooki", name, 5)) { return HD_COOKIE; } break; case 'r': - if (util::streq("serve", name, 5)) { + if (util::streq_l("serve", name, 5)) { return HD_SERVER; } break; case 't': - if (util::streq("expec", name, 5)) { + if (util::streq_l("expec", name, 5)) { return HD_EXPECT; } break; @@ -472,30 +472,30 @@ int lookup_token(const uint8_t *name, size_t namelen) { case 7: switch (name[namelen - 1]) { case 'c': - if (util::streq("alt-sv", name, 6)) { + if (util::streq_l("alt-sv", name, 6)) { return HD_ALT_SVC; } break; case 'd': - if (util::streq(":metho", name, 6)) { + if (util::streq_l(":metho", name, 6)) { return HD__METHOD; } break; case 'e': - if (util::streq(":schem", name, 6)) { + if (util::streq_l(":schem", name, 6)) { return HD__SCHEME; } - if (util::streq("upgrad", name, 6)) { + if (util::streq_l("upgrad", name, 6)) { return HD_UPGRADE; } break; case 'r': - if (util::streq("traile", name, 6)) { + if (util::streq_l("traile", name, 6)) { return HD_TRAILER; } break; case 's': - if (util::streq(":statu", name, 6)) { + if (util::streq_l(":statu", name, 6)) { return HD__STATUS; } break; @@ -504,7 +504,7 @@ int lookup_token(const uint8_t *name, size_t namelen) { case 8: switch (name[namelen - 1]) { case 'n': - if (util::streq("locatio", name, 7)) { + if (util::streq_l("locatio", name, 7)) { return HD_LOCATION; } break; @@ -513,22 +513,22 @@ int lookup_token(const uint8_t *name, size_t namelen) { case 10: switch (name[namelen - 1]) { case 'e': - if (util::streq("keep-aliv", name, 9)) { + if (util::streq_l("keep-aliv", name, 9)) { return HD_KEEP_ALIVE; } break; case 'n': - if (util::streq("connectio", name, 9)) { + if (util::streq_l("connectio", name, 9)) { return HD_CONNECTION; } break; case 't': - if (util::streq("user-agen", name, 9)) { + if (util::streq_l("user-agen", name, 9)) { return HD_USER_AGENT; } break; case 'y': - if (util::streq(":authorit", name, 9)) { + if (util::streq_l(":authorit", name, 9)) { return HD__AUTHORITY; } break; @@ -537,7 +537,7 @@ int lookup_token(const uint8_t *name, size_t namelen) { case 13: switch (name[namelen - 1]) { case 'l': - if (util::streq("cache-contro", name, 12)) { + if (util::streq_l("cache-contro", name, 12)) { return HD_CACHE_CONTROL; } break; @@ -546,12 +546,12 @@ int lookup_token(const uint8_t *name, size_t namelen) { case 14: switch (name[namelen - 1]) { case 'h': - if (util::streq("content-lengt", name, 13)) { + if (util::streq_l("content-lengt", name, 13)) { return HD_CONTENT_LENGTH; } break; case 's': - if (util::streq("http2-setting", name, 13)) { + if (util::streq_l("http2-setting", name, 13)) { return HD_HTTP2_SETTINGS; } break; @@ -560,17 +560,17 @@ int lookup_token(const uint8_t *name, size_t namelen) { case 15: switch (name[namelen - 1]) { case 'e': - if (util::streq("accept-languag", name, 14)) { + if (util::streq_l("accept-languag", name, 14)) { return HD_ACCEPT_LANGUAGE; } break; case 'g': - if (util::streq("accept-encodin", name, 14)) { + if (util::streq_l("accept-encodin", name, 14)) { return HD_ACCEPT_ENCODING; } break; case 'r': - if (util::streq("x-forwarded-fo", name, 14)) { + if (util::streq_l("x-forwarded-fo", name, 14)) { return HD_X_FORWARDED_FOR; } break; @@ -579,7 +579,7 @@ int lookup_token(const uint8_t *name, size_t namelen) { case 16: switch (name[namelen - 1]) { case 'n': - if (util::streq("proxy-connectio", name, 15)) { + if (util::streq_l("proxy-connectio", name, 15)) { return HD_PROXY_CONNECTION; } break; @@ -588,17 +588,17 @@ int lookup_token(const uint8_t *name, size_t namelen) { case 17: switch (name[namelen - 1]) { case 'e': - if (util::streq("if-modified-sinc", name, 16)) { + if (util::streq_l("if-modified-sinc", name, 16)) { return HD_IF_MODIFIED_SINCE; } break; case 'g': - if (util::streq("transfer-encodin", name, 16)) { + if (util::streq_l("transfer-encodin", name, 16)) { return HD_TRANSFER_ENCODING; } break; case 'o': - if (util::streq("x-forwarded-prot", name, 16)) { + if (util::streq_l("x-forwarded-prot", name, 16)) { return HD_X_FORWARDED_PROTO; } break; diff --git a/src/shrpx_downstream.cc b/src/shrpx_downstream.cc index eb574063..831a3027 100644 --- a/src/shrpx_downstream.cc +++ b/src/shrpx_downstream.cc @@ -238,7 +238,7 @@ void Downstream::assemble_request_cookie() { cookie = ""; for (auto &kv : request_headers_) { if (kv.name.size() != 6 || kv.name[5] != 'e' || - !util::streq("cooki", kv.name.c_str(), 5)) { + !util::streq_l("cooki", kv.name.c_str(), 5)) { continue; } @@ -259,7 +259,7 @@ Headers Downstream::crumble_request_cookie() { Headers cookie_hdrs; for (auto &kv : request_headers_) { if (kv.name.size() != 6 || kv.name[5] != 'e' || - !util::streq("cooki", kv.name.c_str(), 5)) { + !util::streq_l("cooki", kv.name.c_str(), 5)) { continue; } size_t last = kv.value.size(); @@ -813,8 +813,8 @@ void Downstream::inspect_http1_request() { auto &val = request_headers_[idx].value; // TODO Perform more strict checking for upgrade headers - if (util::streq(NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, val.c_str(), - val.size())) { + if (util::streq_l(NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, val.c_str(), + val.size())) { http2_upgrade_seen_ = true; } } diff --git a/src/util_test.cc b/src/util_test.cc index 9e6dc624..b973fda6 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -39,26 +39,31 @@ using namespace nghttp2; namespace shrpx { void test_util_streq(void) { - CU_ASSERT(util::streq("alpha", (const uint8_t *)"alpha", 5)); - CU_ASSERT(util::streq("alpha", (const uint8_t *)"alphabravo", 5)); - CU_ASSERT(!util::streq("alpha", (const uint8_t *)"alphabravo", 6)); - CU_ASSERT(!util::streq("alphabravo", (const uint8_t *)"alpha", 5)); - CU_ASSERT(!util::streq("alpha", (const uint8_t *)"alphA", 5)); - CU_ASSERT(!util::streq("", (const uint8_t *)"a", 1)); - CU_ASSERT(util::streq("", (const uint8_t *)"", 0)); - CU_ASSERT(!util::streq("alpha", (const uint8_t *)"", 0)); + CU_ASSERT(util::streq("alpha", "alpha", 5)); + CU_ASSERT(util::streq("alpha", "alphabravo", 5)); + CU_ASSERT(!util::streq("alpha", "alphabravo", 6)); + CU_ASSERT(!util::streq("alphabravo", "alpha", 5)); + CU_ASSERT(!util::streq("alpha", "alphA", 5)); + CU_ASSERT(!util::streq("", "a", 1)); + CU_ASSERT(util::streq("", "", 0)); + CU_ASSERT(!util::streq("alpha", "", 0)); - CU_ASSERT( - util::streq((const uint8_t *)"alpha", 5, (const uint8_t *)"alpha", 5)); - CU_ASSERT( - !util::streq((const uint8_t *)"alpha", 4, (const uint8_t *)"alpha", 5)); - CU_ASSERT( - !util::streq((const uint8_t *)"alpha", 5, (const uint8_t *)"alpha", 4)); - CU_ASSERT( - !util::streq((const uint8_t *)"alpha", 5, (const uint8_t *)"alphA", 5)); + CU_ASSERT(util::streq("alpha", 5, "alpha", 5)); + CU_ASSERT(!util::streq("alpha", 4, "alpha", 5)); + CU_ASSERT(!util::streq("alpha", 5, "alpha", 4)); + CU_ASSERT(!util::streq("alpha", 5, "alphA", 5)); char *a = nullptr; char *b = nullptr; CU_ASSERT(util::streq(a, 0, b, 0)); + + CU_ASSERT(util::streq_l("alpha", "alpha", 5)); + CU_ASSERT(util::streq_l("alpha", "alphabravo", 5)); + CU_ASSERT(!util::streq_l("alpha", "alphabravo", 6)); + CU_ASSERT(!util::streq_l("alphabravo", "alpha", 5)); + CU_ASSERT(!util::streq_l("alpha", "alphA", 5)); + CU_ASSERT(!util::streq_l("", "a", 1)); + CU_ASSERT(util::streq_l("", "", 0)); + CU_ASSERT(!util::streq_l("alpha", "", 0)); } void test_util_strieq(void) {