libzeep

PrevUpHomeNext

Class server

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

Synopsis

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


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

  // public member functions
  security_context & get_security_context();
  bool has_security_context() 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;
  boost::asio::io_service & get_io_service();

  // 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.

server public construct/copy/destruct

  1. server();
    Simple server, no security, no template processor.
  2. server(const std::string & docroot);
    Simple server, no security, create default template processor with docroot.
  3. server(security_context * s_ctxt);
    server with a security context for limited access
  4. server(security_context * s_ctxt, const std::string & docroot);
    server with a security context for limited access, create default template processor with docroot
  5. server(const server &) = delete;
  6. server & operator=(const server &) = delete;
  7. ~server();

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_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

  4. 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

  5. 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.

  6. 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.

  7. void set_template_processor(basic_template_processor * template_processor);
    Set the template processor.

    A template processor handles loading templates and processing the contents.

  8. 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.

  9. 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.

  10. bool has_template_processor() const;
    returns whether template processor has been set
  11. virtual void bind(const std::string & address, unsigned short port);
    Bind the server to address address and port port.
  12. virtual void run(int nr_of_threads);
    Run as many as nr_of_threads threads simultaneously.
  13. virtual void stop();
    Stop all threads and stop listening.
  14. 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
  15. std::string get_address() const;
    returns the address as specified in bind
  16. unsigned short get_port() const;
    returns the port as specified in bind
  17. boost::asio::io_service & get_io_service();
    get_io_service has to be public since we need it to call notify_fork from child code

server public static functions

  1. static std::ostream & get_log();
    to extend the log entry for a current request, use this ostream:

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

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);

PrevUpHomeNext