]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Use pseudo slab element destructor to reset rest request
authorNick Porter <nick@portercomputing.co.uk>
Fri, 3 Feb 2023 17:30:45 +0000 (17:30 +0000)
committerNick Porter <nick@portercomputing.co.uk>
Wed, 15 Feb 2023 16:11:34 +0000 (16:11 +0000)
src/modules/rlm_rest/io.c
src/modules/rlm_rest/rest.c
src/modules/rlm_rest/rest.h
src/modules/rlm_rest/rlm_rest.c

index a58c26bf2ff3dd707fb1c974ddce3688ad38ab8d..97dee323536b206194e2903db9f5090f81da3574 100644 (file)
@@ -47,7 +47,6 @@ void rest_io_module_signal(module_ctx_t const *mctx, request_t *request, fr_stat
        }
        t->mhandle->transfers--;
 
-       rest_request_cleanup(mctx->inst->data, randle);
        fr_rest_slab_release(randle);
 }
 
index 5c98fe61c5e665dc96e6b05fe03074c4d26d0d6e..9f634bf0a4979d72101ca2460163fc51ea654fc4 100644 (file)
@@ -2153,46 +2153,6 @@ int rest_response_decode(rlm_rest_t const *instance, rlm_rest_section_t const *s
        return ret;
 }
 
