Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r58900 - in trunk/libs/asio: doc example/http/server4
From: chris_at_[hidden]
Date: 2010-01-11 07:22:35


Author: chris_kohlhoff
Date: 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
New Revision: 58900
URL: http://svn.boost.org/trac/boost/changeset/58900

Log:
Add HTTP Server 4 example.

Added:
   trunk/libs/asio/example/http/server4/
   trunk/libs/asio/example/http/server4/Jamfile (contents, props changed)
   trunk/libs/asio/example/http/server4/Jamfile.v2 (contents, props changed)
   trunk/libs/asio/example/http/server4/coroutine.hpp (contents, props changed)
   trunk/libs/asio/example/http/server4/file_handler.cpp (contents, props changed)
   trunk/libs/asio/example/http/server4/file_handler.hpp (contents, props changed)
   trunk/libs/asio/example/http/server4/header.hpp (contents, props changed)
   trunk/libs/asio/example/http/server4/mime_types.cpp (contents, props changed)
   trunk/libs/asio/example/http/server4/mime_types.hpp (contents, props changed)
   trunk/libs/asio/example/http/server4/posix_main.cpp (contents, props changed)
   trunk/libs/asio/example/http/server4/reply.cpp (contents, props changed)
   trunk/libs/asio/example/http/server4/reply.hpp (contents, props changed)
   trunk/libs/asio/example/http/server4/request.hpp (contents, props changed)
   trunk/libs/asio/example/http/server4/request_parser.cpp (contents, props changed)
   trunk/libs/asio/example/http/server4/request_parser.hpp (contents, props changed)
   trunk/libs/asio/example/http/server4/server.cpp (contents, props changed)
   trunk/libs/asio/example/http/server4/server.hpp (contents, props changed)
   trunk/libs/asio/example/http/server4/unyield.hpp (contents, props changed)
   trunk/libs/asio/example/http/server4/win_main.cpp (contents, props changed)
   trunk/libs/asio/example/http/server4/yield.hpp (contents, props changed)
Text files modified:
   trunk/libs/asio/doc/Jamfile.v2 | 6 +++---
   trunk/libs/asio/doc/examples.qbk | 23 +++++++++++++++++++++++
   2 files changed, 26 insertions(+), 3 deletions(-)

Modified: trunk/libs/asio/doc/Jamfile.v2
==============================================================================
--- trunk/libs/asio/doc/Jamfile.v2 (original)
+++ trunk/libs/asio/doc/Jamfile.v2 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -33,9 +33,9 @@
   ;
 
 local example-names = allocation buffers chat echo http/client http/server
