[Zrouter-src] ZRouter.org: push to zconf++ libzhttp/connection.cpp libzhttp/co...
zrouter-src at zrouter.org
zrouter-src at zrouter.org
Thu Jan 12 21:11:00 UTC 2012
details: /rev/528ff8f56dd5
changeset: 40:528ff8f56dd5
user: "Nicolai Petri <nicolai at petri.dk>"
date: Thu Jan 12 22:10:42 2012 +0100
description:
Syntaxification (bcpp run)
diffstat:
libzhttp/connection.cpp | 157 ++++-----
libzhttp/connection.hpp | 89 ++--
libzhttp/connection_manager.cpp | 39 +-
libzhttp/connection_manager.hpp | 45 +-
libzhttp/header.hpp | 23 +-
libzhttp/i_request_handler.hpp | 46 +-
libzhttp/mime_types.cpp | 72 ++--
libzhttp/mime_types.hpp | 22 +-
libzhttp/posix_main.cpp | 84 ++--
libzhttp/reply.cpp | 454 ++++++++++++++--------------
libzhttp/reply.hpp | 81 ++--
libzhttp/request.hpp | 38 +-
libzhttp/request_handler.cpp | 216 ++++++-------
libzhttp/request_handler.hpp | 53 +-
libzhttp/request_parser.cpp | 623 ++++++++++++++++++---------------------
libzhttp/request_parser.hpp | 139 ++++----
libzhttp/server.cpp | 120 +++---
libzhttp/server.hpp | 75 ++--
18 files changed, 1151 insertions(+), 1225 deletions(-)
diffs (2628 lines):
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/connection.cpp
--- a/libzhttp/connection.cpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/connection.cpp Thu Jan 12 22:10:42 2012 +0100
@@ -15,96 +15,87 @@
#include "connection_manager.hpp"
#include "request_handler.hpp"
-namespace http {
-namespace server {
+namespace http
+{
+ namespace server
+ {
-connection::connection(boost::asio::io_service& io_service,
- connection_manager& manager, request_handler& handler)
- : socket_(io_service),
- connection_manager_(manager),
- request_handler_(handler)
-{
-}
+ connection::connection(boost::asio::io_service& io_service,
+ connection_manager& manager, request_handler& handler)
+ : socket_(io_service),
+ connection_manager_(manager),
+ request_handler_(handler) {
+ }
-boost::asio::ip::tcp::socket& connection::socket()
-{
- return socket_;
-}
+ boost::asio::ip::tcp::socket& connection::socket() {
+ return socket_;
+ }
-void connection::start()
-{
- socket_.async_read_some(boost::asio::buffer(buffer_),
- boost::bind(&connection::handle_read, shared_from_this(),
- boost::asio::placeholders::error,
- boost::asio::placeholders::bytes_transferred));
-}
+ void connection::start() {
+ socket_.async_read_some(boost::asio::buffer(buffer_),
+ boost::bind(&connection::handle_read, shared_from_this(),
+ boost::asio::placeholders::error,
+ boost::asio::placeholders::bytes_transferred));
+ }
-void connection::stop()
-{
- socket_.close();
-}
+ void connection::stop() {
+ socket_.close();
+ }
-void connection::handle_read(const boost::system::error_code& e,
- std::size_t bytes_transferred)
-{
- if (!e)
- {
- boost::tribool result;
- boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
- request_, buffer_.data(), buffer_.data() + bytes_transferred);
+ void connection::handle_read(const boost::system::error_code& e,
+ std::size_t bytes_transferred) {
+ if (!e) {
+ boost::tribool result;
+ boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
+ request_, buffer_.data(), buffer_.data() + bytes_transferred);
- if (result)
- {
- request_handler_.handle_request(request_, reply_);
- // FIXME: Make output of timestamp correct. (not hardcoded :)
- std::cout << socket_.remote_endpoint().address() << " - - [99/Sep/2099:12:34:56 +0200] \"" << request_.method << " " << request_.uri << " " << "HTTP/" << request_.http_version_major << "." << request_.http_version_minor << "\" " << reply_.status << " " << reply_.content.size() << std::endl;
+ if (result) {
+ request_handler_.handle_request(request_, reply_);
+ // FIXME: Make output of timestamp correct. (not hardcoded :)
+ std::cout << socket_.remote_endpoint().address() << " - - [99/Sep/2099:12:34:56 +0200] \"" << request_.method << " " << request_.uri << " " << "HTTP/" << request_.http_version_major << "." << request_.http_version_minor << "\" " << reply_.status << " " << reply_.content.size() << std::endl;
- boost::asio::async_write(socket_, reply_.to_buffers(),
- boost::bind(&connection::handle_write, shared_from_this(),
- boost::asio::placeholders::error));
- }
- else if (!result)
- {
- reply_ = reply::stock_reply(reply::bad_request);
- boost::asio::async_write(socket_, reply_.to_buffers(),
- boost::bind(&connection::handle_write, shared_from_this(),
- boost::asio::placeholders::error));
- }
- else
- {
- socket_.async_read_some(boost::asio::buffer(buffer_),
- boost::bind(&connection::handle_read, shared_from_this(),
- boost::asio::placeholders::error,
- boost::asio::placeholders::bytes_transferred));
- }
- }
- else if (e != boost::asio::error::operation_aborted)
- {
- connection_manager_.stop(shared_from_this());
- }
-}
+ boost::asio::async_write(socket_, reply_.to_buffers(),
+ boost::bind(&connection::handle_write, shared_from_this(),
+ boost::asio::placeholders::error));
+ }
+ else if (!result) {
+ reply_ = reply::stock_reply(reply::bad_request);
+ boost::asio::async_write(socket_, reply_.to_buffers(),
+ boost::bind(&connection::handle_write, shared_from_this(),
+ boost::asio::placeholders::error));
+ }
+ else {
+ socket_.async_read_some(boost::asio::buffer(buffer_),
+ boost::bind(&connection::handle_read, shared_from_this(),
+ boost::asio::placeholders::error,
+ boost::asio::placeholders::bytes_transferred));
+ }
+ }
+ else if (e != boost::asio::error::operation_aborted) {
+ connection_manager_.stop(shared_from_this());
+ }
+ }
-void connection::handle_write(const boost::system::error_code& e)
-{
- if (!e)
- {
- // Initiate graceful connection closure.
- boost::system::error_code ignored_ec;
- socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
- }
+ void connection::handle_write(const boost::system::error_code& e) {
+ if (!e) {
+ // Initiate graceful connection closure.
+ boost::system::error_code ignored_ec;
+ socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
+ }
- try {
- if (e != boost::asio::error::operation_aborted)
- {
- connection_manager_.stop(shared_from_this());
- }
- //} catch (boost::system::system_error e) {
- } catch (boost::system::error_code e) {
- //std::cout << "handle_write error - ignoring" << std::endl;
- } catch( boost::exception & e ) {
- std::cout << "handle_write error2 - " << std::endl;
- }
-}
+ try
+ {
+ if (e != boost::asio::error::operation_aborted) {
+ connection_manager_.stop(shared_from_this());
+ }
+ }
+ catch (boost::system::error_code e) {
+ //std::cout << "handle_write error - ignoring" << std::endl;
+ }
+ catch( boost::exception & e ) {
+ std::cout << "handle_write error2 - " << std::endl;
+ }
+ }
-} // namespace server
-} // namespace http
+ } // namespace server
+} // namespace http
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/connection.hpp
--- a/libzhttp/connection.hpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/connection.hpp Thu Jan 12 22:10:42 2012 +0100
@@ -21,63 +21,64 @@
#include "request_handler.hpp"
#include "request_parser.hpp"
-namespace http {
-namespace server {
+namespace http
+{
+ namespace server
+ {
-class connection_manager;
+ class connection_manager;
-/// Represents a single connection from a client.
-class connection
- : public boost::enable_shared_from_this<connection>,
- private boost::noncopyable
-{
-public:
- /// Construct a connection with the given io_service.
- explicit connection(boost::asio::io_service& io_service,
- connection_manager& manager, request_handler& handler);
+ /// Represents a single connection from a client.
+ class connection
+ : public boost::enable_shared_from_this<connection>,
+ private boost::noncopyable
+ {
+ public:
+ /// Construct a connection with the given io_service.
+ explicit connection(boost::asio::io_service& io_service,
+ connection_manager& manager, request_handler& handler);
- /// Get the socket associated with the connection.
- boost::asio::ip::tcp::socket& socket();
+ /// Get the socket associated with the connection.
+ boost::asio::ip::tcp::socket& socket();
- /// Start the first asynchronous operation for the connection.
- void start();
+ /// Start the first asynchronous operation for the connection.
+ void start();
- /// Stop all asynchronous operations associated with the connection.
- void stop();
+ /// Stop all asynchronous operations associated with the connection.
+ void stop();
-private:
- /// Handle completion of a read operation.
- void handle_read(const boost::system::error_code& e,
- std::size_t bytes_transferred);
+ private:
+ /// Handle completion of a read operation.
+ void handle_read(const boost::system::error_code& e,
+ std::size_t bytes_transferred);
- /// Handle completion of a write operation.
- void handle_write(const boost::system::error_code& e);
+ /// Handle completion of a write operation.
+ void handle_write(const boost::system::error_code& e);
- /// Socket for the connection.
- boost::asio::ip::tcp::socket socket_;
+ /// Socket for the connection.
+ boost::asio::ip::tcp::socket socket_;
- /// The manager for this connection.
- connection_manager& connection_manager_;
+ /// The manager for this connection.
+ connection_manager& connection_manager_;
- /// The handler used to process the incoming request.
- request_handler& request_handler_;
+ /// The handler used to process the incoming request.
+ request_handler& request_handler_;
- /// Buffer for incoming data.
- boost::array<char, 8192> buffer_;
+ /// Buffer for incoming data.
+ boost::array<char, 8192> buffer_;
- /// The incoming request.
- request request_;
+ /// The incoming request.
+ request request_;
- /// The parser for the incoming request.
- request_parser request_parser_;
+ /// The parser for the incoming request.
+ request_parser request_parser_;
- /// The reply to be sent back to the client.
- reply reply_;
-};
+ /// The reply to be sent back to the client.
+ reply reply_;
+ };
-typedef boost::shared_ptr<connection> connection_ptr;
+ typedef boost::shared_ptr<connection> connection_ptr;
-} // namespace server
-} // namespace http
-
-#endif // HTTP_CONNECTION_HPP
+ } // namespace server
+} // namespace http
+#endif // HTTP_CONNECTION_HPP
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/connection_manager.cpp
--- a/libzhttp/connection_manager.cpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/connection_manager.cpp Thu Jan 12 22:10:42 2012 +0100
@@ -12,27 +12,26 @@
#include <algorithm>
#include <boost/bind.hpp>
-namespace http {
-namespace server {
+namespace http
+{
+ namespace server
+ {
-void connection_manager::start(connection_ptr c)
-{
- connections_.insert(c);
- c->start();
-}
+ void connection_manager::start(connection_ptr c) {
+ connections_.insert(c);
+ c->start();
+ }
-void connection_manager::stop(connection_ptr c)
-{
- connections_.erase(c);
- c->stop();
-}
+ void connection_manager::stop(connection_ptr c) {
+ connections_.erase(c);
+ c->stop();
+ }
-void connection_manager::stop_all()
-{
- std::for_each(connections_.begin(), connections_.end(),
- boost::bind(&connection::stop, _1));
- connections_.clear();
-}
+ void connection_manager::stop_all() {
+ std::for_each(connections_.begin(), connections_.end(),
+ boost::bind(&connection::stop, _1));
+ connections_.clear();
+ }
-} // namespace server
-} // namespace http
+ } // namespace server
+} // namespace http
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/connection_manager.hpp
--- a/libzhttp/connection_manager.hpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/connection_manager.hpp Thu Jan 12 22:10:42 2012 +0100
@@ -15,30 +15,31 @@
#include <boost/noncopyable.hpp>
#include "connection.hpp"
-namespace http {
-namespace server {
+namespace http
+{
+ namespace server
+ {
-/// Manages open connections so that they may be cleanly stopped when the server
-/// needs to shut down.
-class connection_manager
- : private boost::noncopyable
-{
-public:
- /// Add the specified connection to the manager and start it.
- void start(connection_ptr c);
+ /// Manages open connections so that they may be cleanly stopped when the server
+ /// needs to shut down.
+ class connection_manager
+ : private boost::noncopyable
+ {
+ public:
+ /// Add the specified connection to the manager and start it.
+ void start(connection_ptr c);
- /// Stop the specified connection.
- void stop(connection_ptr c);
+ /// Stop the specified connection.
+ void stop(connection_ptr c);
- /// Stop all connections.
- void stop_all();
+ /// Stop all connections.
+ void stop_all();
-private:
- /// The managed connections.
- std::set<connection_ptr> connections_;
-};
+ private:
+ /// The managed connections.
+ std::set<connection_ptr> connections_;
+ };
-} // namespace server
-} // namespace http
-
-#endif // HTTP_CONNECTION_MANAGER_HPP
+ } // namespace server
+} // namespace http
+#endif // HTTP_CONNECTION_MANAGER_HPP
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/header.hpp
--- a/libzhttp/header.hpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/header.hpp Thu Jan 12 22:10:42 2012 +0100
@@ -13,16 +13,17 @@
#include <string>
-namespace http {
-namespace server {
+namespace http
+{
+ namespace server
+ {
-struct header
-{
- std::string name;
- std::string value;
-};
+ struct header
+ {
+ std::string name;
+ std::string value;
+ };
-} // namespace server
-} // namespace http
-
-#endif // HTTP_HEADER_HPP
+ } // namespace server
+} // namespace http
+#endif // HTTP_HEADER_HPP
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/i_request_handler.hpp
--- a/libzhttp/i_request_handler.hpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/i_request_handler.hpp Thu Jan 12 22:10:42 2012 +0100
@@ -3,26 +3,28 @@
#include <string>
-namespace http {
-namespace server {
- struct reply;
- struct request;
+namespace http
+{
+ namespace server
+ {
+ struct reply;
+ struct request;
+ }
+ namespace handlers
+ {
+
+ enum e_request_handler_type { NORMAL, PRE_HOOK, POST_HOOK };
+
+ class i_request_handler
+ {
+ public:
+ virtual const std::string ns_prefix() = 0;
+ virtual const e_request_handler_type type() = 0;
+ virtual const bool can_handle(const std::string &url, const server::request& req) = 0;
+ virtual void handle(const std::string &url, const server::request& req, server::reply& rep) = 0;
+ // virtual void
+ };
+
+ }
}
-namespace handlers {
-
-
-enum e_request_handler_type { NORMAL, PRE_HOOK, POST_HOOK };
-
-class i_request_handler {
- public:
- virtual const std::string ns_prefix() = 0;
- virtual const e_request_handler_type type() = 0;
- virtual const bool can_handle(const std::string &url, const server::request& req) = 0;
- virtual void handle(const std::string &url, const server::request& req, server::reply& rep) = 0;
- // virtual void
-};
-
-}
-}
-#endif // __I_REQUEST_HANDLER__H
-
+#endif // __I_REQUEST_HANDLER__H
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/mime_types.cpp
--- a/libzhttp/mime_types.cpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/mime_types.cpp Thu Jan 12 22:10:42 2012 +0100
@@ -11,41 +11,43 @@
#include <iostream>
#include "mime_types.hpp"
-namespace http {
-namespace server {
-namespace mime_types {
+namespace http
+{
+ namespace server
+ {
+ namespace mime_types
+ {
-struct mapping
-{
- const char* extension;
- const char* mime_type;
-} mappings[] =
-{
- { "gif", "image/gif" },
- { "htm", "text/html" },
- { "html", "text/html" },
- { "css", "text/css" },
- { "js", "text/javascript" },
- { "jpg", "image/jpeg" },
- { "png", "image/png" },
- { "xml", "text/xml" },
- { "ico", "image/x-ico" },
- { 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;
- }
- }
+ struct mapping
+ {
+ const char* extension;
+ const char* mime_type;
+ } mappings[] =
+ {
+ { "gif", "image/gif" },
+ { "htm", "text/html" },
+ { "html", "text/html" },
+ { "css", "text/css" },
+ { "js", "text/javascript" },
+ { "jpg", "image/jpeg" },
+ { "png", "image/png" },
+ { "xml", "text/xml" },
+ { "ico", "image/x-ico" },
+ { // Marks end of list.
+ 0, 0
+ }
+ };
+ std::string extension_to_type(const std::string& extension) {
+ for (mapping* m = mappings; m->extension; ++m) {
+ if (m->extension == extension) {
+ return m->mime_type;
+ }
+ }
- std::cout << "DEBUG: Unknown mime_type for extension " << extension << " using text/plain!" << std::endl;
- return "text/plain";
-}
+ std::cout << "DEBUG: Unknown mime_type for extension " << extension << " using text/plain!" << std::endl;
+ return "text/plain";
+ }
-} // namespace mime_types
-} // namespace server
-} // namespace http
+ } // namespace mime_types
+ } // namespace server
+} // namespace http
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/mime_types.hpp
--- a/libzhttp/mime_types.hpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/mime_types.hpp Thu Jan 12 22:10:42 2012 +0100
@@ -13,15 +13,17 @@
#include <string>
-namespace http {
-namespace server {
-namespace mime_types {
+namespace http
+{
+ namespace server
+ {
+ namespace mime_types
+ {
-/// Convert a file extension into a MIME type.
-std::string extension_to_type(const std::string& extension);
+ /// Convert a file extension into a MIME type.
+ std::string extension_to_type(const std::string& extension);
-} // namespace mime_types
-} // namespace server
-} // namespace http
-
-#endif // HTTP_MIME_TYPES_HPP
+ } // namespace mime_types
+ } // namespace server
+} // namespace http
+#endif // HTTP_MIME_TYPES_HPP
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/posix_main.cpp
--- a/libzhttp/posix_main.cpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/posix_main.cpp Thu Jan 12 22:10:42 2012 +0100
@@ -20,54 +20,50 @@
#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;
- }
+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;
+ }
- // 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);
+ // 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.
- http::server::server s(argv[1], argv[2], argv[3]);
- boost::thread t(boost::bind(&http::server::server::run, &s));
+ // Run server in background thread.
+ http::server::server s(argv[1], argv[2], argv[3]);
+ boost::thread t(boost::bind(&http::server::server::run, &s));
- // Restore previous signals.
- pthread_sigmask(SIG_SETMASK, &old_mask, 0);
+ // 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);
+ // 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.
- s.stop();
- t.join();
- }
- catch (std::exception& e)
- {
- std::cerr << "exception: " << e.what() << "\n";
- }
+ // Stop the server.
+ s.stop();
+ t.join();
+ }
+ catch (std::exception& e) {
+ std::cerr << "exception: " << e.what() << "\n";
+ }
- return 0;
+ return 0;
}
-
-#endif // !defined(_WIN32)
+#endif // !defined(_WIN32)
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/reply.cpp
--- a/libzhttp/reply.cpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/reply.cpp Thu Jan 12 22:10:42 2012 +0100
@@ -12,245 +12,243 @@
#include <string>
#include <boost/lexical_cast.hpp>
-namespace http {
-namespace server {
+namespace http
+{
+ namespace server
+ {
-namespace status_strings {
+ 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";
+ 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);
- }
-}
+ 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 status_strings
-namespace misc_strings {
+ namespace misc_strings
+ {
-const char name_value_separator[] = { ':', ' ' };
-const char crlf[] = { '\r', '\n' };
+ const char name_value_separator[] = { ':', ' ' };
+ const char crlf[] = { '\r', '\n' };
-} // namespace misc_strings
+ } // 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;
-}
+ 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 {
+ 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>";
+ 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;
- }
-}
+ 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
+ } // 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;
-}
+ 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 server
-} // namespace http
+ } // namespace server
+} // namespace http
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/reply.hpp
--- a/libzhttp/reply.hpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/reply.hpp Thu Jan 12 22:10:42 2012 +0100
@@ -16,49 +16,50 @@
#include <boost/asio.hpp>
#include "header.hpp"
-namespace http {
-namespace server {
+namespace http
+{
+ namespace server
+ {
-/// 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;
+ /// 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 headers to be included in the reply.
+ std::vector<header> headers;
- /// The content to be sent in the reply.
- std::string content;
+ /// 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();
+ /// 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);
-};
+ /// Get a stock reply.
+ static reply stock_reply(status_type status);
+ };
-} // namespace server
-} // namespace http
-
-#endif // HTTP_REPLY_HPP
+ } // namespace server
+} // namespace http
+#endif // HTTP_REPLY_HPP
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/request.hpp
--- a/libzhttp/request.hpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/request.hpp Thu Jan 12 22:10:42 2012 +0100
@@ -15,24 +15,24 @@
#include <map>
#include "header.hpp"
+namespace http
+{
+ namespace server
+ {
-namespace http {
-namespace server {
+ /// A request received from a client.
+ struct request
+ {
+ std::string method;
+ std::string uri;
+ int http_version_major;
+ int http_version_minor;
+ bool header_completed;
+ std::map<std::string, std::string> headers;
+ header cur_header;
+ std::string body;
+ };
-/// A request received from a client.
-struct request
-{
- std::string method;
- std::string uri;
- int http_version_major;
- int http_version_minor;
- bool header_completed;
- std::map<std::string, std::string> headers;
- header cur_header;
- std::string body;
-};
-
-} // namespace server
-} // namespace http
-
-#endif // HTTP_REQUEST_HPP
+ } // namespace server
+} // namespace http
+#endif // HTTP_REQUEST_HPP
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/request_handler.cpp
--- a/libzhttp/request_handler.cpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/request_handler.cpp Thu Jan 12 22:10:42 2012 +0100
@@ -19,127 +19,113 @@
#include "reply.hpp"
#include "request.hpp"
-namespace http {
-namespace server {
+namespace http
+{
+ namespace server
+ {
-using namespace handlers;
+ using namespace handlers;
-request_handler::request_handler(const std::string& doc_root)
- : doc_root_(doc_root)
-{
-}
+ request_handler::request_handler(const std::string& doc_root)
+ : doc_root_(doc_root) {
+ }
-void request_handler::register_handler(handlers::i_request_handler *handler) {
- handlers.push_back(handler);
-}
+ void request_handler::register_handler(handlers::i_request_handler *handler) {
+ handlers.push_back(handler);
+ }
-void request_handler::handle_request(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;
- }
- typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
- boost::char_separator<char> sep("?&");
- tokenizer tokens(request_path, sep);
- for (tokenizer::iterator tok_iter = tokens.begin();
- tok_iter != tokens.end(); ++tok_iter) {
- }
- request_path = *(tokens.begin());
+ void request_handler::handle_request(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;
+ }
+ typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
+ boost::char_separator<char> sep("?&");
+ tokenizer tokens(request_path, sep);
+ for (tokenizer::iterator tok_iter = tokens.begin();
+ tok_iter != tokens.end(); ++tok_iter) {
+ }
+ request_path = *(tokens.begin());
- // 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;
- }
+ // 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;
+ }
- std::list<i_request_handler*>::iterator hi = handlers.begin();
- while (hi != handlers.end()) {
- if ( (*hi)->can_handle(request_path, req)) {
- (*hi)->handle(request_path, req, rep);
- return;
+ std::list<i_request_handler*>::iterator hi = handlers.begin();
+ while (hi != handlers.end()) {
+ if ( (*hi)->can_handle(request_path, req)) {
+ (*hi)->handle(request_path, req, rep);
+ return;
+ }
+ ++hi;
+ }
+ // 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[4096];
+ 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);
}
- ++hi;
- }
- // 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);
- }
+ bool request_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;
+ }
- // 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[4096];
- 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 request_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 server
-} // namespace http
+ } // namespace server
+} // namespace http
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/request_handler.hpp
--- a/libzhttp/request_handler.hpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/request_handler.hpp Thu Jan 12 22:10:42 2012 +0100
@@ -17,35 +17,36 @@
#include "i_request_handler.hpp"
-namespace http {
-namespace server {
+namespace http
+{
+ namespace server
+ {
-struct reply;
-struct request;
+ struct reply;
+ struct request;
-/// The common handler for all incoming requests.
-class request_handler
- : private boost::noncopyable
-{
-public:
- /// Construct with a directory containing files to be served.
- explicit request_handler(const std::string& doc_root);
+ /// The common handler for all incoming requests.
+ class request_handler
+ : private boost::noncopyable
+ {
+ public:
+ /// Construct with a directory containing files to be served.
+ explicit request_handler(const std::string& doc_root);
- void register_handler(handlers::i_request_handler *handler);
- /// Handle a request and produce a reply.
- void handle_request(const request& req, reply& rep);
+ void register_handler(handlers::i_request_handler *handler);
+ /// Handle a request and produce a reply.
+ void handle_request(const request& req, reply& rep);
-private:
- /// The directory containing the files to be served.
- std::string doc_root_;
- std::list<http::handlers::i_request_handler*> handlers;
+ private:
+ /// The directory containing the files to be served.
+ std::string doc_root_;
+ std::list<http::handlers::i_request_handler*> handlers;
- /// Perform URL-decoding on a string. Returns false if the encoding was
- /// invalid.
- static bool url_decode(const std::string& in, std::string& out);
-};
+ /// 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 server
-} // namespace http
-
-#endif // HTTP_REQUEST_HANDLER_HPP
+ } // namespace server
+} // namespace http
+#endif // HTTP_REQUEST_HANDLER_HPP
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/request_parser.cpp
--- a/libzhttp/request_parser.cpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/request_parser.cpp Thu Jan 12 22:10:42 2012 +0100
@@ -14,349 +14,298 @@
#include <iostream>
#include <boost/lexical_cast.hpp>
-namespace http {
-namespace server {
+namespace http
+{
+ namespace server
+ {
-request_parser::request_parser()
- : state_(method_start)
-{
-}
+ request_parser::request_parser()
+ : state_(method_start) {
+ }
-void request_parser::reset()
-{
- state_ = method_start;
-}
+ void request_parser::reset() {
+ state_ = method_start;
+ }
-boost::tribool request_parser::consume(request& req, char input)
-{
- switch (state_)
- {
- case method_start:
- if (!is_char(input) || is_ctl(input) || is_tspecial(input))
- {
- return false;
- }
- else
- {
- state_ = method;
- req.method.push_back(input);
- return boost::indeterminate;
- }
- case method:
- if (input == ' ')
- {
- state_ = uri;
- return boost::indeterminate;
- }
- else if (!is_char(input) || is_ctl(input) || is_tspecial(input))
- {
- return false;
- }
- else
- {
- req.method.push_back(input);
- return boost::indeterminate;
- }
- case uri_start:
- if (is_ctl(input))
- {
- return false;
- }
- else
- {
- state_ = uri;
- req.uri.push_back(input);
- return boost::indeterminate;
- }
- case uri:
- if (input == ' ')
- {
- state_ = http_version_h;
- return boost::indeterminate;
- }
- else if (is_ctl(input))
- {
- return false;
- }
- else
- {
- req.uri.push_back(input);
- return boost::indeterminate;
- }
- case http_version_h:
- if (input == 'H')
- {
- state_ = http_version_t_1;
- return boost::indeterminate;
- }
- else
- {
- return false;
- }
- case http_version_t_1:
- if (input == 'T')
- {
- state_ = http_version_t_2;
- return boost::indeterminate;
- }
- else
- {
- return false;
- }
- case http_version_t_2:
- if (input == 'T')
- {
- state_ = http_version_p;
- return boost::indeterminate;
- }
- else
- {
- return false;
- }
- case http_version_p:
- if (input == 'P')
- {
- state_ = http_version_slash;
- return boost::indeterminate;
- }
- else
- {
- return false;
- }
- case http_version_slash:
- if (input == '/')
- {
- req.http_version_major = 0;
- req.http_version_minor = 0;
- state_ = http_version_major_start;
- return boost::indeterminate;
- }
- else
- {
- return false;
- }
- case http_version_major_start:
- if (is_digit(input))
- {
- req.http_version_major = req.http_version_major * 10 + input - '0';
- state_ = http_version_major;
- return boost::indeterminate;
- }
- else
- {
- return false;
- }
- case http_version_major:
- if (input == '.')
- {
- state_ = http_version_minor_start;
- return boost::indeterminate;
- }
- else if (is_digit(input))
- {
- req.http_version_major = req.http_version_major * 10 + input - '0';
- return boost::indeterminate;
- }
- else
- {
- return false;
- }
- case http_version_minor_start:
- if (is_digit(input))
- {
- req.http_version_minor = req.http_version_minor * 10 + input - '0';
- state_ = http_version_minor;
- return boost::indeterminate;
- }
- else
- {
- return false;
- }
- case http_version_minor:
- if (input == '\r')
- {
- state_ = expecting_newline_1;
- return boost::indeterminate;
- }
- else if (is_digit(input))
- {
- req.http_version_minor = req.http_version_minor * 10 + input - '0';
- return boost::indeterminate;
- }
- else
- {
- return false;
- }
- case expecting_newline_1:
- if (input == '\n')
- {
- state_ = header_line_start;
- return boost::indeterminate;
- }
- else
- {
- return false;
- }
- case header_line_start:
- if (input == '\r')
- {
- state_ = expecting_newline_3;
- return boost::indeterminate;
- }
- else if (req.cur_header.name.size() > 0 && (input == ' ' || input == '\t'))
- {
- state_ = header_lws;
- return boost::indeterminate;
- }
- else if (!is_char(input) || is_ctl(input) || is_tspecial(input))
- {
- return false;
- }
- else
- {
- req.cur_header = header();
- req.cur_header.name.push_back(input);
- state_ = header_name;
- return boost::indeterminate;
- }
- case header_lws:
- if (input == '\r')
- {
- state_ = expecting_newline_2;
- return boost::indeterminate;
- }
- else if (input == ' ' || input == '\t')
- {
- return boost::indeterminate;
- }
- else if (is_ctl(input))
- {
- return false;
- }
- else
- {
- state_ = header_value;
- req.cur_header.value.push_back(input);
- return boost::indeterminate;
- }
- case header_name:
- if (input == ':')
- {
- state_ = space_before_header_value;
- return boost::indeterminate;
- }
- else if (!is_char(input) || is_ctl(input) || is_tspecial(input))
- {
- return false;
- }
- else
- {
- req.cur_header.name.push_back(input);
- return boost::indeterminate;
- }
- case space_before_header_value:
- if (input == ' ')
- {
- state_ = header_value;
- return boost::indeterminate;
- }
- else
- {
- return false;
- }
- case header_value:
- if (input == '\r')
- {
- // std::cout << "Saving header : " << req.cur_header.name << std::endl;
- req.headers[req.cur_header.name] = req.cur_header.value;
- state_ = expecting_newline_2;
- return boost::indeterminate;
- }
- else if (is_ctl(input))
- {
- return false;
- }
- else
- {
- req.cur_header.value.push_back(input);
- return boost::indeterminate;
- }
- case expecting_newline_2:
- if (input == '\n')
- {
- state_ = header_line_start;
- return boost::indeterminate;
- } else if (req.method == "POST" || req.method == "PUT") {
- state_ = expecting_body;
- return boost::indeterminate;
- } else {
- req.header_completed = true;
- std::cout << "REQ COMPLETED1" << std::endl;
- return true;
- }
- case expecting_newline_3:
- if (input == '\n') {
- req.header_completed = true;
- if (req.method == "POST" || req.method == "PUT") {
- state_ = expecting_body;
- return boost::indeterminate;
- } else {
- return true;
+ boost::tribool request_parser::consume(request& req, char input) {
+ switch (state_) {
+ case method_start:
+ if (!is_char(input) || is_ctl(input) || is_tspecial(input)) {
+ return false;
+ }
+ else {
+ state_ = method;
+ req.method.push_back(input);
+ return boost::indeterminate;
+ }
+ case method:
+ if (input == ' ') {
+ state_ = uri;
+ return boost::indeterminate;
+ }
+ else if (!is_char(input) || is_ctl(input) || is_tspecial(input)) {
+ return false;
+ }
+ else {
+ req.method.push_back(input);
+ return boost::indeterminate;
+ }
+ case uri_start:
+ if (is_ctl(input)) {
+ return false;
+ }
+ else {
+ state_ = uri;
+ req.uri.push_back(input);
+ return boost::indeterminate;
+ }
+ case uri:
+ if (input == ' ') {
+ state_ = http_version_h;
+ return boost::indeterminate;
+ }
+ else if (is_ctl(input)) {
+ return false;
+ }
+ else {
+ req.uri.push_back(input);
+ return boost::indeterminate;
+ }
+ case http_version_h:
+ if (input == 'H') {
+ state_ = http_version_t_1;
+ return boost::indeterminate;
+ }
+ else {
+ return false;
+ }
+ case http_version_t_1:
+ if (input == 'T') {
+ state_ = http_version_t_2;
+ return boost::indeterminate;
+ }
+ else {
+ return false;
+ }
+ case http_version_t_2:
+ if (input == 'T') {
+ state_ = http_version_p;
+ return boost::indeterminate;
+ }
+ else {
+ return false;
+ }
+ case http_version_p:
+ if (input == 'P') {
+ state_ = http_version_slash;
+ return boost::indeterminate;
+ }
+ else {
+ return false;
+ }
+ case http_version_slash:
+ if (input == '/') {
+ req.http_version_major = 0;
+ req.http_version_minor = 0;
+ state_ = http_version_major_start;
+ return boost::indeterminate;
+ }
+ else {
+ return false;
+ }
+ case http_version_major_start:
+ if (is_digit(input)) {
+ req.http_version_major = req.http_version_major * 10 + input - '0';
+ state_ = http_version_major;
+ return boost::indeterminate;
+ }
+ else {
+ return false;
+ }
+ case http_version_major:
+ if (input == '.') {
+ state_ = http_version_minor_start;
+ return boost::indeterminate;
+ }
+ else if (is_digit(input)) {
+ req.http_version_major = req.http_version_major * 10 + input - '0';
+ return boost::indeterminate;
+ }
+ else {
+ return false;
+ }
+ case http_version_minor_start:
+ if (is_digit(input)) {
+ req.http_version_minor = req.http_version_minor * 10 + input - '0';
+ state_ = http_version_minor;
+ return boost::indeterminate;
+ }
+ else {
+ return false;
+ }
+ case http_version_minor:
+ if (input == '\r') {
+ state_ = expecting_newline_1;
+ return boost::indeterminate;
+ }
+ else if (is_digit(input)) {
+ req.http_version_minor = req.http_version_minor * 10 + input - '0';
+ return boost::indeterminate;
+ }
+ else {
+ return false;
+ }
+ case expecting_newline_1:
+ if (input == '\n') {
+ state_ = header_line_start;
+ return boost::indeterminate;
+ }
+ else {
+ return false;
+ }
+ case header_line_start:
+ if (input == '\r') {
+ state_ = expecting_newline_3;
+ return boost::indeterminate;
+ }
+ else if (req.cur_header.name.size() > 0 && (input == ' ' || input == '\t')) {
+ state_ = header_lws;
+ return boost::indeterminate;
+ }
+ else if (!is_char(input) || is_ctl(input) || is_tspecial(input)) {
+ return false;
+ }
+ else {
+ req.cur_header = header();
+ req.cur_header.name.push_back(input);
+ state_ = header_name;
+ return boost::indeterminate;
+ }
+ case header_lws:
+ if (input == '\r') {
+ state_ = expecting_newline_2;
+ return boost::indeterminate;
+ }
+ else if (input == ' ' || input == '\t') {
+ return boost::indeterminate;
+ }
+ else if (is_ctl(input)) {
+ return false;
+ }
+ else {
+ state_ = header_value;
+ req.cur_header.value.push_back(input);
+ return boost::indeterminate;
+ }
+ case header_name:
+ if (input == ':') {
+ state_ = space_before_header_value;
+ return boost::indeterminate;
+ }
+ else if (!is_char(input) || is_ctl(input) || is_tspecial(input)) {
+ return false;
+ }
+ else {
+ req.cur_header.name.push_back(input);
+ return boost::indeterminate;
+ }
+ case space_before_header_value:
+ if (input == ' ') {
+ state_ = header_value;
+ return boost::indeterminate;
+ }
+ else {
+ return false;
+ }
+ case header_value:
+ if (input == '\r') {
+ // std::cout << "Saving header : " << req.cur_header.name << std::endl;
+ req.headers[req.cur_header.name] = req.cur_header.value;
+ state_ = expecting_newline_2;
+ return boost::indeterminate;
+ }
+ else if (is_ctl(input)) {
+ return false;
+ }
+ else {
+ req.cur_header.value.push_back(input);
+ return boost::indeterminate;
+ }
+ case expecting_newline_2:
+ if (input == '\n') {
+ state_ = header_line_start;
+ return boost::indeterminate;
+ }
+ else if (req.method == "POST" || req.method == "PUT") {
+ state_ = expecting_body;
+ return boost::indeterminate;
+ }
+ else {
+ req.header_completed = true;
+ std::cout << "REQ COMPLETED1" << std::endl;
+ return true;
+ }
+ case expecting_newline_3:
+ if (input == '\n') {
+ req.header_completed = true;
+ if (req.method == "POST" || req.method == "PUT") {
+ state_ = expecting_body;
+ return boost::indeterminate;
+ }
+ else {
+ return true;
+ }
+ }
+ else {
+ return false;
+ }
+ case expecting_body:
+ req.body += input;
+ // http 1.1 with content length
+ if (req.http_version_major == 1 && req.http_version_minor >= 1 && req.headers.find("Content-Length") != req.headers.end()) {
+ int expected_size = boost::lexical_cast<int>(req.headers["Content-Length"]);
+ //std::cout << " size " << expected_size << " vs " << req.body.size() << std::endl;
+ if (expected_size == req.body.size()) {
+ // Completed!!!
+ std::cout << "REQ COMPLETED3" << std::endl;
+ return true;
+ }
+
+ }
+ else {
+ std::cout << "3body |" << req.body << "|" << req.body.size() << " vs " << req.headers["Content-Length"] << std::endl;
+ }
+
+ return boost::indeterminate;
+ // return (input == '\n');
+ default:
+ return false;
}
- } else {
- return false;
}
- case expecting_body:
- req.body += input;
- // http 1.1 with content length
- if (req.http_version_major == 1 && req.http_version_minor >= 1 && req.headers.find("Content-Length") != req.headers.end()) {
- int expected_size = boost::lexical_cast<int>(req.headers["Content-Length"]);
- //std::cout << " size " << expected_size << " vs " << req.body.size() << std::endl;
- if (expected_size == req.body.size()) {
- // Completed!!!
- std::cout << "REQ COMPLETED3" << std::endl;
- return true;
+
+ 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;
}
+ }
- } else {
- std::cout << "3body |" << req.body << "|" << req.body.size() << " vs " << req.headers["Content-Length"] << std::endl;
+ bool request_parser::is_digit(int c) {
+ return c >= '0' && c <= '9';
}
-
- return boost::indeterminate;
- // return (input == '\n');
- default:
- return false;
- }
-}
-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';
-}
-
-} // namespace server
-} // namespace http
+ } // namespace server
+} // namespace http
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/request_parser.hpp
--- a/libzhttp/request_parser.hpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/request_parser.hpp Thu Jan 12 22:10:42 2012 +0100
@@ -15,84 +15,83 @@
#include <boost/logic/tribool.hpp>
#include <boost/tuple/tuple.hpp>
-namespace http {
-namespace server {
+namespace http
+{
+ namespace server
+ {
-struct request;
+ struct request;
-/// Parser for incoming requests.
-class request_parser
-{
-public:
- /// Construct ready to parse the request method.
- request_parser();
+ /// Parser for incoming requests.
+ class request_parser
+ {
+ public:
+ /// Construct ready to parse the request method.
+ request_parser();
- /// Reset to initial parser state.
- void reset();
+ /// Reset to initial parser state.
+ void reset();
- /// 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);
- }
+ /// 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:
- /// Handle the next character of input.
- boost::tribool consume(request& req, char input);
+ private:
+ /// 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 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 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 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 a byte is a digit.
+ static bool is_digit(int c);
- /// The current state of the parser.
- enum state
- {
- method_start,
- method,
- uri_start,
- uri,
- http_version_h,
- http_version_t_1,
- http_version_t_2,
- http_version_p,
- http_version_slash,
- http_version_major_start,
- http_version_major,
- http_version_minor_start,
- http_version_minor,
- expecting_newline_1,
- header_line_start,
- header_lws,
- header_name,
- space_before_header_value,
- header_value,
- expecting_newline_2,
- expecting_newline_3,
- expecting_body
- } state_;
-};
+ /// The current state of the parser.
+ enum state
+ {
+ method_start,
+ method,
+ uri_start,
+ uri,
+ http_version_h,
+ http_version_t_1,
+ http_version_t_2,
+ http_version_p,
+ http_version_slash,
+ http_version_major_start,
+ http_version_major,
+ http_version_minor_start,
+ http_version_minor,
+ expecting_newline_1,
+ header_line_start,
+ header_lws,
+ header_name,
+ space_before_header_value,
+ header_value,
+ expecting_newline_2,
+ expecting_newline_3,
+ expecting_body
+ } state_;
+ };
-} // namespace server
-} // namespace http
-
-#endif // HTTP_REQUEST_PARSER_HPP
+ } // namespace server
+} // namespace http
+#endif // HTTP_REQUEST_PARSER_HPP
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/server.cpp
--- a/libzhttp/server.cpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/server.cpp Thu Jan 12 22:10:42 2012 +0100
@@ -11,72 +11,68 @@
#include "server.hpp"
#include <boost/bind.hpp>
-namespace http {
-namespace server {
+namespace http
+{
+ namespace server
+ {
-server::server(boost::asio::io_service &io_svc, const std::string& address, const std::string& port,
- const std::string& doc_root)
- : io_service_(io_svc),
- acceptor_(io_service_),
- connection_manager_(),
- new_connection_(new connection(io_service_,
- connection_manager_, request_handler_)),
- request_handler_(doc_root)
-{
- // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
- boost::asio::ip::tcp::resolver resolver(io_service_);
- boost::asio::ip::tcp::resolver::query query(address, port);
- boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
- acceptor_.open(endpoint.protocol());
- acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
- acceptor_.bind(endpoint);
- acceptor_.listen();
- acceptor_.async_accept(new_connection_->socket(),
- boost::bind(&server::handle_accept, this,
- boost::asio::placeholders::error));
-}
+ server::server(boost::asio::io_service &io_svc, const std::string& address, const std::string& port,
+ const std::string& doc_root)
+ : io_service_(io_svc),
+ acceptor_(io_service_),
+ connection_manager_(),
+ new_connection_(new connection(io_service_,
+ connection_manager_, request_handler_)),
+ request_handler_(doc_root) {
+ // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
+ boost::asio::ip::tcp::resolver resolver(io_service_);
+ boost::asio::ip::tcp::resolver::query query(address, port);
+ boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
+ acceptor_.open(endpoint.protocol());
+ acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
+ acceptor_.bind(endpoint);
+ acceptor_.listen();
+ acceptor_.async_accept(new_connection_->socket(),
+ boost::bind(&server::handle_accept, this,
+ boost::asio::placeholders::error));
+ }
-void server::register_handler(handlers::i_request_handler *handler) {
- request_handler_.register_handler(handler);
-}
+ void server::register_handler(handlers::i_request_handler *handler) {
+ request_handler_.register_handler(handler);
+ }
-void server::run()
-{
- // The io_service::run() call will block until all asynchronous operations
- // have finished. While the server is running, there is always at least one
- // asynchronous operation outstanding: the asynchronous accept call waiting
- // for new incoming connections.
- io_service_.run();
-}
+ void server::run() {
+ // The io_service::run() call will block until all asynchronous operations
+ // have finished. While the server is running, there is always at least one
+ // asynchronous operation outstanding: the asynchronous accept call waiting
+ // for new incoming connections.
+ io_service_.run();
+ }
-void server::stop()
-{
- // Post a call to the stop function so that server::stop() is safe to call
- // from any thread.
- io_service_.post(boost::bind(&server::handle_stop, this));
-}
+ void server::stop() {
+ // Post a call to the stop function so that server::stop() is safe to call
+ // from any thread.
+ io_service_.post(boost::bind(&server::handle_stop, this));
+ }
-void server::handle_accept(const boost::system::error_code& e)
-{
- if (!e)
- {
- connection_manager_.start(new_connection_);
- new_connection_.reset(new connection(io_service_,
- connection_manager_, request_handler_));
- acceptor_.async_accept(new_connection_->socket(),
- boost::bind(&server::handle_accept, this,
- boost::asio::placeholders::error));
- }
-}
+ void server::handle_accept(const boost::system::error_code& e) {
+ if (!e) {
+ connection_manager_.start(new_connection_);
+ new_connection_.reset(new connection(io_service_,
+ connection_manager_, request_handler_));
+ acceptor_.async_accept(new_connection_->socket(),
+ boost::bind(&server::handle_accept, this,
+ boost::asio::placeholders::error));
+ }
+ }
-void server::handle_stop()
-{
- // The server is stopped by cancelling all outstanding asynchronous
- // operations. Once all operations have finished the io_service::run() call
- // will exit.
- acceptor_.close();
- connection_manager_.stop_all();
-}
+ void server::handle_stop() {
+ // The server is stopped by cancelling all outstanding asynchronous
+ // operations. Once all operations have finished the io_service::run() call
+ // will exit.
+ acceptor_.close();
+ connection_manager_.stop_all();
+ }
-} // namespace server
-} // namespace http
+ } // namespace server
+} // namespace http
diff -r 69aa52291877 -r 528ff8f56dd5 libzhttp/server.hpp
--- a/libzhttp/server.hpp Thu Jan 12 19:46:57 2012 +0100
+++ b/libzhttp/server.hpp Thu Jan 12 22:10:42 2012 +0100
@@ -18,52 +18,53 @@
#include "connection_manager.hpp"
#include "request_handler.hpp"
-namespace http {
-namespace server {
+namespace http
+{
+ namespace server
+ {
-/// The top-level class of the HTTP server.
-class server
- : private boost::noncopyable
-{
-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_svc, const std::string& address, const std::string& port,
- const std::string& doc_root);
+ /// The top-level class of the HTTP server.
+ class server
+ : private boost::noncopyable
+ {
+ 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_svc, const std::string& address, const std::string& port,
+ const std::string& doc_root);
- /// Run the server's io_service loop.
- void run();
+ /// Run the server's io_service loop.
+ void run();
- /// Stop the server.
- void stop();
+ /// Stop the server.
+ void stop();
- /// Register a custom request handler
- void register_handler(handlers::i_request_handler *handler);
+ /// Register a custom request handler
+ void register_handler(handlers::i_request_handler *handler);
-private:
- /// Handle completion of an asynchronous accept operation.
- void handle_accept(const boost::system::error_code& e);
+ private:
+ /// Handle completion of an asynchronous accept operation.
+ void handle_accept(const boost::system::error_code& e);
- /// Handle a request to stop the server.
- void handle_stop();
+ /// Handle a request to stop the server.
+ void handle_stop();
- /// The io_service used to perform asynchronous operations.
- boost::asio::io_service &io_service_;
+ /// The io_service used to perform asynchronous operations.
+ boost::asio::io_service &io_service_;
- /// Acceptor used to listen for incoming connections.
- boost::asio::ip::tcp::acceptor acceptor_;
+ /// Acceptor used to listen for incoming connections.
+ boost::asio::ip::tcp::acceptor acceptor_;
- /// The connection manager which owns all live connections.
- connection_manager connection_manager_;
+ /// The connection manager which owns all live connections.
+ connection_manager connection_manager_;
- /// The next connection to be accepted.
- connection_ptr new_connection_;
+ /// The next connection to be accepted.
+ connection_ptr new_connection_;
- /// The handler for all incoming requests.
- request_handler request_handler_;
-};
+ /// The handler for all incoming requests.
+ request_handler request_handler_;
+ };
-} // namespace server
-} // namespace http
-
-#endif // HTTP_SERVER_HPP
+ } // namespace server
+} // namespace http
+#endif // HTTP_SERVER_HPP
More information about the Zrouter-src
mailing list