@@ -34,6 +34,20 @@ namespace cqhttp::plugins {
3434 };
3535
3636 const auto action_path_regex = " ^/([^/\\ s]+)/?$" ;
37+ server_->resource [action_path_regex][" OPTIONS" ] =
38+ [=](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
39+ log_request (request);
40+ if (enable_cors_) {
41+ response->write (" " ,
42+ {
43+ {" Access-Control-Allow-Origin" , " *" },
44+ {" Access-Control-Allow-Methods" , " *" },
45+ {" Access-Control-Allow-Headers" , " *" },
46+ });
47+ } else {
48+ response->write (SimpleWeb::StatusCode::client_error_method_not_allowed);
49+ }
50+ };
3751 server_->resource [action_path_regex][" GET" ] = server_->resource [action_path_regex][" POST" ] =
3852 [=](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
3953 log_request (request);
@@ -99,7 +113,8 @@ namespace cqhttp::plugins {
99113 response->write (SimpleWeb::StatusCode::client_error_not_found);
100114 } else {
101115 logging::debug (TAG, u8" 动作 " + action + u8" 执行成功" );
102- const decltype (request->header ) headers{{" Content-Type" , " application/json; charset=UTF-8" }};
116+ decltype (request->header ) headers{{" Content-Type" , " application/json; charset=UTF-8" }};
117+ if (enable_cors_) headers.emplace (" Access-Control-Allow-Origin" , " *" );
103118 const auto resp_body = json (result).dump ();
104119 logging::debug (TAG, u8" 响应数据已准备完毕:" + resp_body);
105120 response->write (resp_body, headers);
@@ -176,6 +191,7 @@ namespace cqhttp::plugins {
176191 use_http_ = ctx.config ->get_bool (" use_http" , true );
177192 access_token_ = ctx.config ->get_string (" access_token" , " " );
178193 serve_data_files_ = ctx.config ->get_bool (" serve_data_files" , false );
194+ enable_cors_ = ctx.config ->get_bool (" enable_cors" , false );
179195
180196 if (use_http_) {
181197 init_server ();
0 commit comments