- http/server2 http/server3 icmp invocation iostreams local multicast
- nonblocking porthopper serialization services socks4 ssl timeouts timers
- windows ;
+ http/server2 http/server3 http/server4 icmp invocation iostreams local
+ multicast nonblocking porthopper serialization services socks4 ssl timeouts
+ timers windows ;
 
 for local l in $(example-names)
 {

Modified: trunk/libs/asio/doc/examples.qbk
==============================================================================
--- trunk/libs/asio/doc/examples.qbk (original)
+++ trunk/libs/asio/doc/examples.qbk 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -135,6 +135,29 @@
 * [@boost_asio/example/http/server3/win_main.cpp]
 
 
+[heading HTTP Server 4]
+
+A single-threaded HTTP server implemented using stackless coroutines.
+
+* [@boost_asio/example/http/server4/coroutine.hpp]
+* [@boost_asio/example/http/server4/file_handler.cpp]
+* [@boost_asio/example/http/server4/file_handler.hpp]
+* [@boost_asio/example/http/server4/header.hpp]
+* [@boost_asio/example/http/server4/mime_types.cpp]
+* [@boost_asio/example/http/server4/mime_types.hpp]
+* [@boost_asio/example/http/server4/posix_main.cpp]
+* [@boost_asio/example/http/server4/reply.cpp]
+* [@boost_asio/example/http/server4/reply.hpp]
+* [@boost_asio/example/http/server4/request.hpp]
+* [@boost_asio/example/http/server4/request_parser.cpp]
+* [@boost_asio/example/http/server4/request_parser.hpp]
+* [@boost_asio/example/http/server4/server.cpp]
+* [@boost_asio/example/http/server4/server.hpp]
+* [@boost_asio/example/http/server4/unyield.hpp]
+* [@boost_asio/example/http/server4/win_main.cpp]
+* [@boost_asio/example/http/server4/yield.hpp]
+
+
 [heading ICMP]
 
 This example shows how to use raw sockets with ICMP to ping a remote host.

Added: trunk/libs/asio/example/http/server4/Jamfile
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/Jamfile 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,40 @@
+#
+# Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+#
+# 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)
+#
+
+subproject libs/asio/example/http/server4 ;
+
+project boost : $(BOOST_ROOT) ;
+
+if $(UNIX)
+{
+ switch $(JAMUNAME)
+ {
+ case SunOS* :
+ {
+ SOCKET_LIBS = <find-library>socket <find-library>nsl ;
+ }
+ }
+}
+
+exe http_server
+ : <lib>@boost/libs/thread/build/boost_thread
+ <lib>@boost/libs/system/build/boost_system
+ file_handler.cpp
+ mime_types.cpp
+ posix_main.cpp
+ reply.cpp
+ request_parser.cpp
+ server.cpp
+ win_main.cpp
+ : <include>$(BOOST_ROOT)
+ <include>../../../../..
+ <define>BOOST_ALL_NO_LIB=1
+ <threading>multi
+ <mingw><*><find-library>ws2_32
+ <mingw><*><find-library>mswsock
+ $(SOCKET_LIBS)
+ ;

Added: trunk/libs/asio/example/http/server4/Jamfile.v2
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/Jamfile.v2 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,45 @@
+#
+# Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+#
+# 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)
+#
+
+import os ;
+
+if [ os.name ] = SOLARIS
+{
+ lib socket ;
+ lib nsl ;
+}
+else if [ os.name ] = NT
+{
+ lib ws2_32 ;
+ lib mswsock ;
+}
+else if [ os.name ] = HPUX
+{
+ lib ipv6 ;
+}
+
+exe server
+ : file_handler.cpp
+ mime_types.cpp
+ posix_main.cpp
+ reply.cpp
+ request_parser.cpp
+ server.cpp
+ win_main.cpp
+ /boost/system//boost_system
+ /boost/thread//boost_thread
+ : <define>BOOST_ALL_NO_LIB=1
+ <threading>multi
+ <os>SOLARIS:<library>socket
+ <os>SOLARIS:<library>nsl
+ <os>NT:<define>_WIN32_WINNT=0x0501
+ <os>NT,<toolset>gcc:<library>ws2_32
+ <os>NT,<toolset>gcc:<library>mswsock
+ <os>NT,<toolset>gcc-cygwin:<define>__USE_W32_SOCKETS
+ <os>HPUX,<toolset>gcc:<define>_XOPEN_SOURCE_EXTENDED
+ <os>HPUX:<library>ipv6
+ ;

Added: trunk/libs/asio/example/http/server4/coroutine.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/coroutine.hpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,70 @@
+//
+// coroutine.hpp
+// ~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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)
+//
+
+#ifndef COROUTINE_HPP
+#define COROUTINE_HPP
+
+class coroutine
+{
+public:
+ coroutine() : value_(0) {}
+ bool is_child() const { return value_ < 0; }
+ bool is_parent() const { return !is_child(); }
+private:
+ friend class coroutine_ref;
+ int value_;
+};
+
+class coroutine_ref
+{
+public:
+ coroutine_ref(coroutine& c) : value_(c.value_) {}
+ coroutine_ref(coroutine* c) : value_(c->value_) {}
+ operator int() const { return value_; }
+ int& operator=(int v) { return value_ = v; }
+private:
+ void operator=(const coroutine_ref&);
+ int& value_;
+};
+
+#define CORO_REENTER(c) \
+ switch (coroutine_ref _coro_value = c) \
+ case -1: if (_coro_value) \
+ { \
+ goto bail_out_of_coroutine; \
+ bail_out_of_coroutine: \
+ break; \
+ } \
+ else case 0:
+
+#define CORO_YIELD \
+ for (_coro_value = __LINE__;;) \
+ if (_coro_value == 0) \
+ { \
+ case __LINE__: ; \
+ break; \
+ } \
+ else \
+ switch (_coro_value ? 0 : 1) \
+ for (;;) \
+ case 1: if (_coro_value) \
+ goto bail_out_of_coroutine; \
+ else case 0:
+
+#define CORO_FORK \
+ for (_coro_value = -__LINE__;; _coro_value = __LINE__) \
+ if (_coro_value == __LINE__) \
+ { \
+ case -__LINE__: ; \
+ break; \
+ } \
+ else
+
+#endif // COROUTINE_HPP

Added: trunk/libs/asio/example/http/server4/file_handler.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/file_handler.cpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,122 @@
+//
+// file_handler.cpp
+// ~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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 "file_handler.hpp"
+#include <fstream>
+#include <sstream>
+#include <string>
+#include <boost/lexical_cast.hpp>
+#include "mime_types.hpp"
+#include "reply.hpp"
+#include "request.hpp"
+
+namespace http {
+namespace server4 {
+
+file_handler::file_handler(const std::string& doc_root)
+ : doc_root_(doc_root)
+{
+}
+
+void file_handler::operator()(const request& req, reply& rep)
+{
+ // Decode url to path.
+ std::string request_path;
+ if (!url_decode(req.uri, request_path))
+ {
+ rep = reply::stock_reply(reply::bad_request);
+ return;
+ }
+
+ // Request path must be absolute and not contain "..".
+ if (request_path.empty() || request_path[0] != '/'
+ || request_path.find("..") != std::string::npos)
+ {
+ rep = reply::stock_reply(reply::bad_request);
+ return;
+ }
+
+ // If path ends in slash (i.e. is a directory) then add "index.html".
+ if (request_path[request_path.size() - 1] == '/')
+ {
+ request_path += "index.html";
+ }
+
+ // Determine the file extension.
+ std::size_t last_slash_pos = request_path.find_last_of("/");
+ std::size_t last_dot_pos = request_path.find_last_of(".");
+ std::string extension;
+ if (last_dot_pos != std::string::npos && last_dot_pos > last_slash_pos)
+ {
+ extension = request_path.substr(last_dot_pos + 1);
+ }
+
+ // Open the file to send back.
+ std::string full_path = doc_root_ + request_path;
+ std::ifstream is(full_path.c_str(), std::ios::in | std::ios::binary);
+ if (!is)
+ {
+ rep = reply::stock_reply(reply::not_found);
+ return;
+ }
+
+ // Fill out the reply to be sent to the client.
+ rep.status = reply::ok;
+ char buf[512];
+ while (is.read(buf, sizeof(buf)).gcount() > 0)
+ rep.content.append(buf, is.gcount());
+ rep.headers.resize(2);
+ rep.headers[0].name = "Content-Length";
+ rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size());
+ rep.headers[1].name = "Content-Type";
+ rep.headers[1].value = mime_types::extension_to_type(extension);
+}
+
+bool file_handler::url_decode(const std::string& in, std::string& out)
+{
+ out.clear();
+ out.reserve(in.size());
+ for (std::size_t i = 0; i < in.size(); ++i)
+ {
+ if (in[i] == '%')
+ {
+ if (i + 3 <= in.size())
+ {
+ int value = 0;
+ std::istringstream is(in.substr(i + 1, 2));
+ if (is >> std::hex >> value)
+ {
+ out += static_cast<char>(value);
+ i += 2;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ else
+ {
+ return false;
+ }
+ }
+ else if (in[i] == '+')
+ {
+ out += ' ';
+ }
+ else
+ {
+ out += in[i];
+ }
+ }
+ return true;
+}
+
+} // namespace server4
+} // namespace http

Added: trunk/libs/asio/example/http/server4/file_handler.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/file_handler.hpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,44 @@
+//
+// file_handler.hpp
+// ~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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)
+//
+
+#ifndef HTTP_SERVER4_FILE_HANDLER_HPP
+#define HTTP_SERVER4_FILE_HANDLER_HPP
+
+#include <string>
+
+namespace http {
+namespace server4 {
+
+struct reply;
+struct request;
+
+/// The common handler for all incoming requests.
+class file_handler
+{
+public:
+ /// Construct with a directory containing files to be served.
+ explicit file_handler(const std::string& doc_root);
+
+ /// Handle a request and produce a reply.
+ void operator()(const request& req, reply& rep);
+
+private:
+ /// The directory containing the files to be served.
+ std::string doc_root_;
+
+ /// Perform URL-decoding on a string. Returns false if the encoding was
+ /// invalid.
+ static bool url_decode(const std::string& in, std::string& out);
+};
+
+} // namespace server4
+} // namespace http
+
+#endif // HTTP_SERVER4_FILE_HANDLER_HPP

Added: trunk/libs/asio/example/http/server4/header.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/header.hpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,28 @@
+//
+// header.hpp
+// ~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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)
+//
+
+#ifndef HTTP_SERVER4_HEADER_HPP
+#define HTTP_SERVER4_HEADER_HPP
+
+#include <string>
+
+namespace http {
+namespace server4 {
+
+struct header
+{
+ std::string name;
+ std::string value;
+};
+
+} // namespace server4
+} // namespace http
+
+#endif // HTTP_SERVER4_HEADER_HPP

Added: trunk/libs/asio/example/http/server4/mime_types.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/mime_types.cpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,46 @@
+//
+// mime_types.cpp
+// ~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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 "mime_types.hpp"
+
+namespace http {
+namespace server4 {
+namespace mime_types {
+
+struct mapping
+{
+ const char* extension;
+ const char* mime_type;
+} mappings[] =
+{
+ { "gif", "image/gif" },
+ { "htm", "text/html" },
+ { "html", "text/html" },
+ { "jpg", "image/jpeg" },
+ { "png", "image/png" },
+ { 0, 0 } // Marks end of list.
+};
+
+std::string extension_to_type(const std::string& extension)
+{
+ for (mapping* m = mappings; m->extension; ++m)
+ {
+ if (m->extension == extension)
+ {
+ return m->mime_type;
+ }
+ }
+
+ return "text/plain";
+}
+
+} // namespace mime_types
+} // namespace server4
+} // namespace http

Added: trunk/libs/asio/example/http/server4/mime_types.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/mime_types.hpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,27 @@
+//
+// mime_types.hpp
+// ~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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)
+//
+
+#ifndef HTTP_SERVER4_MIME_TYPES_HPP
+#define HTTP_SERVER4_MIME_TYPES_HPP
+
+#include <string>
+
+namespace http {
+namespace server4 {
+namespace mime_types {
+
+/// Convert a file extension into a MIME type.
+std::string extension_to_type(const std::string& extension);
+
+} // namespace mime_types
+} // namespace server4
+} // namespace http
+
+#endif // HTTP_SERVER4_MIME_TYPES_HPP

Added: trunk/libs/asio/example/http/server4/posix_main.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/posix_main.cpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,78 @@
+//
+// posix_main.cpp
+// ~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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 <iostream>
+#include <boost/asio.hpp>
+#include <boost/thread.hpp>
+#include <boost/bind.hpp>
+#include "server.hpp"
+#include "file_handler.hpp"
+
+#if !defined(_WIN32)
+
+#include <pthread.h>
+#include <signal.h>
+
+int main(int argc, char* argv[])
+{
+ try
+ {
+ // Check command line arguments.
+ if (argc != 4)
+ {
+ std::cerr << "Usage: http_server <address> <port> <doc_root>\n";
+ std::cerr << " For IPv4, try:\n";
+ std::cerr << " receiver 0.0.0.0 80 .\n";
+ std::cerr << " For IPv6, try:\n";
+ std::cerr << " receiver 0::0 80 .\n";
+ return 1;
+ }
+
+ boost::asio::io_service io_service;
+
+ // Launch the initial server coroutine.
+ http::server4::server(io_service, argv[1], argv[2],
+ http::server4::file_handler(argv[3]))();
+
+ // Block all signals for background thread.
+ sigset_t new_mask;
+ sigfillset(&new_mask);
+ sigset_t old_mask;
+ pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);
+
+ // Run server in background thread.
+ boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
+
+ // Restore previous signals.
+ pthread_sigmask(SIG_SETMASK, &old_mask, 0);
+
+ // Wait for signal indicating time to shut down.
+ sigset_t wait_mask;
+ sigemptyset(&wait_mask);
+ sigaddset(&wait_mask, SIGINT);
+ sigaddset(&wait_mask, SIGQUIT);
+ sigaddset(&wait_mask, SIGTERM);
+ pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
+ int sig = 0;
+ sigwait(&wait_mask, &sig);
+
+ // Stop the server and wait for the background thread to exit.
+ io_service.stop();
+ t.join();
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << "exception: " << e.what() << "\n";
+ }
+
+ return 0;
+}
+
+#endif // !defined(_WIN32)

Added: trunk/libs/asio/example/http/server4/reply.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/reply.cpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,256 @@
+//
+// reply.cpp
+// ~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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 "reply.hpp"
+#include <string>
+#include <boost/lexical_cast.hpp>
+
+namespace http {
+namespace server4 {
+
+namespace status_strings {
+
+const std::string ok =
+ "HTTP/1.0 200 OK\r\n";
+const std::string created =
+ "HTTP/1.0 201 Created\r\n";
+const std::string accepted =
+ "HTTP/1.0 202 Accepted\r\n";
+const std::string no_content =
+ "HTTP/1.0 204 No Content\r\n";
+const std::string multiple_choices =
+ "HTTP/1.0 300 Multiple Choices\r\n";
+const std::string moved_permanently =
+ "HTTP/1.0 301 Moved Permanently\r\n";
+const std::string moved_temporarily =
+ "HTTP/1.0 302 Moved Temporarily\r\n";
+const std::string not_modified =
+ "HTTP/1.0 304 Not Modified\r\n";
+const std::string bad_request =
+ "HTTP/1.0 400 Bad Request\r\n";
+const std::string unauthorized =
+ "HTTP/1.0 401 Unauthorized\r\n";
+const std::string forbidden =
+ "HTTP/1.0 403 Forbidden\r\n";
+const std::string not_found =
+ "HTTP/1.0 404 Not Found\r\n";
+const std::string internal_server_error =
+ "HTTP/1.0 500 Internal Server Error\r\n";
+const std::string not_implemented =
+ "HTTP/1.0 501 Not Implemented\r\n";
+const std::string bad_gateway =
+ "HTTP/1.0 502 Bad Gateway\r\n";
+const std::string service_unavailable =
+ "HTTP/1.0 503 Service Unavailable\r\n";
+
+boost::asio::const_buffer to_buffer(reply::status_type status)
+{
+ switch (status)
+ {
+ case reply::ok:
+ return boost::asio::buffer(ok);
+ case reply::created:
+ return boost::asio::buffer(created);
+ case reply::accepted:
+ return boost::asio::buffer(accepted);
+ case reply::no_content:
+ return boost::asio::buffer(no_content);
+ case reply::multiple_choices:
+ return boost::asio::buffer(multiple_choices);
+ case reply::moved_permanently:
+ return boost::asio::buffer(moved_permanently);
+ case reply::moved_temporarily:
+ return boost::asio::buffer(moved_temporarily);
+ case reply::not_modified:
+ return boost::asio::buffer(not_modified);
+ case reply::bad_request:
+ return boost::asio::buffer(bad_request);
+ case reply::unauthorized:
+ return boost::asio::buffer(unauthorized);
+ case reply::forbidden:
+ return boost::asio::buffer(forbidden);
+ case reply::not_found:
+ return boost::asio::buffer(not_found);
+ case reply::internal_server_error:
+ return boost::asio::buffer(internal_server_error);
+ case reply::not_implemented:
+ return boost::asio::buffer(not_implemented);
+ case reply::bad_gateway:
+ return boost::asio::buffer(bad_gateway);
+ case reply::service_unavailable:
+ return boost::asio::buffer(service_unavailable);
+ default:
+ return boost::asio::buffer(internal_server_error);
+ }
+}
+
+} // namespace status_strings
+
+namespace misc_strings {
+
+const char name_value_separator[] = { ':', ' ' };
+const char crlf[] = { '\r', '\n' };
+
+} // namespace misc_strings
+
+std::vector<boost::asio::const_buffer> reply::to_buffers()
+{
+ std::vector<boost::asio::const_buffer> buffers;
+ buffers.push_back(status_strings::to_buffer(status));
+ for (std::size_t i = 0; i < headers.size(); ++i)
+ {
+ header& h = headers[i];
+ buffers.push_back(boost::asio::buffer(h.name));
+ buffers.push_back(boost::asio::buffer(misc_strings::name_value_separator));
+ buffers.push_back(boost::asio::buffer(h.value));
+ buffers.push_back(boost::asio::buffer(misc_strings::crlf));
+ }
+ buffers.push_back(boost::asio::buffer(misc_strings::crlf));
+ buffers.push_back(boost::asio::buffer(content));
+ return buffers;
+}
+
+namespace stock_replies {
+
+const char ok[] = "";
+const char created[] =
+ "<html>"
+ "<head><title>Created</title></head>"
+ "<body><h1>201 Created</h1></body>"
+ "</html>";
+const char accepted[] =
+ "<html>"
+ "<head><title>Accepted</title></head>"
+ "<body><h1>202 Accepted</h1></body>"
+ "</html>";
+const char no_content[] =
+ "<html>"
+ "<head><title>No Content</title></head>"
+ "<body><h1>204 Content</h1></body>"
+ "</html>";
+const char multiple_choices[] =
+ "<html>"
+ "<head><title>Multiple Choices</title></head>"
+ "<body><h1>300 Multiple Choices</h1></body>"
+ "</html>";
+const char moved_permanently[] =
+ "<html>"
+ "<head><title>Moved Permanently</title></head>"
+ "<body><h1>301 Moved Permanently</h1></body>"
+ "</html>";
+const char moved_temporarily[] =
+ "<html>"
+ "<head><title>Moved Temporarily</title></head>"
+ "<body><h1>302 Moved Temporarily</h1></body>"
+ "</html>";
+const char not_modified[] =
+ "<html>"
+ "<head><title>Not Modified</title></head>"
+ "<body><h1>304 Not Modified</h1></body>"
+ "</html>";
+const char bad_request[] =
+ "<html>"
+ "<head><title>Bad Request</title></head>"
+ "<body><h1>400 Bad Request</h1></body>"
+ "</html>";
+const char unauthorized[] =
+ "<html>"
+ "<head><title>Unauthorized</title></head>"
+ "<body><h1>401 Unauthorized</h1></body>"
+ "</html>";
+const char forbidden[] =
+ "<html>"
+ "<head><title>Forbidden</title></head>"
+ "<body><h1>403 Forbidden</h1></body>"
+ "</html>";
+const char not_found[] =
+ "<html>"
+ "<head><title>Not Found</title></head>"
+ "<body><h1>404 Not Found</h1></body>"
+ "</html>";
+const char internal_server_error[] =
+ "<html>"
+ "<head><title>Internal Server Error</title></head>"
+ "<body><h1>500 Internal Server Error</h1></body>"
+ "</html>";
+const char not_implemented[] =
+ "<html>"
+ "<head><title>Not Implemented</title></head>"
+ "<body><h1>501 Not Implemented</h1></body>"
+ "</html>";
+const char bad_gateway[] =
+ "<html>"
+ "<head><title>Bad Gateway</title></head>"
+ "<body><h1>502 Bad Gateway</h1></body>"
+ "</html>";
+const char service_unavailable[] =
+ "<html>"
+ "<head><title>Service Unavailable</title></head>"
+ "<body><h1>503 Service Unavailable</h1></body>"
+ "</html>";
+
+std::string to_string(reply::status_type status)
+{
+ switch (status)
+ {
+ case reply::ok:
+ return ok;
+ case reply::created:
+ return created;
+ case reply::accepted:
+ return accepted;
+ case reply::no_content:
+ return no_content;
+ case reply::multiple_choices:
+ return multiple_choices;
+ case reply::moved_permanently:
+ return moved_permanently;
+ case reply::moved_temporarily:
+ return moved_temporarily;
+ case reply::not_modified:
+ return not_modified;
+ case reply::bad_request:
+ return bad_request;
+ case reply::unauthorized:
+ return unauthorized;
+ case reply::forbidden:
+ return forbidden;
+ case reply::not_found:
+ return not_found;
+ case reply::internal_server_error:
+ return internal_server_error;
+ case reply::not_implemented:
+ return not_implemented;
+ case reply::bad_gateway:
+ return bad_gateway;
+ case reply::service_unavailable:
+ return service_unavailable;
+ default:
+ return internal_server_error;
+ }
+}
+
+} // namespace stock_replies
+
+reply reply::stock_reply(reply::status_type status)
+{
+ reply rep;
+ rep.status = status;
+ rep.content = stock_replies::to_string(status);
+ rep.headers.resize(2);
+ rep.headers[0].name = "Content-Length";
+ rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size());
+ rep.headers[1].name = "Content-Type";
+ rep.headers[1].value = "text/html";
+ return rep;
+}
+
+} // namespace server4
+} // namespace http

