From 26bb7bc6e6827a3db7611218f4b23b15feb42648 Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Tue, 16 May 2023 08:24:29 -0400 Subject: [PATCH] rgw: use string_view to parse Accept header avoid copying the header into a separate buffer to do comparisons Fixes: https://tracker.ceph.com/issues/59490 Signed-off-by: Casey Bodley --- src/rgw/rgw_rest.cc | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/rgw/rgw_rest.cc b/src/rgw/rgw_rest.cc index a8b8e0dff99..5bba3fce4d1 100644 --- a/src/rgw/rgw_rest.cc +++ b/src/rgw/rgw_rest.cc @@ -1727,17 +1727,15 @@ int RGWHandler_REST::allocate_formatter(req_state *s, } else { const char *accept = s->info.env->get("HTTP_ACCEPT"); if (accept) { - char format_buf[64]; - unsigned int i = 0; - for (; i < sizeof(format_buf) - 1 && accept[i] && accept[i] != ';'; ++i) { - format_buf[i] = accept[i]; - } - format_buf[i] = 0; - if ((strcmp(format_buf, "text/xml") == 0) || (strcmp(format_buf, "application/xml") == 0)) { + // trim at first ; + std::string_view format = accept; + format = format.substr(0, format.find(';')); + + if (format == "text/xml" || format == "application/xml") { type = RGWFormat::XML; - } else if (strcmp(format_buf, "application/json") == 0) { + } else if (format == "application/json") { type = RGWFormat::JSON; - } else if (strcmp(format_buf, "text/html") == 0) { + } else if (format == "text/html") { type = RGWFormat::HTML; } } -- 2.39.5