Skip to content

Commit

Permalink
Added primitive CORS header support (#1885)
Browse files Browse the repository at this point in the history
* Copied over patch from github user fecristovao

* Added nullptr check
  • Loading branch information
sandervankasteel committed Oct 15, 2021
1 parent fb39c46 commit d11ccf1
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions libtransmission/rpc-server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit d11ccf1

Please sign in to comment.