Skip to content

Commit

Permalink
bp/Request: pass std::string_view to DispatchError()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Jan 17, 2025
1 parent f6b2675 commit 504c585
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/bp/Handler.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ Request::OnTranslateResponse(UniquePoolPtr<TranslateResponse> _response) noexcep
return;
}

const char *redirect = MakeHttpsRedirect(pool, host, response.https_only, request.uri);
std::string_view redirect = MakeHttpsRedirect(pool, host, response.https_only, request.uri);
_response.reset();
DispatchRedirect(HttpStatus::MOVED_PERMANENTLY,
redirect,
Expand Down
5 changes: 3 additions & 2 deletions src/bp/ProxyWidget.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ ProxyWidget::ResolverCallback() noexcept
Destroy();
_request.LogDispatchError(HttpStatus::BAD_GATEWAY,
"No such widget type",
log_msg);
log_msg.c_str());
return;
}

Expand Down Expand Up @@ -306,7 +306,8 @@ ProxyWidget::WidgetNotFound() noexcept

auto &_request = request;
Destroy();
_request.LogDispatchError(HttpStatus::NOT_FOUND, "No such widget", log_msg);
_request.LogDispatchError(HttpStatus::NOT_FOUND, "No such widget",
log_msg.c_str());
}

void
Expand Down
15 changes: 9 additions & 6 deletions src/bp/RError.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "net/SocketProtocolError.hxx"
#include "net/TimeoutError.hxx"
#include "util/Exception.hxx"
#include "AllocatorPtr.hxx"

#include <string.h> // for strerror()

Expand Down Expand Up @@ -139,19 +140,21 @@ ToResponse(struct pool &pool, std::exception_ptr ep) noexcept

void
Request::LogDispatchError(HttpStatus status,
const char *msg, const char *log_msg,
std::string_view msg, std::string_view log_msg,
unsigned log_level) noexcept
{
logger(log_level, "error on '", request.uri, "': ", log_msg);

if (instance.config.verbose_response)
msg = p_strdup(&pool, log_msg);
if (instance.config.verbose_response) {
AllocatorPtr alloc{pool};
msg = alloc.Dup(log_msg);
}

DispatchError(status, msg);
}

void
Request::LogDispatchError(HttpStatus status, const char *log_msg,
Request::LogDispatchError(HttpStatus status, std::string_view log_msg,
unsigned log_level) noexcept
{
LogDispatchError(status, http_status_to_string(status),
Expand All @@ -177,7 +180,7 @@ Request::LogDispatchError(std::exception_ptr ep) noexcept
}

void
Request::LogDispatchError(HttpStatus status, const char *msg,
Request::LogDispatchError(HttpStatus status, std::string_view msg,
std::exception_ptr ep, unsigned log_level) noexcept
{
if (DispatchHttpMessageResponse(ep))
Expand All @@ -194,7 +197,7 @@ Request::LogDispatchError(HttpStatus status, const char *msg,
}

void
Request::LogDispatchErrno(int error, const char *msg) noexcept
Request::LogDispatchErrno(int error, std::string_view msg) noexcept
{
auto response = ErrnoToResponse(error);
if (response.status == HttpStatus{})
Expand Down
18 changes: 9 additions & 9 deletions src/bp/Request.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -818,15 +818,15 @@ public:
DispatchError(status, std::move(headers), UnusedIstreamPtr());
}

void DispatchError(HttpStatus status, const char *msg) noexcept;
void DispatchError(HttpStatus status, std::string_view msg) noexcept;

void DispatchError(HttpStatus status) noexcept;

void DispatchError(HttpStatus status, HttpHeaders &&headers,
const char *msg) noexcept;
std::string_view msg) noexcept;

void DispatchRedirect(HttpStatus status, const char *location,
const char *msg) noexcept;
void DispatchRedirect(HttpStatus status, std::string_view location,
std::string_view msg) noexcept;

void DispatchMethodNotAllowed(const char *allow) noexcept;

Expand Down Expand Up @@ -895,20 +895,20 @@ private:
bool DispatchHttpMessageResponse(std::exception_ptr e) noexcept;

public:
void LogDispatchError(HttpStatus status, const char *log_msg,
void LogDispatchError(HttpStatus status, std::string_view log_msg,
unsigned log_level=2) noexcept;

void LogDispatchError(HttpStatus status, const char *msg,
const char *log_msg,
void LogDispatchError(HttpStatus status, std::string_view msg,
std::string_view log_msg,
unsigned log_level=2) noexcept;

void LogDispatchError(std::exception_ptr ep) noexcept;

void LogDispatchError(HttpStatus status, const char *msg,
void LogDispatchError(HttpStatus status, std::string_view msg,
std::exception_ptr ep,
unsigned log_level=2) noexcept;

void LogDispatchErrno(int error, const char *msg) noexcept;
void LogDispatchErrno(int error, std::string_view msg) noexcept;

private:
/**
Expand Down
10 changes: 4 additions & 6 deletions src/bp/Response.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -891,10 +891,9 @@ Request::DispatchError(HttpStatus status, HttpHeaders &&headers,

void
Request::DispatchError(HttpStatus status,
HttpHeaders &&headers, const char *msg) noexcept
HttpHeaders &&headers, std::string_view msg) noexcept
{
assert(http_status_is_valid(status));
assert(msg != nullptr);

headers.Write("content-type", "text/plain");

Expand All @@ -903,7 +902,7 @@ Request::DispatchError(HttpStatus status,
}

void
Request::DispatchError(HttpStatus status, const char *msg) noexcept
Request::DispatchError(HttpStatus status, std::string_view msg) noexcept
{
DispatchError(status, {}, msg);
}
Expand All @@ -916,12 +915,11 @@ Request::DispatchError(HttpStatus status) noexcept

void
Request::DispatchRedirect(HttpStatus status,
const char *location, const char *msg) noexcept
std::string_view location, std::string_view msg) noexcept
{
assert(http_status_is_redirect(status));
assert(location != nullptr);

if (msg == nullptr)
if (msg.data() == nullptr)
msg = "redirection";

HttpHeaders headers;
Expand Down
2 changes: 1 addition & 1 deletion src/bp/TokenAuth.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Request::OnTokenAuthTranslateResponse(UniquePoolPtr<TranslateResponse> &&_respon
dissected_uri,
tr);

DispatchRedirect(HttpStatus::SEE_OTHER, redirect_uri, nullptr);
DispatchRedirect(HttpStatus::SEE_OTHER, redirect_uri, {});
}

inline void
Expand Down

0 comments on commit 504c585

Please sign in to comment.