Skip to content

Commit 446bd38

Browse files
committed
Pulling in changes from master.
2 parents c0e214b + e13cf78 commit 446bd38

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

libs/network/example/http/fileserver.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ struct file_cache {
4747
boost::upgrade_lock<boost::shared_mutex> lock(cache_mutex);
4848
std::string real_filename = doc_root_+path;
4949
if (regions.find(real_filename) != regions.end()) return true;
50+
#ifdef O_NOATIME
5051
int fd = open(real_filename.c_str(), O_RDONLY|O_NOATIME|O_NONBLOCK);
52+
#else
53+
int fd = open(real_filename.c_str(), O_RDONLY|O_NONBLOCK);
54+
#endif
5155
if (fd == -1) return false;
5256
std::size_t size = lseek(fd, 0, SEEK_END);
5357
void * region = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
// Copyright 2010 Dean Michael Berris.
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+
#define BOOST_TEST_MODULE HTTP Asynchronous Server Tests
8+
9+
#include <vector>
10+
11+
#include <boost/config/warning_disable.hpp>
12+
#include <boost/network/include/http/server.hpp>
13+
#include <boost/network/utils/thread_pool.hpp>
14+
#include <boost/range/algorithm/find_if.hpp>
15+
16+
namespace net = boost::network;
17+
namespace http = boost::network::http;
18+
namespace utils = boost::network::utils;
19+
20+
struct async_hello_world;
21+
typedef http::async_server<async_hello_world> server;
22+
23+
struct async_hello_world {
24+
25+
struct is_content_length {
26+
template <class Header>
27+
bool operator()(Header const & header) {
28+
return boost::iequals(header.name, "content-length");
29+
}
30+
};
31+
32+
void operator()(server::request const & request, server::connection_ptr connection) {
33+
static server::response_header headers[] = {
34+
{"Connection", "close"}
35+
, {"Content-Type", "text/plain"}
36+
, {"Server", "cpp-netlib/0.9-devel"}
37+
};
38+
if (request.method == "HEAD") {
39+
connection->set_status(server::connection::ok);
40+
connection->set_headers(boost::make_iterator_range(headers, headers+3));
41+
} else {
42+
if (request.method == "PUT" || request.method == "POST") {
43+
static std::string bad_request("Bad Request.");
44+
server::request::headers_container_type::iterator found =
45+
boost::find_if(request.headers, is_content_length());
46+
if (found == request.headers.end()) {
47+
connection->set_status(server::connection::bad_request);
48+
connection->set_headers(boost::make_iterator_range(headers, headers+3));
49+
connection->write(bad_request);
50+
return;
51+
}
52+
}
53+
static char const * hello_world = "Hello, World!";
54+
connection->set_status(server::connection::ok);
55+
connection->set_headers(boost::make_iterator_range(headers, headers+3));
56+
std::vector<boost::asio::const_buffer> iovec;
57+
iovec.push_back(boost::asio::const_buffer(hello_world, 13));
58+
connection->write(iovec, boost::bind(&async_hello_world::error, this, _1));
59+
}
60+
}
61+
62+
void error(boost::system::error_code const & ec) {
63+
// do nothing here.
64+
}
65+
};
66+
67+
int main(int argc, char * argv[]) {
68+
utils::thread_pool thread_pool(2);
69+
async_hello_world handler;
70+
server instance("127.0.0.1", "8000", handler, thread_pool);
71+
instance.run();
72+
return 0;
73+
}
74+

0 commit comments

Comments
 (0)