Przeglądaj źródła

Added exception if CivetServer constructor fails. Added getListeningPorts() method.

Mark Lakata 10 lat temu
rodzic
commit
82a7ed8b53
2 zmienionych plików z 36 dodań i 0 usunięć
  1. 22 0
      include/CivetServer.h
  2. 14 0
      src/CivetServer.cpp

+ 22 - 0
include/CivetServer.h

@@ -11,11 +11,21 @@
 #include "civetweb.h"
 #include <map>
 #include <string>
+#include <vector>
 
 // forward declaration
 class CivetServer;
 
 /**
+ * Exception class for thrown exceptions within the CivetHandler object.
+ */
+class CivetException : public std::runtime_error
+{
+    public:
+    CivetException(const std::string& msg) : std::runtime_error(msg) {}
+};
+
+/**
  * Basic interface for a URI request handler.  Handlers implementations
  * must be reentrant.
  */
@@ -93,6 +103,8 @@ public:
      *
      * @param options - the web server options.
      * @param callbacks - optional web server callback methods.
+     *
+     * @throws CivetException
      */
     CivetServer(const char **options, const struct mg_callbacks *callbacks = 0);
 
@@ -141,6 +153,16 @@ public:
     void removeHandler(const std::string &uri);
 
     /**
+     * getListeningPorts()
+     *
+     * Returns a list of ports that are listening
+     *
+     * @return A vector of ports
+     */
+    
+    std::vector<int> getListeningPorts();
+
+    /**
      * getCookie(struct mg_connection *conn, const std::string &cookieName, std::string &cookieValue)
      *
      * Puts the cookie value string that matches the cookie name in the cookieValue destinaton string.

+ 14 - 0
src/CivetServer.cpp

@@ -9,6 +9,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
+#include <stdexcept>
 
 #ifndef UNUSED_PARAMETER
 #define UNUSED_PARAMETER(x) (void)(x)
@@ -97,6 +98,7 @@ CivetServer::CivetServer(const char **options,
     }
     callbacks.connection_close = closeHandler;
     context = mg_start(&callbacks, this, options);
+    if (context == nullptr) throw CivetException::("null context when constructing CivetServer. Possible problem binding to port.");
 }
 
 CivetServer::~CivetServer()
@@ -294,6 +296,18 @@ CivetServer::urlEncode(const char *src, size_t src_len, std::string &dst, bool a
     }
 }
 
+std::vector<int>
+CivetServer::getListeningPorts()
+{
+    std::vector<int> ports(10);
+    std::vector<int> ssl(10);
+    size_t size = mg_get_ports(context, ports.size(), &ports[0], &ssl[0]);
+    ports.resize(size);
+    ssl.resize(size);
+    return ports;
+}
+
+
 CivetServer::CivetConnection::CivetConnection() {
     postData = NULL;
     postDataLen = 0;