add options method

This commit is contained in:
Artur 2021-09-18 09:36:02 +00:00
parent 7246825d8d
commit 82a849ba8e
3 changed files with 59 additions and 2 deletions

View File

@ -62,6 +62,25 @@
-->
</logger>
<http_options_response>
<header>
<name>Access-Control-Allow-Origin</name>
<value>*</value>
</header>
<header>
<name>Access-Control-Allow-Headers</name>
<value>origin, x-requested-with</value>
</header>
<header>
<name>Access-Control-Allow-Methods</name>
<value>POST, GET, OPTIONS</value>
</header>
<header>
<name>Access-Control-Max-Age</name>
<value>86400</value>
</header>
</http_options_response>
<!-- It is the name that will be shown in the clickhouse-client.
By default, anything with "production" will be highlighted in red in query prompt.
-->

View File

@ -35,8 +35,9 @@ public:
enum class HTTPMethod : uint8_t
{
UNKNOWN = 0,
GET = 1,
POST = 2,
GET = 1,
POST = 2,
OPTIONS = 3
};
enum class QueryKind : uint8_t

View File

@ -32,6 +32,7 @@
#include <Common/typeid_cast.h>
#include <common/getFQDNOrHostName.h>
#include <common/scope_guard.h>
#include "Server/HTTP/HTTPResponse.h"
#if !defined(ARCADIA_BUILD)
# include <Common/config.h>
@ -108,6 +109,37 @@ namespace ErrorCodes
extern const int HTTP_LENGTH_REQUIRED;
}
namespace
{
/// Process options request. Usefull for CORS.
void processOptionsRequest(HTTPServerResponse & response, const Poco::Util::LayeredConfiguration & config)
{
/// If answer for options request was not defined, return 501 to client.
if (!config.has("http_options_response"))
{
response.setStatusAndReason(HTTPResponse::HTTP_NOT_IMPLEMENTED);
response.send();
}
else
{
/// otherwise fill response.
Strings config_keys;
config.keys("http_options_response", config_keys);
for (const std::string & config_key : config_keys)
{
if (config_key == "header" || config_key.starts_with("header["))
{
response.add(config.getString("http_options_response." + config_key + ".name", "Empty header"),
config.getString("http_options_response." + config_key + ".value", ""));
response.setKeepAlive(false);
}
}
response.setStatusAndReason(HTTPResponse::HTTP_NO_CONTENT);
response.send();
}
}
}
static String base64Decode(const String & encoded)
{
String decoded;
@ -850,6 +882,11 @@ void HTTPHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse
try
{
if (request.getMethod() == HTTPServerRequest::HTTP_OPTIONS)
{
processOptionsRequest(response, server.config());
return;
}
response.setContentType("text/plain; charset=UTF-8");
response.set("X-ClickHouse-Server-Display-Name", server_display_name);
/// For keep-alive to work.