]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Feature] Add CORS support to the controller
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 11 Jan 2017 13:50:17 +0000 (13:50 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 11 Jan 2017 13:50:17 +0000 (13:50 +0000)
src/controller.c
src/libutil/http.c
src/libutil/http.h

index cae23d3114174c424d0b6b95889e932c460fc7ef..06eb6f0df79dffa169647be5dca0146a49807969 100644 (file)
@@ -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);
index deed78a1a89cfff84e4a349d6649c3c86709d0cf..eec53b515b2e76e51baf2ceb47b500bb919e9f96 100644 (file)
@@ -3187,6 +3187,15 @@ rspamd_http_router_add_path (struct rspamd_http_connection_router *router,
        }
 }
 
+void
+rspamd_http_router_set_unknown_handler (struct rspamd_http_connection_router *router,
+               rspamd_http_router_handler_t handler)
+{
+       if (router != NULL) {
+               router->unknown_method_handler = handler;
+       }
+}
+
 void
 rspamd_http_router_add_header (struct rspamd_http_connection_router *router,
                const gchar *name, const gchar *value)
index 3bad64ae2d137367867b32b4fc686b8e38f5b295..3f5288eca6ad952f07a809c83fdf1ab1aa8a44d3 100644 (file)
@@ -468,6 +468,14 @@ void rspamd_http_router_add_path (struct rspamd_http_connection_router *router,
 void rspamd_http_router_add_header (struct rspamd_http_connection_router *router,
                const gchar *name, const gchar *value);
 
+/**
+ * Sets method to handle unknown request methods
+ * @param router
+ * @param handler
+ */
+void rspamd_http_router_set_unknown_handler (struct rspamd_http_connection_router *router,
+               rspamd_http_router_handler_t handler);
+
 /**
  * Inserts router headers to the outbound message
  * @param router