src: Use util::streq_l to compare against string literal

This commit is contained in:
Tatsuhiro Tsujikawa 2015-02-20 23:57:40 +09:00
parent ab93a700ce
commit 9302e3edf4
6 changed files with 62 additions and 57 deletions

View File

@ -84,7 +84,7 @@ int lookup_token(const uint8_t *name, size_t namelen) {
case '{}':'''.format(c) case '{}':'''.format(c)
for k in headers: for k in headers:
print '''\ print '''\
if (util::streq("{}", name, {})) {{ if (util::streq_l("{}", name, {})) {{
return {}; return {};
}}'''.format(k[:-1], size - 1, to_enum_hd(k)) }}'''.format(k[:-1], size - 1, to_enum_hd(k))
print '''\ print '''\

View File

@ -350,25 +350,25 @@ int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame,
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
} }
if (util::streq(":method", name, namelen)) { if (util::streq_l(":method", name, namelen)) {
if (!req.method().empty()) { if (!req.method().empty()) {
stream_error(session, stream_id, NGHTTP2_PROTOCOL_ERROR); stream_error(session, stream_id, NGHTTP2_PROTOCOL_ERROR);
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
} }
req.method(std::string(value, value + valuelen)); 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()) { if (!req.scheme().empty()) {
stream_error(session, stream_id, NGHTTP2_PROTOCOL_ERROR); stream_error(session, stream_id, NGHTTP2_PROTOCOL_ERROR);
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
} }
req.scheme(std::string(value, value + valuelen)); 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()) { if (!req.authority().empty()) {
stream_error(session, stream_id, NGHTTP2_PROTOCOL_ERROR); stream_error(session, stream_id, NGHTTP2_PROTOCOL_ERROR);
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
} }
req.authority(std::string(value, value + valuelen)); 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()) { if (!req.path().empty()) {
stream_error(session, stream_id, NGHTTP2_PROTOCOL_ERROR); stream_error(session, stream_id, NGHTTP2_PROTOCOL_ERROR);
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; 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; 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)); req.host(std::string(value, value + valuelen));
} }

View File

