Skip to content

Commit 2a6f4b9

Browse files
committed
Lookup https_fetch_ctx once
1 parent e4f8acd commit 2a6f4b9

File tree

1 file changed

+35
-41
lines changed

1 file changed

+35
-41
lines changed

src/https_client.c

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -486,58 +486,52 @@ static int https_fetch_ctx_process_response(https_client_t *client,
486486
}
487487

488488
static void https_fetch_ctx_cleanup(https_client_t *client,
489+
struct https_fetch_ctx *prev,
489490
struct https_fetch_ctx *ctx,
490491
int curl_result_code) {
491-
struct https_fetch_ctx *last = NULL;
492-
struct https_fetch_ctx *cur = client->fetches;
493-
while (cur) {
494-
if (cur == ctx) {
495-
CURLMcode code = curl_multi_remove_handle(client->curlm, ctx->curl);
496-
if (code != CURLM_OK) {
497-
FLOG_REQ("curl_multi_remove_handle error %d: %s", code, curl_multi_strerror(code));
498-
}
499-
int drop_reply = 0;
500-
if (curl_result_code < 0) {
501-
WLOG_REQ("Request was aborted.");
502-
drop_reply = 1;
503-
} else if (https_fetch_ctx_process_response(client, ctx, curl_result_code) != 0) {
504-
ILOG_REQ("Response was faulty, skipping DNS reply.");
505-
drop_reply = 1;
506-
}
507-
if (drop_reply) {
508-
free(ctx->buf);
509-
ctx->buf = NULL;
510-
ctx->buflen = 0;
511-
}
512-
// callback must be called to avoid memleak
513-
ctx->cb(ctx->cb_data, ctx->buf, ctx->buflen);
514-
curl_easy_cleanup(ctx->curl);
515-
free(ctx->buf);
516-
if (last) {
517-
last->next = ctx->next;
518-
} else {
519-
client->fetches = ctx->next;
520-
}
521-
free(ctx);
522-
return;
523-
}
524-
last = cur;
525-
cur = cur->next;
492+
CURLMcode code = curl_multi_remove_handle(client->curlm, ctx->curl);
493+
if (code != CURLM_OK) {
494+
FLOG_REQ("curl_multi_remove_handle error %d: %s", code, curl_multi_strerror(code));
495+
}
496+
int drop_reply = 0;
497+
if (curl_result_code < 0) {
498+
WLOG_REQ("Request was aborted.");
499+
drop_reply = 1;
500+
} else if (https_fetch_ctx_process_response(client, ctx, curl_result_code) != 0) {
501+
ILOG_REQ("Response was faulty, skipping DNS reply.");
502+
drop_reply = 1;
503+
}
504+
if (drop_reply) {
505+
free(ctx->buf);
506+
ctx->buf = NULL;
507+
ctx->buflen = 0;
508+
}
509+
// callback must be called to avoid memleak
510+
ctx->cb(ctx->cb_data, ctx->buf, ctx->buflen);
511+
curl_easy_cleanup(ctx->curl);
512+
free(ctx->buf);
513+
if (prev) {
514+
prev->next = ctx->next;
515+
} else {
516+
client->fetches = ctx->next;
526517
}
518+
free(ctx);
527519
}
528520

529521
static void check_multi_info(https_client_t *c) {
530522
CURLMsg *msg = NULL;
531523
int msgs_left = 0;
532524
while ((msg = curl_multi_info_read(c->curlm, &msgs_left))) {
533525
if (msg->msg == CURLMSG_DONE) {
534-
struct https_fetch_ctx *n = c->fetches;
535-
while (n) {
536-
if (n->curl == msg->easy_handle) {
537-
https_fetch_ctx_cleanup(c, n, msg->data.result);
526+
struct https_fetch_ctx *prev = NULL;
527+
struct https_fetch_ctx *cur = c->fetches;
528+
while (cur) {
529+
if (cur->curl == msg->easy_handle) {
530+
https_fetch_ctx_cleanup(c, prev, cur, msg->data.result);
538531
break;
539532
}
540-
n = n->next;
533+
prev = cur;
534+
cur = cur->next;
541535
}
542536
}
543537
}
@@ -665,7 +659,7 @@ void https_client_reset(https_client_t *c) {
665659

666660
void https_client_cleanup(https_client_t *c) {
667661
while (c->fetches) {
668-
https_fetch_ctx_cleanup(c, c->fetches, -1);
662+
https_fetch_ctx_cleanup(c, NULL, c->fetches, -1);
669663
}
670664
curl_slist_free_all(c->header_list);
671665
curl_multi_cleanup(c->curlm);

0 commit comments

Comments
 (0)