Added: trunk/libs/asio/example/http/server4/reply.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/reply.hpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,64 @@
+//
+// reply.hpp
+// ~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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)
+//
+
+#ifndef HTTP_SERVER4_REPLY_HPP
+#define HTTP_SERVER4_REPLY_HPP
+
+#include <string>
+#include <vector>
+#include <boost/asio.hpp>
+#include "header.hpp"
+
+namespace http {
+namespace server4 {
+
+/// A reply to be sent to a client.
+struct reply
+{
+ /// The status of the reply.
+ enum status_type
+ {
+ ok = 200,
+ created = 201,
+ accepted = 202,
+ no_content = 204,
+ multiple_choices = 300,
+ moved_permanently = 301,
+ moved_temporarily = 302,
+ not_modified = 304,
+ bad_request = 400,
+ unauthorized = 401,
+ forbidden = 403,
+ not_found = 404,
+ internal_server_error = 500,
+ not_implemented = 501,
+ bad_gateway = 502,
+ service_unavailable = 503
+ } status;
+
+ /// The headers to be included in the reply.
+ std::vector<header> headers;
+
+ /// The content to be sent in the reply.
+ std::string content;
+
+ /// Convert the reply into a vector of buffers. The buffers do not own the
+ /// underlying memory blocks, therefore the reply object must remain valid and
+ /// not be changed until the write operation has completed.
+ std::vector<boost::asio::const_buffer> to_buffers();
+
+ /// Get a stock reply.
+ static reply stock_reply(status_type status);
+};
+
+} // namespace server4
+} // namespace http
+
+#endif // HTTP_SERVER4_REPLY_HPP

