Browse Source

Merge pull request #4288

ed5769f Move AcceptedConnection class to rpcserver.h. (Jeff Garzik)
854d013 RPC code movement: separate out JSON-RPC execution logic from HTTP server logic (Jeff Garzik)
c912e22 RPC cleanup: Improve HTTP server replies (Jeff Garzik)
0.10
Wladimir J. van der Laan 11 years ago
parent
commit
d77f761be9
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6
  1. 17
      src/rpcprotocol.cpp
  2. 4
      src/rpcprotocol.h
  3. 75
      src/rpcserver.cpp
  4. 10
      src/rpcserver.h

17
src/rpcprotocol.cpp

@ -54,7 +54,8 @@ static string rfc1123Time() @@ -54,7 +54,8 @@ static string rfc1123Time()
return DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", GetTime());
}
string HTTPReply(int nStatus, const string& strMsg, bool keepalive)
string HTTPReply(int nStatus, const string& strMsg, bool keepalive,
bool headersOnly, const char *contentType)
{
if (nStatus == HTTP_UNAUTHORIZED)
return strprintf("HTTP/1.0 401 Authorization Required\r\n"
@ -73,6 +74,7 @@ string HTTPReply(int nStatus, const string& strMsg, bool keepalive) @@ -73,6 +74,7 @@ string HTTPReply(int nStatus, const string& strMsg, bool keepalive)
"</HEAD>\r\n"
"<BODY><H1>401 Unauthorized.</H1></BODY>\r\n"
"</HTML>\r\n", rfc1123Time(), FormatFullVersion());
const char *cStatus;
if (nStatus == HTTP_OK) cStatus = "OK";
else if (nStatus == HTTP_BAD_REQUEST) cStatus = "Bad Request";
@ -80,12 +82,19 @@ string HTTPReply(int nStatus, const string& strMsg, bool keepalive) @@ -80,12 +82,19 @@ string HTTPReply(int nStatus, const string& strMsg, bool keepalive)
else if (nStatus == HTTP_NOT_FOUND) cStatus = "Not Found";
else if (nStatus == HTTP_INTERNAL_SERVER_ERROR) cStatus = "Internal Server Error";
else cStatus = "";
bool useInternalContent = false;
if (nStatus != HTTP_OK) {
contentType = "text/plain";
useInternalContent = true;
}
return strprintf(
"HTTP/1.1 %d %s\r\n"
"Date: %s\r\n"
"Connection: %s\r\n"
"Content-Length: %u\r\n"
"Content-Type: application/json\r\n"
"Content-Type: %s\r\n"
"Server: bitcoin-json-rpc/%s\r\n"
"\r\n"
"%s",
@ -94,8 +103,10 @@ string HTTPReply(int nStatus, const string& strMsg, bool keepalive) @@ -94,8 +103,10 @@ string HTTPReply(int nStatus, const string& strMsg, bool keepalive)
rfc1123Time(),
keepalive ? "keep-alive" : "close",
strMsg.size(),
contentType,
FormatFullVersion(),
strMsg);
(headersOnly ? "" :
(useInternalContent ? cStatus : strMsg.c_str())));
}
bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto,

4
src/rpcprotocol.h

@ -141,7 +141,9 @@ private: @@ -141,7 +141,9 @@ private:
};
std::string HTTPPost(const std::string& strMsg, const std::map<std::string,std::string>& mapRequestHeaders);
std::string HTTPReply(int nStatus, const std::string& strMsg, bool keepalive);
std::string HTTPReply(int nStatus, const std::string& strMsg, bool keepalive,
bool headerOnly = false,
const char *contentType = "application/json");
bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto,
std::string& http_method, std::string& http_uri);
int ReadHTTPStatus(std::basic_istream<char>& stream, int &proto);

75
src/rpcserver.cpp

