[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/itamago/libhttpserver/master/src/httpserver/http_resource.hpp [Back]  [Original]

/*
     This file is part of libhttpserver
     Copyright (C) 2011-2019 Sebastiano Merlino

     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public
     License as published by the Free Software Foundation; either
     version 2.1 of the License, or (at your option) any later version.

     This library is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Lesser General Public License for more details.

     You should have received a copy of the GNU Lesser General Public
     License along with this library; if not, write to the Free Software
     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

#if !defined (_HTTPSERVER_HPP_INSIDE_) && !defined (HTTPSERVER_COMPILATION)
#error "Only  or  can be included directly."
#endif

#ifndef SRC_HTTPSERVER_HTTP_RESOURCE_HPP_
#define SRC_HTTPSERVER_HTTP_RESOURCE_HPP_

#ifdef DEBUG
#include 
#endif

#include 
#include 
#include 
#include 

namespace httpserver { class http_request; }
namespace httpserver { class http_response; }

namespace httpserver {

namespace details { std::shared_ptr empty_render(const http_request& r); }

void resource_init(std::map* res);

/**
 * Class representing a callable http resource.
**/
class http_resource {
 public:
     /**
      * Class destructor
     **/
     virtual ~http_resource() = default;

     /**
      * Method used to answer to a generic request
      * @param req Request passed through http
      * @return A http_response object
     **/
     virtual const std::shared_ptr render(const http_request& req) {
         return details::empty_render(req);
     }

     /**
      * Method used to answer to a GET request
      * @param req Request passed through http
      * @return A http_response object
     **/
     virtual const std::shared_ptr render_GET(const http_request& req) {
         return render(req);
     }

     /**
      * Method used to answer to a POST request
      * @param req Request passed through http
      * @return A http_response object
     **/
     virtual const std::shared_ptr render_POST(const http_request& req) {
         return render(req);
     }

     /**
      * Method used to answer to a PUT request
      * @param req Request passed through http
      * @return A http_response object
     **/
     virtual const std::shared_ptr render_PUT(const http_request& req) {
         return render(req);
     }

     /**
      * Method used to answer to a HEAD request
      * @param req Request passed through http
      * @return A http_response object
     **/
     virtual const std::shared_ptr render_HEAD(const http_request& req) {
         return render(req);
     }

     /**
      * Method used to answer to a DELETE request
      * @param req Request passed through http
      * @return A http_response object
     **/
     virtual const std::shared_ptr render_DELETE(const http_request& req) {
         return render(req);
     }

     /**
      * Method used to answer to a TRACE request
      * @param req Request passed through http
      * @return A http_response object
     **/
     virtual const std::shared_ptr render_TRACE(const http_request& req) {
         return render(req);
     }

     /**
      * Method used to answer to a OPTIONS request
      * @param req Request passed through http
      * @return A http_response object
     **/
     virtual const std::shared_ptr render_OPTIONS(const http_request& req) {
         return render(req);
     }

     /**
      * Method used to answer to a PATCH request
      * @param req Request passed through http
      * @return A http_response object
     **/
     virtual const std::shared_ptr render_PATCH(const http_request& req) {
         return render(req);
     }

     /**
      * Method used to answer to a CONNECT request
      * @param req Request passed through http
      * @return A http_response object
     **/
     virtual const std::shared_ptr render_CONNECT(const http_request& req) {
         return render(req);
     }

     /**
      * Method used to set if a specific method is allowed or not on this request
      * @param method method to set permission on
      * @param allowed boolean indicating if the method is allowed or not
     **/
     void set_allowing(const std::string& method, bool allowed) {
         if (allowed_methods.count(method)) {
             allowed_methods[method] = allowed;
         }
     }

     /**
      * Method used to implicitly allow all methods
     **/
     void allow_all() {
         std::map::iterator it;
         for (it=allowed_methods.begin(); it != allowed_methods.end(); ++it) {
             allowed_methods[(*it).first] = true;
         }
     }

     /**
      * Method used to implicitly disallow all methods
     **/
     void disallow_all() {
         std::map::iterator it;
         for (it=allowed_methods.begin(); it != allowed_methods.end(); ++it) {
             allowed_methods[(*it).first] = false;
         }
     }

     /**
      * Method used to discover if an http method is allowed or not for this resource
      * @param method Method to discover allowings
      * @return true if the method is allowed
     **/
     bool is_allowed(const std::string& method) {
         if (allowed_methods.count(method)) {
             return allowed_methods[method];
         } else {
#ifdef DEBUG
             std::map::iterator it;
             for (it = allowed_methods.begin(); it != allowed_methods.end(); ++it) {
                 std::cout 

Web Proxy Viewer  |  New URL  |  Original Page