Added: trunk/libs/asio/example/http/server4/request.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/request.hpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,46 @@
+//
+// request.hpp
+// ~~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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)
+//
+
+#ifndef HTTP_SERVER4_REQUEST_HPP
+#define HTTP_SERVER4_REQUEST_HPP
+
+#include <string>
+#include <vector>
+#include "header.hpp"
+
+namespace http {
+namespace server4 {
+
+/// A request received from a client.
+struct request
+{
+ /// The request method, e.g. "GET", "POST".
+ std::string method;
+
+ /// The requested URI, such as a path to a file.
+ std::string uri;
+
+ /// Major version number, usually 1.
+ int http_version_major;
+
+ /// Minor version number, usually 0 or 1.
+ int http_version_minor;
+
+ /// The headers included with the request.
+ std::vector<header> headers;
+
+ /// The optional content sent with the request.
+ std::string content;
+};
+
+} // namespace server4
+} // namespace http
+
+#endif // HTTP_SERVER4_REQUEST_HPP

Added: trunk/libs/asio/example/http/server4/request_parser.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/request_parser.cpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,224 @@
+//
+// request_parser.cpp
+// ~~~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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 "request_parser.hpp"
+#include <algorithm>
+#include <cctype>
+#include <boost/lexical_cast.hpp>
+#include "request.hpp"
+
+namespace http {
+namespace server4 {
+
+#include "yield.hpp" // Enable the pseudo-keywords reenter, yield and fork.
+
+std::string request_parser::content_length_name_ = "Content-Length";
+
+boost::tribool request_parser::consume(request& req, char c)
+{
+ reenter (this)
+ {
+ req.method.clear();
+ req.uri.clear();
+ req.http_version_major = 0;
+ req.http_version_minor = 0;
+ req.headers.clear();
+ req.content.clear();
+ content_length_ = 0;
+
+ // Request method.
+ while (is_char(c) && !is_ctl(c) && !is_tspecial(c) && c != ' ')
+ {
+ req.method.push_back(c);
+ yield return boost::indeterminate;
+ }
+ if (req.method.empty())
+ return false;
+
+ // Space.
+ if (c != ' ') return false;
+ yield return boost::indeterminate;
+
+ // URI.
+ while (!is_ctl(c) && c != ' ')
+ {
+ req.uri.push_back(c);
+ yield return boost::indeterminate;
+ }
+ if (req.uri.empty()) return false;
+
+ // Space.
+ if (c != ' ') return false;
+ yield return boost::indeterminate;
+
+ // HTTP protocol identifier.
+ if (c != 'H') return false;
+ yield return boost::indeterminate;
+ if (c != 'T') return false;
+ yield return boost::indeterminate;
+ if (c != 'T') return false;
+ yield return boost::indeterminate;
+ if (c != 'P') return false;
+ yield return boost::indeterminate;
+
+ // Slash.
+ if (c != '/') return false;
+ yield return boost::indeterminate;
+
+ // Major version number.
+ if (!is_digit(c)) return false;
+ while (is_digit(c))
+ {
+ req.http_version_major = req.http_version_major * 10 + c - '0';
+ yield return boost::indeterminate;
+ }
+
+ // Dot.
+ if (c != '.') return false;
+ yield return boost::indeterminate;
+
+ // Minor version number.
+ if (!is_digit(c)) return false;
+ while (is_digit(c))
+ {
+ req.http_version_minor = req.http_version_minor * 10 + c - '0';
+ yield return boost::indeterminate;
+ }
+
+ // CRLF.
+ if (c != '\r') return false;
+ yield return boost::indeterminate;
+ if (c != '\n') return false;
+ yield return boost::indeterminate;
+
+ // Headers.
+ while ((is_char(c) && !is_ctl(c) && !is_tspecial(c) && c != '\r')
+ || (c == ' ' || c == '\t'))
+ {
+ if (c == ' ' || c == '\t')
+ {
+ // Leading whitespace. Must be continuation of previous header's value.
+ if (req.headers.empty()) return false;
+ while (c == ' ' || c == '\t')
+ yield return boost::indeterminate;
+ }
+ else
+ {
+ // Start the next header.
+ req.headers.push_back(header());
+
+ // Header name.
+ while (is_char(c) && !is_ctl(c) && !is_tspecial(c) && c != ':')
+ {
+ req.headers.back().name.push_back(c);
+ yield return boost::indeterminate;
+ }
+
+ // Colon and space separates the header name from the header value.
+ if (c != ':') return false;
+ yield return boost::indeterminate;
+ if (c != ' ') return false;
+ yield return boost::indeterminate;
+ }
+
+ // Header value.
+ while (is_char(c) && !is_ctl(c) && c != '\r')
+ {
+ req.headers.back().value.push_back(c);
+ yield return boost::indeterminate;
+ }
+
+ // CRLF.
+ if (c != '\r') return false;
+ yield return boost::indeterminate;
+ if (c != '\n') return false;
+ yield return boost::indeterminate;
+ }
+
+ // CRLF.
+ if (c != '\r') return false;
+ yield return boost::indeterminate;
+ if (c != '\n') return false;
+
+ // Check for optional Content-Length header.
+ for (std::size_t i = 0; i < req.headers.size(); ++i)
+ {
+ if (headers_equal(req.headers[i].name, content_length_name_))
+ {
+ try
+ {
+ content_length_ =
+ boost::lexical_cast<std::size_t>(req.headers[i].value);
+ }
+ catch (boost::bad_lexical_cast&)
+ {
+ return false;
+ }
+ }
+ }
+
+ // Content.
+ while (req.content.size() < content_length_)
+ {
+ yield return boost::indeterminate;
+ req.content.push_back(c);
+ }
+ }
+
+ return true;
+}
+
+#include "unyield.hpp" // Disable the pseudo-keywords reenter, yield and fork.
+
+bool request_parser::is_char(int c)
+{
+ return c >= 0 && c <= 127;
+}
+
+bool request_parser::is_ctl(int c)
+{
+ return (c >= 0 && c <= 31) || (c == 127);
+}
+
+bool request_parser::is_tspecial(int c)
+{
+ switch (c)
+ {
+ case '(': case ')': case '<': case '>': case '@':
+ case ',': case ';': case ':': case '\\': case '"':
+ case '/': case '[': case ']': case '?': case '=':
+ case '{': case '}': case ' ': case '\t':
+ return true;
+ default:
+ return false;
+ }
+}
+
+bool request_parser::is_digit(int c)
+{
+ return c >= '0' && c <= '9';
+}
+
+bool request_parser::tolower_compare(char a, char b)
+{
+ return std::tolower(a) == std::tolower(b);
+}
+
+bool request_parser::headers_equal(const std::string& a, const std::string& b)
+{
+ if (a.length() != b.length())
+ return false;
+
+ return std::equal(a.begin(), a.end(), b.begin(),
+ &request_parser::tolower_compare);
+}
+
+} // namespace server4
+} // namespace http

