output/httpd/Client: convert variable to std::string_view

In all cases, we already know the length, and casting the C string to
std::string_view has to call strlen() again.
This commit is contained in:
Max Kellermann 2024-04-03 20:19:52 +02:00
parent e4ba736d03
commit f8a838db5d

View File

@ -17,6 +17,8 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
using std::string_view_literals::operator""sv;
HttpdClient::~HttpdClient() noexcept HttpdClient::~HttpdClient() noexcept
{ {
if (IsDefined()) if (IsDefined())
@ -121,7 +123,7 @@ bool
HttpdClient::SendResponse() noexcept HttpdClient::SendResponse() noexcept
{ {
std::string allocated; std::string allocated;
const char *response; std::string_view response;
assert(state == State::RESPONSE); assert(state == State::RESPONSE);
@ -131,14 +133,14 @@ HttpdClient::SendResponse() noexcept
"Content-Type: text/plain\r\n" "Content-Type: text/plain\r\n"
"Connection: close\r\n" "Connection: close\r\n"
"\r\n" "\r\n"
"404 not found"; "404 not found"sv;
} else if (metadata_requested) { } else if (metadata_requested) {
allocated = allocated =
icy_server_metadata_header(httpd.name, httpd.genre, icy_server_metadata_header(httpd.name, httpd.genre,
httpd.website, httpd.website,
httpd.content_type, httpd.content_type,
metaint); metaint);
response = allocated.c_str(); response = allocated;
} else { /* revert to a normal HTTP request */ } else { /* revert to a normal HTTP request */
allocated = fmt::format("HTTP/1.1 200 OK\r\n" allocated = fmt::format("HTTP/1.1 200 OK\r\n"
"Content-Type: {}\r\n" "Content-Type: {}\r\n"
@ -148,10 +150,10 @@ HttpdClient::SendResponse() noexcept
"Access-Control-Allow-Origin: *\r\n" "Access-Control-Allow-Origin: *\r\n"
"\r\n", "\r\n",
httpd.content_type); httpd.content_type);
response = allocated.c_str(); response = allocated;
} }
ssize_t nbytes = GetSocket().WriteNoWait(AsBytes(std::string_view{response})); ssize_t nbytes = GetSocket().WriteNoWait(AsBytes(response));
if (nbytes < 0) [[unlikely]] { if (nbytes < 0) [[unlikely]] {
const SocketErrorMessage msg; const SocketErrorMessage msg;
FmtWarning(httpd_output_domain, FmtWarning(httpd_output_domain,