// Copyright 2021 Jeisson Hidalgo-Cespedes. Universidad de Costa Rica. CC BY 4.0 #ifndef FACTWEBAPP_HPP #define FACTWEBAPP_HPP #include #include "HttpApp.hpp" /** @brief A web application that calculates prime factors */ class FactWebApp : public HttpApp { /// Objects of this class cannot be copied DISABLE_COPY(FactWebApp); public: /// Constructor FactWebApp(); /// Destructor ~FactWebApp(); /// Called by the web server when the web server is started void start() override; /// Handle HTTP requests. @see HttpServer::handleHttpRequest() /// @return true If this application handled the request, false otherwise /// and another chained application should handle it bool handleHttpRequest(HttpRequest& httpRequest, HttpResponse& httpResponse) override; /// Called when the web server stops, in order to allow the web application /// clean up and finish as well void stop() override; protected: /// Handle HTTP requests. @see HttpServer::handleHttpRequest() /// @return true If this application handled the request, false otherwise /// Sends the homepage as HTTP response bool serveHomepage(HttpRequest& httpRequest, HttpResponse& httpResponse); /// @brief Utility method to serve the common header for each page void serveHeader(HttpResponse& httpResponse, const std::string& title = "Prime factorization"); /// Handle a HTTP request that starts with "/fact" /// @return true if the factorization was handled, false if it must be /// handled by another application bool serveFactorization(HttpRequest& httpRequest, HttpResponse& httpResponse); /// @brief Extract comma-separated numbers from uri and add their /// prime factorization to the HTTP response /// @param uri request URI in form "/fact=x1,x2,...,xn" or /// "/fact?number=x1,x2,...,xn" /// @param httpResponse The object to answer to the client/user void factorizeNumbers(const std::string& uri, HttpResponse& httpResponse); }; #endif // FACTWEBAPP_HPP