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