@ -336,7 +336,7 @@ void Client::on_header(int32_t stream_id, const uint8_t *name, size_t namelen,
} }
auto &stream = (*itr).second; auto &stream = (*itr).second;
if (stream.status_success == -1 && namelen == 7 && if (stream.status_success == -1 && namelen == 7 &&
util::streq(":status", 7, name, namelen)) { util::streq_l(":status", name, namelen)) {
int status = 0; int status = 0;
for (size_t i = 0; i < valuelen; ++i) { for (size_t i = 0; i < valuelen; ++i) {
if ('0' <= value[i] && value[i] <= '9') { if ('0' <= value[i] && value[i] <= '9') {

View File

@ -407,7 +407,7 @@ int lookup_token(const uint8_t *name, size_t namelen) {
case 2: case 2:
switch (name[namelen - 1]) { switch (name[namelen - 1]) {
case 'e': case 'e':
if (util::streq("t", name, 1)) { if (util::streq_l("t", name, 1)) {
return HD_TE; return HD_TE;
} }
break; break;
@ -416,7 +416,7 @@ int lookup_token(const uint8_t *name, size_t namelen) {
case 3: case 3:
switch (name[namelen - 1]) { switch (name[namelen - 1]) {
case 'a': case 'a':
if (util::streq("vi", name, 2)) { if (util::streq_l("vi", name, 2)) {
return HD_VIA; return HD_VIA;
} }
break; break;
@ -425,12 +425,12 @@ int lookup_token(const uint8_t *name, size_t namelen) {
case 4: case 4:
switch (name[namelen - 1]) { switch (name[namelen - 1]) {
case 'k': case 'k':
if (util::streq("lin", name, 3)) { if (util::streq_l("lin", name, 3)) {
return HD_LINK; return HD_LINK;
} }
break; break;
case 't': case 't':
if (util::streq("hos", name, 3)) { if (util::streq_l("hos", name, 3)) {
return HD_HOST; return HD_HOST;
} }
break; break;
@ -439,12 +439,12 @@ int lookup_token(const uint8_t *name, size_t namelen) {
case 5: case 5:
switch (name[namelen - 1]) { switch (name[namelen - 1]) {
case 'h': case 'h':
if (util::streq(":pat", name, 4)) { if (util::streq_l(":pat", name, 4)) {
return HD__PATH; return HD__PATH;
} }
break; break;
case 't': case 't':
if (util::streq(":hos", name, 4)) { if (util::streq_l(":hos", name, 4)) {
return HD__HOST; return HD__HOST;
} }
break; break;
@ -453,17 +453,17 @@ int lookup_token(const uint8_t *name, size_t namelen) {
case 6: case 6:
switch (name[namelen - 1]) { switch (name[namelen - 1]) {
case 'e': case 'e':
if (util::streq("cooki", name, 5)) { if (util::streq_l("cooki", name, 5)) {
return HD_COOKIE; return HD_COOKIE;
} }
break; break;
case 'r': case 'r':
if (util::streq("serve", name, 5)) { if (util::streq_l("serve", name, 5)) {
return HD_SERVER; return HD_SERVER;
} }
break; break;
case 't': case 't':
if (util::streq("expec", name, 5)) { if (util::streq_l("expec", name, 5)) {
return HD_EXPECT; return HD_EXPECT;
} }
break; break;
@ -472,30 +472,30 @@ int lookup_token(const uint8_t *name, size_t namelen) {
case 7: case 7:
switch (name[namelen - 1]) { switch (name[namelen - 1]) {
case 'c': case 'c':
if (util::streq("alt-sv", name, 6)) { if (util::streq_l("alt-sv", name, 6)) {
return HD_ALT_SVC; return HD_ALT_SVC;
} }
break; break;
case 'd': case 'd':
if (util::streq(":metho", name, 6)) { if (util::streq_l(":metho", name, 6)) {
return HD__METHOD; return HD__METHOD;
} }
break; break;
case 'e': case 'e':
if (util::streq(":schem", name, 6)) { if (util::streq_l(":schem", name, 6)) {
return HD__SCHEME; return HD__SCHEME;
} }
if (util::streq("upgrad", name, 6)) { if (util::streq_l("upgrad", name, 6)) {
return HD_UPGRADE; return HD_UPGRADE;
} }
break; break;
case 'r': case 'r':
if (util::streq("traile", name, 6)) { if (util::streq_l("traile", name, 6)) {
return HD_TRAILER; return HD_TRAILER;
} }
break; break;
case 's': case 's':
if (util::streq(":statu", name, 6)) { if (util::streq_l(":statu", name, 6)) {
return HD__STATUS; return HD__STATUS;
} }
break; break;
@ -504,7 +504,7 @@ int lookup_token(const uint8_t *name, size_t namelen) {
case 8: case 8:
switch (name[namelen - 1]) { switch (name[namelen - 1]) {
case 'n': case 'n':
if (util::streq("locatio", name, 7)) { if (util::streq_l("locatio", name, 7)) {
return HD_LOCATION; return HD_LOCATION;
} }
break; break;
@ -513,22 +513,22 @@ int lookup_token(const uint8_t *name, size_t namelen) {
case 10: case 10:
switch (name[namelen - 1]) { switch (name[namelen - 1]) {
case 'e': case 'e':
if (util::streq("keep-aliv", name, 9)) { if (util::streq_l("keep-aliv", name, 9)) {
return HD_KEEP_ALIVE; return HD_KEEP_ALIVE;
} }
break; break;
case 'n': case 'n':
if (util::streq("connectio", name, 9)) { if (util::streq_l("connectio", name, 9)) {
return HD_CONNECTION; return HD_CONNECTION;
} }
break; break;
case 't': case 't':
if (util::streq("user-agen", name, 9)) { if (util::streq_l("user-agen", name, 9)) {
return HD_USER_AGENT; return HD_USER_AGENT;
} }
break; break;
case 'y': case 'y':
if (util::streq(":authorit", name, 9)) { if (util::streq_l(":authorit", name, 9)) {
return HD__AUTHORITY; return HD__AUTHORITY;
} }
break; break;
@ -537,7 +537,7 @@ int lookup_token(const uint8_t *name, size_t namelen) {
case 13: case 13:
switch (name[namelen - 1]) { switch (name[namelen - 1]) {
case 'l': case 'l':
if (util::streq("cache-contro", name, 12)) { if (util::streq_l("cache-contro", name, 12)) {
return HD_CACHE_CONTROL; return HD_CACHE_CONTROL;
} }
break; break;
@ -546,12 +546,12 @@ int lookup_token(const uint8_t *name, size_t namelen) {
case 14: case 14:
switch (name[namelen - 1]) { switch (name[namelen - 1]) {
case 'h': case 'h':
if (util::streq("content-lengt", name, 13)) { if (util::streq_l("content-lengt", name, 13)) {
return HD_CONTENT_LENGTH; return HD_CONTENT_LENGTH;
} }
break; break;
case 's': case 's':
if (util::streq("http2-setting", name, 13)) { if (util::streq_l("http2-setting", name, 13)) {
return HD_HTTP2_SETTINGS; return HD_HTTP2_SETTINGS;
} }
break; break;
@ -560,17 +560,17 @@ int lookup_token(const uint8_t *name, size_t namelen) {
case 15: case 15:
switch (name[namelen - 1]) { switch (name[namelen - 1]) {
case 'e': case 'e':
if (util::streq("accept-languag", name, 14)) { if (util::streq_l("accept-languag", name, 14)) {
return HD_ACCEPT_LANGUAGE; return HD_ACCEPT_LANGUAGE;
} }
break; break;
case 'g': case 'g':
if (util::streq("accept-encodin", name, 14)) { if (util::streq_l("accept-encodin", name, 14)) {
return HD_ACCEPT_ENCODING; return HD_ACCEPT_ENCODING;
} }
break; break;
case 'r': case 'r':
if (util::streq("x-forwarded-fo", name, 14)) { if (util::streq_l("x-forwarded-fo", name, 14)) {
return HD_X_FORWARDED_FOR; return HD_X_FORWARDED_FOR;
} }
break; break;
@ -579,7 +579,7 @@ int lookup_token(const uint8_t *name, size_t namelen) {
case 16: case 16:
switch (name[namelen - 1]) { switch (name[namelen - 1]) {
case 'n': case 'n':
if (util::streq("proxy-connectio", name, 15)) { if (util::streq_l("proxy-connectio", name, 15)) {
return HD_PROXY_CONNECTION; return HD_PROXY_CONNECTION;
} }
break; break;
@ -588,17 +588,17 @@ int lookup_token(const uint8_t *name, size_t namelen) {
case 17: case 17:
switch (name[namelen - 1]) { switch (name[namelen - 1]) {
case 'e': case 'e':
if (util::streq("if-modified-sinc", name, 16)) { if (util::streq_l("if-modified-sinc", name, 16)) {
return HD_IF_MODIFIED_SINCE; return HD_IF_MODIFIED_SINCE;
} }
break; break;
case 'g': case 'g':
if (util::streq("transfer-encodin", name, 16)) { if (util::streq_l("transfer-encodin", name, 16)) {
return HD_TRANSFER_ENCODING; return HD_TRANSFER_ENCODING;
} }
break; break;
case 'o': case 'o':
if (util::streq("x-forwarded-prot", name, 16)) { if (util::streq_l("x-forwarded-prot", name, 16)) {
return HD_X_FORWARDED_PROTO; return HD_X_FORWARDED_PROTO;
} }
break; break;

View File

@ -238,7 +238,7 @@ void Downstream::assemble_request_cookie() {
cookie = ""; cookie = "";
for (auto &kv : request_headers_) { for (auto &kv : request_headers_) {
if (kv.name.size() != 6 || kv.name[5] != 'e' || 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; continue;
} }
@ -259,7 +259,7 @@ Headers Downstream::crumble_request_cookie() {
Headers cookie_hdrs; Headers cookie_hdrs;
for (auto &kv : request_headers_) { for (auto &kv : request_headers_) {
if (kv.name.size() != 6 || kv.name[5] != 'e' || 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; continue;
} }
size_t last = kv.value.size(); size_t last = kv.value.size();
@ -813,8 +813,8 @@ void Downstream::inspect_http1_request() {
auto &val = request_headers_[idx].value; auto &val = request_headers_[idx].value;
// TODO Perform more strict checking for upgrade headers // TODO Perform more strict checking for upgrade headers
if (util::streq(NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, val.c_str(), if (util::streq_l(NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, val.c_str(),
val.size())) { val.size())) {
http2_upgrade_seen_ = true; http2_upgrade_seen_ = true;
} }
} }

View File

@ -39,26 +39,31 @@ using namespace nghttp2;
namespace shrpx { namespace shrpx {
void test_util_streq(void) { void test_util_streq(void) {
CU_ASSERT(util::streq("alpha", (const uint8_t *)"alpha", 5)); CU_ASSERT(util::streq("alpha", "alpha", 5));
CU_ASSERT(util::streq("alpha", (const uint8_t *)"alphabravo", 5)); CU_ASSERT(util::streq("alpha", "alphabravo", 5));
CU_ASSERT(!util::streq("alpha", (const uint8_t *)"alphabravo", 6)); CU_ASSERT(!util::streq("alpha", "alphabravo", 6));
CU_ASSERT(!util::streq("alphabravo", (const uint8_t *)"alpha", 5)); CU_ASSERT(!util::streq("alphabravo", "alpha", 5));
CU_ASSERT(!util::streq("alpha", (const uint8_t *)"alphA", 5)); CU_ASSERT(!util::streq("alpha", "alphA", 5));
CU_ASSERT(!util::streq("", (const uint8_t *)"a", 1)); CU_ASSERT(!util::streq("", "a", 1));
CU_ASSERT(util::streq("", (const uint8_t *)"", 0)); CU_ASSERT(util::streq("", "", 0));
CU_ASSERT(!util::streq("alpha", (const uint8_t *)"", 0)); CU_ASSERT(!util::streq("alpha", "", 0));
CU_ASSERT( CU_ASSERT(util::streq("alpha", 5, "alpha", 5));
util::streq((const uint8_t *)"alpha", 5, (const uint8_t *)"alpha", 5)); CU_ASSERT(!util::streq("alpha", 4, "alpha", 5));
CU_ASSERT( CU_ASSERT(!util::streq("alpha", 5, "alpha", 4));
!util::streq((const uint8_t *)"alpha", 4, (const uint8_t *)"alpha", 5)); CU_ASSERT(!util::streq("alpha", 5, "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));
char *a = nullptr; char *a = nullptr;
char *b = nullptr; char *b = nullptr;
CU_ASSERT(util::streq(a, 0, b, 0)); 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) { void test_util_strieq(void) {