There’s a clear way to modify the simWS C++ plugin to allow setting custom HTTP headers (like Content-Type) for served resources. The main idea is to extend server_meta and the onWSHTTP method.
- Add a header map to server_meta
struct server_meta : public _meta
{
my_server *srv{nullptr};
optional<std::string> httpHandler;
std::map<std::string, std::string> responseHeaders; // <-- new
virtual ~server_meta() { if(srv) delete srv; }
};
- Modify onWSHTTP to apply headers
void onWSHTTP(server_meta *meta, websocketpp::connection_hdl hdl)
{
my_server::connection_ptr con = meta->srv->get_con_from_hdl(hdl);
sim::addLog(sim_verbosity_debug, "onWSHTTP: %s", con->get_resource());
if(!meta->httpHandler)
{
con->set_status(websocketpp::http::status_code::not_found);
return;
}
if(auto h = hdl.lock())
{
httpCallback_in in;
in.serverHandle = srvHandles.add(meta, meta->scriptID);
in.connectionHandle = connHandles.add(hdl, meta->scriptID);
in.resource = con->get_resource();
in.data = con->get_request_body();
httpCallback_out out;
if(httpCallback(meta->scriptID, meta->httpHandler->c_str(), &in, &out))
{
// Set status and body
con->set_status(static_cast<websocketpp::http::status_code::value>(out.status));
con->set_body(out.data);
// Apply custom headers
for(const auto &kv : meta->responseHeaders)
{
con->replace_header(kv.first, kv.second);
}
}
else
{
con->set_status(websocketpp::http::status_code::internal_server_error);
}
}
else
{
throw std::runtime_error("connection_hdl weak_ptr expired");
}
}
- Expose a function:
void setResponseHeader(setResponseHeader_in *in, setResponseHeader_out *out)
{
auto meta = srvHandles.get(in->serverHandle);
meta->responseHeaders[in->headerName] = in->headerValue;
}
So from the lua code it is possible to serve js files calling:
simWS.setResponseHeader(server, "Content-Type", "application/javascript")
There’s a clear way to modify the simWS C++ plugin to allow setting custom HTTP headers (like Content-Type) for served resources. The main idea is to extend server_meta and the onWSHTTP method.
So from the lua code it is possible to serve js files calling:
simWS.setResponseHeader(server, "Content-Type", "application/javascript")