2021-06-20 17:53:07 +02:00
|
|
|
#ifndef DWEB_APPLICATION_H
|
2021-07-06 18:59:30 +02:00
|
|
|
#define DWEB_APPLICATION_H
|
2021-06-20 17:53:07 +02:00
|
|
|
|
2021-07-06 20:06:38 +02:00
|
|
|
#include "core/http/web_application.h"
|
|
|
|
|
2021-06-20 17:53:07 +02:00
|
|
|
#include "core/object.h"
|
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
2021-07-06 20:06:38 +02:00
|
|
|
#include "core/http/handler_instance.h"
|
2021-06-20 17:53:07 +02:00
|
|
|
|
2021-06-20 18:41:09 +02:00
|
|
|
#include <trantor/net/InetAddress.h>
|
2021-07-06 13:42:27 +02:00
|
|
|
#include <trantor/net/Resolver.h>
|
2021-07-06 15:03:46 +02:00
|
|
|
#include <trantor/utils/Logger.h>
|
2021-06-20 18:41:09 +02:00
|
|
|
|
|
|
|
#include "http/HttpRequestImpl.h"
|
|
|
|
#include "http/HttpResponse.h"
|
|
|
|
|
2021-07-06 13:42:27 +02:00
|
|
|
#include "./drogon/lib/src/ListenerManager.h"
|
2021-07-06 15:03:46 +02:00
|
|
|
#include "./drogon/lib/src/impl_forwards.h"
|
2021-06-20 18:41:09 +02:00
|
|
|
|
|
|
|
#include "http/SessionManager.h"
|
|
|
|
|
|
|
|
using namespace drogon;
|
|
|
|
|
2021-07-06 20:06:38 +02:00
|
|
|
class Request;
|
2021-06-20 17:53:07 +02:00
|
|
|
|
2021-07-06 22:06:18 +02:00
|
|
|
class DWebApplication : public WebApplication {
|
2021-11-21 14:40:47 +01:00
|
|
|
RCPP_OBJECT(DWebApplication, WebApplication);
|
|
|
|
|
2021-06-20 17:53:07 +02:00
|
|
|
public:
|
2021-07-06 22:06:18 +02:00
|
|
|
void load_settings();
|
|
|
|
void setup_routes();
|
|
|
|
void setup_middleware();
|
2021-06-20 17:53:07 +02:00
|
|
|
|
2021-07-06 22:06:18 +02:00
|
|
|
void migrate();
|
2021-06-20 17:53:07 +02:00
|
|
|
|
2021-07-06 22:20:38 +02:00
|
|
|
void add_listener(const std::string &ip, uint16_t port,
|
|
|
|
bool useSSL = false, const std::string &certFile = "", const std::string &keyFile = "", bool useOldTLS = false,
|
|
|
|
const std::vector<std::pair<std::string, std::string> > &sslConfCmds = {});
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 13:42:27 +02:00
|
|
|
void set_thread_num(size_t thread_num);
|
2021-07-06 12:04:33 +02:00
|
|
|
size_t get_thread_num() const;
|
|
|
|
|
|
|
|
void set_ssl_config_commands(const std::vector<std::pair<std::string, std::string> > &sslConfCmds);
|
|
|
|
void set_ssl_files(const std::string &certPath, const std::string &keyPath);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
|
|
|
void run();
|
|
|
|
void stop();
|
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void enable_session(const size_t timeout);
|
|
|
|
void disable_session();
|
2021-06-20 18:41:09 +02:00
|
|
|
|
|
|
|
//todo think about it
|
2021-07-06 12:04:33 +02:00
|
|
|
const std::string &get_document_root() const;
|
|
|
|
void set_document_root(const std::string &rootPath);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void set_static_file_headers(const std::vector<std::pair<std::string, std::string> > &headers);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
const std::string &get_upload_path() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
const std::shared_ptr<trantor::Resolver> &get_resolver() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void set_upload_path(const std::string &uploadPath);
|
|
|
|
void set_file_types(const std::vector<std::string> &types);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void set_max_connection_num(size_t maxConnections);
|
|
|
|
void set_max_connection_num_per_ip(size_t maxConnectionsPerIP);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void set_log_path(const std::string &logPath, const std::string &logfileBaseName, size_t logfileSize);
|
|
|
|
void set_log_level(trantor::Logger::LogLevel level);
|
|
|
|
void enable_sendfile(bool sendFile);
|
|
|
|
void enable_gzip(bool useGzip);
|
|
|
|
bool is_gzip_enabled() const;
|
|
|
|
void enable_brotli(bool useBrotli);
|
|
|
|
bool is_brotli_enabled() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void set_static_files_cache_time(int cacheTime);
|
|
|
|
int static_files_cache_time() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void set_idle_connection_timeout(size_t timeout);
|
|
|
|
void set_keepalive_requests_number(const size_t number);
|
|
|
|
void set_pipelining_requests_number(const size_t number);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void set_gzip_static(bool useGzipStatic);
|
|
|
|
void set_br_static(bool useGzipStatic);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void set_client_max_body_size(size_t maxSize);
|
|
|
|
void set_client_max_memory_body_size(size_t maxSize);
|
|
|
|
void set_client_max_web_socket_message_size(size_t maxSize);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void set_home_page(const std::string &homePageFile);
|
|
|
|
const std::string &get_home_page() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void set_implicit_page_enable(bool useImplicitPage);
|
|
|
|
bool is_implicit_page_enabled() const;
|
|
|
|
void set_implicit_page(const std::string &implicitPageFile);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
const std::string &get_implicit_page() const;
|
|
|
|
size_t get_client_max_body_size() const;
|
|
|
|
size_t get_client_max_memory_body_size() const;
|
|
|
|
size_t get_client_max_web_socket_message_size() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
size_t keepalive_requests_number() const;
|
|
|
|
size_t pipelining_requests_number() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
bool is_running();
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void set_unicode_escaping_in_json(bool enable);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
bool is_unicode_escaping_used_in_json() const;
|
|
|
|
void set_float_precision_in_json(unsigned int precision, const std::string &precisionType);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
const std::pair<unsigned int, std::string> &get_float_precision_in_json() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
trantor::EventLoop *get_loop() const;
|
|
|
|
trantor::EventLoop *get_io_loop(size_t id) const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void set_server_header_field(const std::string &server);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void enable_server_header(bool flag);
|
|
|
|
void enable_date_header(bool flag);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
bool send_server_header() const;
|
|
|
|
bool send_date_header() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
const std::string &get_server_header_string() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
std::vector<trantor::InetAddress> get_listeners() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
bool use_sendfile() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
bool support_ssl() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
size_t get_current_thread_index() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void enable_reuse_port(bool enable);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
bool reuse_port() const;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void on_async_request(const HttpRequestImplPtr &req, std::function<void(const HttpResponsePtr &)> &&callback);
|
|
|
|
void on_new_websock_request(const HttpRequestImplPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const WebSocketConnectionImplPtr &wsConnPtr);
|
|
|
|
void on_connection(const trantor::TcpConnectionPtr &conn);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 12:04:33 +02:00
|
|
|
void find_session_for_request(const HttpRequestImplPtr &req);
|
2021-06-20 18:41:09 +02:00
|
|
|
|
|
|
|
//to request
|
|
|
|
//void forward(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,const std::string &hostString,double timeout);
|
|
|
|
//void forward(const HttpRequestImplPtr &req,std::function<void(const HttpResponsePtr &)> &&callback,const std::string &hostString,double timeout = 0);
|
|
|
|
|
2021-06-20 17:53:07 +02:00
|
|
|
DWebApplication();
|
|
|
|
virtual ~DWebApplication();
|
|
|
|
|
|
|
|
protected:
|
2021-06-20 18:41:09 +02:00
|
|
|
// We use a uuid string as session id;
|
|
|
|
// set sessionTimeout_=0 to make location session valid forever based on
|
|
|
|
// cookies;
|
|
|
|
size_t _session_timeout{ 0 };
|
|
|
|
size_t _idle_connection_timeout{ 60 };
|
|
|
|
bool _use_session{ false };
|
2021-07-06 15:03:46 +02:00
|
|
|
std::string _server_header{ "server: rcpp_framework\r\n" };
|
2021-06-20 18:41:09 +02:00
|
|
|
|
2021-07-06 15:03:46 +02:00
|
|
|
std::string _root_path{ "./" };
|
2021-06-20 18:41:09 +02:00
|
|
|
std::string _upload_path;
|
2021-07-06 15:03:46 +02:00
|
|
|
std::atomic_bool _running{ false };
|
2021-06-20 18:41:09 +02:00
|
|
|
|
|
|
|
size_t _thread_num{ 1 };
|
|
|
|
|
|
|
|
std::vector<std::pair<std::string, std::string> > _ssl_conf_cmds;
|
|
|
|
std::string _ssl_cert_path;
|
|
|
|
std::string _ssl_key_path;
|
|
|
|
|
|
|
|
size_t _max_connection_num_per_ip{ 0 };
|
|
|
|
std::unordered_map<std::string, size_t> _connections_num_map;
|
|
|
|
|
|
|
|
int64_t _max_connection_num{ 100000 };
|
|
|
|
std::atomic<int64_t> _connection_num{ 0 };
|
|
|
|
|
|
|
|
std::string _log_path;
|
|
|
|
std::string _logfile_base_name;
|
|
|
|
size_t _logfile_size{ 100000000 };
|
|
|
|
size_t _keepalive_requests_number{ 0 };
|
|
|
|
size_t _pipelining_requests_number{ 0 };
|
|
|
|
bool _use_sendfile{ true };
|
|
|
|
bool _use_gzip{ true };
|
|
|
|
bool _use_brotli{ false };
|
|
|
|
bool _using_unicode_escaping{ true };
|
|
|
|
|
2021-07-06 15:03:46 +02:00
|
|
|
std::pair<unsigned int, std::string> _float_precision_in_json{ 0, "significant" };
|
2021-07-06 13:42:27 +02:00
|
|
|
|
2021-06-20 18:41:09 +02:00
|
|
|
size_t _client_max_body_size{ 1024 * 1024 };
|
|
|
|
size_t _client_max_memory_body_size{ 64 * 1024 };
|
|
|
|
size_t _client_max_web_socket_message_size{ 128 * 1024 };
|
|
|
|
std::string _home_page_file{ "index.html" };
|
|
|
|
|
2021-07-06 13:42:27 +02:00
|
|
|
const std::unique_ptr<ListenerManager> _listener_manager;
|
|
|
|
std::unique_ptr<SessionManager> _session_manager;
|
2021-06-20 18:41:09 +02:00
|
|
|
|
|
|
|
bool _enable_server_header{ true };
|
|
|
|
bool _enable_date_header{ true };
|
|
|
|
bool _reuse_port{ false };
|
2021-07-06 15:03:46 +02:00
|
|
|
|
|
|
|
trantor::EventLoop *_loop;
|
|
|
|
|
|
|
|
std::vector<std::function<void()> > _beginning_advices;
|
2021-07-06 15:11:03 +02:00
|
|
|
std::vector<std::function<bool(const trantor::InetAddress &, const trantor::InetAddress &)> > _new_connection_advices;
|
2021-07-06 15:03:46 +02:00
|
|
|
std::vector<std::function<HttpResponsePtr(const HttpRequestPtr &)> > _sync_advices;
|
|
|
|
std::vector<std::function<void(const HttpRequestPtr &, const HttpResponsePtr &)> > _pre_sending_advices;
|
2021-06-20 17:53:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|