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;
}
// 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);
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.
// 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.