Skip to content

Commit

Permalink
Add Access-Control-Allow-Origin to config.ini
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Sep 5, 2017
1 parent ec68dc7 commit 914c526
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Docker/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ shared-file-size = 8192
# The local IP and port to listen for incoming http connections.
http-server-endpoint = 0.0.0.0:8888

# The Access-Control-Allow-Origin http value
# access-control-allow-origin = *

# The local IP address and port to listen for incoming connections.
listen-endpoint = 0.0.0.0:9876

Expand Down
12 changes: 11 additions & 1 deletion plugins/http_plugin/http_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ namespace eos {
//asio::io_service http_ios;
map<string,url_handler> url_handlers;
optional<tcp::endpoint> listen_endpoint;
string access_control_allow_origin;

websocket_server_type server;
};
Expand All @@ -91,6 +92,8 @@ namespace eos {
cfg.add_options()
("http-server-endpoint", bpo::value<string>()->default_value("127.0.0.1:8888"),
"The local IP and port to listen for incoming http connections.")
("access-control-allow-origin", bpo::value<string>(),
"Specify the Access-Control-Allow-Origin to be returned on each request.")
;
}

Expand All @@ -112,6 +115,11 @@ namespace eos {
ilog("configured http to listen on ${h}:${p}", ("h",host)("p",port));
}

if (options.count("access-control-allow-origin")) {
my->access_control_allow_origin = options.at("access-control-allow-origin").as<string>();
ilog("configured http with access-control-allow-origin : ${o}", ("o", my->access_control_allow_origin));
}

// uint32_t addr = my->listen_endpoint->address().to_v4().to_ulong();
// auto fcep = fc::ip::endpoint (addr,my->listen_endpoint->port());
}
Expand All @@ -133,14 +141,16 @@ namespace eos {
//ilog("handle http request: ${url}", ("url",con->get_uri()->str()));
//ilog("${body}", ("body", con->get_request_body()));

if (!my->access_control_allow_origin.empty()) {
con->append_header("Access-Control-Allow-Origin", my->access_control_allow_origin);
}
auto body = con->get_request_body();
auto resource = con->get_uri()->get_resource();
auto handler_itr = my->url_handlers.find(resource);
if(handler_itr != my->url_handlers.end()) {
handler_itr->second(resource, body, [con,this](int code, string body) {
con->set_body(body);
con->set_status(websocketpp::http::status_code::value(code));
con->append_header("Access-Control-Allow-Origin", "*");
});
} else {
wlog("404 - not found: ${ep}", ("ep",resource));
Expand Down

0 comments on commit 914c526

Please sign in to comment.