Browse Source

RPC, cosmetic: push down ReadHTTPStatus() calls into ReadHTTP() callers

ReadHTTPStatus() is currently overloaded:  In client mode, it properly parses
and receives an HTTP status line.  In server mode, it incorrectly parses the
HTTP request line as an HTTP status line.

This server mode bug has never mattered, because the RPC server never
cared about the URI (path) provided in the HTTP request.  That will change in
the future, so go ahead and begin fixing the problem.

This patch is cosmetic, and should result in NO behavior changes.

Further renames:
ReadHTTPHeader -> ReadHTTPHeaders
ReadHTTP -> ReadHTTPMessage
miguelfreitas
Jeff Garzik 12 years ago committed by Jeff Garzik
parent
commit
2306dc4b76
  1. 30
      src/bitcoinrpc.cpp

30
src/bitcoinrpc.cpp

@ -377,7 +377,7 @@ int ReadHTTPStatus(std::basic_istream<char>& stream, int &proto)
return atoi(vWords[1].c_str()); return atoi(vWords[1].c_str());
} }
int ReadHTTPHeader(std::basic_istream<char>& stream, map<string, string>& mapHeadersRet) int ReadHTTPHeaders(std::basic_istream<char>& stream, map<string, string>& mapHeadersRet)
{ {
int nLen = 0; int nLen = 0;
loop loop
@ -402,17 +402,15 @@ int ReadHTTPHeader(std::basic_istream<char>& stream, map<string, string>& mapHea
return nLen; return nLen;
} }
int ReadHTTP(std::basic_istream<char>& stream, map<string, string>& mapHeadersRet, string& strMessageRet) int ReadHTTPMessage(std::basic_istream<char>& stream, map<string,
string>& mapHeadersRet, string& strMessageRet,
int nProto)
{ {
mapHeadersRet.clear(); mapHeadersRet.clear();
strMessageRet = ""; strMessageRet = "";
// Read status
int nProto = 0;
int nStatus = ReadHTTPStatus(stream, nProto);
// Read header // Read header
int nLen = ReadHTTPHeader(stream, mapHeadersRet); int nLen = ReadHTTPHeaders(stream, mapHeadersRet);
if (nLen < 0 || nLen > (int)MAX_SIZE) if (nLen < 0 || nLen > (int)MAX_SIZE)
return HTTP_INTERNAL_SERVER_ERROR; return HTTP_INTERNAL_SERVER_ERROR;
@ -434,7 +432,7 @@ int ReadHTTP(std::basic_istream<char>& stream, map<string, string>& mapHeadersRe
mapHeadersRet["connection"] = "close"; mapHeadersRet["connection"] = "close";
} }
return nStatus; return HTTP_OK;
} }
bool HTTPAuthorized(map<string, string>& mapHeaders) bool HTTPAuthorized(map<string, string>& mapHeaders)
@ -944,7 +942,12 @@ void ThreadRPCServer3(void* parg)
map<string, string> mapHeaders; map<string, string> mapHeaders;
string strRequest; string strRequest;
ReadHTTP(conn->stream(), mapHeaders, strRequest); // Read HTTP status (um, we mean, HTTP request line)
int nProto = 0;
ReadHTTPStatus(conn->stream(), nProto);
// Read HTTP message headers and body
ReadHTTPMessage(conn->stream(), mapHeaders, strRequest, nProto);
// Check authorization // Check authorization
if (mapHeaders.count("authorization") == 0) if (mapHeaders.count("authorization") == 0)
@ -1076,10 +1079,15 @@ Object CallRPC(const string& strMethod, const Array& params)
string strPost = HTTPPost(strRequest, mapRequestHeaders); string strPost = HTTPPost(strRequest, mapRequestHeaders);
stream << strPost << std::flush; stream << strPost << std::flush;
// Receive reply // Receive HTTP reply status
int nProto = 0;
int nStatus = ReadHTTPStatus(stream, nProto);
// Receive HTTP reply message headers and body
map<string, string> mapHeaders; map<string, string> mapHeaders;
string strReply; string strReply;
int nStatus = ReadHTTP(stream, mapHeaders, strReply); ReadHTTPMessage(stream, mapHeaders, strReply, nProto);
if (nStatus == HTTP_UNAUTHORIZED) if (nStatus == HTTP_UNAUTHORIZED)
throw runtime_error("incorrect rpcuser or rpcpassword (authorization failed)"); throw runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");
else if (nStatus >= 400 && nStatus != HTTP_BAD_REQUEST && nStatus != HTTP_NOT_FOUND && nStatus != HTTP_INTERNAL_SERVER_ERROR) else if (nStatus >= 400 && nStatus != HTTP_BAD_REQUEST && nStatus != HTTP_NOT_FOUND && nStatus != HTTP_INTERNAL_SERVER_ERROR)

Loading…
Cancel
Save