Add CORS hearder,param:add_http_cors_header and check request header

This commit is contained in:
igor 2016-08-23 21:35:23 +03:00
parent 9148209bc2
commit 3bbbdb29f5
3 changed files with 22 additions and 3 deletions

View File

@ -35,7 +35,7 @@ class WriteBufferFromHTTPServerResponse : public BufferWithOwnMemory<WriteBuffer
{
private:
Poco::Net::HTTPServerResponse & response;
bool add_cors_header;
bool compress;
Poco::DeflatingStreamBuf::StreamType compression_method;
int compression_level = Z_DEFAULT_COMPRESSION;
@ -48,6 +48,11 @@ private:
{
if (!ostr)
{
if (add_cors_header)
{
response.set("Access-Control-Allow-Origin","*");
}
if (compress && offset()) /// Пустой ответ сжимать не нужно.
{
if (compression_method == Poco::DeflatingStreamBuf::STREAM_GZIP)
@ -120,6 +125,15 @@ public:
compression_level = level;
}
/** Включить или отключить CORS.
* Работает только перед тем, как были отправлены HTTP заголовки.
* Иначе - не имеет эффекта.
*/
void addHeaderCORS(bool enable_cors)
{
add_cors_header = enable_cors;
}
~WriteBufferFromHTTPServerResponse()
{
if (!offset())

View File

@ -220,8 +220,10 @@ struct Settings
\
/** Write statistics about read rows, bytes, time elapsed in suitable output formats */ \
M(SettingBool, output_format_write_statistics, true) \
\
/** Write add http CORS header */ \
M(SettingBool, add_http_cors_header, false) \
/// Всевозможные ограничения на выполнение запроса.
Limits limits;

View File

@ -246,6 +246,9 @@ void HTTPHandler::processQuery(
if (in_post_compressed && context.getSettingsRef().http_native_compression_disable_checksumming_on_decompress)
static_cast<CompressedReadBuffer &>(*in_post_maybe_compressed).disableChecksumming();
/// Добавить CORS header выставлена настройка, и если клиент передал заголовок Origin
used_output.out->addHeaderCORS( context.getSettingsRef().add_http_cors_header && !request.get("Origin", "").empty() );
context.setInterface(Context::Interface::HTTP);
Context::HTTPMethod http_method = Context::HTTPMethod::UNKNOWN;