From daa23e4e2148fa61e0bb5b636b01b6ea4b34df74 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Mon, 13 Oct 2014 19:04:13 +0200 Subject: [PATCH] corshttpserver --- eth/main.cpp | 5 +++-- libethrpc/CorsHttpServer.cpp | 29 +++++++++++++++++++++++++++++ libethrpc/CorsHttpServer.h | 16 ++++++++++++++++ libethrpc/eth.js | 3 ++- 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 libethrpc/CorsHttpServer.cpp create mode 100644 libethrpc/CorsHttpServer.h diff --git a/eth/main.cpp b/eth/main.cpp index 45acbef1c..67f8d5439 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -29,6 +29,7 @@ #include #if ETH_JSONRPC #include +#include #endif #include #include @@ -339,7 +340,7 @@ int main(int argc, char** argv) auto_ptr jsonrpcServer; if (jsonrpc > -1) { - jsonrpcServer = auto_ptr(new EthStubServer(new jsonrpc::HttpServer(jsonrpc), web3)); + jsonrpcServer = auto_ptr(new EthStubServer(new jsonrpc::CorsHttpServer(jsonrpc), web3)); jsonrpcServer->setKeys({us}); jsonrpcServer->StartListening(); } @@ -427,7 +428,7 @@ int main(int argc, char** argv) { if (jsonrpc < 0) jsonrpc = 8080; - jsonrpcServer = auto_ptr(new EthStubServer(new jsonrpc::HttpServer(jsonrpc), web3)); + jsonrpcServer = auto_ptr(new EthStubServer(new jsonrpc::CorsHttpServer(jsonrpc), web3)); jsonrpcServer->setKeys({us}); jsonrpcServer->StartListening(); } diff --git a/libethrpc/CorsHttpServer.cpp b/libethrpc/CorsHttpServer.cpp new file mode 100644 index 000000000..16e78af66 --- /dev/null +++ b/libethrpc/CorsHttpServer.cpp @@ -0,0 +1,29 @@ + +#include "CorsHttpServer.h" + +namespace jsonrpc +{ + +bool CorsHttpServer::SendResponse(const std::string &response, void *addInfo) +{ + struct mg_connection* conn = (struct mg_connection*) addInfo; + if (mg_printf(conn, "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json\r\n" + "Content-Length: %d\r\n" + "Access-Control-Allow-Origin: *\r\n" + "Access-Control-Allow-Headers: Content-Type\r\n" + "\r\n" + "%s",(int)response.length(), response.c_str()) > 0) + + + { + return true; + } + else + { + return false; + } +} + + +} diff --git a/libethrpc/CorsHttpServer.h b/libethrpc/CorsHttpServer.h new file mode 100644 index 000000000..f354c28f7 --- /dev/null +++ b/libethrpc/CorsHttpServer.h @@ -0,0 +1,16 @@ + +#include + +namespace jsonrpc +{ + +class CorsHttpServer : public HttpServer +{ +public: + using HttpServer::HttpServer; + bool virtual SendResponse(const std::string& response, + void* addInfo = NULL); +}; + +} + diff --git a/libethrpc/eth.js b/libethrpc/eth.js index 1c5e2343c..a52a504d8 100644 --- a/libethrpc/eth.js +++ b/libethrpc/eth.js @@ -82,7 +82,8 @@ window.eth = (function ethScope() { request.send(JSON.stringify(req)) request.onreadystatechange = function() { if (request.readyState === 4) - f(reformat(m, JSON.parse(request.responseText).result)) + if (f) + f(reformat(m, JSON.parse(request.responseText).result)); }; } function isEmpty(obj) {