]> git.sesse.net Git - nageru/commitdiff
Add a CORS policy to the channel endpoints, so that external applications can query...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 18 Feb 2018 19:08:45 +0000 (20:08 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 18 Feb 2018 19:08:45 +0000 (20:08 +0100)
httpd.cpp
httpd.h
mixer.cpp

index 811e095dc9541a442742e02c7d7454b0284cc207..fe2c929969de53db70e179a5f5200db201732631 100644 (file)
--- 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<string, string> contents_and_type = endpoints[url]();
+               pair<string, string> 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 878a184d12858341287a17f20d22e03a1e19bc83..509ae0146a344f101d34d199b36bcab3380c06a6 100644 (file)
--- 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<Stream *> streams;  // Not owned.
-       std::unordered_map<std::string, EndpointCallback> endpoints;
+       struct Endpoint {
+               EndpointCallback callback;
+               CORSPolicy cors_policy;
+       };
+       std::unordered_map<std::string, Endpoint> endpoints;
        std::string header;
 
        // Metrics.
index 70175a163c4a2c9a51be1690a9c9c408ac16f81f..55604c15115a0d7d1c84f032ba3a540ebce43df2 100644 (file)
--- 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.