@@ -26,10 +26,10 @@ inline auto f$curl_init(string url = string{""}) noexcept -> kphp::coro::task<kp
2626 co_return 0 ;
2727 }
2828 const auto st{kphp::web::simple_transfer{*open_res}};
29+ auto & easy_ctx{CurlInstanceState::get ().easy_ctx .get_or_init (st.descriptor )};
2930 auto setopt_res{
3031 kphp::web::set_transfer_property (st, static_cast <kphp::web::property_id>(kphp::web::curl::CURLOPT::URL), kphp::web::property_value::as_string (url))};
3132 if (!setopt_res.has_value ()) [[unlikely]] {
32- auto & easy_ctx{CurlInstanceState::get ().easy_ctx .get_or_init (st.descriptor )};
3333 easy_ctx.set_errno (setopt_res.error ().code , setopt_res.error ().description );
3434 kphp::web::curl::print_error (" could not set URL for a new curl easy handle" , std::move (setopt_res.error ()));
3535 co_return 0 ;
@@ -38,7 +38,11 @@ inline auto f$curl_init(string url = string{""}) noexcept -> kphp::coro::task<kp
3838}
3939
4040inline auto f$curl_setopt(kphp::web::curl::easy_type easy_id, int64_t option, const mixed& value) noexcept -> bool { // NOLINT
41- auto & easy_ctx{CurlInstanceState::get ().easy_ctx .get_or_init (easy_id)};
41+ auto & curl_state{CurlInstanceState::get ()};
42+ if (!curl_state.easy_ctx .has (easy_id)) {
43+ return false ;
44+ }
45+ auto & easy_ctx{curl_state.easy_ctx .get_or_init (easy_id)};
4246 auto st{kphp::web::simple_transfer{.descriptor = easy_id}};
4347
4448 if (option == static_cast <int64_t >(kphp::web::curl::CURLINFO::HEADER_OUT)) {
@@ -399,8 +403,12 @@ inline auto f$curl_setopt_array(kphp::web::curl::easy_type easy_id, const array<
399403}
400404
401405inline auto f$curl_exec(kphp::web::curl::easy_type easy_id) noexcept -> kphp::coro::task<mixed> {
406+ auto & curl_state{CurlInstanceState::get ()};
407+ if (!curl_state.easy_ctx .has (easy_id)) {
408+ co_return false ;
409+ }
402410 auto res{co_await kphp::forks::id_managed (kphp::web::simple_transfer_perform (kphp::web::simple_transfer{easy_id}))};
403- auto & easy_ctx{CurlInstanceState::get () .easy_ctx .get_or_init (easy_id)};
411+ auto & easy_ctx{curl_state .easy_ctx .get_or_init (easy_id)};
404412 easy_ctx.has_been_executed = true ;
405413 if (!res.has_value ()) [[unlikely]] {
406414 easy_ctx.set_errno (res.error ().code , res.error ().description );
@@ -415,7 +423,11 @@ inline auto f$curl_exec(kphp::web::curl::easy_type easy_id) noexcept -> kphp::co
415423}
416424
417425inline auto f$curl_close(kphp::web::curl::easy_type easy_id) noexcept -> kphp::coro::task<void > {
418- auto & easy_ctx{CurlInstanceState::get ().easy_ctx .get_or_init (easy_id)};
426+ auto & curl_state{CurlInstanceState::get ()};
427+ if (!curl_state.easy_ctx .has (easy_id)) {
428+ co_return ;
429+ }
430+ auto & easy_ctx{curl_state.easy_ctx .get_or_init (easy_id)};
419431 auto res{co_await kphp::forks::id_managed (kphp::web::simple_transfer_close (kphp::web::simple_transfer{easy_id}))};
420432 if (!res.has_value ()) [[unlikely]] {
421433 easy_ctx.set_errno (res.error ().code , res.error ().description );
@@ -424,19 +436,26 @@ inline auto f$curl_close(kphp::web::curl::easy_type easy_id) noexcept -> kphp::c
424436}
425437
426438inline auto f$curl_reset(kphp::web::curl::easy_type easy_id) noexcept -> kphp::coro::task<void > {
427- auto & easy_ctx{CurlInstanceState::get ().easy_ctx .get_or_init (easy_id)};
439+ auto & curl_state{CurlInstanceState::get ()};
440+ if (!curl_state.easy_ctx .has (easy_id)) {
441+ co_return ;
442+ }
443+ auto & easy_ctx{curl_state.easy_ctx .get_or_init (easy_id)};
428444 auto res{co_await kphp::forks::id_managed (kphp::web::simple_transfer_reset (kphp::web::simple_transfer{easy_id}))};
429445 if (!res.has_value ()) [[unlikely]] {
430446 easy_ctx.set_errno (res.error ().code , res.error ().description );
431447 kphp::web::curl::print_error (" could not reset curl easy handle" , std::move (res.error ()));
432448 co_return ;
433449 }
434- easy_ctx.return_transfer = false ;
435- easy_ctx.private_data = std::nullopt ;
450+ easy_ctx.reset ();
436451}
437452
438453inline auto f$curl_exec_concurrently(kphp::web::curl::easy_type easy_id, double timeout_sec = 1.0 ) noexcept -> kphp::coro::task<Optional<string>> {
439- auto & easy_ctx{CurlInstanceState::get ().easy_ctx .get_or_init (easy_id)};
454+ auto & curl_state{CurlInstanceState::get ()};
455+ if (!curl_state.easy_ctx .has (easy_id)) {
456+ co_return false ;
457+ }
458+ auto & easy_ctx{curl_state.easy_ctx .get_or_init (easy_id)};
440459 auto sched_res{co_await kphp::coro::io_scheduler::get ().schedule (
441460 kphp::forks::id_managed (kphp::web::simple_transfer_perform (kphp::web::simple_transfer{easy_id})),
442461 kphp::web::curl::details::normalize_timeout (std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::duration<double >{timeout_sec})))};
@@ -457,7 +476,11 @@ inline auto f$curl_exec_concurrently(kphp::web::curl::easy_type easy_id, double
457476}
458477
459478inline auto f$curl_error(kphp::web::curl::easy_type easy_id) noexcept -> string {
460- auto & easy_ctx{CurlInstanceState::get ().easy_ctx .get_or_init (easy_id)};
479+ auto & curl_state{CurlInstanceState::get ()};
480+ if (!curl_state.easy_ctx .has (easy_id)) {
481+ return {};
482+ }
483+ auto & easy_ctx{curl_state.easy_ctx .get_or_init (easy_id)};
461484 if (easy_ctx.error_code != static_cast <int64_t >(kphp::web::curl::CURLE::OK)) [[likely]] {
462485 const auto * const desc_data{reinterpret_cast <const char *>(easy_ctx.error_description .data ())};
463486 const auto desc_size{static_cast <string::size_type>(easy_ctx.error_description .size ())};
@@ -467,16 +490,26 @@ inline auto f$curl_error(kphp::web::curl::easy_type easy_id) noexcept -> string
467490}
468491
469492inline auto f$curl_errno(kphp::web::curl::easy_type easy_id) noexcept -> int64_t {
470- auto & easy_ctx{CurlInstanceState::get ().easy_ctx .get_or_init (easy_id)};
493+ auto & curl_state{CurlInstanceState::get ()};
494+ if (!curl_state.easy_ctx .has (easy_id)) {
495+ return 0 ;
496+ }
497+ auto & easy_ctx{curl_state.easy_ctx .get_or_init (easy_id)};
471498 return easy_ctx.error_code ;
472499}
473500
474501inline auto f$curl_getinfo(kphp::web::curl::easy_type easy_id, int64_t option = 0 ) noexcept -> kphp::coro::task<mixed> {
475- auto & easy_ctx{CurlInstanceState::get ().easy_ctx .get_or_init (easy_id)};
502+ auto & curl_state{CurlInstanceState::get ()};
503+ if (!curl_state.easy_ctx .has (easy_id)) {
504+ co_return false ;
505+ }
506+ auto & easy_ctx{curl_state.easy_ctx .get_or_init (easy_id)};
476507 switch (static_cast <kphp::web::curl::CURLINFO>(option)) {
477508 case kphp::web::curl::CURLINFO::NONE: {
478- auto res{co_await kphp::forks::id_managed (kphp::web::get_transfer_properties (kphp::web::simple_transfer{easy_id}, std::nullopt ))};
509+ auto res{co_await kphp::forks::id_managed (
510+ kphp::web::get_transfer_properties (kphp::web::simple_transfer{easy_id}, std::nullopt , kphp::web::get_properties_policy::load))};
479511 if (!res.has_value ()) [[unlikely]] {
512+ easy_ctx.set_errno (res.error ().code , res.error ().description );
480513 kphp::web::curl::print_error (" could not get all info options of easy handle" , std::move (res.error ()));
481514 co_return false ;
482515 }
@@ -494,7 +527,8 @@ inline auto f$curl_getinfo(kphp::web::curl::easy_type easy_id, int64_t option =
494527
495528 if (!easy_ctx.has_been_executed ) {
496529 const auto url_opt_id{static_cast <kphp::web::property_id>(kphp::web::curl::CURLOPT::URL)};
497- const auto url{co_await kphp::forks::id_managed (kphp::web::get_transfer_properties (kphp::web::simple_transfer{easy_id}, url_opt_id))};
530+ const auto url{co_await kphp::forks::id_managed (
531+ kphp::web::get_transfer_properties (kphp::web::simple_transfer{easy_id}, url_opt_id, kphp::web::get_properties_policy::cached))};
498532 if (url.has_value ()) {
499533 const auto & v{(*url).find (url_opt_id)};
500534 kphp::log::assertion (v != (*url).end ());
@@ -538,7 +572,8 @@ inline auto f$curl_getinfo(kphp::web::curl::easy_type easy_id, int64_t option =
538572 case kphp::web::curl::CURLINFO::EFFECTIVE_URL:
539573 if (!easy_ctx.has_been_executed ) {
540574 const auto url_opt_id{static_cast <kphp::web::property_id>(kphp::web::curl::CURLOPT::URL)};
541- const auto url{co_await kphp::forks::id_managed (kphp::web::get_transfer_properties (kphp::web::simple_transfer{easy_id}, url_opt_id))};
575+ const auto url{co_await kphp::forks::id_managed (
576+ kphp::web::get_transfer_properties (kphp::web::simple_transfer{easy_id}, url_opt_id, kphp::web::get_properties_policy::cached))};
542577 if (url.has_value ()) {
543578 co_return (*url).find (url_opt_id)->second .to_mixed ();
544579 }
@@ -570,8 +605,10 @@ inline auto f$curl_getinfo(kphp::web::curl::easy_type easy_id, int64_t option =
570605 case kphp::web::curl::CURLINFO::CONDITION_UNMET:
571606 case kphp::web::curl::CURLINFO::NUM_CONNECTS:
572607 case kphp::web::curl::CURLINFO::HEADER_OUT: {
573- auto res{co_await kphp::forks::id_managed (kphp::web::get_transfer_properties (kphp::web::simple_transfer{easy_id}, option))};
608+ auto res{co_await kphp::forks::id_managed (
609+ kphp::web::get_transfer_properties (kphp::web::simple_transfer{easy_id}, option, kphp::web::get_properties_policy::load))};
574610 if (!res.has_value ()) [[unlikely]] {
611+ easy_ctx.set_errno (res.error ().code , res.error ().description );
575612 kphp::web::curl::print_error (" could not get a specific info of easy handle" , std::move (res.error ()));
576613 co_return 0 ;
577614 }
0 commit comments