@ -393,16 +393,6 @@ bool ClientAllowed(const boost::asio::ip::address& address) @@ -393,16 +393,6 @@ bool ClientAllowed(const boost::asio::ip::address& address)
return false;
}
class AcceptedConnection
{
public:
virtual ~AcceptedConnection() {}
virtual std::iostream& stream() = 0;
virtual std::string peer_address_to_string() const = 0;
virtual void close() = 0;
};
template <typename Protocol>
class AcceptedConnectionImpl : public AcceptedConnection
{
@ -819,33 +809,18 @@ static string JSONRPCExecBatch(const Array& vReq) @@ -819,33 +809,18 @@ static string JSONRPCExecBatch(const Array& vReq)
return write_string(Value(ret), false) + "\n";
}
void ServiceConnection(AcceptedConnection *conn)
static bool HTTPReq_JSONRPC(AcceptedConnection *conn,
string& strRequest,
map<string, string>& mapHeaders,
bool fRun)
{
bool fRun = true;
while (fRun && !ShutdownRequested())
{
int nProto = 0;
map<string, string> mapHeaders;
string strRequest, strMethod, strURI;
// Read HTTP request line
if (!ReadHTTPRequestLine(conn->stream(), nProto, strMethod, strURI))
break;
// Read HTTP message headers and body
ReadHTTPMessage(conn->stream(), mapHeaders, strRequest, nProto);
if (strURI != "/") {
conn->stream() << HTTPReply(HTTP_NOT_FOUND, "", false) << std::flush;
break;
}
// Check authorization
if (mapHeaders.count("authorization") == 0)
{
conn->stream() << HTTPReply(HTTP_UNAUTHORIZED, "", false) << std::flush;
break;
return false;
}
if (!HTTPAuthorized(mapHeaders))
{
LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", conn->peer_address_to_string());
@ -856,10 +831,8 @@ void ServiceConnection(AcceptedConnection *conn) @@ -856,10 +831,8 @@ void ServiceConnection(AcceptedConnection *conn)
MilliSleep(250);
conn->stream() << HTTPReply(HTTP_UNAUTHORIZED, "", false) << std::flush;
break;
return false;
}
if (mapHeaders["connection"] == "close")
fRun = false;
JSONRequest jreq;
try
@ -891,11 +864,43 @@ void ServiceConnection(AcceptedConnection *conn) @@ -891,11 +864,43 @@ void ServiceConnection(AcceptedConnection *conn)
catch (Object& objError)
{
ErrorReply(conn->stream(), objError, jreq.id);
break;
return false;
}
catch (std::exception& e)
{
ErrorReply(conn->stream(), JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id);
return false;
}
return true;
}
void ServiceConnection(AcceptedConnection *conn)
{
bool fRun = true;
while (fRun && !ShutdownRequested())
{
int nProto = 0;
map<string, string> mapHeaders;
string strRequest, strMethod, strURI;
// Read HTTP request line
if (!ReadHTTPRequestLine(conn->stream(), nProto, strMethod, strURI))
break;
// Read HTTP message headers and body
ReadHTTPMessage(conn->stream(), mapHeaders, strRequest, nProto);
// HTTP Keep-Alive is false; close connection immediately
if (mapHeaders["connection"] == "close")
fRun = false;
if (strURI == "/") {
if (!HTTPReq_JSONRPC(conn, strRequest, mapHeaders, fRun))
break;
}
else {
conn->stream() << HTTPReply(HTTP_NOT_FOUND, "", false) << std::flush;
break;
}
}

10
src/rpcserver.h

@ -21,6 +21,16 @@ @@ -21,6 +21,16 @@
class CBlockIndex;
class CNetAddr;
class AcceptedConnection
{
public:
virtual ~AcceptedConnection() {}
virtual std::iostream& stream() = 0;
virtual std::string peer_address_to_string() const = 0;
virtual void close() = 0;
};
/* Start RPC threads */
void StartRPCThreads();
/* Alternative to StartRPCThreads for the GUI, when no server is

Loading…
Cancel
Save