## Class basic_server

zeep::http::basic_server — The libzeep HTTP server implementation. Originally based on example code found in boost::asio.

## Synopsis

    // In header: <zeep/http/server.hpp>

    class basic_server {
    public:
      // construct/copy/destruct
      basic_server();
      basic_server(const std::string &);
      basic_server(security_context *);
      basic_server(security_context *, const std::string &);
      basic_server(const basic_server &) = delete;
      basic_server & operator=(const basic_server &) = delete;
      ~basic_server();

      // public member functions
      security_context & get_security_context();
      bool has_security_context() const;
      void set_allowed_methods(const std::set< std::string > &);
      std::set< std::string > get_allowed_methods() const;
      void set_context_name(const std::string &);
      std::string get_context_name() const;
      void add_controller(controller *);
      void add_error_handler(error_handler *);
      void set_template_processor(basic_template_processor *);
      basic_template_processor & get_template_processor();
      const basic_template_processor & get_template_processor() const;
      bool has_template_processor() const;
      virtual void bind(const std::string &, unsigned short);
      virtual void run(int);
      virtual void stop();
      void set_log_forwarded(bool);
      std::string get_address() const;
      unsigned short get_port() const;
      virtual boost::asio::io_context & get_io_context() = 0;
      boost::asio::io_context::executor_type get_executor();

      // public static functions
      static std::ostream & get_log();

      // protected member functions
      virtual void
      log_request(const std::string &, const request &, const reply &,
                  const boost::posix_time::ptime &, const std::string &,
                  const std::string &, const std::string &) noexcept;

      // private member functions
      virtual void
      handle_request(boost::asio::ip::tcp::socket &, request &, reply &);
      void handle_accept(boost::system::error_code);
    };

## Description

The server class is a simple, stand alone HTTP server. Call bind to make it listen to an address/port combination. Add controller classes to do the actual work. These controllers will be tried in the order at which they were added to see if they want to process a request.

### `basic_server` public construct/copy/destruct

1.     basic_server();

   Simple server, no security, no template processor.

2.     basic_server(const std::string & docroot);

   Simple server, no security, create default template processor with docroot.

3.     basic_server(security_context * s_ctxt);

   server with a security context for limited access

4.     basic_server(security_context * s_ctxt, const std::string & docroot);

   server with a security context for limited access, create default template processor with docroot

5.     basic_server(const basic_server &) = delete;

6.     basic_server & operator=(const basic_server &) = delete;

7.     ~basic_server();

### `basic_server` public member functions

1.     security_context & get_security_context();

   Get the security context provided in the constructor.

2.     bool has_security_context() const;

   Test if a security context was provided in the constructor.

3.     void set_allowed_methods(const std::set< std::string > & methods);

   Set the set of allowed methods (default is "GET", "POST", "PUT", "OPTIONS", "HEAD", "DELETE")

4.     std::set< std::string > get_allowed_methods() const;

   Get the set of allowed methods.

5.     void set_context_name(const std::string & context_name);

   Set the context_name.

   The context name is used in constructing relative URL's that start with a forward slash

6.     std::string get_context_name() const;

   Get the context_name.

   The context name is used in constructing relative URL's that start with a forward slash

7.     void add_controller(controller * c);

   Add controller to the list of controllers.

   When a request is received, the list of controllers get a chance of handling it, in the order of which they were added to this server. If none of the controller handle the request the not_found error is returned.

8.     void add_error_handler(error_handler * eh);

   Add an error handler.

   Errors are handled by the error handler list. The last added handler is called first.

9.     void set_template_processor(basic_template_processor * template_processor);

   Set the template processor.

   A template processor handles loading templates and processing the contents.

10.     basic_template_processor & get_template_processor();

    Get the template processor.

    A template processor handles loading templates and processing the contents. This will throw if the processor has not been set yet.

11.     const basic_template_processor & get_template_processor() const;

    Get the template processor.

    A template processor handles loading templates and processing the contents. This will throw if the processor has not been set yet.

12.     bool has_template_processor() const;

    returns whether template processor has been set

13.     virtual void bind(const std::string & address, unsigned short port);

    Bind the server to address address and port port.

14.     virtual void run(int nr_of_threads);

    Run as many as nr_of_threads threads simultaneously.

15.     virtual void stop();

    Stop all threads and stop listening.

16.     void set_log_forwarded(bool v);

    log_forwarded tells the HTTP server to use the last entry in X-Forwarded-For as client log entry

17.     std::string get_address() const;

    returns the address as specified in bind

18.     unsigned short get_port() const;

    returns the port as specified in bind

19.     virtual boost::asio::io_context & get_io_context() = 0;

    get_io_context has to be public since we need it to call notify_fork from child code

20.     boost::asio::io_context::executor_type get_executor();

    get_executor has to be public since we need it to call notify_fork from child code

### `basic_server` public static functions

1.     static std::ostream & get_log();

   to extend the log entry for a current request, use this ostream:

### `basic_server` protected member functions

1.     virtual void
       log_request(const std::string & client, const request & req,
                   const reply & rep, const boost::posix_time::ptime & start,
                   const std::string & referer, const std::string & userAgent,
                   const std::string & entry) noexcept;

   the default entry logger

### `basic_server` private member functions

1.     virtual void
       handle_request(boost::asio::ip::tcp::socket & socket, request & req,
                      reply & rep);

2.     void handle_accept(boost::system::error_code ec);