aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2012-10-04 10:43:40 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2012-10-04 15:49:15 +0200
commit285746d3dbed46d0e444d7a907c08453d36d99cd (patch)
treebb49c9ccf3d0d6b64f5d0fc62937b5a1472a1ac7
parent738835d7b8ab80bd0812805f197471c54c72d7f9 (diff)
downloadbitcoin-285746d3dbed46d0e444d7a907c08453d36d99cd.tar.xz
Add constants for HTTP status codes
-rw-r--r--src/bitcoinrpc.cpp34
-rw-r--r--src/bitcoinrpc.h11
2 files changed, 28 insertions, 17 deletions
diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp
index 0221975736..0704173e54 100644
--- a/src/bitcoinrpc.cpp
+++ b/src/bitcoinrpc.cpp
@@ -317,7 +317,7 @@ string rfc1123Time()
static string HTTPReply(int nStatus, const string& strMsg, bool keepalive)
{
- if (nStatus == 401)
+ if (nStatus == HTTP_UNAUTHORIZED)
return strprintf("HTTP/1.0 401 Authorization Required\r\n"
"Date: %s\r\n"
"Server: bitcoin-json-rpc/%s\r\n"
@@ -335,11 +335,11 @@ static string HTTPReply(int nStatus, const string& strMsg, bool keepalive)
"<BODY><H1>401 Unauthorized.</H1></BODY>\r\n"
"</HTML>\r\n", rfc1123Time().c_str(), FormatFullVersion().c_str());
const char *cStatus;
- if (nStatus == 200) cStatus = "OK";
- else if (nStatus == 400) cStatus = "Bad Request";
- else if (nStatus == 403) cStatus = "Forbidden";
- else if (nStatus == 404) cStatus = "Not Found";
- else if (nStatus == 500) cStatus = "Internal Server Error";
+ if (nStatus == HTTP_OK) cStatus = "OK";
+ else if (nStatus == HTTP_BAD_REQUEST) cStatus = "Bad Request";
+ else if (nStatus == HTTP_FORBIDDEN) cStatus = "Forbidden";
+ else if (nStatus == HTTP_NOT_FOUND) cStatus = "Not Found";
+ else if (nStatus == HTTP_INTERNAL_SERVER_ERROR) cStatus = "Internal Server Error";
else cStatus = "";
return strprintf(
"HTTP/1.1 %d %s\r\n"
@@ -366,7 +366,7 @@ int ReadHTTPStatus(std::basic_istream<char>& stream, int &proto)
vector<string> vWords;
boost::split(vWords, str, boost::is_any_of(" "));
if (vWords.size() < 2)
- return 500;
+ return HTTP_INTERNAL_SERVER_ERROR;
proto = 0;
const char *ver = strstr(str.c_str(), "HTTP/1.");
if (ver != NULL)
@@ -411,7 +411,7 @@ int ReadHTTP(std::basic_istream<char>& stream, map<string, string>& mapHeadersRe
// Read header
int nLen = ReadHTTPHeader(stream, mapHeadersRet);
if (nLen < 0 || nLen > (int)MAX_SIZE)
- return 500;
+ return HTTP_INTERNAL_SERVER_ERROR;
// Read message
if (nLen > 0)
@@ -484,10 +484,10 @@ string JSONRPCReply(const Value& result, const Value& error, const Value& id)
void ErrorReply(std::ostream& stream, const Object& objError, const Value& id)
{
// Send error reply from json-rpc error object
- int nStatus = 500;
+ int nStatus = HTTP_INTERNAL_SERVER_ERROR;
int code = find_value(objError, "code").get_int();
- if (code == RPC_INVALID_REQUEST) nStatus = 400;
- else if (code == RPC_METHOD_NOT_FOUND) nStatus = 404;
+ if (code == RPC_INVALID_REQUEST) nStatus = HTTP_BAD_REQUEST;
+ else if (code == RPC_METHOD_NOT_FOUND) nStatus = HTTP_NOT_FOUND;
string strReply = JSONRPCReply(Value::null, objError, id);
stream << HTTPReply(nStatus, strReply, false) << std::flush;
}
@@ -699,7 +699,7 @@ static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptor<Protocol,
{
// Only send a 403 if we're not using SSL to prevent a DoS during the SSL handshake.
if (!fUseSSL)
- conn->stream() << HTTPReply(403, "", false) << std::flush;
+ conn->stream() << HTTPReply(HTTP_FORBIDDEN, "", false) << std::flush;
delete conn;
}
@@ -946,7 +946,7 @@ void ThreadRPCServer3(void* parg)
// Check authorization
if (mapHeaders.count("authorization") == 0)
{
- conn->stream() << HTTPReply(401, "", false) << std::flush;
+ conn->stream() << HTTPReply(HTTP_UNAUTHORIZED, "", false) << std::flush;
break;
}
if (!HTTPAuthorized(mapHeaders))
@@ -958,7 +958,7 @@ void ThreadRPCServer3(void* parg)
if (mapArgs["-rpcpassword"].size() < 20)
Sleep(250);
- conn->stream() << HTTPReply(401, "", false) << std::flush;
+ conn->stream() << HTTPReply(HTTP_UNAUTHORIZED, "", false) << std::flush;
break;
}
if (mapHeaders["connection"] == "close")
@@ -989,7 +989,7 @@ void ThreadRPCServer3(void* parg)
else
throw JSONRPCError(RPC_PARSE_ERROR, "Top-level object parse error");
- conn->stream() << HTTPReply(200, strReply, fRun) << std::flush;
+ conn->stream() << HTTPReply(HTTP_OK, strReply, fRun) << std::flush;
}
catch (Object& objError)
{
@@ -1077,9 +1077,9 @@ Object CallRPC(const string& strMethod, const Array& params)
map<string, string> mapHeaders;
string strReply;
int nStatus = ReadHTTP(stream, mapHeaders, strReply);
- if (nStatus == 401)
+ if (nStatus == HTTP_UNAUTHORIZED)
throw runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");
- else if (nStatus >= 400 && nStatus != 400 && nStatus != 404 && nStatus != 500)
+ else if (nStatus >= 400 && nStatus != HTTP_BAD_REQUEST && nStatus != HTTP_NOT_FOUND && nStatus != HTTP_INTERNAL_SERVER_ERROR)
throw runtime_error(strprintf("server returned HTTP error %d", nStatus));
else if (strReply.empty())
throw runtime_error("no response from server");
diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h
index 8154f3242c..89ab976f56 100644
--- a/src/bitcoinrpc.h
+++ b/src/bitcoinrpc.h
@@ -18,6 +18,17 @@ class CBlockIndex;
#include "util.h"
+// HTTP status codes
+enum HTTPStatusCode
+{
+ HTTP_OK = 200,
+ HTTP_BAD_REQUEST = 400,
+ HTTP_UNAUTHORIZED = 401,
+ HTTP_FORBIDDEN = 403,
+ HTTP_NOT_FOUND = 404,
+ HTTP_INTERNAL_SERVER_ERROR = 500,
+};
+
// Bitcoin RPC error codes
enum RPCErrorCode
{