Go to the documentation of this file.00001 #ifndef CPP_WEB_SERVER_HTTP_REPLY_HPP
00002 #define CPP_WEB_SERVER_HTTP_REPLY_HPP
00003
00004 #include <vector>
00005 #include <string>
00006 #include <boost/asio.hpp>
00007 #include "async_web_server_cpp/http_header.hpp"
00008 #include "async_web_server_cpp/http_connection.hpp"
00009 #include "async_web_server_cpp/http_request_handler.hpp"
00010 #include <boost/filesystem.hpp>
00011
00012 namespace async_web_server_cpp
00013 {
00014
00015 class ReplyBuilder;
00016
00020 struct HttpReply
00021 {
00022 enum status_type
00023 {
00024 switching_protocols = 101,
00025 ok = 200,
00026 created = 201,
00027 accepted = 202,
00028 no_content = 204,
00029 multiple_choices = 300,
00030 moved_permanently = 301,
00031 moved_temporarily = 302,
00032 not_modified = 304,
00033 bad_request = 400,
00034 unauthorized = 401,
00035 forbidden = 403,
00036 not_found = 404,
00037 internal_server_error = 500,
00038 not_implemented = 501,
00039 bad_gateway = 502,
00040 service_unavailable = 503
00041 } status;
00042
00043 static std::vector<boost::asio::const_buffer> to_buffers(const std::vector<HttpHeader> &headers);
00044
00048 static HttpServerRequestHandler stock_reply(status_type status);
00049
00053 static HttpServerRequestHandler from_file(HttpReply::status_type status,
00054 const std::string& content_type,
00055 const std::string& filename,
00056 const std::vector<HttpHeader>& additional_headers = std::vector<HttpHeader>());
00057
00064 static HttpServerRequestHandler from_filesystem(HttpReply::status_type status,
00065 const std::string& path_root,
00066 const std::string& filesystem_root,
00067 bool list_directories,
00068 const std::vector<HttpHeader>& additional_headers = std::vector<HttpHeader>());
00069
00073 static HttpServerRequestHandler static_reply(status_type status,
00074 const std::string& content_type,
00075 const std::string& content,
00076 const std::vector<HttpHeader>& additional_headers = std::vector<HttpHeader>());
00077
00081 static ReplyBuilder builder(status_type status);
00082 };
00083
00087 class ReplyBuilder
00088 {
00089 public:
00090 ReplyBuilder(HttpReply::status_type status);
00091
00095 ReplyBuilder &header(const std::string &name, const std::string &value);
00096
00100 ReplyBuilder &header(const HttpHeader &header);
00101
00105 ReplyBuilder &headers(const std::vector<HttpHeader> &headers);
00106
00110 void write(HttpConnectionPtr connection);
00111
00112 private:
00113 HttpReply::status_type status_;
00114 boost::shared_ptr<std::vector<HttpHeader> > headers_;
00115 };
00116
00117
00121 class StaticHttpRequestHandler
00122 {
00123 public:
00124 StaticHttpRequestHandler(HttpReply::status_type status,
00125 const std::vector<HttpHeader> &headers,
00126 const std::string &content);
00127
00128 bool operator()(const HttpRequest &, boost::shared_ptr<HttpConnection>, const char* begin, const char* end);
00129
00130 private:
00131 ReplyBuilder reply_builder_;
00132 const std::string content_string_;
00133 };
00134
00138 class FileHttpRequestHandler
00139 {
00140 public:
00141 FileHttpRequestHandler(HttpReply::status_type status,
00142 const std::string& filename,
00143 const std::vector<HttpHeader>& headers);
00144
00145 bool operator()(const HttpRequest &, boost::shared_ptr<HttpConnection>, const char* begin, const char* end);
00146
00147 private:
00148 HttpReply::status_type status_;
00149 std::vector<HttpHeader> headers_;
00150 std::string filename_;
00151 };
00152
00156 class FilesystemHttpRequestHandler
00157 {
00158 public:
00159 FilesystemHttpRequestHandler(HttpReply::status_type status,
00160 const std::string& path_root,
00161 const std::string& filesystem_root,
00162 bool list_directories,
00163 const std::vector<HttpHeader>& headers);
00164
00165 bool operator()(const HttpRequest &, boost::shared_ptr<HttpConnection>, const char* begin, const char* end);
00166
00167 private:
00168 HttpReply::status_type status_;
00169 std::vector<HttpHeader> headers_;
00170 std::string path_root_;
00171 boost::filesystem::path filesystem_root_;
00172 bool list_directories_;
00173 };
00174
00175 }
00176
00177 #endif