Added: trunk/libs/asio/example/http/server4/request_parser.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/request_parser.hpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,78 @@
+//
+// request_parser.hpp
+// ~~~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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)
+//
+
+#ifndef HTTP_SERVER4_REQUEST_PARSER_HPP
+#define HTTP_SERVER4_REQUEST_PARSER_HPP
+
+#include <string>
+#include <boost/logic/tribool.hpp>
+#include <boost/tuple/tuple.hpp>
+#include "coroutine.hpp"
+
+namespace http {
+namespace server4 {
+
+struct request;
+
+/// Parser for incoming requests.
+class request_parser : coroutine
+{
+public:
+ /// Parse some data. The tribool return value is true when a complete request
+ /// has been parsed, false if the data is invalid, indeterminate when more
+ /// data is required. The InputIterator return value indicates how much of the
+ /// input has been consumed.
+ template <typename InputIterator>
+ boost::tuple<boost::tribool, InputIterator> parse(request& req,
+ InputIterator begin, InputIterator end)
+ {
+ while (begin != end)
+ {
+ boost::tribool result = consume(req, *begin++);
+ if (result || !result)
+ return boost::make_tuple(result, begin);
+ }
+ boost::tribool result = boost::indeterminate;
+ return boost::make_tuple(result, begin);
+ }
+
+private:
+ /// The name of the content length header.
+ static std::string content_length_name_;
+
+ /// Content length as decoded from headers. Defaults to 0.
+ std::size_t content_length_;
+
+ /// Handle the next character of input.
+ boost::tribool consume(request& req, char input);
+
+ /// Check if a byte is an HTTP character.
+ static bool is_char(int c);
+
+ /// Check if a byte is an HTTP control character.
+ static bool is_ctl(int c);
+
+ /// Check if a byte is defined as an HTTP tspecial character.
+ static bool is_tspecial(int c);
+
+ /// Check if a byte is a digit.
+ static bool is_digit(int c);
+
+ /// Check if two characters are equal, without regard to case.
+ static bool tolower_compare(char a, char b);
+
+ /// Check whether the two request header names match.
+ bool headers_equal(const std::string& a, const std::string& b);
+};
+
+} // namespace server4
+} // namespace http
+
+#endif // HTTP_SERVER4_REQUEST_PARSER_HPP

