From: Steinar H. Gunderson Date: Sun, 18 Feb 2018 19:08:45 +0000 (+0100) Subject: Add a CORS policy to the channel endpoints, so that external applications can query... X-Git-Tag: 1.7.0~26 X-Git-Url: https://git.sesse.net/?p=nageru;a=commitdiff_plain;h=d3e48df512d9476d3849227067792a3537bb094e Add a CORS policy to the channel endpoints, so that external applications can query the tally light. --- diff --git a/httpd.cpp b/httpd.cpp index 811e095..fe2c929 100644 --- a/httpd.cpp +++ b/httpd.cpp @@ -89,10 +89,13 @@ int HTTPD::answer_to_connection(MHD_Connection *connection, return ret; } if (endpoints.count(url)) { - pair contents_and_type = endpoints[url](); + pair contents_and_type = endpoints[url].callback(); MHD_Response *response = MHD_create_response_from_buffer( contents_and_type.first.size(), &contents_and_type.first[0], MHD_RESPMEM_MUST_COPY); MHD_add_response_header(response, "Content-type", contents_and_type.second.c_str()); + if (endpoints[url].cors_policy == ALLOW_ALL_ORIGINS) { + MHD_add_response_header(response, "Access-Control-Allow-Origin", "*"); + } int ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done. return ret; diff --git a/httpd.h b/httpd.h index 878a184..509ae01 100644 --- a/httpd.h +++ b/httpd.h @@ -33,8 +33,12 @@ public: } // Should be called before start() (due to threading issues). - void add_endpoint(const std::string &url, const EndpointCallback &callback) { - endpoints[url] = callback; + enum CORSPolicy { + NO_CORS_POLICY, + ALLOW_ALL_ORIGINS + }; + void add_endpoint(const std::string &url, const EndpointCallback &callback, CORSPolicy cors_policy) { + endpoints[url] = Endpoint{ callback, cors_policy }; } void start(int port); @@ -92,7 +96,11 @@ private: MHD_Daemon *mhd = nullptr; std::mutex streams_mutex; std::set streams; // Not owned. - std::unordered_map endpoints; + struct Endpoint { + EndpointCallback callback; + CORSPolicy cors_policy; + }; + std::unordered_map endpoints; std::string header; // Metrics. diff --git a/mixer.cpp b/mixer.cpp index 70175a1..55604c1 100644 --- a/mixer.cpp +++ b/mixer.cpp @@ -356,11 +356,11 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards) // Must be instantiated after VideoEncoder has initialized global_flags.use_zerocopy. theme.reset(new Theme(global_flags.theme_filename, global_flags.theme_dirs, resource_pool.get(), num_cards)); - httpd.add_endpoint("/channels", bind(&Mixer::get_channels_json, this)); + httpd.add_endpoint("/channels", bind(&Mixer::get_channels_json, this), HTTPD::ALLOW_ALL_ORIGINS); for (int channel_idx = 2; channel_idx < theme->get_num_channels(); ++channel_idx) { char url[256]; snprintf(url, sizeof(url), "/channels/%d/color", channel_idx); - httpd.add_endpoint(url, bind(&Mixer::get_channel_color_http, this, unsigned(channel_idx))); + httpd.add_endpoint(url, bind(&Mixer::get_channel_color_http, this, unsigned(channel_idx)), HTTPD::ALLOW_ALL_ORIGINS); } // Start listening for clients only once VideoEncoder has written its header, if any.