15 #include <boost/algorithm/string.hpp>
16 #include <boost/asio.hpp>
17 #include <boost/asio/ssl.hpp>
18 #include <boost/bind.hpp>
19 #include <boost/filesystem.hpp>
20 #include <boost/foreach.hpp>
21 #include <boost/iostreams/concepts.hpp>
22 #include <boost/iostreams/stream.hpp>
23 #include <boost/shared_ptr.hpp>
24 #include "json/json_spirit_writer_template.h"
27 using namespace boost;
38 string HTTPPost(
const string& strMsg,
const map<string,string>& mapRequestHeaders)
41 s <<
"POST / HTTP/1.1\r\n"
43 <<
"Host: 127.0.0.1\r\n"
44 <<
"Content-Type: application/json\r\n"
45 <<
"Content-Length: " << strMsg.size() <<
"\r\n"
46 <<
"Connection: close\r\n"
47 <<
"Accept: application/json\r\n";
48 BOOST_FOREACH(
const PAIRTYPE(
string,
string)& item, mapRequestHeaders)
49 s << item.first <<
": " << item.second <<
"\r\n";
50 s <<
"\r\n" << strMsg;
55 static string rfc1123Time()
60 string HTTPReply(
int nStatus,
const string& strMsg,
bool keepalive)
63 return strprintf(
"HTTP/1.0 401 Authorization Required\r\n"
65 "Server: anoncoin-json-rpc/%s\r\n"
66 "WWW-Authenticate: Basic realm=\"jsonrpc\"\r\n"
67 "Content-Type: text/html\r\n"
68 "Content-Length: 296\r\n"
70 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\r\n"
71 "\"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd\">\r\n"
74 "<TITLE>Error</TITLE>\r\n"
75 "<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>\r\n"
77 "<BODY><H1>401 Unauthorized.</H1></BODY>\r\n"
80 if (nStatus ==
HTTP_OK) cStatus =
"OK";
90 "Content-Length: %u\r\n"
91 "Content-Type: application/json\r\n"
92 "Server: anoncoin-json-rpc/%s\r\n"
98 keepalive ?
"keep-alive" :
"close",
104 int static GetPostIndex(
const vector<string>& vWords)
106 if (vWords[0] ==
"GET" || vWords[0] ==
"POST")
108 if (vWords[1] ==
"GET" || vWords[1] ==
"POST")
113 int static GetURIIndex(
const vector<string>& vWords)
115 if (vWords[0].size() > 0 && vWords[0][0] ==
'/')
117 if (vWords[1].size() > 0 && vWords[1][0] ==
'/')
123 string& http_method,
string& http_uri)
126 getline(stream, str);
129 vector<string> vWords;
130 boost::split(vWords, str, boost::is_any_of(
" "));
131 if (vWords.size() < 2)
135 int postIndex = GetPostIndex(vWords);
138 printf(
"ReadHTTPRequestLine GetPostIndex FAIL\n");
141 http_method = vWords[postIndex];
143 int URIIndex = GetURIIndex(vWords);
146 printf(
"ReadHTTPRequestLine GetURIIndex FAIL\n");
150 http_uri = vWords[URIIndex];
153 string strProto =
"";
154 if (vWords.size() > 2)
155 strProto = vWords[2];
158 const char *ver = strstr(strProto.c_str(),
"HTTP/1.");
168 getline(stream, str);
169 vector<string> vWords;
170 boost::split(vWords, str, boost::is_any_of(
" "));
171 if (vWords.size() < 2)
174 const char *ver = strstr(str.c_str(),
"HTTP/1.");
177 return atoi(vWords[1].c_str());
180 int ReadHTTPHeaders(std::basic_istream<char>& stream, map<string, string>& mapHeadersRet)
186 std::getline(stream, str);
187 if (str.empty() || str ==
"\r")
189 string::size_type nColon = str.find(
":");
190 if (nColon != string::npos)
192 string strHeader = str.substr(0, nColon);
193 boost::trim(strHeader);
194 boost::to_lower(strHeader);
195 string strValue = str.substr(nColon+1);
196 boost::trim(strValue);
197 mapHeadersRet[strHeader] = strValue;
198 if (strHeader ==
"content-length")
199 nLen =
atoi(strValue.c_str());
207 string>& mapHeadersRet,
string& strMessageRet,
210 mapHeadersRet.clear();
215 if (nLen < 0 || nLen > (
int)MAX_SIZE)
221 vector<char> vch(nLen);
222 stream.read(&vch[0], nLen);
223 strMessageRet = string(vch.begin(), vch.end());
226 string sConHdr = mapHeadersRet[
"connection"];
228 if ((sConHdr !=
"close") && (sConHdr !=
"keep-alive"))
231 mapHeadersRet[
"connection"] =
"keep-alive";
233 mapHeadersRet[
"connection"] =
"close";
249 string JSONRPCRequest(
const string& strMethod,
const Array& params,
const Value&
id)
252 request.push_back(Pair(
"method", strMethod));
253 request.push_back(Pair(
"params", params));
254 request.push_back(Pair(
"id",
id));
255 return write_string(Value(request),
false) +
"\n";
261 if (error.type() != null_type)
262 reply.push_back(Pair(
"result", Value::null));
264 reply.push_back(Pair(
"result", result));
265 reply.push_back(Pair(
"error", error));
266 reply.push_back(Pair(
"id",
id));
270 string JSONRPCReply(
const Value& result,
const Value& error,
const Value&
id)
273 return write_string(Value(reply),
false) +
"\n";
279 error.push_back(Pair(
"code", code));
280 error.push_back(Pair(
"message", message));
int ReadHTTPStatus(std::basic_istream< char > &stream, int &proto)
string JSONRPCReply(const Value &result, const Value &error, const Value &id)
bool ReadHTTPRequestLine(std::basic_istream< char > &stream, int &proto, string &http_method, string &http_uri)
Object JSONRPCError(int code, const string &message)
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
int ReadHTTPMessage(std::basic_istream< char > &stream, map< string, string > &mapHeadersRet, string &strMessageRet, int nProto)
string HTTPReply(int nStatus, const string &strMsg, bool keepalive)
std::string FormatFullVersion()
Object JSONRPCReplyObj(const Value &result, const Value &error, const Value &id)
string HTTPPost(const string &strMsg, const map< string, string > &mapRequestHeaders)
int ReadHTTPHeaders(std::basic_istream< char > &stream, map< string, string > &mapHeadersRet)
string JSONRPCRequest(const string &strMethod, const Array ¶ms, const Value &id)
int atoi(const std::string &str)