Explorar o código

Add callbacks for creation and deletion of the server context

bel %!s(int64=11) %!d(string=hai) anos
pai
achega
64c97d56e4
Modificáronse 2 ficheiros con 33 adicións e 0 borrados
  1. 15 0
      include/civetweb.h
  2. 18 0
      src/civetweb.c

+ 15 - 0
include/civetweb.h

@@ -153,6 +153,17 @@ struct mg_callbacks {
        Parameters:
          status: HTTP error status code. */
     int  (*http_error)(struct mg_connection *, int status);
+
+    /* Called after civetweb context has been created, before requests
+       are processed.
+       Parameters:
+         ctx: context handle */
+    void (*init_context)(struct mg_context * ctx);
+
+    /* Called when civetweb context is deleted.
+       Parameters:
+         ctx: context handle */
+    void (*exit_context)(struct mg_context * ctx);
 };
 
 
@@ -236,6 +247,10 @@ CIVETWEB_API void mg_set_request_handler(struct mg_context *ctx, const char *uri
 CIVETWEB_API const char *mg_get_option(const struct mg_context *ctx, const char *name);
 
 
+/* Get context from connection. */
+CIVETWEB_API struct mg_context *mg_get_context(struct mg_connection *conn);
+
+
 #if defined(MG_LEGACY_INTERFACE)
 /* Return array of strings that represent valid configuration options.
    For each option, option name and default value is returned, i.e. the

+ 18 - 0
src/civetweb.c

@@ -1060,6 +1060,11 @@ const char *mg_get_option(const struct mg_context *ctx, const char *name)
     }
 }
 
+struct mg_context *mg_get_context(struct mg_connection * conn)
+{
+    return (conn == NULL) ? (struct mg_context *)NULL : &(conn->ctx);
+}
+
 size_t mg_get_ports(const struct mg_context *ctx, size_t size, int* ports, int* ssl)
 {
     size_t i;
@@ -6791,6 +6796,10 @@ static void free_context(struct mg_context *ctx)
     if (ctx == NULL)
         return;
 
+    if (ctx->callbacks.exit_context) {
+        ctx->callbacks.exit_context(ctx);
+    }
+
     /* All threads exited, no sync is needed. Destroy thread mutex and condvars */
     (void) pthread_mutex_destroy(&ctx->thread_mutex);
     (void) pthread_cond_destroy(&ctx->thread_cond);
@@ -6903,6 +6912,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;
 
 #if defined(_WIN32) && !defined(__SYMBIAN32__)
     WSADATA data;
@@ -6945,6 +6955,8 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
 
     if (callbacks) {
         ctx->callbacks = *callbacks;
+        exit_callback = callbacks->exit_context;
+        ctx->callbacks.exit_context = 0;
     }
     ctx->user_data = user_data;
     ctx->request_handlers = NULL;
@@ -7028,6 +7040,12 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
     }
 #endif
 
+    /* Context has been created - init user libraries */
+    if (ctx->callbacks.init_context) {
+        ctx->callbacks.init_context(ctx);
+    }
+    ctx->callbacks.exit_context = exit_callback;
+
     /* Start master (listening) thread */
     mg_start_thread_with_id(master_thread, ctx, &ctx->masterthreadid);