Added: trunk/libs/asio/example/http/server4/server.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/server.cpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,119 @@
+//
+// server.cpp
+// ~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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 "server.hpp"
+#include "request.hpp"
+#include "reply.hpp"
+
+namespace http {
+namespace server4 {
+
+server::server(boost::asio::io_service& io_service,
+ const std::string& address, const std::string& port,
+ boost::function<void(const request&, reply&)> request_handler)
+ : request_handler_(request_handler)
+{
+ tcp::resolver resolver(io_service);
+ tcp::resolver::query query(address, port);
+ acceptor_.reset(new tcp::acceptor(io_service, *resolver.resolve(query)));
+}
+
+#include "yield.hpp" // Enable the pseudo-keywords reenter, yield and fork.
+
+void server::operator()(boost::system::error_code ec, std::size_t length)
+{
+ // In this example we keep the error handling code in one place by
+ // hoisting it outside the coroutine. An alternative approach would be to
+ // check the value of ec after each yield for an asynchronous operation.
+ if (!ec)
+ {
+ // On reentering a coroutine, control jumps to the location of the last
+ // yield or fork. The argument to the "reenter" pseudo-keyword can be a
+ // pointer or reference to an object of type coroutine.
+ reenter (this)
+ {
+ // Loop to accept incoming connections.
+ do
+ {
+ // Create a new socket for the next incoming connection.
+ socket_.reset(new tcp::socket(acceptor_->get_io_service()));
+
+ // Accept a new connection. The "yield" pseudo-keyword saves the current
+ // line number and exits the coroutine's "reenter" block. We use the
+ // server coroutine as the completion handler for the async_accept
+ // operation. When the asynchronous operation completes, the io_service
+ // invokes the function call operator, we "reenter" the coroutine, and
+ // then control resumes at the following line.
+ yield acceptor_->async_accept(*socket_, *this);
+
+ // We "fork" by cloning a new server coroutine to handle the connection.
+ // After forking we have a parent coroutine and a child coroutine. Both
+ // parent and child continue execution at the following line. They can
+ // be distinguished using the functions coroutine::is_parent() and
+ // coroutine::is_child().
+ fork server(*this)();
+
+ // The parent continues looping to accept the next incoming connection.
+ // The child exits the loop and processes the connection.
+ } while (is_parent());
+
+ // Create the objects needed to receive a request on the connection.
+ buffer_.reset(new boost::array<char, 8192>);
+ request_.reset(new request);
+
+ // Loop until a complete request (or an invalid one) has been received.
+ do
+ {
+ // Receive some more data. When control resumes at the following line,
+ // the ec and length parameters reflect the result of the asynchronous
+ // operation.
+ yield socket_->async_read_some(boost::asio::buffer(*buffer_), *this);
+
+ // Parse the data we just received.
+ boost::tie(valid_request_, boost::tuples::ignore)
+ = request_parser_.parse(*request_,
+ buffer_->data(), buffer_->data() + length);
+
+ // An indeterminate result means we need more data, so keep looping.
+ } while (boost::indeterminate(valid_request_));
+
+ // Create the reply object that will be sent back to the client.
+ reply_.reset(new reply);
+
+ if (valid_request_)
+ {
+ // A valid request was received. Call the user-supplied function object
+ // to process the request and compose a reply.
+ request_handler_(*request_, *reply_);
+ }
+ else
+ {
+ // The request was invalid.
+ *reply_ = reply::stock_reply(reply::bad_request);
+ }
+
+ // Send the reply back to the client.
+ yield boost::asio::async_write(*socket_, reply_->to_buffers(), *this);
+
+ // Initiate graceful connection closure.
+ socket_->shutdown(tcp::socket::shutdown_both, ec);
+ }
+ }
+
+ // If an error occurs then the coroutine is not reentered. Consequently, no
+ // new asynchronous operations are started. This means that all shared_ptr
+ // references will disappear and the resources associated with the coroutine
+ // will be destroyed automatically after this function call returns.
+}
+
+#include "unyield.hpp" // Disable the pseudo-keywords reenter, yield and fork.
+
+} // namespace server4
+} // namespace http

