Browse Source

Avoid a copy in RPC output

Split up HTTPReply into HTTPReply and HTTPReplyHeader, so that
the message data can be streamed directly.

Also removes a c_str(), which would have prevented binary
output with NUL characters in it.
0.10
Wladimir J. van der Laan 10 years ago
parent
commit
e17151ad2a
  1. 23
      src/rpcprotocol.cpp
  2. 2
      src/rpcprotocol.h
  3. 2
      src/rpcserver.cpp

23
src/rpcprotocol.cpp

@ -93,8 +93,7 @@ string HTTPError(int nStatus, bool keepalive, bool headersOnly)
headersOnly, "text/plain"); headersOnly, "text/plain");
} }
string HTTPReply(int nStatus, const string& strMsg, bool keepalive, string HTTPReplyHeader(int nStatus, bool keepalive, size_t contentLength, const char *contentType)
bool headersOnly, const char *contentType)
{ {
return strprintf( return strprintf(
"HTTP/1.1 %d %s\r\n" "HTTP/1.1 %d %s\r\n"
@ -103,17 +102,25 @@ string HTTPReply(int nStatus, const string& strMsg, bool keepalive,
"Content-Length: %u\r\n" "Content-Length: %u\r\n"
"Content-Type: %s\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,
httpStatusDescription(nStatus), httpStatusDescription(nStatus),
rfc1123Time(), rfc1123Time(),
keepalive ? "keep-alive" : "close", keepalive ? "keep-alive" : "close",
(headersOnly ? 0 : strMsg.size()), contentLength,
contentType, contentType,
FormatFullVersion(), FormatFullVersion());
(headersOnly ? "" : strMsg.c_str()) }
);
string HTTPReply(int nStatus, const string& strMsg, bool keepalive,
bool headersOnly, const char *contentType)
{
if (headersOnly)
{
return HTTPReplyHeader(nStatus, keepalive, 0, contentType);
} else {
return HTTPReplyHeader(nStatus, keepalive, strMsg.size(), contentType) + strMsg;
}
} }
bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto, bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto,

2
src/rpcprotocol.h

@ -143,6 +143,8 @@ private:
std::string HTTPPost(const std::string& strMsg, const std::map<std::string,std::string>& mapRequestHeaders); std::string HTTPPost(const std::string& strMsg, const std::map<std::string,std::string>& mapRequestHeaders);
std::string HTTPError(int nStatus, bool keepalive, std::string HTTPError(int nStatus, bool keepalive,
bool headerOnly = false); bool headerOnly = false);
std::string HTTPReplyHeader(int nStatus, bool keepalive, size_t contentLength,
const char *contentType = "application/json");
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, bool headerOnly = false,
const char *contentType = "application/json"); const char *contentType = "application/json");

2
src/rpcserver.cpp

@ -862,7 +862,7 @@ static bool HTTPReq_JSONRPC(AcceptedConnection *conn,
else else
throw JSONRPCError(RPC_PARSE_ERROR, "Top-level object parse error"); throw JSONRPCError(RPC_PARSE_ERROR, "Top-level object parse error");
conn->stream() << HTTPReply(HTTP_OK, strReply, fRun) << std::flush; conn->stream() << HTTPReplyHeader(HTTP_OK, fRun, strReply.size()) << strReply << std::flush;
} }
catch (Object& objError) catch (Object& objError)
{ {

Loading…
Cancel
Save