Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions include/tsutil/StringCompare.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/** @file

Helper for std::string_view comparison

@section license License

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include <strings.h>
#include <string_view>

namespace ts
{
/**
Returns true iff @a lhs and @a rhs compare equal, ignoring case.

Prefer this over libswoc's @c strcasecmp(std::string_view, std::string_view) when you only need
an equality check: this short-circuits on length mismatch, whereas the libswoc version must keep
comparing bytes to produce a correct ordering result even when the lengths differ.

For case-sensitive comparison, use @c std::string_view::operator==.
*/
inline bool
iequals(std::string_view lhs, std::string_view rhs) noexcept
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standard library doesn't have this, but boost has one. I followed the naming.
https://www.boost.org/doc/libs/latest/libs/beast/doc/html/beast/ref/boost__beast__iequals.html

{
if (lhs.size() != rhs.size()) {
return false;
}

return ::strncasecmp(lhs.data(), rhs.data(), lhs.size()) == 0;
}
Comment thread
masaori335 marked this conversation as resolved.
} // namespace ts
7 changes: 5 additions & 2 deletions src/proxy/hdrs/MIME.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include "tscore/ink_defs.h"
#include "tscore/ink_platform.h"
#include "tscore/ink_memory.h"

#include "tsutil/StringCompare.h"

#include <cassert>
#include <cctype>
#include <cstdio>
Expand Down Expand Up @@ -1161,8 +1164,8 @@ _mime_hdr_field_list_search_by_string(MIMEHdrImpl *mh, std::string_view field_na
too_far_field = &(fblock->m_field_slots[fblock->m_freetop]);
while (field < too_far_field) {
if (field->is_live() &&
strcasecmp(std::string_view{field->m_ptr_name, static_cast<std::string_view::size_type>(field->m_len_name)},
field_name) == 0) {
ts::iequals(std::string_view{field->m_ptr_name, static_cast<std::string_view::size_type>(field->m_len_name)},
field_name)) {
return field;
}
++field;
Expand Down
3 changes: 2 additions & 1 deletion src/proxy/http/HttpSM.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "proxy/http/HttpConfig.h"
#include "tscore/ink_hrtime.h"
#include "tsutil/Metrics.h"
#include "tsutil/StringCompare.h"
#include "tsutil/ts_bw_format.h"
#include "proxy/ProxyTransaction.h"
#include "proxy/http/HttpSM.h"
Expand Down Expand Up @@ -791,7 +792,7 @@ HttpSM::state_read_client_request_header(int event, void *data)
(t_state.hdr_info.client_request.method_get_wksidx() == HTTP_WKSIDX_POST ||
t_state.hdr_info.client_request.method_get_wksidx() == HTTP_WKSIDX_PUT)) {
auto expect{t_state.hdr_info.client_request.value_get(static_cast<std::string_view>(MIME_FIELD_EXPECT))};
if (strcasecmp(expect, static_cast<std::string_view>(HTTP_VALUE_100_CONTINUE)) == 0) {
if (ts::iequals(expect, static_cast<std::string_view>(HTTP_VALUE_100_CONTINUE))) {
// When receive an "Expect: 100-continue" request from client, ATS sends a "100 Continue" response to client
// immediately, before receive the real response from original server.
if (t_state.http_config_param->send_100_continue_response) {
Expand Down
3 changes: 2 additions & 1 deletion src/proxy/http/HttpTransactHeaders.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include "iocore/utils/Machine.h"
#include "tsutil/DbgCtl.h"
#include "tsutil/StringCompare.h"

using namespace std::literals;

Expand Down Expand Up @@ -898,7 +899,7 @@ HttpTransactHeaders::remove_100_continue_headers(HttpTransact::State *s, HTTPHdr
{
auto expect{s->hdr_info.client_request.value_get(static_cast<std::string_view>(MIME_FIELD_EXPECT))};

if (strcasecmp(expect, static_cast<std::string_view>(HTTP_VALUE_100_CONTINUE)) == 0) {
if (ts::iequals(expect, static_cast<std::string_view>(HTTP_VALUE_100_CONTINUE))) {
outgoing->field_delete(static_cast<std::string_view>(MIME_FIELD_EXPECT));
}
}
Expand Down