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;
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 switch (nStatus)
72 {
73 case 200:
74 output("HTTP/1.1 200 OK\r\n");
75 break;
76 case 202:
77 output("HTTP/1.1 202 Accepted\r\n");
78 break;
79 case 400:
80 output("HTTP/1.1 400 Bad Request\r\n");
81 break;
82 case 401:
83 output("HTTP/1.1 401 Authorization Required\r\n");
84 break;
85 case 403:
86 output("HTTP/1.1 403 Forbidden\r\n");
87 break;
88 case 404:
89 output("HTTP/1.1 404 Not Found\r\n");
90 break;
91 case 405:
92 output("HTTP/1.1 405 Method Not Allowed\r\n");
93 break;
94 case 429:
95 output("HTTP/1.1 429 Too Many Requests\r\n");
96 break;
97 case 500:
98 output("HTTP/1.1 500 Internal Server Error\r\n");
99 break;
100 case 501:
101 output("HTTP/1.1 501 Not Implemented\r\n");
102 break;
103 case 503:
104 output("HTTP/1.1 503 Server is overloaded\r\n");
105 break;
106 }
107
108 output(getHTTPHeaderTimestamp());
109
110 output(
111 "Connection: Keep-Alive\r\n"
112 "Content-Length: ");
113
114 // VFALCO TODO Determine if/when this header should be added
115 // if (context.app.config().RPC_ALLOW_REMOTE)
116 // output ("Access-Control-Allow-Origin: *\r\n");
117
118 output(std::to_string(content.size() + 2));
119 output(
120 "\r\n"
121 "Content-Type: application/json; charset=UTF-8\r\n");
122
123 output("Server: " + systemName() + "-json-rpc/");
125 output(
126 "\r\n"
127 "\r\n");
128 output(content);
129 output("\r\n");
130}
131
132} // 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:64
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)