How to add cors in response header of oat++ hls server

522 Views Asked by At
auto response = controller->createResponse(Status::CODE_200, controller->livePlaylist->generateForTime(time, 5)->toString());
      response->putHeader("Accept-Ranges", "bytes");
      //response->putHeader(allow_origin = "*");
      response->putHeader(Header::CONNECTION, Header::Value::CONNECTION_KEEP_ALIVE);
      response->putHeader(Header::CONTENT_TYPE, "application/x-mpegURL");
      response->putHeader(Header::CORS_METHODS,"GET, POST, PUT, OPTIONS, DELETE");
      response->putHeader(Header::CORS_ORIGIN, "*");
      response->putHeader(Header::CORS_HEADERS, "DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Range");
      response->putHeader(Header::CORS_MAX_AGE, "1728000");
      return _return(response);

This is a response header of Async Endpoint.

Is this a proper way of adding cor in response header of hls stream using oat++ framework.

What is the Correct method for adding in cors in oat++ framework ?

1

There are 1 best solutions below

0
lganzzzo On

Oat++ has built-in functionality to handle CORS. Just add these request and response interceptors to your AsyncHttpConnectionHandler

#include "oatpp/web/server/interceptor/AllowCorsGlobal.hpp"

...

  OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, serverConnectionHandler)([] {

    OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router); // get Router component

    auto connectionHandler = oatpp::web::server::AsyncHttpConnectionHandler::createShared(router);

    /* Add CORS-enabling interceptors */
    connectionHandler->addRequestInterceptor(std::make_shared<oatpp::web::server::interceptor::AllowOptionsGlobal>());
    connectionHandler->addResponseInterceptor(std::make_shared<oatpp::web::server::interceptor::AllowCorsGlobal>());

    return connectionHandler;

  }());