Update style to (almost) conform to ndn-cxx style using clang-format
diff --git a/server/connection.cpp b/server/connection.cpp
index 207c8c8..cdbf1b2 100644
--- a/server/connection.cpp
+++ b/server/connection.cpp
@@ -28,88 +28,85 @@
//
#include "connection.hpp"
-#include <vector>
-#include <boost/bind.hpp>
#include "connection_manager.hpp"
#include "request_handler.hpp"
+#include <boost/bind.hpp>
+#include <vector>
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()
+boost::asio::ip::tcp::socket&
+connection::socket()
{
return socket_;
}
-void connection::start()
+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));
+ boost::bind(&connection::handle_read, shared_from_this(),
+ boost::asio::placeholders::error,
+ boost::asio::placeholders::bytes_transferred));
}
-void connection::stop()
+void
+connection::stop()
{
socket_.close();
}
-void connection::handle_read(const boost::system::error_code& e,
- std::size_t bytes_transferred)
+void
+connection::handle_read(const boost::system::error_code& e, std::size_t bytes_transferred)
{
- if (!e)
- {
+ if (!e) {
boost::tribool result;
- boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
- request_, buffer_.data(), buffer_.data() + bytes_transferred);
+ boost::tie(result, boost::tuples::ignore) =
+ request_parser_.parse(request_, buffer_.data(), buffer_.data() + bytes_transferred);
- if (result)
- {
+ if (result) {
request_handler_.handle_request(request_, reply_);
boost::asio::async_write(socket_, reply_.to_buffers(),
- boost::bind(&connection::handle_write, shared_from_this(),
- boost::asio::placeholders::error));
+ boost::bind(&connection::handle_write, shared_from_this(),
+ boost::asio::placeholders::error));
}
- else if (!result)
- {
+ 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));
+ boost::bind(&connection::handle_write, shared_from_this(),
+ boost::asio::placeholders::error));
}
- else
- {
+ 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));
+ 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)
- {
+ else if (e != boost::asio::error::operation_aborted) {
connection_manager_.stop(shared_from_this());
}
}
-void connection::handle_write(const boost::system::error_code& e)
+void
+connection::handle_write(const boost::system::error_code& e)
{
- if (!e)
- {
+ if (!e) {
// Initiate graceful connection closure.
boost::system::error_code ignored_ec;
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
}
- if (e != boost::asio::error::operation_aborted)
- {
+ if (e != boost::asio::error::operation_aborted) {
connection_manager_.stop(shared_from_this());
}
}
diff --git a/server/connection.hpp b/server/connection.hpp
index 107af14..791de02 100644
--- a/server/connection.hpp
+++ b/server/connection.hpp
@@ -30,15 +30,15 @@
#ifndef HTTP_CONNECTION_HPP
#define HTTP_CONNECTION_HPP
-#include <boost/asio.hpp>
-#include <boost/array.hpp>
-#include <boost/noncopyable.hpp>
-#include <boost/shared_ptr.hpp>
-#include <boost/enable_shared_from_this.hpp>
#include "reply.hpp"
#include "request.hpp"
#include "request_handler.hpp"
#include "request_parser.hpp"
+#include <boost/array.hpp>
+#include <boost/asio.hpp>
+#include <boost/enable_shared_from_this.hpp>
+#include <boost/noncopyable.hpp>
+#include <boost/shared_ptr.hpp>
namespace http {
namespace server {
@@ -46,31 +46,33 @@
class connection_manager;
/// Represents a single connection from a client.
-class connection
- : public boost::enable_shared_from_this<connection>,
- private boost::noncopyable
+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);
+ 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();
+ boost::asio::ip::tcp::socket&
+ socket();
/// Start the first asynchronous operation for the connection.
- void start();
+ void
+ start();
/// Stop all asynchronous operations associated with the connection.
- void stop();
+ void
+ stop();
private:
/// Handle completion of a read operation.
- void handle_read(const boost::system::error_code& e,
- std::size_t bytes_transferred);
+ 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);
+ void
+ handle_write(const boost::system::error_code& e);
/// Socket for the connection.
boost::asio::ip::tcp::socket socket_;
diff --git a/server/connection_manager.cpp b/server/connection_manager.cpp
index 1119656..2e149fe 100644
--- a/server/connection_manager.cpp
+++ b/server/connection_manager.cpp
@@ -28,28 +28,30 @@
//
#include "connection_manager.hpp"
-#include <algorithm>
#include <boost/bind.hpp>
+#include <algorithm>
namespace http {
namespace server {
-void connection_manager::start(connection_ptr c)
+void
+connection_manager::start(connection_ptr c)
{
connections_.insert(c);
c->start();
}
-void connection_manager::stop(connection_ptr c)
+void
+connection_manager::stop(connection_ptr c)
{
connections_.erase(c);
c->stop();
}
-void connection_manager::stop_all()
+void
+connection_manager::stop_all()
{
- std::for_each(connections_.begin(), connections_.end(),
- boost::bind(&connection::stop, _1));
+ std::for_each(connections_.begin(), connections_.end(), boost::bind(&connection::stop, _1));
connections_.clear();
}
diff --git a/server/connection_manager.hpp b/server/connection_manager.hpp
index 35b77ef..627184b 100644
--- a/server/connection_manager.hpp
+++ b/server/connection_manager.hpp
@@ -30,27 +30,29 @@
#ifndef HTTP_CONNECTION_MANAGER_HPP
#define HTTP_CONNECTION_MANAGER_HPP
-#include <set>
-#include <boost/noncopyable.hpp>
#include "connection.hpp"
+#include <boost/noncopyable.hpp>
+#include <set>
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
+class connection_manager : private boost::noncopyable
{
public:
/// Add the specified connection to the manager and start it.
- void start(connection_ptr c);
+ void
+ start(connection_ptr c);
/// Stop the specified connection.
- void stop(connection_ptr c);
+ void
+ stop(connection_ptr c);
/// Stop all connections.
- void stop_all();
+ void
+ stop_all();
private:
/// The managed connections.
diff --git a/server/mime_types.cpp b/server/mime_types.cpp
index c5fe883..22c0cdf 100644
--- a/server/mime_types.cpp
+++ b/server/mime_types.cpp
@@ -37,25 +37,17 @@
{
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" },
- { "ico", "image/x-icon" },
- { 0, 0 } // Marks end of list.
+} mappings[] = {
+ {"gif", "image/gif"}, {"htm", "text/html"}, {"html", "text/html"},
+ {"css", "text/css"}, {"js", "text/javascript"}, {"jpg", "image/jpeg"},
+ {"png", "image/png"}, {"ico", "image/x-icon"}, {0, 0} // Marks end of list.
};
-std::string extension_to_type(const std::string& extension)
+std::string
+extension_to_type(const std::string& extension)
{
- for (mapping* m = mappings; m->extension; ++m)
- {
- if (m->extension == extension)
- {
+ for (mapping* m = mappings; m->extension; ++m) {
+ if (m->extension == extension) {
return m->mime_type;
}
}
diff --git a/server/mime_types.hpp b/server/mime_types.hpp
index e3ed2d9..c77c2e0 100644
--- a/server/mime_types.hpp
+++ b/server/mime_types.hpp
@@ -37,7 +37,8 @@
namespace mime_types {
/// Convert a file extension into a MIME type.
-std::string extension_to_type(const std::string& extension);
+std::string
+extension_to_type(const std::string& extension);
} // namespace mime_types
} // namespace server
diff --git a/server/reply.cpp b/server/reply.cpp
index b0e5aaa..7c7133c 100644
--- a/server/reply.cpp
+++ b/server/reply.cpp
@@ -28,85 +28,69 @@
//
#include "reply.hpp"
-#include <string>
#include <boost/lexical_cast.hpp>
+#include <string>
namespace http {
namespace server {
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)
+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);
+ 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);
}
}
@@ -114,17 +98,17 @@
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
-std::vector<boost::asio::const_buffer> reply::to_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)
- {
+ 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));
@@ -139,126 +123,112 @@
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 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)
+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;
+ 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
+reply::stock_reply(reply::status_type status)
{
reply rep;
rep.status = status;
diff --git a/server/reply.hpp b/server/reply.hpp
index d4c701c..2c73613 100644
--- a/server/reply.hpp
+++ b/server/reply.hpp
@@ -30,10 +30,10 @@
#ifndef HTTP_REPLY_HPP
#define HTTP_REPLY_HPP
+#include "header.hpp"
+#include <boost/asio.hpp>
#include <string>
#include <vector>
-#include <boost/asio.hpp>
-#include "header.hpp"
namespace http {
namespace server {
@@ -42,8 +42,7 @@
struct reply
{
/// The status of the reply.
- enum status_type
- {
+ enum status_type {
ok = 200,
created = 201,
accepted = 202,
@@ -71,10 +70,12 @@
/// 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();
+ std::vector<boost::asio::const_buffer>
+ to_buffers();
/// Get a stock reply.
- static reply stock_reply(status_type status);
+ static reply
+ stock_reply(status_type status);
};
} // namespace server
diff --git a/server/request.hpp b/server/request.hpp
index 49ccbf4..97dc3f1 100644
--- a/server/request.hpp
+++ b/server/request.hpp
@@ -30,9 +30,9 @@
#ifndef HTTP_REQUEST_HPP
#define HTTP_REQUEST_HPP
+#include "header.hpp"
#include <string>
#include <vector>
-#include "header.hpp"
namespace http {
namespace server {
diff --git a/server/request_handler.cpp b/server/request_handler.cpp
index 6087046..d2b339e 100644
--- a/server/request_handler.cpp
+++ b/server/request_handler.cpp
@@ -28,18 +28,18 @@
//
#include "request_handler.hpp"
-#include <fstream>
-#include <sstream>
-#include <string>
-#include <boost/lexical_cast.hpp>
+#include "logging.h"
#include "mime_types.hpp"
#include "reply.hpp"
#include "request.hpp"
-#include <QIODevice>
-#include <QFile>
+#include <boost/lexical_cast.hpp>
#include <QDataStream>
+#include <QFile>
+#include <QIODevice>
#include <QString>
-#include "logging.h"
+#include <fstream>
+#include <sstream>
+#include <string>
INIT_LOGGER("HttpServer")
@@ -51,27 +51,24 @@
{
}
-void request_handler::handle_request(const request& req, reply& rep)
+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))
- {
+ 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)
- {
+ 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] == '/')
- {
+ if (request_path[request_path.size() - 1] == '/') {
request_path += "index.html";
}
@@ -79,8 +76,7 @@
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)
- {
+ if (last_dot_pos != std::string::npos && last_dot_pos > last_slash_pos) {
extension = request_path.substr(last_dot_pos + 1);
}
@@ -92,8 +88,7 @@
// in /usr/local/share
QString full_path = doc_root_.absolutePath() + QString(request_path.c_str());
QFile file(full_path);
- if (!file.exists() || !file.open(QIODevice::ReadOnly))
- {
+ if (!file.exists() || !file.open(QIODevice::ReadOnly)) {
rep = reply::stock_reply(reply::not_found);
return;
}
@@ -103,15 +98,12 @@
rep.status = reply::ok;
char buf[512];
QDataStream in(&file);
- while (true)
- {
+ while (true) {
int bytes = in.readRawData(buf, sizeof(buf));
- if (bytes > 0)
- {
+ if (bytes > 0) {
rep.content.append(buf, bytes);
}
- else
- {
+ else {
break;
}
}
@@ -122,39 +114,32 @@
rep.headers[1].value = mime_types::extension_to_type(extension);
}
-bool request_handler::url_decode(const std::string& in, std::string& out)
+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())
- {
+ 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)
- {
+ if (is >> std::hex >> value) {
out += static_cast<char>(value);
i += 2;
}
- else
- {
+ else {
return false;
}
}
- else
- {
+ else {
return false;
}
}
- else if (in[i] == '+')
- {
+ else if (in[i] == '+') {
out += ' ';
}
- else
- {
+ else {
out += in[i];
}
}
diff --git a/server/request_handler.hpp b/server/request_handler.hpp
index 08252b4..3311da2 100644
--- a/server/request_handler.hpp
+++ b/server/request_handler.hpp
@@ -30,9 +30,9 @@
#ifndef HTTP_REQUEST_HANDLER_HPP
#define HTTP_REQUEST_HANDLER_HPP
-#include <string>
#include <boost/noncopyable.hpp>
#include <QDir>
+#include <string>
namespace http {
namespace server {
@@ -41,15 +41,15 @@
struct request;
/// The common handler for all incoming requests.
-class request_handler
- : private boost::noncopyable
+class request_handler : private boost::noncopyable
{
public:
/// Construct with a directory containing files to be served.
explicit request_handler(const std::string& doc_root);
/// Handle a request and produce a reply.
- void handle_request(const request& req, reply& rep);
+ void
+ handle_request(const request& req, reply& rep);
private:
/// The directory containing the files to be served.
@@ -57,7 +57,8 @@
/// Perform URL-decoding on a string. Returns false if the encoding was
/// invalid.
- static bool url_decode(const std::string& in, std::string& out);
+ static bool
+ url_decode(const std::string& in, std::string& out);
};
} // namespace server
diff --git a/server/request_parser.cpp b/server/request_parser.cpp
index 0ba3748..e8dee11 100644
--- a/server/request_parser.cpp
+++ b/server/request_parser.cpp
@@ -38,294 +38,265 @@
{
}
-void request_parser::reset()
+void
+request_parser::reset()
{
state_ = method_start;
}
-boost::tribool request_parser::consume(request& req, char input)
+boost::tribool
+request_parser::consume(request& req, char input)
{
- switch (state_)
- {
- case method_start:
- if (!is_char(input) || is_ctl(input) || is_tspecial(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:
+ 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.headers.empty() && (input == ' ' || input == '\t')) {
+ state_ = header_lws;
+ return boost::indeterminate;
+ }
+ else if (!is_char(input) || is_ctl(input) || is_tspecial(input)) {
+ return false;
+ }
+ else {
+ req.headers.push_back(header());
+ req.headers.back().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.headers.back().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.headers.back().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') {
+ state_ = expecting_newline_2;
+ return boost::indeterminate;
+ }
+ else if (is_ctl(input)) {
+ return false;
+ }
+ else {
+ req.headers.back().value.push_back(input);
+ return boost::indeterminate;
+ }
+ case expecting_newline_2:
+ if (input == '\n') {
+ state_ = header_line_start;
+ return boost::indeterminate;
+ }
+ else {
+ return false;
+ }
+ case expecting_newline_3:
+ return (input == '\n');
+ default:
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:
- 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.headers.empty() && (input == ' ' || input == '\t'))
- {
- state_ = header_lws;
- return boost::indeterminate;
- }
- else if (!is_char(input) || is_ctl(input) || is_tspecial(input))
- {
- return false;
- }
- else
- {
- req.headers.push_back(header());
- req.headers.back().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.headers.back().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.headers.back().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')
- {
- state_ = expecting_newline_2;
- return boost::indeterminate;
- }
- else if (is_ctl(input))
- {
- return false;
- }
- else
- {
- req.headers.back().value.push_back(input);
- return boost::indeterminate;
- }
- case expecting_newline_2:
- if (input == '\n')
- {
- state_ = header_line_start;
- return boost::indeterminate;
- }
- else
- {
- return false;
- }
- case expecting_newline_3:
- return (input == '\n');
- default:
- return false;
}
}
-bool request_parser::is_char(int c)
+bool
+request_parser::is_char(int c)
{
return c >= 0 && c <= 127;
}
-bool request_parser::is_ctl(int c)
+bool
+request_parser::is_ctl(int c)
{
return (c >= 0 && c <= 31) || (c == 127);
}
-bool request_parser::is_tspecial(int c)
+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;
+ 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)
+bool
+request_parser::is_digit(int c)
{
return c >= '0' && c <= '9';
}
diff --git a/server/request_parser.hpp b/server/request_parser.hpp
index cd571cc..9d16713 100644
--- a/server/request_parser.hpp
+++ b/server/request_parser.hpp
@@ -46,18 +46,18 @@
request_parser();
/// Reset to initial parser state.
- void reset();
+ 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)
+ boost::tuple<boost::tribool, InputIterator>
+ parse(request& req, InputIterator begin, InputIterator end)
{
- while (begin != end)
- {
+ while (begin != end) {
boost::tribool result = consume(req, *begin++);
if (result || !result)
return boost::make_tuple(result, begin);
@@ -68,23 +68,27 @@
private:
/// Handle the next character of input.
- boost::tribool consume(request& req, char input);
+ boost::tribool
+ consume(request& req, char input);
/// Check if a byte is an HTTP character.
- static bool is_char(int c);
+ static bool
+ is_char(int c);
/// Check if a byte is an HTTP control character.
- static bool is_ctl(int c);
+ static bool
+ is_ctl(int c);
/// Check if a byte is defined as an HTTP tspecial character.
- static bool is_tspecial(int c);
+ static bool
+ is_tspecial(int c);
/// Check if a byte is a digit.
- static bool is_digit(int c);
+ static bool
+ is_digit(int c);
/// The current state of the parser.
- enum state
- {
+ enum state {
method_start,
method,
uri,
diff --git a/server/server.cpp b/server/server.cpp
index 0fc7747..9ffa4eb 100644
--- a/server/server.cpp
+++ b/server/server.cpp
@@ -28,32 +28,32 @@
//
#include "server.hpp"
+#include "logging.h"
#include <boost/bind.hpp>
#include <signal.h>
-#include "logging.h"
-INIT_LOGGER ("HttpServer");
+INIT_LOGGER("HttpServer");
namespace http {
namespace server {
-server::server(const std::string& address, const std::string& port,
- const std::string& doc_root)
- : io_service_(),
- // signals_(io_service_),
- acceptor_(io_service_),
- connection_manager_(),
- new_connection_(),
- request_handler_(doc_root)
+server::server(const std::string& address, const std::string& port, const std::string& doc_root)
+ : io_service_()
+ ,
+ // signals_(io_service_),
+ acceptor_(io_service_)
+ , connection_manager_()
+ , new_connection_()
+ , request_handler_(doc_root)
{
// Register to handle the signals that indicate when the server should exit.
// It is safe to register for the same signal multiple times in a program,
// provided all registration for the specified signal is made through Asio.
-// signals_.add(SIGINT);
-// signals_.add(SIGTERM);
-//#if defined(SIGQUIT)
-// signals_.add(SIGQUIT);
-//#endif // defined(SIGQUIT)
-// signals_.async_wait(boost::bind(&server::handle_stop, this));
+ // signals_.add(SIGINT);
+ // signals_.add(SIGTERM);
+ //#if defined(SIGQUIT)
+ // signals_.add(SIGQUIT);
+ //#endif // defined(SIGQUIT)
+ // signals_.async_wait(boost::bind(&server::handle_stop, this));
// Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
boost::asio::ip::tcp::resolver resolver(io_service_);
@@ -74,7 +74,8 @@
// handle_stop();
}
-void server::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
@@ -83,33 +84,32 @@
io_service_.run();
}
-void server::start_accept()
+void
+server::start_accept()
{
- new_connection_.reset(new connection(io_service_,
- connection_manager_, request_handler_));
+ 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));
+ boost::bind(&server::handle_accept, this, boost::asio::placeholders::error));
}
-void server::handle_accept(const boost::system::error_code& e)
+void
+server::handle_accept(const boost::system::error_code& e)
{
// Check whether the server was stopped by a signal before this completion
// handler had a chance to run.
- if (!acceptor_.is_open())
- {
+ if (!acceptor_.is_open()) {
return;
}
- if (!e)
- {
+ if (!e) {
connection_manager_.start(new_connection_);
}
start_accept();
}
-void server::handle_stop()
+void
+server::handle_stop()
{
// The server is stopped by cancelling all outstanding asynchronous
// operations. Once all operations have finished the io_service::run() call
diff --git a/server/server.hpp b/server/server.hpp
index cedb593..d5bafd6 100644
--- a/server/server.hpp
+++ b/server/server.hpp
@@ -30,41 +30,42 @@
#ifndef HTTP_SERVER_HPP
#define HTTP_SERVER_HPP
-#include <boost/asio.hpp>
-#include <string>
-#include <boost/noncopyable.hpp>
#include "connection.hpp"
#include "connection_manager.hpp"
#include "request_handler.hpp"
+#include <boost/asio.hpp>
+#include <boost/noncopyable.hpp>
+#include <string>
namespace http {
namespace server {
/// The top-level class of the HTTP server.
-class server
- : private boost::noncopyable
+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(const std::string& address, const std::string& port,
- const std::string& doc_root);
+ explicit server(const std::string& address, const std::string& port, const std::string& doc_root);
~server();
/// Run the server's io_service loop.
- void run();
+ void
+ run();
/// Handle a request to stop the server.
- void handle_stop();
+ void
+ handle_stop();
private:
-
/// Initiate an asynchronous accept operation.
- void start_accept();
+ void
+ start_accept();
/// Handle completion of an asynchronous accept operation.
- void handle_accept(const boost::system::error_code& e);
+ void
+ handle_accept(const boost::system::error_code& e);
/// The io_service used to perform asynchronous operations.
boost::asio::io_service io_service_;