-/** Cleans up after a REST request.
- *
- * Resets all options associated with a CURL handle, and frees any headers
- * associated with it.
- *
- * Calls rest_read_ctx_free and rest_response_free to free any memory used by
- * context data.
- *
- * @param[in] instance configuration data.
- * @param[in] randle to cleanup.
- */
-void rest_request_cleanup(UNUSED rlm_rest_t const *instance, fr_curl_io_request_t *randle)
-{
-       rlm_rest_curl_context_t *ctx = talloc_get_type_abort(randle->uctx, rlm_rest_curl_context_t);
-       CURL                    *candle = randle->candle;
-
-       /*
-        *  Clear any previously configured options
-        */
-       curl_easy_reset(candle);
-
-       /*
-        *  Free header list
-        */
-       if (ctx->headers != NULL) {
-               curl_slist_free_all(ctx->headers);
-               ctx->headers = NULL;
-       }
-
-       /*
-        *  Free response data
-        */
-       TALLOC_FREE(ctx->body);
-       TALLOC_FREE(ctx->response.buffer);
-       TALLOC_FREE(ctx->request.encoder);
-       TALLOC_FREE(ctx->response.decoder);
-
-       randle->request = NULL;
-}
-
 /** URL encodes a string.
  *
  * Encode special chars as per RFC 3986 section 4.
index 901e2d9bba79ee0858133888c3cdb61c04d7548c..9533d05bf0e714d91f639a6b55282455cd9a1fcf 100644 (file)
@@ -295,8 +295,6 @@ int rest_response_decode(rlm_rest_t const *instance,
 void rest_response_error(request_t *request, fr_curl_io_request_t *handle);
 void rest_response_debug(request_t *request, fr_curl_io_request_t *handle);
 
-void rest_request_cleanup(rlm_rest_t const *instance, fr_curl_io_request_t *randle);
-
 #define rest_get_handle_code(_handle)(((rlm_rest_curl_context_t*)((fr_curl_io_request_t*)(_handle))->uctx)->response.code)
 
 #define rest_get_handle_type(_handle)(((rlm_rest_curl_context_t*)((fr_curl_io_request_t*)(_handle))->uctx)->response.type)
index 5e34e4cdaeaf183bbb2a4a851fae4d14860e171a..24a3d567d8089507016f3befe8aff646767dd8ca 100644 (file)
@@ -264,7 +264,6 @@ static xlat_action_t rest_xlat_resume(TALLOC_CTX *ctx, fr_dcursor_t *out,
                                      xlat_ctx_t const *xctx,
                                      request_t *request, UNUSED FR_DLIST_HEAD(fr_value_box_list) *in)
 {
-       rlm_rest_t const                *inst = talloc_get_type_abort(xctx->mctx->inst->data, rlm_rest_t);
        rlm_rest_xlat_rctx_t            *rctx = talloc_get_type_abort(xctx->rctx, rlm_rest_xlat_rctx_t);
        int                             hcode;
        ssize_t                         len;
@@ -329,8 +328,6 @@ finish:
                fr_dcursor_insert(out, vb);
        }
 
-       rest_request_cleanup(inst, handle);
-
        fr_rest_slab_release(handle);
 
        talloc_free(rctx);
@@ -480,7 +477,6 @@ static xlat_action_t rest_xlat(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out,
                RPEDEBUG("Failed escaping URI");
 
        error:
-               rest_request_cleanup(inst, randle);
                fr_rest_slab_release(randle);
                talloc_free(section);
 
@@ -614,8 +610,6 @@ static unlang_action_t mod_authorize_result(rlm_rcode_t *p_result, module_ctx_t
        }
 
 finish:
-       rest_request_cleanup(inst, handle);
-
        fr_rest_slab_release(handle);
 
        RETURN_MODULE_RCODE(rcode);
@@ -643,7 +637,6 @@ static unlang_action_t CC_HINT(nonnull) mod_authorize(rlm_rcode_t *p_result, mod
 
        ret = rlm_rest_perform(mctx, section, handle, request, NULL, NULL);
        if (ret < 0) {
-               rest_request_cleanup(inst, handle);
                fr_rest_slab_release(handle);
 
                RETURN_MODULE_FAIL;
@@ -728,8 +721,6 @@ static unlang_action_t mod_authenticate_result(rlm_rcode_t *p_result,
        }
 
 finish:
-       rest_request_cleanup(inst, handle);
-
        fr_rest_slab_release(handle);
 
        RETURN_MODULE_RCODE(rcode);
@@ -792,7 +783,6 @@ static unlang_action_t CC_HINT(nonnull) mod_authenticate(rlm_rcode_t *p_result,
        ret = rlm_rest_perform(mctx, section,
                               handle, request, username->vp_strvalue, password->vp_strvalue);
        if (ret < 0) {
-               rest_request_cleanup(inst, handle);
                fr_rest_slab_release(handle);
 
                RETURN_MODULE_FAIL;
@@ -844,8 +834,6 @@ static unlang_action_t mod_accounting_result(rlm_rcode_t *p_result, module_ctx_t
        }
 
 finish:
-       rest_request_cleanup(inst, handle);
-
        fr_rest_slab_release(handle);
 
        RETURN_MODULE_RCODE(rcode);
@@ -870,7 +858,6 @@ static unlang_action_t CC_HINT(nonnull) mod_accounting(rlm_rcode_t *p_result, mo
 
        ret = rlm_rest_perform(mctx, section, handle, request, NULL, NULL);
        if (ret < 0) {
-               rest_request_cleanup(inst, handle);
                fr_rest_slab_release(handle);
 
                RETURN_MODULE_FAIL;
@@ -922,8 +909,6 @@ static unlang_action_t mod_post_auth_result(rlm_rcode_t *p_result, module_ctx_t
        }
 
 finish:
-       rest_request_cleanup(inst, handle);
-
        fr_rest_slab_release(handle);
 
        RETURN_MODULE_RCODE(rcode);
@@ -948,8 +933,6 @@ static unlang_action_t CC_HINT(nonnull) mod_post_auth(rlm_rcode_t *p_result, mod
 
        ret = rlm_rest_perform(mctx, section, handle, request, NULL, NULL);
        if (ret < 0) {
-               rest_request_cleanup(inst, handle);
-
                fr_rest_slab_release(handle);
 
                RETURN_MODULE_FAIL;
@@ -1098,6 +1081,47 @@ static int _mod_conn_free(fr_curl_io_request_t *randle)
        return 0;
 }
 
+/** Cleans up after a REST request.
+ *
+ * Resets all options associated with a CURL handle, and frees any headers
+ * associated with it.
+ *
+ * Calls rest_read_ctx_free and rest_response_free to free any memory used by
+ * context data.
+ *
+ * @param[in] randle to cleanup.
+ * @param[in] uctx unused.
+ */
+static int rest_request_cleanup(fr_curl_io_request_t *randle, UNUSED void *uctx)
+{
+       rlm_rest_curl_context_t *ctx = talloc_get_type_abort(randle->uctx, rlm_rest_curl_context_t);
+       CURL                    *candle = randle->candle;
+
+       /*
+        *  Clear any previously configured options
+        */
+       curl_easy_reset(candle);
+
+       /*
+        *  Free header list
+        */
+       if (ctx->headers != NULL) {
+               curl_slist_free_all(ctx->headers);
+               ctx->headers = NULL;
+       }
+
+       /*
+        *  Free response data
+        */
+       TALLOC_FREE(ctx->body);
+       TALLOC_FREE(ctx->response.buffer);
+       TALLOC_FREE(ctx->request.encoder);
+       TALLOC_FREE(ctx->response.decoder);
+
+       randle->request = NULL;
+       return 0;
+}
+
 static int rest_conn_alloc(fr_curl_io_request_t *randle, void *uctx)
 {
        rlm_rest_t const        *inst = talloc_get_type_abort(uctx, rlm_rest_t);
@@ -1117,6 +1141,8 @@ static int rest_conn_alloc(fr_curl_io_request_t *randle, void *uctx)
        randle->uctx = curl_ctx;
        talloc_set_destructor(randle, _mod_conn_free);
 
+       fr_rest_slab_element_set_destructor(randle, rest_request_cleanup, NULL);
+
        return 0;
 }