rippled
Loading...
Searching...
No Matches
JSONRPCUtil.cpp
1#include <xrpl/basics/Log.h>
2#include <xrpl/beast/utility/Journal.h>
3#include <xrpl/json/Output.h>
4#include <xrpl/protocol/BuildInfo.h>
5#include <xrpl/protocol/SystemParameters.h>
6#include <xrpl/server/detail/JSONRPCUtil.h>
7
8#include <ctime>
9#include <string>
10
11namespace xrpl {
12
15{
16 // CHECKME This is probably called often enough that optimizing it makes
17 // sense. There's no point in doing all this work if this function
18 // gets called multiple times a second.
19 char buffer[96];
20 time_t now = 0;
21 time(&now);
22 struct tm now_gmt{};
23#ifndef _MSC_VER
24 gmtime_r(&now, &now_gmt);
25#else
26 gmtime_s(&now_gmt, &now);
27#endif
28 strftime(buffer, sizeof(buffer), "Date: %a, %d %b %Y %H:%M:%S +0000\r\n", &now_gmt);
29 return std::string(buffer);
30}
31
32void
33HTTPReply(int nStatus, std::string const& content, Json::Output const& output, beast::Journal j)
34{
35 JLOG(j.trace()) << "HTTP Reply " << nStatus << " " << content;
36
37 if (content.empty() && nStatus == 401)
38 {
39 output("HTTP/1.0 401 Authorization Required\r\n");
40 output(getHTTPHeaderTimestamp());
41
42 // CHECKME this returns a different version than the replies below. Is
43 // this by design or an accident or should it be using
44 // BuildInfo::getFullVersionString () as well?
45 output("Server: " + systemName() + "-json-rpc/v1");
46 output("\r\n");
47
48 // Be careful in modifying this! If you change the contents you MUST
49 // update the Content-Length header as well to indicate the correct
50 // size of the data.
51 output(
52 "WWW-Authenticate: Basic realm=\"jsonrpc\"\r\n"
53 "Content-Type: text/html\r\n"
54 "Content-Length: 296\r\n"
55 "\r\n"
56 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 "
57 "Transitional//EN\"\r\n"
58 "\"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"
59 "\">\r\n"
60 "<HTML>\r\n"
61 "<HEAD>\r\n"
62 "<TITLE>Error</TITLE>\r\n"
63 "<META HTTP-EQUIV='Content-Type' "
64 "CONTENT='text/html; charset=ISO-8859-1'>\r\n"
65 "</HEAD>\r\n"
66 "<BODY><H1>401 Unauthorized.</H1></BODY>\r\n");
67
68 return;
69 }
70
71 // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
72 switch (nStatus)
73 {
74 case 200:
75 output("HTTP/1.1 200 OK\r\n");
76 break;
77 case 202:
78 output("HTTP/1.1 202 Accepted\r\n");
79 break;
80 case 400:
81 output("HTTP/1.1 400 Bad Request\r\n");
82 break;
83 case 401:
84 output("HTTP/1.1 401 Authorization Required\r\n");
85 break;
86 case 403:
87 output("HTTP/1.1 403 Forbidden\r\n");
88 break;
89 case 404:
90 output("HTTP/1.1 404 Not Found\r\n");
91 break;
92 case 405:
93 output("HTTP/1.1 405 Method Not Allowed\r\n");
94 break;
95 case 429:
96 output("HTTP/1.1 429 Too Many Requests\r\n");
97 break;
98 case 500:
99 output("HTTP/1.1 500 Internal Server Error\r\n");
100 break;
101 case 501:
102 output("HTTP/1.1 501 Not Implemented\r\n");
103 break;
104 case 503:
105 output("HTTP/1.1 503 Server is overloaded\r\n");
106 break;
107 }
108
109 output(getHTTPHeaderTimestamp());
110
111 output(
112 "Connection: Keep-Alive\r\n"
113 "Content-Length: ");
114
115 // VFALCO TODO Determine if/when this header should be added
116 // if (context.app.config().RPC_ALLOW_REMOTE)
117 // output ("Access-Control-Allow-Origin: *\r\n");
118
119 output(std::to_string(content.size() + 2));
120 output(
121 "\r\n"
122 "Content-Type: application/json; charset=UTF-8\r\n");
123
124 output("Server: " + systemName() + "-json-rpc/");
126 output(
127 "\r\n"
128 "\r\n");
129 output(content);
130 output("\r\n");
131}
132
133} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Stream trace() const
Severity stream access functions.
Definition Journal.h:295
T empty(T... args)
std::string const & getFullVersionString()
Full server version string.
Definition BuildInfo.cpp:82
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void HTTPReply(int nStatus, std::string const &strMsg, Json::Output const &, beast::Journal j)
std::string getHTTPHeaderTimestamp()
static std::string const & systemName()
T size(T... args)
T to_string(T... args)