implement very very simple html server to fix cross-domain problems executing json-rpc from browser

This commit is contained in:
Miguel Freitas 2013-09-28 07:47:07 -03:00
parent 8294e75672
commit adeeb15213
3 changed files with 45 additions and 32 deletions

View File

@ -12,6 +12,8 @@
#include "bitcoinrpc.h" #include "bitcoinrpc.h"
#include "db.h" #include "db.h"
#include "twister.h"
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/asio/ip/v6_only.hpp> #include <boost/asio/ip/v6_only.hpp>
@ -25,6 +27,9 @@
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <list> #include <list>
#include <string>
#include <fstream>
#include <streambuf>
using namespace std; using namespace std;
using namespace boost; using namespace boost;
@ -306,7 +311,7 @@ string rfc1123Time()
return string(buffer); return string(buffer);
} }
static string HTTPReply(int nStatus, const string& strMsg, bool keepalive) static string HTTPReply(int nStatus, const string& strMsg, bool keepalive, const char *contentType = "application/json")
{ {
if (nStatus == HTTP_UNAUTHORIZED) if (nStatus == HTTP_UNAUTHORIZED)
return strprintf("HTTP/1.0 401 Authorization Required\r\n" return strprintf("HTTP/1.0 401 Authorization Required\r\n"
@ -332,22 +337,23 @@ static string HTTPReply(int nStatus, const string& strMsg, bool keepalive)
else if (nStatus == HTTP_NOT_FOUND) cStatus = "Not Found"; else if (nStatus == HTTP_NOT_FOUND) cStatus = "Not Found";
else if (nStatus == HTTP_INTERNAL_SERVER_ERROR) cStatus = "Internal Server Error"; else if (nStatus == HTTP_INTERNAL_SERVER_ERROR) cStatus = "Internal Server Error";
else cStatus = ""; else cStatus = "";
return strprintf(
string strReply = strprintf(
"HTTP/1.1 %d %s\r\n" "HTTP/1.1 %d %s\r\n"
"Date: %s\r\n" "Date: %s\r\n"
"Connection: %s\r\n" "Connection: %s\r\n"
"Content-Length: %"PRIszu"\r\n" "Content-Length: %"PRIszu"\r\n"
"Content-Type: application/json\r\n" "Content-Type: %s\r\n"
"Server: bitcoin-json-rpc/%s\r\n" "Server: bitcoin-json-rpc/%s\r\n"
"\r\n" "\r\n",
"%s",
nStatus, nStatus,
cStatus, cStatus,
rfc1123Time().c_str(), rfc1123Time().c_str(),
keepalive ? "keep-alive" : "close", keepalive ? "keep-alive" : "close",
strMsg.size(), strMsg.size(),
FormatFullVersion().c_str(), contentType,
strMsg.c_str()); FormatFullVersion().c_str());
return strReply + strMsg;
} }
bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto, bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto,
@ -364,6 +370,19 @@ bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto,
// HTTP methods permitted: GET, POST // HTTP methods permitted: GET, POST
http_method = vWords[0]; http_method = vWords[0];
if (http_method == "OPTIONS") {
string replyOptions= strprintf(
"HTTP/1.1 %d %s\r\n"
"Date: %s\r\n"
"Connection: close\r\n"
"Allow: GET,POST,OPTIONS\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Access-Control-Allow-Methods: POST, GET, OPTIONS\r\n"
"Access-Control-Allow-Headers: origin, x-csrf-token, content-type, accept, authorization\r\n"
"Server: bitcoin-json-rpc/%s\r\n"
"\r\n", HTTP_OK, "OK", rfc1123Time().c_str(), FormatFullVersion().c_str());
(*static_cast<std::iostream*>(&stream)) << replyOptions << std::flush;
}
if (http_method != "GET" && http_method != "POST") if (http_method != "GET" && http_method != "POST")
return false; return false;
@ -952,7 +971,13 @@ void ServiceConnection(AcceptedConnection *conn)
ReadHTTPMessage(conn->stream(), mapHeaders, strRequest, nProto); ReadHTTPMessage(conn->stream(), mapHeaders, strRequest, nProto);
if (strURI != "/") { if (strURI != "/") {
std::vector<char> file_data;
if( load_file(strURI.c_str(), file_data) == 0 ) {
std::string str(file_data.data(), file_data.size());
conn->stream() << HTTPReply(HTTP_OK, str, false, "text/html") << std::flush;
} else {
conn->stream() << HTTPReply(HTTP_NOT_FOUND, "", false) << std::flush; conn->stream() << HTTPReply(HTTP_NOT_FOUND, "", false) << std::flush;
}
break; break;
} }

View File

@ -52,56 +52,41 @@ sha1_hash dhtTargetHash(std::string const &username, std::string const &resource
return hasher(buf.data(), buf.size()).final(); return hasher(buf.data(), buf.size()).final();
} }
int load_file(std::string const& filename, std::vector<char>& v, libtorrent::error_code& ec, int limit = 8000000) int load_file(std::string const& filename, std::vector<char>& v, int limit)
{ {
ec.clear();
FILE* f = fopen(filename.c_str(), "rb"); FILE* f = fopen(filename.c_str(), "rb");
if (f == NULL) if (f == NULL)
{
ec.assign(errno, boost::system::get_generic_category());
return -1; return -1;
}
int r = fseek(f, 0, SEEK_END); int r = fseek(f, 0, SEEK_END);
if (r != 0) if (r != 0) {
{
ec.assign(errno, boost::system::get_generic_category());
fclose(f); fclose(f);
return -1; return -1;
} }
long s = ftell(f); long s = ftell(f);
if (s < 0) if (s < 0) {
{
ec.assign(errno, boost::system::get_generic_category());
fclose(f); fclose(f);
return -1; return -1;
} }
if (s > limit) if (s > limit) {
{
fclose(f); fclose(f);
return -2; return -2;
} }
r = fseek(f, 0, SEEK_SET); r = fseek(f, 0, SEEK_SET);
if (r != 0) if (r != 0) {
{
ec.assign(errno, boost::system::get_generic_category());
fclose(f); fclose(f);
return -1; return -1;
} }
v.resize(s); v.resize(s);
if (s == 0) if (s == 0) {
{
fclose(f); fclose(f);
return 0; return 0;
} }
r = fread(&v[0], 1, v.size(), f); r = fread(&v[0], 1, v.size(), f);
if (r < 0) if (r < 0) {
{
ec.assign(errno, boost::system::get_generic_category());
fclose(f); fclose(f);
return -1; return -1;
} }
@ -145,7 +130,7 @@ torrent_handle startTorrentUser(std::string const &username)
create_directory(tparams.save_path, ec); create_directory(tparams.save_path, ec);
std::string filename = combine_path(tparams.save_path, to_hex(ih.to_string()) + ".resume"); std::string filename = combine_path(tparams.save_path, to_hex(ih.to_string()) + ".resume");
load_file(filename.c_str(), tparams.resume_data, ec); load_file(filename.c_str(), tparams.resume_data);
m_userTorrent[username] = ses->add_torrent(tparams); m_userTorrent[username] = ses->add_torrent(tparams);
m_userTorrent[username].force_dht_announce(); m_userTorrent[username].force_dht_announce();
@ -194,7 +179,7 @@ void ThreadWaitExtIP()
std::vector<char> in; std::vector<char> in;
boost::filesystem::path sesStatePath = GetDataDir() / "ses_state"; boost::filesystem::path sesStatePath = GetDataDir() / "ses_state";
if (load_file(sesStatePath.string(), in, ec) == 0) if (load_file(sesStatePath.string(), in) == 0)
{ {
lazy_entry e; lazy_entry e;
if (lazy_bdecode(&in[0], &in[0] + in.size(), e, ec) == 0) if (lazy_bdecode(&in[0], &in[0] + in.size(), e, ec) == 0)

View File

@ -27,4 +27,7 @@ bool validatePostNumberForUser(std::string const &username, int k);
int getBestHeight(); int getBestHeight();
int load_file(std::string const& filename, std::vector<char>& v, int limit = 8000000);
#endif // TWISTER_H #endif // TWISTER_H