rippled
JSONRPCUtil.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/basics/Log.h>
21 #include <ripple/server/impl/JSONRPCUtil.h>
22 #include <ripple/protocol/jss.h>
23 #include <ripple/protocol/BuildInfo.h>
24 #include <ripple/protocol/SystemParameters.h>
25 #include <ripple/json/to_string.h>
26 #include <boost/algorithm/string.hpp>
27 
28 namespace ripple {
29 
31 {
32  // CHECKME This is probably called often enough that optimizing it makes
33  // sense. There's no point in doing all this work if this function
34  // gets called multiple times a second.
35  char buffer[96];
36  time_t now;
37  time (&now);
38  struct tm now_gmt{};
39 #ifndef _MSC_VER
40  gmtime_r(&now, &now_gmt);
41 #else
42  gmtime_s(&now_gmt, &now);
43 #endif
44  strftime (buffer, sizeof (buffer),
45  "Date: %a, %d %b %Y %H:%M:%S +0000\r\n",
46  &now_gmt);
47  return std::string (buffer);
48 }
49 
50 void HTTPReply (
51  int nStatus, std::string const& content, Json::Output const& output, beast::Journal j)
52 {
53  JLOG (j.trace())
54  << "HTTP Reply " << nStatus << " " << content;
55 
56  if (nStatus == 401)
57  {
58  output ("HTTP/1.0 401 Authorization Required\r\n");
59  output (getHTTPHeaderTimestamp ());
60 
61  // CHECKME this returns a different version than the replies below. Is
62  // this by design or an accident or should it be using
63  // BuildInfo::getFullVersionString () as well?
64  output ("Server: " + systemName () + "-json-rpc/v1");
65  output ("\r\n");
66 
67  // Be careful in modifying this! If you change the contents you MUST
68  // update the Content-Length header as well to indicate the correct
69  // size of the data.
70  output ("WWW-Authenticate: Basic realm=\"jsonrpc\"\r\n"
71  "Content-Type: text/html\r\n"
72  "Content-Length: 296\r\n"
73  "\r\n"
74  "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 "
75  "Transitional//EN\"\r\n"
76  "\"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"
77  "\">\r\n"
78  "<HTML>\r\n"
79  "<HEAD>\r\n"
80  "<TITLE>Error</TITLE>\r\n"
81  "<META HTTP-EQUIV='Content-Type' "
82  "CONTENT='text/html; charset=ISO-8859-1'>\r\n"
83  "</HEAD>\r\n"
84  "<BODY><H1>401 Unauthorized.</H1></BODY>\r\n");
85 
86  return;
87  }
88 
89  switch (nStatus)
90  {
91  case 200: output ("HTTP/1.1 200 OK\r\n"); break;
92  case 400: output ("HTTP/1.1 400 Bad Request\r\n"); break;
93  case 403: output ("HTTP/1.1 403 Forbidden\r\n"); break;
94  case 404: output ("HTTP/1.1 404 Not Found\r\n"); break;
95  case 500: output ("HTTP/1.1 500 Internal Server Error\r\n"); break;
96  case 503: output ("HTTP/1.1 503 Server is overloaded\r\n"); break;
97  }
98 
99  output (getHTTPHeaderTimestamp ());
100 
101  output ("Connection: Keep-Alive\r\n"
102  "Content-Length: ");
103 
104  // VFALCO TODO Determine if/when this header should be added
105  //if (context.app.config().RPC_ALLOW_REMOTE)
106  // output ("Access-Control-Allow-Origin: *\r\n");
107 
108  output (std::to_string(content.size () + 2));
109  output ("\r\n"
110  "Content-Type: application/json; charset=UTF-8\r\n");
111 
112  output ("Server: " + systemName () + "-json-rpc/");
114  output ("\r\n"
115  "\r\n");
116  output (content);
117  output ("\r\n");
118 }
119 
120 } // ripple
std::string
STL class.
beast::Journal::trace
Stream trace() const
Severity stream access functions.
Definition: Journal.h:287
ripple::getHTTPHeaderTimestamp
std::string getHTTPHeaderTimestamp()
Definition: JSONRPCUtil.cpp:30
ripple::HTTPReply
void HTTPReply(int nStatus, std::string const &content, Json::Output const &output, beast::Journal j)
Definition: JSONRPCUtil.cpp:50
std::function
std::to_string
T to_string(T... args)
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
ripple::BuildInfo::getFullVersionString
std::string const & getFullVersionString()
Full server version string.
Definition: BuildInfo.cpp:70
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::systemName
static std::string const & systemName()
Definition: SystemParameters.h:34