nghttp2: use custom memory functions

Provide libcurl's memory functions as callbacks to replace nghttp2's own memory
functions. This allows custom memory callbacks provided by users of libcurl to
be used by nghttp2 as well.

Closes #15527
This commit is contained in:
Ethan Everett 2024-11-08 11:49:32 -08:00 committed by Daniel Stenberg
parent 1cd745a581
commit 9089ef1f4f
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
3 changed files with 35 additions and 2 deletions

View file

@ -277,6 +277,8 @@ static int proxy_h2_client_new(struct Curl_cfilter *cf,
{
struct cf_h2_proxy_ctx *ctx = cf->ctx;
nghttp2_option *o;
nghttp2_mem mem = {NULL, Curl_nghttp2_malloc, Curl_nghttp2_free,
Curl_nghttp2_calloc, Curl_nghttp2_realloc};
int rc = nghttp2_option_new(&o);
if(rc)
@ -289,7 +291,7 @@ static int proxy_h2_client_new(struct Curl_cfilter *cf,
HTTP field value. */
nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(o, 1);
#endif
rc = nghttp2_session_client_new2(&ctx->h2, cbs, cf, o);
rc = nghttp2_session_client_new3(&ctx->h2, cbs, cf, o, &mem);
nghttp2_option_del(o);
return rc;
}

View file

@ -433,6 +433,8 @@ static int h2_client_new(struct Curl_cfilter *cf,
{
struct cf_h2_ctx *ctx = cf->ctx;
nghttp2_option *o;
nghttp2_mem mem = {NULL, Curl_nghttp2_malloc, Curl_nghttp2_free,
Curl_nghttp2_calloc, Curl_nghttp2_realloc};
int rc = nghttp2_option_new(&o);
if(rc)
@ -445,7 +447,7 @@ static int h2_client_new(struct Curl_cfilter *cf,
HTTP field value. */
nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(o, 1);
#endif
rc = nghttp2_session_client_new2(&ctx->h2, cbs, cf, o);
rc = nghttp2_session_client_new3(&ctx->h2, cbs, cf, o, &mem);
nghttp2_option_del(o);
return rc;
}
@ -2960,6 +2962,30 @@ bool Curl_h2_http_1_1_error(struct Curl_easy *data)
return FALSE;
}
void *Curl_nghttp2_malloc(size_t size, void *user_data)
{
(void)user_data;
return Curl_cmalloc(size);
}
void Curl_nghttp2_free(void *ptr, void *user_data)
{
(void)user_data;
Curl_cfree(ptr);
}
void *Curl_nghttp2_calloc(size_t nmemb, size_t size, void *user_data)
{
(void)user_data;
return Curl_ccalloc(nmemb, size);
}
void *Curl_nghttp2_realloc(void *ptr, size_t size, void *user_data)
{
(void)user_data;
return Curl_crealloc(ptr, size);
}
#else /* !USE_NGHTTP2 */
/* Satisfy external references even if http2 is not compiled in. */

View file

@ -60,6 +60,11 @@ CURLcode Curl_http2_upgrade(struct Curl_easy *data,
struct connectdata *conn, int sockindex,
const char *ptr, size_t nread);
void *Curl_nghttp2_malloc(size_t size, void *user_data);
void Curl_nghttp2_free(void *ptr, void *user_data);
void *Curl_nghttp2_calloc(size_t nmemb, size_t size, void *user_data);
void *Curl_nghttp2_realloc(void *ptr, size_t size, void *user_data);
extern struct Curl_cftype Curl_cft_nghttp2;
#else /* USE_NGHTTP2 */