Added: trunk/libs/asio/example/http/server4/server.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/server.hpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,74 @@
+//
+// server.hpp
+// ~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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)
+//
+
+#ifndef HTTP_SERVER4_SERVER_HPP
+#define HTTP_SERVER4_SERVER_HPP
+
+#include <boost/asio.hpp>
+#include <string>
+#include <boost/array.hpp>
+#include <boost/function.hpp>
+#include <boost/shared_ptr.hpp>
+#include "coroutine.hpp"
+#include "request_parser.hpp"
+
+namespace http {
+namespace server4 {
+
+struct request;
+struct reply;
+
+/// The top-level coroutine of the HTTP server.
+class server : coroutine
+{
+public:
+ /// Construct the server to listen on the specified TCP address and port, and
+ /// serve up files from the given directory.
+ explicit server(boost::asio::io_service& io_service,
+ const std::string& address, const std::string& port,
+ boost::function<void(const request&, reply&)> request_handler);
+
+ /// Perform work associated with the server.
+ void operator()(
+ boost::system::error_code ec = boost::system::error_code(),
+ std::size_t length = 0);
+
+private:
+ typedef boost::asio::ip::tcp tcp;
+
+ /// The user-supplied handler for all incoming requests.
+ boost::function<void(const request&, reply&)> request_handler_;
+
+ /// Acceptor used to listen for incoming connections.
+ boost::shared_ptr<tcp::acceptor> acceptor_;
+
+ /// The current connection from a client.
+ boost::shared_ptr<tcp::socket> socket_;
+
+ /// Buffer for incoming data.
+ boost::shared_ptr<boost::array<char, 8192> > buffer_;
+
+ /// The incoming request.
+ boost::shared_ptr<request> request_;
+
+ /// Whether the request is valid or not.
+ boost::tribool valid_request_;
+
+ /// The parser for the incoming request.
+ request_parser request_parser_;
+
+ /// The reply to be sent back to the client.
+ boost::shared_ptr<reply> reply_;
+};
+
+} // namespace server4
+} // namespace http
+
+#endif // HTTP_SERVER4_SERVER_HPP

