From d11ccf113c21c78f3e30259f8fd72527f6a89f0e Mon Sep 17 00:00:00 2001 From: Sander van Kasteel Date: Fri, 15 Oct 2021 22:10:09 +0200 Subject: [PATCH] Added primitive CORS header support (#1885) * Copied over patch from github user fecristovao * Added nullptr check --- libtransmission/rpc-server.cc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/libtransmission/rpc-server.cc b/libtransmission/rpc-server.cc index f6b69a551..2a854b6f6 100644 --- a/libtransmission/rpc-server.cc +++ b/libtransmission/rpc-server.cc @@ -633,6 +633,21 @@ static void handle_request(struct evhttp_request* req, void* arg) return; } + evhttp_add_header(req->output_headers, "Access-Control-Allow-Origin", "*"); + + if (req->type == EVHTTP_REQ_OPTIONS) + { + char const* headers = evhttp_find_header(req->input_headers, "Access-Control-Request-Headers"); + if (headers != nullptr) + { + evhttp_add_header(req->output_headers, "Access-Control-Allow-Headers", headers); + } + + evhttp_add_header(req->output_headers, "Access-Control-Allow-Methods", "GET, POST, OPTIONS"); + send_simple_response(req, 200, ""); + return; + } + auth = evhttp_find_header(req->input_headers, "Authorization"); if (auth != nullptr && evutil_ascii_strncasecmp(auth, "basic ", 6) == 0) @@ -728,6 +743,7 @@ static void handle_request(struct evhttp_request* req, void* arg) TR_RPC_SESSION_ID_HEADER, sessionId); evhttp_add_header(req->output_headers, TR_RPC_SESSION_ID_HEADER, sessionId); + evhttp_add_header(req->output_headers, "Access-Control-Expose-Headers", TR_RPC_SESSION_ID_HEADER); send_simple_response(req, 409, tmp); tr_free(tmp); } @@ -799,6 +815,7 @@ static void startServer(void* vserver) } struct evhttp* httpd = evhttp_new(server->session->event_base); + evhttp_set_allowed_methods(httpd, EVHTTP_REQ_GET | EVHTTP_REQ_POST | EVHTTP_REQ_OPTIONS); char const* address = tr_rpcGetBindAddress(server);