Forráskód Böngészése

Setting user data pointer properly

Sergey Lyubka 12 éve
szülő
commit
b80157b7ea
2 módosított fájl, 25 hozzáadás és 1 törlés
  1. 2 1
      mongoose.c
  2. 23 0
      test/unit_test.c

+ 2 - 1
mongoose.c

@@ -4878,12 +4878,13 @@ static void *worker_thread(void *thread_func_param) {
   } else {
     conn->buf_size = MAX_REQUEST_SIZE;
     conn->buf = (char *) (conn + 1);
+    conn->ctx = ctx;
+    conn->request_info.user_data = ctx->user_data;
 
     // Call consume_socket() even when ctx->stop_flag > 0, to let it signal
     // sq_empty condvar to wake up the master waiting in produce_socket()
     while (consume_socket(ctx, &conn->client)) {
       conn->birth_time = time(NULL);
-      conn->ctx = ctx;
 
       // Fill in IP, port info early so even if SSL setup below fails,
       // error handler would have the corresponding info.

+ 23 - 0
test/unit_test.c

@@ -539,6 +539,28 @@ static void test_request_replies(void) {
   mg_stop(ctx);
 }
 
+static int user_data_callback(struct mg_connection *conn) {
+  ASSERT(mg_get_request_info(conn)->user_data == (void *) 123);
+  mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\nhi");
+  return 1;
+}
+
+static void test_user_data(void) {
+  char ebuf[100];
+  struct mg_callbacks callbacks;
+  struct mg_connection *conn;
+  struct mg_context *ctx;
+
+  memset(&callbacks, 0, sizeof(callbacks));
+  callbacks.begin_request = user_data_callback;
+  ASSERT((ctx = mg_start(&callbacks, (void *) 123, OPTIONS)) != NULL);
+  ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1,
+                             ebuf, sizeof(ebuf),
+                             "%s", "GET / HTTP/1.0\r\n\r\n")) != NULL);
+  mg_close_connection(conn);
+  mg_stop(ctx);
+}
+
 int __cdecl main(void) {
   test_alloc_vprintf();
   test_base64_encode();
@@ -554,6 +576,7 @@ int __cdecl main(void) {
   test_skip_quoted();
   test_mg_upload();
   test_request_replies();
+  test_user_data();
 #ifdef USE_LUA
   test_lua();
 #endif