Added: trunk/libs/asio/example/http/server4/unyield.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/unyield.hpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,21 @@
+//
+// unyield.hpp
+// ~~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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)
+//
+
+#ifdef reenter
+# undef reenter
+#endif
+
+#ifdef yield
+# undef yield
+#endif
+
+#ifdef fork
+# undef fork
+#endif

Added: trunk/libs/asio/example/http/server4/win_main.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/win_main.cpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,74 @@
+//
+// win_main.cpp
+// ~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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 <iostream>
+#include <boost/asio.hpp>
+#include <boost/bind.hpp>
+#include <boost/function.hpp>
+#include "server.hpp"
+#include "file_handler.hpp"
+
+#if defined(_WIN32)
+
+boost::function0<void> console_ctrl_function;
+
+BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
+{
+ switch (ctrl_type)
+ {
+ case CTRL_C_EVENT:
+ case CTRL_BREAK_EVENT:
+ case CTRL_CLOSE_EVENT:
+ case CTRL_SHUTDOWN_EVENT:
+ console_ctrl_function();
+ return TRUE;
+ default:
+ return FALSE;
+ }
+}
+
+int main(int argc, char* argv[])
+{
+ try
+ {
+ // Check command line arguments.
+ if (argc != 4)
+ {
+ std::cerr << "Usage: http_server <address> <port> <doc_root>\n";
+ std::cerr << " For IPv4, try:\n";
+ std::cerr << " http_server 0.0.0.0 80 .\n";
+ std::cerr << " For IPv6, try:\n";
+ std::cerr << " http_server 0::0 80 .\n";
+ return 1;
+ }
+
+ boost::asio::io_service io_service;
+
+ // Launch the initial server coroutine.
+ http::server4::server(io_service, argv[1], argv[2],
+ http::server4::file_handler(argv[3]))();
+
+ // Set console control handler to allow server to be stopped.
+ console_ctrl_function = boost::bind(
+ &boost::asio::io_service::stop, &io_service);
+ SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
+
+ // Run the server until stopped.
+ io_service.run();
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << "exception: " << e.what() << "\n";
+ }
+
+ return 0;
+}
+
+#endif // defined(_WIN32)

Added: trunk/libs/asio/example/http/server4/yield.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/http/server4/yield.hpp 2010-01-11 07:22:33 EST (Mon, 11 Jan 2010)
@@ -0,0 +1,23 @@
+//
+// yield.hpp
+// ~~~~~~~~~
+//
+// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// 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 "coroutine.hpp"
+
+#ifndef reenter
+# define reenter(c) CORO_REENTER(c)
+#endif
+
+#ifndef yield
+# define yield CORO_YIELD
+#endif
+
+#ifndef fork
+# define fork CORO_FORK
+#endif


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk