فهرست منبع

Merge branch 'master' of https://github.com/bel2125/civetweb

Maarten Fremouw 10 سال پیش
والد
کامیت
15a1123f9c
5فایلهای تغییر یافته به همراه75 افزوده شده و 14 حذف شده
  1. 22 0
      include/CivetServer.h
  2. 21 0
      src/CivetServer.cpp
  3. 20 1
      src/civetweb.c
  4. 2 2
      src/main.c
  5. 10 11
      src/mod_lua.inl

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

+ 21 - 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)
@@ -55,6 +56,10 @@ int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
     assert(request_info != NULL);
     CivetServer *me = (CivetServer*) (request_info->user_data);
     assert(me != NULL);
+
+    // Happens when a request hits the server before the context is saved
+    if (me->context == NULL) return 0;
+
     mg_lock_context(me->context);
     me->connections[conn] = CivetConnection();
     mg_unlock_context(me->context);
@@ -93,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()
@@ -107,6 +113,9 @@ void CivetServer::closeHandler(struct mg_connection *conn)
     CivetServer *me = (CivetServer*) (request_info->user_data);
     assert(me != NULL);
 
+    // Happens when a request hits the server before the context is saved
+    if (me->context == NULL) return;
+
     if (me->userCloseHandler) me->userCloseHandler(conn);
     mg_lock_context(me->context);
     me->connections.erase(conn);
@@ -287,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;

+ 20 - 1
src/civetweb.c

@@ -2472,6 +2472,24 @@ static int pull_all(FILE *fp, struct mg_connection *conn, char *buf, int len)
     return nread;
 }
 
+static void discard_unread_request_data(struct mg_connection *conn)
+{
+    char buf[MG_BUF_LEN];
+    int to_read, nread;
+
+    while (conn->consumed_content < conn->content_len) {
+        to_read = sizeof(buf);
+        if ((int64_t) to_read > conn->content_len - conn->consumed_content) {
+            to_read = (int) (conn->content_len - conn->consumed_content);
+        }
+
+        nread = mg_read(conn, buf, to_read);
+        if (nread <= 0) {
+            break;
+        }
+    }
+}
+
 int mg_read(struct mg_connection *conn, void *buf, size_t len)
 {
     int64_t n, buffered_len, nread;
@@ -2913,7 +2931,7 @@ static void interpret_uri(struct mg_connection *conn,    /* in: request */
                    requests, should replace/delete the script file.
                    Requests that read or write from/to a resource, like GET and POST requests,
                    should call the script and return the generated response. */
-                *is_script_ressource = !is_put_or_delete_request;
+                *is_script_ressource = !*is_put_or_delete_request;
         }
         return;
     }
@@ -6269,6 +6287,7 @@ static void handle_request(struct mg_connection *conn)
     /* 8. check if there are request handlers for this path */
     if (conn->ctx->request_handlers != NULL && use_request_handler(conn)) {
         /* Do nothing, callback has served the request */
+        discard_unread_request_data(conn);
         return;
     }
 

+ 2 - 2
src/main.c

@@ -557,9 +557,9 @@ static void set_absolute_path(char *options[], const char *option_name,
 
 #ifdef USE_LUA
 #define main luatest_main
-#define luaL_openlibs lua_civet_openlibs
+#define luaL_openlibs lua_civet_open_all_libs
 struct lua_State;
-extern void lua_civet_openlibs(struct lua_State *L);
+extern void lua_civet_open_all_libs(struct lua_State *L);
 #include "../src/third_party/lua-5.2.3/src/lua.c"
 #undef main
 #endif

+ 10 - 11
src/mod_lua.inl

@@ -982,12 +982,12 @@ static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
     lua_rawset(L, -3);
 }
 
-static void prepare_lua_environment(struct mg_context * ctx, struct mg_connection *conn, struct lua_websock_data *conn_list, lua_State *L, const char *script_name, int lua_env_type)
+void lua_civet_open_all_libs(lua_State *L)
 {
-    extern void luaL_openlibs(lua_State *);
-    luaL_openlibs(L);
-
-    assert(ctx);
+    {
+        extern void luaL_openlibs(lua_State *);
+        luaL_openlibs(L);
+    }
 
 #ifdef USE_LUA_SQLITE3
     {
@@ -1007,6 +1007,11 @@ static void prepare_lua_environment(struct mg_context * ctx, struct mg_connectio
         luaopen_lfs(L);
     }
 #endif
+}
+
+static void prepare_lua_environment(struct mg_context * ctx, struct mg_connection *conn, struct lua_websock_data *conn_list, lua_State *L, const char *script_name, int lua_env_type)
+{
+    lua_civet_open_all_libs(L);
 
     luaL_newmetatable(L, LUASOCKET);
     lua_pushliteral(L, "__index");
@@ -1108,12 +1113,6 @@ static void prepare_lua_environment(struct mg_context * ctx, struct mg_connectio
     }
 }
 
-void lua_civet_openlibs(lua_State *L)
-{
-    static struct mg_context fake_ctx;
-    prepare_lua_environment(&fake_ctx, NULL, NULL, L, NULL, 0);
-}
-
 static int lua_error_handler(lua_State *L)
 {
     const char *error_msg =  lua_isstring(L, -1) ?  lua_tostring(L, -1) : "?\n";