|
| 1 | +// Copyright 2009 (c) Tarro, Inc. |
| 2 | +// Copyright 2009-2010 (c) Dean Michael Berris <mikhailberis@gmail.com> |
| 3 | +// Distributed under the Boost Software License, Version 1.0. |
| 4 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | + |
| 8 | +#include <cstdlib> |
| 9 | +#include <boost/config/warning_disable.hpp> |
| 10 | +#define BOOST_NETWORK_NO_LIB |
| 11 | +#include <boost/network/protocol/http/server.hpp> |
| 12 | +#include <boost/thread/thread.hpp> |
| 13 | +#include <boost/assign/list_of.hpp> |
| 14 | +#include <boost/lexical_cast.hpp> |
| 15 | +#include <string> |
| 16 | +#include <iostream> |
| 17 | + |
| 18 | +namespace http = boost::network::http; |
| 19 | +using boost::assign::list_of; |
| 20 | +using boost::lexical_cast; |
| 21 | +using std::string; |
| 22 | +using std::cerr; |
| 23 | +using std::endl; |
| 24 | + |
| 25 | +struct hello_world; |
| 26 | +typedef http::server<hello_world> server; |
| 27 | + |
| 28 | +struct hello_world { |
| 29 | + |
| 30 | + void operator()(server::request const & request, server::response & response) { |
| 31 | + static server::response::header_type header = {"Connection", "close"}; |
| 32 | + response = server::response::stock_reply(server::response::ok, "Hello, World!"); |
| 33 | + response.headers.push_back(header); |
| 34 | + assert(response.status == server::response::ok); |
| 35 | + assert(response.headers.size() == 3); |
| 36 | + assert(response.content == "Hello, World!"); |
| 37 | + } |
| 38 | + |
| 39 | + void log(string const & data) { |
| 40 | + cerr << data << endl; |
| 41 | + abort(); |
| 42 | + } |
| 43 | + |
| 44 | +}; |
| 45 | + |
| 46 | +int main(int argc, char * argv[]) { |
| 47 | + hello_world handler; |
| 48 | + std::string port = "8000"; |
| 49 | + if (argc > 1) port = argv[1]; |
| 50 | + server server_("127.0.0.1", port, handler, http::_reuse_address=true); |
| 51 | + boost::thread runner(boost::bind(&server::run, &server_)); |
| 52 | + server_.stop(); |
| 53 | + runner.join(); |
| 54 | + return EXIT_SUCCESS; |
| 55 | +} |
| 56 | + |
0 commit comments