1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
#include <boost/beast/core.hpp> #include <boost/beast/http.hpp> #include <boost/beast/version.hpp> #include <boost/asio.hpp> #include <chrono> #include <ctime> #include<cstdlib> #include<memory> #include<string> #include<json/json.h> #include<json/value.h> #include<json/reader.h> #include <iostream>
using namespace Json; using namespace std;
namespace beast = boost::beast; namespace http = beast::http; namespace net = boost::asio; using tcp = boost::asio::ip::tcp;
namespace my_program_state { std::size_t request_count() { static std::size_t count = 0; return count++; }
std::time_t now() { return std::time(0); } }
class http_connection :public std::enable_shared_from_this<http_connection> { public:
http_connection(tcp::socket socket):socket_(std::move(socket)) {
}
void start() { read_request(); check_deadline();
} private: tcp::socket socket_; beast::flat_buffer buffer_{ 8192 }; http::request<http::dynamic_body> request_; http::response<http::dynamic_body> response_;
net::steady_timer deadline_{socket_.get_executor(),std::chrono::seconds(60) };
void read_request() { auto self = shared_from_this(); http::async_read(socket_, buffer_, request_, [self](beast::error_code ec,std::size_t bytes_transferred) { boost::ignore_unused(bytes_transferred); if (!ec) { self->progress_request(); } }); }
void check_deadline() { auto self = shared_from_this();
deadline_.async_wait([self] (boost::system::error_code ec) { if (!ec) { self->socket_.close(); } }); }
void progress_request() { response_.version(request_.version()); response_.keep_alive(false); switch (request_.method()) { case http::verb::get: response_.result(http::status::ok); response_.set(http::field::server, "Beast"); create_response(); break; case http::verb::post: create_post_response(); break; default: response_.result(http::status::bad_request); response_.set(http::field::content_type, "text/plain"); beast::ostream(response_.body()) << "Invalid request-method ' " << std::string(request_.method_string()) << "'"; break; }
write_response(); }
void create_response() { if (request_.target() == "/count") { response_.set(http::field::content_type,"text/html"); beast::ostream(response_.body()) << "<html>\n" << "<head><title>Request count</title></head>\n" << "<body>\n" << "<h1>Request count</h1>\n" << "<p>There have been " << my_program_state::request_count() << " requests so far.</p>\n" << "</body>\n" << "</html>\n"; } else if (request_.target() == "/time") { response_.set(http::field::content_type, "text/html"); beast::ostream(response_.body()) << "<html>\n" << "<head><title>Current time</title></head>\n" << "<body>\n" << "<h1>Current time</h1>\n" << "<p>The current time is " << my_program_state::now() << " seconds since the epoch.</p>\n" << "</body>\n" << "</html>\n"; } else { response_.result(http::status::not_found); response_.set(http::field::content_type, "text/plain"); beast::ostream(response_.body()) << "File not found\r\n"; }
}
void create_post_response() { if (request_.target() == "/email") { auto& body = this->request_.body(); auto body_str = boost::beast::buffers_to_string(body.data()); std::cout << "receive body is " << body_str << std::endl; this->response_.set(http::field::content_type, "text/json"); Json::Value root; Json::Reader reader; Json::Value src_root; bool parse_success = reader.parse(body_str, src_root); if (!parse_success) { std::cout << "Failed to parse JSON data!" << std::endl; root["error"] = 1001; std::string jsonstr = root.toStyledString(); beast::ostream(this->response_.body()) << jsonstr; return; }
auto email = src_root["email"].asString(); std::cout << "email is " << email << std::endl;
root["error"] = 0; root["email"] = src_root["email"]; root["msg"] = "recevie email post success"; std::string jsonstr = root.toStyledString(); beast::ostream(this->response_.body()) << jsonstr; } else { response_.result(http::status::not_found); response_.set(http::field::content_type, "text/plain"); beast::ostream(response_.body()) << "File not found\r\n"; } }
void write_response() { auto self = shared_from_this();
response_.content_length(response_.body().size());
http::async_write( socket_, response_, [self](beast::error_code ec, std::size_t) { self->socket_.shutdown(tcp::socket::shutdown_send, ec); self->deadline_.cancel(); }); } };
void http_server(tcp::acceptor& acceptor, tcp::socket& socket) { acceptor.async_accept(socket,[&](beast::error_code ec){ if (!ec) std::make_shared<http_connection>(std::move(socket))->start(); http_server(acceptor, socket); }); }
int main() { try {
auto const address = net::ip::make_address("127.0.0.1"); unsigned short port = static_cast<unsigned short>(8080);
net::io_context ioc{ 1 };
tcp::acceptor acceptor{ ioc, {address, port} }; tcp::socket socket{ ioc }; http_server(acceptor, socket);
std::cout << "server" << endl;
ioc.run(); } catch (std::exception const& e) { std::cerr << "Error: " << e.what() << std::endl; return EXIT_FAILURE; } }
|