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 ripple {
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(
31 buffer,
32 sizeof(buffer),
33 "Date: %a, %d %b %Y %H:%M:%S +0000\r\n",
34 &now_gmt);
35 return std::string(buffer);
36}
37
38void
40 int nStatus,
41 std::string const& content,
42 Json::Output const& output,
44{
45 JLOG(j.trace()) << "HTTP Reply " << nStatus << " " << content;
46
47 if (content.empty() && nStatus == 401)
48 {
49 output("HTTP/1.0 401 Authorization Required\r\n");
50 output(getHTTPHeaderTimestamp());
51
52 // CHECKME this returns a different version than the replies below. Is
53 // this by design or an accident or should it be using
54 // BuildInfo::getFullVersionString () as well?
55 output("Server: " + systemName() + "-json-rpc/v1");
56 output("\r\n");
57
58 // Be careful in modifying this! If you change the contents you MUST
59 // update the Content-Length header as well to indicate the correct
60 // size of the data.
61 output(
62 "WWW-Authenticate: Basic realm=\"jsonrpc\"\r\n"
63 "Content-Type: text/html\r\n"
64 "Content-Length: 296\r\n"
65 "\r\n"
66 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 "
67 "Transitional//EN\"\r\n"
68 "\"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"
69 "\">\r\n"
70 "<HTML>\r\n"
71 "<HEAD>\r\n"
72 "<TITLE>Error</TITLE>\r\n"
73 "<META HTTP-EQUIV='Content-Type' "
74 "CONTENT='text/html; charset=ISO-8859-1'>\r\n"
75 "</HEAD>\r\n"
76 "<BODY><H1>401 Unauthorized.</H1></BODY>\r\n");
77
78 return;
79 }
80
81 switch (nStatus)
82 {
83 case 200:
84 output("HTTP/1.1 200 OK\r\n");
85 break;
86 case 202:
87 output("HTTP/1.1 202 Accepted\r\n");
88 break;
89 case 400:
90 output("HTTP/1.1 400 Bad Request\r\n");
91 break;
92 case 401:
93 output("HTTP/1.1 401 Authorization Required\r\n");
94 break;
95 case 403:
96 output("HTTP/1.1 403 Forbidden\r\n");
97 break;
98 case 404:
99 output("HTTP/1.1 404 Not Found\r\n");
100 break;
101 case 405:
102 output("HTTP/1.1 405 Method Not Allowed\r\n");
103 break;
104 case 429:
105 output("HTTP/1.1 429 Too Many Requests\r\n");
106 break;
107 case 500:
108 output("HTTP/1.1 500 Internal Server Error\r\n");
109 break;
110 case 501:
111 output("HTTP/1.1 501 Not Implemented\r\n");
112 break;
113 case 503:
114 output("HTTP/1.1 503 Server is overloaded\r\n");
115 break;
116 }
117
118 output(getHTTPHeaderTimestamp());
119
120 output(
121 "Connection: Keep-Alive\r\n"
122 "Content-Length: ");
123
124 // VFALCO TODO Determine if/when this header should be added
125 // if (context.app.config().RPC_ALLOW_REMOTE)
126 // output ("Access-Control-Allow-Origin: *\r\n");
127
128 output(std::to_string(content.size() + 2));
129 output(
130 "\r\n"
131 "Content-Type: application/json; charset=UTF-8\r\n");
132
133 output("Server: " + systemName() + "-json-rpc/");
135 output(
136 "\r\n"
137 "\r\n");
138 output(content);
139 output("\r\n");
140}
141
142} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:41
Stream trace() const
Severity stream access functions.
Definition Journal.h:303
T empty(T... args)
std::string const & getFullVersionString()
Full server version string.
Definition BuildInfo.cpp:62
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)
static std::string const & systemName()
std::string getHTTPHeaderTimestamp()
T size(T... args)
T to_string(T... args)