diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2017-01-11 13:50:17 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2017-01-11 13:50:17 +0000 |
commit | be333bcd20ec9f6ce27075e71c4952f8bd626d4f (patch) | |
tree | fcf11ae1b94cd3d06606af5de36a0120f82161d3 /src/controller.c | |
parent | 53ab3de17aed083910e2d6df4de96aafbcce97db (diff) | |
download | rspamd-be333bcd20ec9f6ce27075e71c4952f8bd626d4f.tar.gz rspamd-be333bcd20ec9f6ce27075e71c4952f8bd626d4f.zip |
[Feature] Add CORS support to the controller
Diffstat (limited to 'src/controller.c')
-rw-r--r-- | src/controller.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/controller.c b/src/controller.c index cae23d311..06eb6f0df 100644 --- a/src/controller.c +++ b/src/controller.c @@ -2627,6 +2627,61 @@ rspamd_controller_handle_plugins (struct rspamd_http_connection_entry *conn_ent, return 0; } +/* + * Called on unknown methods and is used to deal with CORS as per + * https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS + */ +static int +rspamd_controller_handle_unknown (struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) +{ + struct rspamd_http_message *rep; + + if (msg->method == HTTP_OPTIONS) { + /* Assume CORS request */ + + rep = rspamd_http_new_message (HTTP_RESPONSE); + rep->date = time (NULL); + rep->code = 200; + rep->status = rspamd_fstring_new_init ("OK", 2); + rspamd_http_message_add_header (rep, "Access-Control-Allow-Methods", + "POST, GET, OPTIONS"); + rspamd_http_message_add_header (rep, "Access-Control-Allow-Headers", + "Content-Type, Password, Map"); + rspamd_http_connection_reset (conn_ent->conn); + rspamd_http_router_insert_headers (conn_ent->rt, rep); + rspamd_http_connection_write_message (conn_ent->conn, + rep, + NULL, + "text/plain", + conn_ent, + conn_ent->conn->fd, + conn_ent->rt->ptv, + conn_ent->rt->ev_base); + conn_ent->is_reply = TRUE; + } + else { + rep = rspamd_http_new_message (HTTP_RESPONSE); + rep->date = time (NULL); + rep->code = 500; + rep->status = rspamd_fstring_new_init ("Invalid method", + strlen ("Invalid method")); + rspamd_http_connection_reset (conn_ent->conn); + rspamd_http_router_insert_headers (conn_ent->rt, rep); + rspamd_http_connection_write_message (conn_ent->conn, + rep, + NULL, + "text/plain", + conn_ent, + conn_ent->conn->fd, + conn_ent->rt->ptv, + conn_ent->rt->ev_base); + conn_ent->is_reply = TRUE; + } + + return 0; +} + static int rspamd_controller_handle_lua_plugin (struct rspamd_http_connection_entry *conn_ent, struct rspamd_http_message *msg) @@ -3504,6 +3559,9 @@ start_controller_worker (struct rspamd_worker *worker) "Access-Control-Allow-Origin", "*"); } + rspamd_http_router_set_unknown_handler (ctx->http, + rspamd_controller_handle_unknown); + ctx->resolver = dns_resolver_init (worker->srv->logger, ctx->ev_base, worker->srv->cfg); |