diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2014-08-06 13:01:49 +0200 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2014-08-06 13:01:49 +0200 |
commit | e17151ad2a45599e8cad90552ffcd979730b7a32 (patch) | |
tree | feccddd99d652a0dce8f4c054af2d8451a81f9ce /src/rpcprotocol.cpp | |
parent | 5e94d0036a76ea9d63e4ed17b12554caef7f55da (diff) |
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.
Diffstat (limited to 'src/rpcprotocol.cpp')
-rw-r--r-- | src/rpcprotocol.cpp | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/src/rpcprotocol.cpp b/src/rpcprotocol.cpp index 9e18ca847e..48c6c68eb1 100644 --- a/src/rpcprotocol.cpp +++ b/src/rpcprotocol.cpp @@ -93,8 +93,7 @@ string HTTPError(int nStatus, bool keepalive, bool headersOnly) headersOnly, "text/plain"); } -string HTTPReply(int nStatus, const string& strMsg, bool keepalive, - bool headersOnly, const char *contentType) +string HTTPReplyHeader(int nStatus, bool keepalive, size_t contentLength, const char *contentType) { return strprintf( "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-Type: %s\r\n" "Server: bitcoin-json-rpc/%s\r\n" - "\r\n" - "%s", + "\r\n", nStatus, httpStatusDescription(nStatus), rfc1123Time(), keepalive ? "keep-alive" : "close", - (headersOnly ? 0 : strMsg.size()), + contentLength, contentType, - FormatFullVersion(), - (headersOnly ? "" : strMsg.c_str()) - ); + FormatFullVersion()); +} + +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, |