Bläddra i källkod

Clean usage of const/not const mg_context* and mg_connection* handles in the API.

The API had an inconsistant usage of `const mg_connection *` and `mg_connection *`,
that may cause warnings (e.g., #109). Now all non-const `mg_connection *` is only
required to read/write the connection.
bel 10 år sedan
förälder
incheckning
d264808e52

+ 1 - 1
examples/embedded_c/embedded_c.c

@@ -70,7 +70,7 @@ int ABHandler(struct mg_connection *conn, void *cbdata)
 int FooHandler(struct mg_connection *conn, void *cbdata)
 {
     /* Handler may access the request info using mg_get_request_info */
-    struct mg_request_info * req_info = mg_get_request_info(conn);
+    const struct mg_request_info * req_info = mg_get_request_info(conn);
 
     mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
     mg_printf(conn, "<html><body>");

+ 1 - 1
examples/embedded_cpp/embedded_cpp.cpp

@@ -89,7 +89,7 @@ class FooHandler: public CivetHandler
 public:
     bool handleGet(CivetServer *server, struct mg_connection *conn) {
         /* Handler may access the request info using mg_get_request_info */
-        struct mg_request_info * req_info = mg_get_request_info(conn);
+        const struct mg_request_info * req_info = mg_get_request_info(conn);
 
         mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
         mg_printf(conn, "<html><body>");

+ 11 - 11
examples/websocket/WebSockCallbacks.c

@@ -37,13 +37,13 @@ static void send_to_all_websockets(struct mg_context *ctx, const char * data, in
 void websocket_ready_handler(struct mg_connection *conn) {
 
     int i;
-    struct mg_request_info * rq = mg_get_request_info(conn);
+    const struct mg_request_info * rq = mg_get_request_info(conn);
     struct mg_context * ctx = mg_get_context(conn);
     tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
     tWebSockInfo * wsock = malloc(sizeof(tWebSockInfo));
     assert(wsock);
     wsock->webSockState = 0;
-    rq->conn_data = wsock;
+    mg_set_user_connection_data(conn, wsock);
 
     mg_lock_context(ctx);
     for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
@@ -79,7 +79,7 @@ static void websocket_done(tWebSockContext *ws_ctx, tWebSockInfo * wsock) {
 
 int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len) {
 
-    struct mg_request_info * rq = mg_get_request_info(conn);
+    const struct mg_request_info * rq = mg_get_request_info(conn);
     tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
     struct mg_context * ctx = mg_get_context(conn);
     tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
@@ -89,7 +89,7 @@ int websocket_data_handler(struct mg_connection *conn, int flags, char *data, si
     if (flags==136) {
         // close websock
         websocket_done(ws_ctx, wsock);
-        rq->conn_data = 0;
+        mg_set_user_connection_data(conn, NULL);
         mg_unlock_context(ctx);
         return 1;
     }
@@ -129,16 +129,16 @@ int websocket_data_handler(struct mg_connection *conn, int flags, char *data, si
 }
 
 
-void connection_close_handler(struct mg_connection *conn) {
+void connection_close_handler(const struct mg_connection *conn) {
 
-    struct mg_request_info * rq = mg_get_request_info(conn);
+    const struct mg_request_info * rq = mg_get_request_info(conn);
     tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
     struct mg_context * ctx = mg_get_context(conn);
     tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
 
     mg_lock_context(ctx);
     websocket_done(ws_ctx, wsock);
-    rq->conn_data = 0;
+    mg_set_user_connection_data(conn, NULL);
     mg_unlock_context(ctx);
 }
 
@@ -174,15 +174,15 @@ void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_
     }
 }
 
-void websock_init_lib(struct mg_context *ctx) {
+void websock_init_lib(const struct mg_context *ctx) {
 
     tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
-    memset(ws_ctx,0,sizeof(*ws_ctx));
+    memset(ws_ctx, 0, sizeof(*ws_ctx));
     /* todo: use mg_start_thread_id instead of mg_start_thread */
-    mg_start_thread(eventMain, ctx);
+    mg_start_thread(eventMain, (void*)ctx);
 }
 
-void websock_exit_lib(struct mg_context *ctx) {
+void websock_exit_lib(const struct mg_context *ctx) {
 
     tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
     ws_ctx->runLoop = 0;

+ 3 - 3
examples/websocket/WebSockCallbacks.h

@@ -22,14 +22,14 @@ typedef struct tWebSockContext {
 } tWebSockContext;
 
 
-void websock_init_lib(struct mg_context *ctx);
-void websock_exit_lib(struct mg_context *ctx);
+void websock_init_lib(const struct mg_context *ctx);
+void websock_exit_lib(const struct mg_context *ctx);
 
 void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_len);
 
 void websocket_ready_handler(struct mg_connection *conn);
 int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len);
-void connection_close_handler(struct mg_connection *conn);
+void connection_close_handler(const struct mg_connection *conn);
 
 
 #ifdef __cplusplus

+ 1 - 1
examples/websocket_client/websocket_client.c

@@ -67,7 +67,7 @@ int websocket_server_data(struct mg_connection * conn, int bits, char *data, siz
     return 1; /* return 1 to keep the connetion open */
 }
 
-void websocket_server_connection_close(struct mg_connection * conn)
+void websocket_server_connection_close(const struct mg_connection * conn)
 {
     printf("Server: Close connection\n");
 

+ 2 - 2
include/CivetServer.h

@@ -344,12 +344,12 @@ private:
      *
      * @param conn - the connection information
      */
-    static void closeHandler(struct mg_connection *conn);
+    static void closeHandler(const struct mg_connection *conn);
 
     /**
      * Stores the user provided close handler
      */
-    void (*userCloseHandler)(struct mg_connection *conn);
+    void (*userCloseHandler)(const struct mg_connection *conn);
 
 };
 

+ 15 - 7
include/civetweb.h

@@ -145,7 +145,7 @@ struct mg_callbacks {
        list of clients.
        Using this callback for websocket connections is deprecated, use
        mg_set_websocket_handler instead. */
-    void (*connection_close)(struct mg_connection *);
+    void (*connection_close)(const struct mg_connection *);
 
     /* Called when civetweb tries to open a file. Used to intercept file open
        calls, and serve file data from memory instead.
@@ -164,7 +164,7 @@ struct mg_callbacks {
        Lua support is enabled.
        Parameters:
          lua_context: "lua_State *" pointer. */
-    void (*init_lua)(struct mg_connection *, void *lua_context);
+    void (*init_lua)(const struct mg_connection *, void *lua_context);
 
     /* Called when civetweb has uploaded a file to a temporary directory as a
        result of mg_upload() call.
@@ -185,12 +185,12 @@ struct mg_callbacks {
        are processed.
        Parameters:
          ctx: context handle */
-    void (*init_context)(struct mg_context * ctx);
+    void (*init_context)(const struct mg_context * ctx);
 
     /* Called when civetweb context is deleted.
        Parameters:
          ctx: context handle */
-    void (*exit_context)(struct mg_context * ctx);
+    void (*exit_context)(const struct mg_context * ctx);
 };
 
 
@@ -324,7 +324,15 @@ CIVETWEB_API struct mg_context *mg_get_context(const struct mg_connection *conn)
 
 
 /* Get user data passed to mg_start from context. */
-CIVETWEB_API void *mg_get_user_data(struct mg_context *ctx);
+CIVETWEB_API void *mg_get_user_data(const struct mg_context *ctx);
+
+
+/* Set user data for the current connection. */
+CIVETWEB_API void mg_set_user_connection_data(const struct mg_connection *conn, void *data);
+
+
+/* Get user data set for the current connection. */
+CIVETWEB_API void *mg_get_user_connection_data(const struct mg_connection *conn);
 
 
 #if defined(MG_LEGACY_INTERFACE)
@@ -389,7 +397,7 @@ CIVETWEB_API int mg_modify_passwords_file(const char *passwords_file_name,
 
 
 /* Return information associated with the request. */
-CIVETWEB_API struct mg_request_info *mg_get_request_info(struct mg_connection *);
+CIVETWEB_API const struct mg_request_info *mg_get_request_info(const struct mg_connection *);
 
 
 /* Send data to the client.
@@ -636,7 +644,7 @@ CIVETWEB_API char *mg_md5(char buf[33], ...);
      ...: variable argument list
    Example:
      mg_cry(conn,"i like %s", "logging"); */
-CIVETWEB_API void mg_cry(struct mg_connection *conn,
+CIVETWEB_API void mg_cry(const struct mg_connection *conn,
                          PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(2, 3);
 
 

+ 5 - 5
src/CivetServer.cpp

@@ -52,7 +52,7 @@ bool CivetHandler::handleOptions(CivetServer *server, struct mg_connection *conn
 
 int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
 {
-    struct mg_request_info *request_info = mg_get_request_info(conn);
+    const struct mg_request_info *request_info = mg_get_request_info(conn);
     assert(request_info != NULL);
     CivetServer *me = (CivetServer*) (request_info->user_data);
     assert(me != NULL);
@@ -106,9 +106,9 @@ CivetServer::~CivetServer()
     close();
 }
 
-void CivetServer::closeHandler(struct mg_connection *conn)
+void CivetServer::closeHandler(const struct mg_connection *conn)
 {
-    struct mg_request_info *request_info = mg_get_request_info(conn);
+    const struct mg_request_info *request_info = mg_get_request_info(conn);
     assert(request_info != NULL);
     CivetServer *me = (CivetServer*) (request_info->user_data);
     assert(me != NULL);
@@ -118,7 +118,7 @@ void CivetServer::closeHandler(struct mg_connection *conn)
 
     if (me->userCloseHandler) me->userCloseHandler(conn);
     mg_lock_context(me->context);
-    me->connections.erase(conn);
+    me->connections.erase(const_cast<struct mg_connection *>(conn));
     mg_unlock_context(me->context);
 }
 
@@ -190,7 +190,7 @@ CivetServer::getParam(struct mg_connection *conn, const char *name,
                       std::string &dst, size_t occurrence)
 {
     const char *formParams = NULL;
-    struct mg_request_info *ri = mg_get_request_info(conn);
+    const struct mg_request_info *ri = mg_get_request_info(conn);
     assert(ri != NULL);
     CivetServer *me = (CivetServer*) (ri->user_data);
     assert(me != NULL);

+ 27 - 7
src/civetweb.c

@@ -1218,7 +1218,7 @@ static int get_option_index(const char *name)
     return -1;
 }
 
-const char *mg_get_option(const struct mg_context *ctx, const char *name)
+const char * mg_get_option(const struct mg_context *ctx, const char *name)
 {
     int i;
     if ((i = get_option_index(name)) == -1) {
@@ -1230,16 +1230,36 @@ const char *mg_get_option(const struct mg_context *ctx, const char *name)
     }
 }
 
-struct mg_context *mg_get_context(const struct mg_connection * conn)
+
+struct mg_context * mg_get_context(const struct mg_connection * conn)
 {
     return (conn == NULL) ? (struct mg_context *)NULL : (conn->ctx);
 }
 
-void *mg_get_user_data(struct mg_context *ctx)
+
+void * mg_get_user_data(const struct mg_context *ctx)
 {
     return (ctx == NULL) ? NULL : ctx->user_data;
 }
 
+
+void mg_set_user_connection_data(const struct mg_connection *conn, void *data)
+{
+    if (conn != NULL) {
+        ((struct mg_connection *)conn)->request_info.conn_data = data;
+    }
+}
+
+
+void * mg_get_user_connection_data(const struct mg_connection *conn)
+{
+    if (conn != NULL) {
+        return conn->request_info.conn_data;
+    }
+    return NULL;
+}
+
+
 size_t mg_get_ports(const struct mg_context *ctx, size_t size, int* ports, int* ssl)
 {
     size_t i;
@@ -1288,7 +1308,7 @@ static double mg_difftimespec(const struct timespec *ts_now, const struct timesp
 }
 
 /* Print error message to the opened error log stream. */
-void mg_cry(struct mg_connection *conn, const char *fmt, ...)
+void mg_cry(const struct mg_connection *conn, const char *fmt, ...)
 {
     char buf[MG_BUF_LEN], src_addr[IP_ADDR_STR_LEN];
     va_list ap;
@@ -1342,7 +1362,7 @@ const char *mg_version(void)
     return CIVETWEB_VERSION;
 }
 
-struct mg_request_info *mg_get_request_info(struct mg_connection *conn)
+const struct mg_request_info *mg_get_request_info(const struct mg_connection *conn)
 {
     return &conn->request_info;
 }
@@ -6468,7 +6488,7 @@ static int get_request_handler(struct mg_connection *conn,
                                void **cbdata
                                )
 {
-    struct mg_request_info *request_info = mg_get_request_info(conn);
+    const struct mg_request_info *request_info = mg_get_request_info(conn);
     const char *uri = request_info->uri;
     size_t urilen = strlen(uri);
     struct mg_request_handler_info *tmp_rh;
@@ -8343,7 +8363,7 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
     const char *name, *value, *default_value;
     int i, ok;
     int workerthreadcount;
-    void (*exit_callback)(struct mg_context * ctx) = 0;
+    void (*exit_callback)(const struct mg_context * ctx) = 0;
 
 #if defined(_WIN32) && !defined(__SYMBIAN32__)
     WSADATA data;

+ 2 - 2
test/unit_test.c

@@ -407,7 +407,7 @@ static void test_mg_download(int use_ssl) {
     int i, len1, len2, port;
     struct mg_connection *conn;
     struct mg_context *ctx;
-    struct mg_request_info *ri;
+    const struct mg_request_info *ri;
 
     if (use_ssl) port = atoi(HTTPS_PORT); else port = atoi(HTTP_PORT);
 
@@ -946,7 +946,7 @@ static void test_request_handlers(void) {
 }
 
 static int api_callback(struct mg_connection *conn) {
-    struct mg_request_info *ri = mg_get_request_info(conn);
+    const struct mg_request_info *ri = mg_get_request_info(conn);
     char post_data[100] = "";
 
     ASSERT(ri->user_data == (void *) 123);