| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 | 
							- /*
 
-  * Copyright (c) 2013 No Face Press, LLC
 
-  * License http://opensource.org/licenses/mit-license.php MIT License
 
-  */
 
- #ifndef _CIVETWEB_SERVER_H_
 
- #define _CIVETWEB_SERVER_H_
 
- #ifdef __cplusplus
 
- #include "civetweb.h"
 
- #include <vector>
 
- #include <string>
 
- class CivetServer; // forward declaration
 
- /**
 
-  * Basic interface for a URI request handler.  Handlers implementations
 
-  * must be reentrant.
 
-  */
 
- class CivetHandler {
 
- public:
 
- 	/**
 
- 	 * Destructor
 
- 	 */
 
- 	virtual ~CivetHandler() {
 
- 	}
 
- 	/**
 
- 	 * Callback method for GET request.
 
- 	 *
 
- 	 * @param server - the calling server
 
- 	 * @param conn - the connection information
 
- 	 * @returns true if implemented, false otherwise
 
- 	 */
 
- 	virtual bool handleGet(CivetServer *server, struct mg_connection *conn) {
 
- 		return false;
 
- 	}
 
- 	/**
 
- 	 * Callback method for POST request.
 
- 	 *
 
- 	 * @param server - the calling server
 
- 	 * @param conn - the connection information
 
- 	 * @returns true if implemented, false otherwise
 
- 	 */
 
- 	virtual bool handlePost(CivetServer *server, struct mg_connection *conn) {
 
- 		return false;
 
- 	}
 
- 	/**
 
- 	 * Callback method for PUT request.
 
- 	 *
 
- 	 * @param server - the calling server
 
- 	 * @param conn - the connection information
 
- 	 * @returns true if implemented, false otherwise
 
- 	 */
 
- 	virtual bool handlePut(CivetServer *server, struct mg_connection *conn) {
 
- 		return false;
 
- 	}
 
- 	/**
 
- 	 * Callback method for DELETE request.
 
- 	 *
 
- 	 * @param server - the calling server
 
- 	 * @param conn - the connection information
 
- 	 * @returns true if implemented, false otherwise
 
- 	 */
 
- 	virtual bool handleDelete(CivetServer *server, struct mg_connection *conn) {
 
- 		return false;
 
- 	}
 
- };
 
- /**
 
-  * CivetServer
 
-  *
 
-  * Basic class for embedded web server.  This has a URL mapping built-in.
 
-  */
 
- class CivetServer {
 
- public:
 
- 	/**
 
- 	 * Constructor
 
- 	 *
 
- 	 * This automatically starts the sever.
 
- 	 * It is good practice to call getContext() after this in case there
 
- 	 * were errors starting the server.
 
- 	 *
 
- 	 * @param options - the web server options.
 
- 	 * @param callbacks - optional web server callback methods.
 
- 	 *    Note that this class overrides begin_request callback.
 
- 	 */
 
- 	CivetServer(const char **options, const struct mg_callbacks *callbacks = 0);
 
- 	/**
 
- 	 * Destructor
 
- 	 */
 
- 	virtual ~CivetServer();
 
- 	/**
 
- 	 * close()
 
- 	 *
 
- 	 * Stops server and frees resources.
 
- 	 */
 
- 	void close();
 
- 	/**
 
- 	 * getContext()
 
- 	 *
 
- 	 * @return the context or 0 if not running.
 
- 	 */
 
- 	const struct mg_context *getContext() const {
 
- 		return context;
 
- 	}
 
- 	/**
 
- 	 * addHandler(const std::string &, CivetHandler *)
 
- 	 *
 
- 	 * Adds a URI handler.  If there is existing URI handler, it will
 
- 	 * be replaced with this one.  The handler is "owned" by this server
 
- 	 * and will be deallocated with it.
 
- 	 *
 
- 	 * URI's are ordered and partcial URI's are supported. For example,
 
- 	 * consider two URIs in order: /a/b and /a; /a matches
 
- 	 *  /a, /a/b matches /a/b, /a/c matches /a.  Reversing the order to
 
- 	 *  /a and /a/b; /a matches /a/b, /a/b matches /a. /a/c matches /a.
 
- 	 *
 
- 	 *  @param uri - URI to match.
 
- 	 *  @param handler - handler instance to use.  This will be free'ed
 
- 	 *      when the server closes and instances cannot be reused.
 
- 	 */
 
- 	void addHandler(const std::string &uri, CivetHandler *handler);
 
- 	/**
 
- 	 * removeHandler(const std::string &)
 
- 	 *
 
- 	 * Removes a handler, deleting it if found.
 
- 	 *
 
- 	 * @param - the exact URL used in addHandler().
 
- 	 */
 
- 	void removeHandler(const std::string &uri);
 
- 	/**
 
- 	 * getHandler(const std::string &uri)
 
- 	 *
 
- 	 * @param uri - the URI
 
- 	 * @returns the handler that matches the requested URI or 0 if none were found.
 
- 	 */
 
- 	CivetHandler *getHandler(const std::string &uri) const {
 
- 		return getHandler(uri.data(), uri.length());
 
- 	}
 
- 	/**
 
- 	 * getHandler(const char *uri, unsigned urilen)
 
- 	 *
 
- 	 * @param uri - the URI
 
- 	 * @param urilen - the length of the URI
 
- 	 * @returns the handler that matches the requested URI or 0 if none were found.
 
- 	 */
 
- 	CivetHandler *getHandler(const char *uri, unsigned urilen) const;
 
- protected:
 
- 	/**
 
- 	 * handleRequest(struct mg_connection *)
 
- 	 *
 
- 	 * Handles the incomming request.
 
- 	 *
 
- 	 * @param conn - the connection information
 
- 	 * @returns true if implemented, false otherwise
 
- 	 */
 
- 	virtual bool handleRequest(struct mg_connection *conn);
 
- 	/**
 
- 	 *  Returns the index of the handler that matches the
 
- 	 *  URI exactly.
 
- 	 *
 
- 	 *  @param uri - the url to match
 
- 	 */
 
- 	int getIndex(const std::string &uri) const;
 
- 	std::vector<std::string> uris;
 
- 	std::vector<CivetHandler *> handlers;
 
- 	struct mg_context *context;
 
- private:
 
- 	static int begin_request_callback(struct mg_connection *conn);
 
- };
 
- #endif /*  __cplusplus */
 
- #endif /* _CIVETWEB_SERVER_H_ */
 
 
  |