// Copyright 2021 Jeisson Hidalgo-Cespedes. Universidad de Costa Rica. CC BY 4.0 #ifndef HTTPAPP_H #define HTTPAPP_H #include #include "common.hpp" class HttpRequest; class HttpResponse; /** @brief Base class for all web applications that can be registered with the web server. */ class HttpApp { /// Web application objects are usually complex. This base class does not /// require child classes to allow copying DISABLE_COPY(HttpApp); public: /// Constructor HttpApp() = default; /// Destructor ~HttpApp() = default; /// Called by the web server when the web server is started virtual void start(); /// @brief Utility method to serve the common header for each page static void serveHeader(HttpResponse& httpResponse, const std::string& title); /// Add content to the website's homepage if this application requires it /// @remark Do not call httpResponse.send(). It will be called automatically /// by the web server after each application has added its content to the /// homepage response virtual void addToHomepage(HttpRequest& httpRequest, HttpResponse& httpResponse); /// Handle HTTP requests. @see HttpServer::handleHttpRequest() /// @return true If this application handled the request, false otherwise /// and another chained application should handle it virtual bool handleHttpRequest(HttpRequest& httpRequest, HttpResponse& httpResponse) = 0; /// Called when the web server stops, in order to allow the web application /// clean up and finish as well virtual void stop(); /// Sanitize data received from the client static std::string sanitizeHtml(const std::string& text); }; #endif // HTTPAPP_H