Program Listing for File http_reply.hpp

Return to documentation for file (/tmp/ws/src/async_web_server_cpp/include/async_web_server_cpp/http_reply.hpp)

#ifndef CPP_WEB_SERVER_HTTP_REPLY_HPP
#define CPP_WEB_SERVER_HTTP_REPLY_HPP

#include "async_web_server_cpp/http_connection.hpp"
#include "async_web_server_cpp/http_header.hpp"
#include "async_web_server_cpp/http_request_handler.hpp"

#include <boost/asio.hpp>
#include <boost/filesystem.hpp>

#include <string>
#include <vector>

namespace async_web_server_cpp
{

class ReplyBuilder;

struct HttpReply
{
    enum status_type
    {
        switching_protocols = 101,
        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;

    static std::vector<boost::asio::const_buffer>
    to_buffers(const std::vector<HttpHeader>& headers);

    static HttpServerRequestHandler stock_reply(status_type status);

    static HttpServerRequestHandler
    from_file(HttpReply::status_type status, const std::string& content_type,
              const std::string& filename,
              const std::vector<HttpHeader>& additional_headers =
                  std::vector<HttpHeader>());

    static HttpServerRequestHandler
    from_filesystem(HttpReply::status_type status, const std::string& path_root,
                    const std::string& filesystem_root, bool list_directories,
                    const std::vector<HttpHeader>& additional_headers =
                        std::vector<HttpHeader>());

    static HttpServerRequestHandler
    static_reply(status_type status, const std::string& content_type,
                 const std::string& content,
                 const std::vector<HttpHeader>& additional_headers =
                     std::vector<HttpHeader>());

    static ReplyBuilder builder(status_type status);
};

class ReplyBuilder
{
public:
    ReplyBuilder(HttpReply::status_type status);

    ReplyBuilder& header(const std::string& name, const std::string& value);

    ReplyBuilder& header(const HttpHeader& header);

    ReplyBuilder& headers(const std::vector<HttpHeader>& headers);

    void write(HttpConnectionPtr connection);

private:
    HttpReply::status_type status_;
    std::shared_ptr<std::vector<HttpHeader>> headers_;
};

class StaticHttpRequestHandler
{
public:
    StaticHttpRequestHandler(HttpReply::status_type status,
                             const std::vector<HttpHeader>& headers,
                             const std::string& content);

    bool operator()(const HttpRequest&, boost::shared_ptr<HttpConnection>,
                    const char* begin, const char* end);

private:
    ReplyBuilder reply_builder_;
    const std::string content_string_;
};

class FileHttpRequestHandler
{
public:
    FileHttpRequestHandler(HttpReply::status_type status,
                           const std::string& filename,
                           const std::vector<HttpHeader>& headers);

    bool operator()(const HttpRequest&, boost::shared_ptr<HttpConnection>,
                    const char* begin, const char* end);

private:
    HttpReply::status_type status_;
    std::vector<HttpHeader> headers_;
    std::string filename_;
};

class FilesystemHttpRequestHandler
{
public:
    FilesystemHttpRequestHandler(HttpReply::status_type status,
                                 const std::string& path_root,
                                 const std::string& filesystem_root,
                                 bool list_directories,
                                 const std::vector<HttpHeader>& headers);

    bool operator()(const HttpRequest&, boost::shared_ptr<HttpConnection>,
                    const char* begin, const char* end);

private:
    HttpReply::status_type status_;
    std::vector<HttpHeader> headers_;
    std::string path_root_;
    boost::filesystem::path filesystem_root_;
    bool list_directories_;
};

}  // namespace async_web_server_cpp

#endif