Browse Source

corshttpserver

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
daa23e4e21
  1. 5
      eth/main.cpp
  2. 29
      libethrpc/CorsHttpServer.cpp
  3. 16
      libethrpc/CorsHttpServer.h
  4. 3
      libethrpc/eth.js

5
eth/main.cpp

@ -29,6 +29,7 @@
#include <boost/algorithm/string/trim_all.hpp> #include <boost/algorithm/string/trim_all.hpp>
#if ETH_JSONRPC #if ETH_JSONRPC
#include <jsonrpc/connectors/httpserver.h> #include <jsonrpc/connectors/httpserver.h>
#include <libethrpc/CorsHttpServer.h>
#endif #endif
#include <libdevcrypto/FileSystem.h> #include <libdevcrypto/FileSystem.h>
#include <libevmface/Instruction.h> #include <libevmface/Instruction.h>
@ -339,7 +340,7 @@ int main(int argc, char** argv)
auto_ptr<EthStubServer> jsonrpcServer; auto_ptr<EthStubServer> jsonrpcServer;
if (jsonrpc > -1) if (jsonrpc > -1)
{ {
jsonrpcServer = auto_ptr<EthStubServer>(new EthStubServer(new jsonrpc::HttpServer(jsonrpc), web3)); jsonrpcServer = auto_ptr<EthStubServer>(new EthStubServer(new jsonrpc::CorsHttpServer(jsonrpc), web3));
jsonrpcServer->setKeys({us}); jsonrpcServer->setKeys({us});
jsonrpcServer->StartListening(); jsonrpcServer->StartListening();
} }
@ -427,7 +428,7 @@ int main(int argc, char** argv)
{ {
if (jsonrpc < 0) if (jsonrpc < 0)
jsonrpc = 8080; jsonrpc = 8080;
jsonrpcServer = auto_ptr<EthStubServer>(new EthStubServer(new jsonrpc::HttpServer(jsonrpc), web3)); jsonrpcServer = auto_ptr<EthStubServer>(new EthStubServer(new jsonrpc::CorsHttpServer(jsonrpc), web3));
jsonrpcServer->setKeys({us}); jsonrpcServer->setKeys({us});
jsonrpcServer->StartListening(); jsonrpcServer->StartListening();
} }

29
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;
}
}
}

16
libethrpc/CorsHttpServer.h

@ -0,0 +1,16 @@
#include <jsonrpc/connectors/httpserver.h>
namespace jsonrpc
{
class CorsHttpServer : public HttpServer
{
public:
using HttpServer::HttpServer;
bool virtual SendResponse(const std::string& response,
void* addInfo = NULL);
};
}

3
libethrpc/eth.js

@ -82,7 +82,8 @@ window.eth = (function ethScope() {
request.send(JSON.stringify(req)) request.send(JSON.stringify(req))
request.onreadystatechange = function() { request.onreadystatechange = function() {
if (request.readyState === 4) 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) { function isEmpty(obj) {

Loading…
Cancel
Save