forked from cpp-netlib/cpp-netlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_include_inlined.cpp
64 lines (55 loc) · 1.7 KB
/
server_include_inlined.cpp
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
// Copyright 2009 (c) Tarro, Inc.
// Copyright 2009-2010 (c) Dean Michael Berris <[email protected]>
// Copyright 2012 Google, Inc.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <cstdlib>
#include <boost/config/warning_disable.hpp>
#define NETWORK_NO_LIB
#include <network/protocol/http/server.hpp>
#include <boost/thread/thread.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
#include <iostream>
namespace http = boost::network::http;
using boost::assign::list_of;
using boost::lexical_cast;
using std::string;
using std::cerr;
using std::endl;
struct hello_world;
typedef http::server<hello_world> server;
struct hello_world {
void operator()(server::request const& request, server::response& response) {
static server::response::header_type header = { "Connection", "close" };
response = server::response::stock_reply(server::response::ok,
"Hello, World!");
response.headers.push_back(header);
assert(response.status == server::response::ok);
assert(response.headers.size() == 3);
assert(response.content == "Hello, World!");
}
void log(string const& data) {
cerr << data << endl;
abort();
}
};
int main(int argc, char* argv[]) {
hello_world handler;
std::string port = "8000";
if (argc > 1)
port = argv[1];
server server_("127.0.0.1", port, handler, http::_reuse_address = true);
boost::thread runner(boost::bind(&server::run, &server_));
try {
server_.stop();
runner.join();
}
catch (...) {
/* ignore all errors */
}
return EXIT_SUCCESS;
}