Skip to content

Commit eba8c44

Browse files
committed
Merge pull request #4440
16f33f1 fix RPC error replies (kazcw)
2 parents a11f648 + 16f33f1 commit eba8c44

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

src/rpcprotocol.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,19 @@ static string rfc1123Time()
5454
return DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", GetTime());
5555
}
5656

57-
string HTTPReply(int nStatus, const string& strMsg, bool keepalive,
58-
bool headersOnly, const char *contentType)
57+
static const char *httpStatusDescription(int nStatus)
58+
{
59+
switch (nStatus) {
60+
case HTTP_OK: return "OK";
61+
case HTTP_BAD_REQUEST: return "Bad Request";
62+
case HTTP_FORBIDDEN: return "Forbidden";
63+
case HTTP_NOT_FOUND: return "Not Found";
64+
case HTTP_INTERNAL_SERVER_ERROR: return "Internal Server Error";
65+
default: return "";
66+
}
67+
}
68+
69+
string HTTPError(int nStatus, bool keepalive, bool headersOnly)
5970
{
6071
if (nStatus == HTTP_UNAUTHORIZED)
6172
return strprintf("HTTP/1.0 401 Authorization Required\r\n"
@@ -75,20 +86,13 @@ string HTTPReply(int nStatus, const string& strMsg, bool keepalive,
7586
"<BODY><H1>401 Unauthorized.</H1></BODY>\r\n"
7687
"</HTML>\r\n", rfc1123Time(), FormatFullVersion());
7788

78-
const char *cStatus;
79-
if (nStatus == HTTP_OK) cStatus = "OK";
80-
else if (nStatus == HTTP_BAD_REQUEST) cStatus = "Bad Request";
81-
else if (nStatus == HTTP_FORBIDDEN) cStatus = "Forbidden";
82-
else if (nStatus == HTTP_NOT_FOUND) cStatus = "Not Found";
83-
else if (nStatus == HTTP_INTERNAL_SERVER_ERROR) cStatus = "Internal Server Error";
84-
else cStatus = "";
85-
86-
bool useInternalContent = false;
87-
if (nStatus != HTTP_OK) {
88-
contentType = "text/plain";
89-
useInternalContent = true;
90-
}
89+
return HTTPReply(nStatus, httpStatusDescription(nStatus), keepalive,
90+
headersOnly, "text/plain");
91+
}
9192

93+
string HTTPReply(int nStatus, const string& strMsg, bool keepalive,
94+
bool headersOnly, const char *contentType)
95+
{
9296
return strprintf(
9397
"HTTP/1.1 %d %s\r\n"
9498
"Date: %s\r\n"
@@ -99,14 +103,14 @@ string HTTPReply(int nStatus, const string& strMsg, bool keepalive,
99103
"\r\n"
100104
"%s",
101105
nStatus,
102-
cStatus,
106+
httpStatusDescription(nStatus),
103107
rfc1123Time(),
104108
keepalive ? "keep-alive" : "close",
105-
strMsg.size(),
109+
(headersOnly ? 0 : strMsg.size()),
106110
contentType,
107111
FormatFullVersion(),
108-
(headersOnly ? "" :
109-
(useInternalContent ? cStatus : strMsg.c_str())));
112+
(headersOnly ? "" : strMsg.c_str())
113+
);
110114
}
111115

112116
bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto,

src/rpcprotocol.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ class SSLIOStreamDevice : public boost::iostreams::device<boost::iostreams::bidi
141141
};
142142

143143
std::string HTTPPost(const std::string& strMsg, const std::map<std::string,std::string>& mapRequestHeaders);
144+
std::string HTTPError(int nStatus, bool keepalive,
145+
bool headerOnly = false);
144146
std::string HTTPReply(int nStatus, const std::string& strMsg, bool keepalive,
145147
bool headerOnly = false,
146148
const char *contentType = "application/json");

src/rpcserver.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptor<Protocol,
481481
{
482482
// Only send a 403 if we're not using SSL to prevent a DoS during the SSL handshake.
483483
if (!fUseSSL)
484-
conn->stream() << HTTPReply(HTTP_FORBIDDEN, "", false) << std::flush;
484+
conn->stream() << HTTPError(HTTP_FORBIDDEN, false) << std::flush;
485485
conn->close();
486486
}
487487
else {
@@ -807,7 +807,7 @@ static bool HTTPReq_JSONRPC(AcceptedConnection *conn,
807807
// Check authorization
808808
if (mapHeaders.count("authorization") == 0)
809809
{
810-
conn->stream() << HTTPReply(HTTP_UNAUTHORIZED, "", false) << std::flush;
810+
conn->stream() << HTTPError(HTTP_UNAUTHORIZED, false) << std::flush;
811811
return false;
812812
}
813813

@@ -820,7 +820,7 @@ static bool HTTPReq_JSONRPC(AcceptedConnection *conn,
820820
if (mapArgs["-rpcpassword"].size() < 20)
821821
MilliSleep(250);
822822

823-
conn->stream() << HTTPReply(HTTP_UNAUTHORIZED, "", false) << std::flush;
823+
conn->stream() << HTTPError(HTTP_UNAUTHORIZED, false) << std::flush;
824824
return false;
825825
}
826826

@@ -888,7 +888,7 @@ void ServiceConnection(AcceptedConnection *conn)
888888
if (!HTTPReq_JSONRPC(conn, strRequest, mapHeaders, fRun))
889889
break;
890890
} else {
891-
conn->stream() << HTTPReply(HTTP_NOT_FOUND, "", false) << std::flush;
891+
conn->stream() << HTTPError(HTTP_NOT_FOUND, false) << std::flush;
892892
break;
893893
}
894894
}

0 commit comments

Comments
 (0)