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 {
24 };
25#ifndef _MSC_VER
26 gmtime_r(&now, &now_gmt);
27#else
28 gmtime_s(&now_gmt, &now);
29#endif
30 strftime(buffer, sizeof(buffer), "Date: %a, %d %b %Y %H:%M:%S +0000\r\n", &now_gmt);
31 return std::string(buffer);
32}
33
34void
35HTTPReply(int nStatus, std::string const& content, Json::Output const& output, beast::Journal j)
36{
37 JLOG(j.trace()) << "HTTP Reply " << nStatus << " " << content;
38
39 if (content.empty() && nStatus == 401)
40 {
41 output("HTTP/1.0 401 Authorization Required\r\n");
42 output(getHTTPHeaderTimestamp());
43
44 // CHECKME this returns a different version than the replies below. Is
45 // this by design or an accident or should it be using
46 // BuildInfo::getFullVersionString () as well?
47 output("Server: " + systemName() + "-json-rpc/v1");
48 output("\r\n");
49
50 // Be careful in modifying this! If you change the contents you MUST
51 // update the Content-Length header as well to indicate the correct
52 // size of the data.
53 output(
54 "WWW-Authenticate: Basic realm=\"jsonrpc\"\r\n"
55 "Content-Type: text/html\r\n"
56 "Content-Length: 296\r\n"
57 "\r\n"
58 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 "
59 "Transitional//EN\"\r\n"
60 "\"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"
61 "\">\r\n"
62 "<HTML>\r\n"
63 "<HEAD>\r\n"
64 "<TITLE>Error</TITLE>\r\n"
65 "<META HTTP-EQUIV='Content-Type' "
66 "CONTENT='text/html; charset=ISO-8859-1'>\r\n"
67 "</HEAD>\r\n"
68 "<BODY><H1>401 Unauthorized.</H1></BODY>\r\n");
69
70 return;
71 }
72
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:41
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:6
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)