icinga2/lib/remote/httputility.cpp

175 lines
5.2 KiB
C++
Raw Normal View History

Replace all existing copyright headers with `SPDX` headers I've used the following command to replace the original copyright header lines in a C-style comment block: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{/\*[^*]*\(\s*c\s*\)\s*(\d{4})\s*Icinga\s+GmbH[^*]*\*/}{// SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n// SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ``` For files that use shell-style comments (#) like CMakeLists.txt, I've used this command: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{#.*\(\s*c\s*\)\s(\d{4})\sIcinga\s+GmbH.*}{# SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n# SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ``` And for SQL files: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{--.*\(c\)\s(\d{4})\sIcinga\sGmbH.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{-- Copyright \(c\)\s(\d{4})\sIcinga\s+Development\sTeam.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ```
2026-01-27 09:06:40 -05:00
// SPDX-FileCopyrightText: 2012 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
#include "remote/httputility.hpp"
#include "remote/url.hpp"
#include "base/json.hpp"
2015-11-09 16:48:03 -05:00
#include "base/logger.hpp"
2018-12-21 05:52:37 -05:00
#include <map>
#include <string>
2018-12-21 05:52:37 -05:00
#include <vector>
#include <boost/beast/http.hpp>
using namespace icinga;
Dictionary::Ptr HttpUtility::FetchRequestParameters(const Url::Ptr& url, const std::string& body)
{
Dictionary::Ptr result;
if (!body.empty()) {
2015-11-09 16:48:03 -05:00
Log(LogDebug, "HttpUtility")
<< "Request body: '" << body << '\'';
result = JsonDecode(body);
2015-11-09 16:48:03 -05:00
}
if (!result)
result = new Dictionary();
2018-12-21 05:52:37 -05:00
std::map<String, std::vector<String>> query;
for (const auto& kv : url->GetQuery()) {
2018-12-21 05:52:37 -05:00
query[kv.first].emplace_back(kv.second);
}
for (auto& kv : query) {
2015-07-29 07:09:42 -04:00
result->Set(kv.first, Array::FromVector(kv.second));
}
return result;
}
Value HttpUtility::GetLastParameter(const Dictionary::Ptr& params, const String& key)
2015-07-29 07:39:58 -04:00
{
Value varr = params->Get(key);
if (!varr.IsObjectType<Array>())
return varr;
2015-07-29 07:39:58 -04:00
Array::Ptr arr = varr;
if (arr->GetLength() == 0)
return Empty;
2015-07-29 07:39:58 -04:00
else
return arr->Get(arr->GetLength() - 1);
}
/**
* Stream a JSON-encoded body to the client.
*
* This function sets the Content-Type header to "application/json", starts the streaming of the response,
* and encodes the given value as JSON to the client. If pretty-print is requested, the JSON output will be
* formatted accordingly. It is assumed that the response status code and other necessary headers have already
* been set.
*
* @param response The HTTP response to send the body to.
* @param params The request parameters.
* @param val The value to encode as JSON and stream to the client.
* @param yc The yield context to use for asynchronous operations.
*/
void HttpUtility::SendJsonBody(HttpApiResponse& response, const Dictionary::Ptr& params, const Value& val, boost::asio::yield_context& yc)
{
namespace http = boost::beast::http;
response.set(http::field::content_type, "application/json");
response.StartStreaming(false);
response.GetJsonEncoder(params && GetLastParameter(params, "pretty")).Encode(val, &yc);
}
void HttpUtility::SendJsonBody(HttpApiResponse& response, const Dictionary::Ptr& params, const Value& val)
{
namespace http = boost::beast::http;
response.set(http::field::content_type, "application/json");
response.GetJsonEncoder(params && GetLastParameter(params, "pretty")).Encode(val);
}
void HttpUtility::SendJsonError(HttpApiResponse& response,
const Dictionary::Ptr& params, int code, const String& info, const String& diagnosticInformation)
{
Dictionary::Ptr result = new Dictionary({ { "error", code } });
if (!info.IsEmpty()) {
result->Set("status", info);
}
if (params && HttpUtility::GetLastParameter(params, "verbose") && !diagnosticInformation.IsEmpty()) {
result->Set("diagnostic_information", diagnosticInformation);
}
response.Clear();
response.result(code);
HttpUtility::SendJsonBody(response, params, result);
}
/**
* Check if the given string is suitable to be used as an HTTP header name.
*
* @param name The value to check for validity
* @return true if the argument is a valid header name, false otherwise
*/
bool HttpUtility::IsValidHeaderName(std::string_view name)
{
/*
* Derived from the following syntax definition in RFC9110:
*
* field-name = token
* token = 1*tchar
* tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
* ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
* DIGIT = %x30-39 ; 0-9
*
* References:
* - https://datatracker.ietf.org/doc/html/rfc9110#section-5.1
* - https://datatracker.ietf.org/doc/html/rfc9110#appendix-A
* - https://www.rfc-editor.org/rfc/rfc5234#appendix-B.1
*/
return !name.empty() && std::all_of(name.begin(), name.end(), [](char c) {
switch (c) {
case '!': case '#': case '$': case '%': case '&': case '\'': case '*': case '+':
case '-': case '.': case '^': case '_': case '`': case '|': case '~':
return true;
default:
return ('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
}
});
}
/**
* Check if the given string is suitable to be used as an HTTP header value.
*
* @param value The value to check for validity
* @return true if the argument is a valid header value, false otherwise
*/
bool HttpUtility::IsValidHeaderValue(std::string_view value)
{
/*
* Derived from the following syntax definition in RFC9110:
*
* field-value = *field-content
* field-content = field-vchar [ 1*( SP / HTAB / field-vchar ) field-vchar ]
* field-vchar = VCHAR / obs-text
* obs-text = %x80-FF
* VCHAR = %x21-7E ; visible (printing) characters
*
* References:
* - https://datatracker.ietf.org/doc/html/rfc9110#section-5.5
* - https://datatracker.ietf.org/doc/html/rfc9110#appendix-A
* - https://www.rfc-editor.org/rfc/rfc5234#appendix-B.1
*/
if (!value.empty()) {
// Must not start or end with space or tab.
for (char c : {value.front(), value.back()}) {
if (c == ' ' || c == '\t') {
return false;
}
}
}
return std::all_of(value.begin(), value.end(), [](char c) {
return c == ' ' || c == '\t' || ('\x21' <= c && c <= '\x7e') || ('\x80' <= c && c <= '\xff');
});
}