--- parser3/src/classes/curl.C 2020/10/14 21:20:15 1.63 +++ parser3/src/classes/curl.C 2024/09/28 14:37:53 1.73 @@ -1,7 +1,8 @@ /** @file Parser: @b curl parser class. - Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev */ #include "pa_config_includes.h" @@ -17,7 +18,7 @@ #include "pa_http.h" #include "ltdl.h" -volatile const char * IDENT_CURL_C="$Id: curl.C,v 1.63 2020/10/14 21:20:15 moko Exp $"; +volatile const char * IDENT_CURL_C="$Id: curl.C,v 1.73 2024/09/28 14:37:53 moko Exp $"; class MCurl: public Methoded { public: @@ -52,7 +53,7 @@ static const char *dlink(const char *dlo if(!handle){ if(const char* result=lt_dlerror()) return result; - return "can not open the dynamic link module"; + return "cannot open the dynamic link module"; } DLINK(curl_easy_init); @@ -116,10 +117,14 @@ static ParserOptions &options(){ class Temp_curl { CURL *saved_curl; ParserOptions *saved_options; + + // every TLS should be referenced elsewhere, or GC will collect it + CURL *thread_curl; + ParserOptions *thread_options; public: Temp_curl() : saved_curl(fcurl), saved_options(foptions){ - fcurl = f_curl_easy_init(); - foptions = new ParserOptions(); + thread_curl = fcurl = f_curl_easy_init(); + thread_options = foptions = new ParserOptions(); f_curl_easy_setopt(fcurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); // avoid ipv6 by default } @@ -144,7 +149,8 @@ static void temp_curl(void (*action)(Req Temp_curl temp_curl; action(r,params); } else { - throw Exception("curl", 0, "failed to load curl library %s: %s", curl_library, curl_status); + const char *hint=strcmp(curl_library, "libcurl" LT_MODULE_EXT) ? "" : " (at first call ^curl:options[ $.library[correct.libcurl" LT_MODULE_EXT ".name] ])"; + throw Exception("curl", 0, "failed to load curl library %s%s", curl_status, hint); } } @@ -393,7 +399,7 @@ static void curl_form(HashStringValue *v CURLFORM_PTRCONTENTS, curl_transcode(String(tvalue->get(t)->get(0)->cstr()), r), CURLFORM_END); } - } else if(VFile* fvalue=static_cast(i.value()->as("file"))){ + } else if(VFile* fvalue=dynamic_cast(i.value())){ // file f_curl_formadd(&options().f_post, &f_last, CURLFORM_PTRNAME, key, @@ -495,7 +501,7 @@ static void curl_setopt(HashStringValue: if( (res=f_curl_easy_setopt(curl(), CURLOPT_POSTFIELDSIZE, -1L)) == CURLE_OK ) res=f_curl_easy_setopt(curl(), opt->id, curl_urlencode(v.as_string(), r)); } else { - VFile *file=v.as_vfile(String::L_AS_IS); + VFile *file=v.as_vfile(); if( (res=f_curl_easy_setopt(curl(), CURLOPT_POSTFIELDSIZE, (long)file->value_size())) == CURLE_OK ) res=f_curl_easy_setopt(curl(), opt->id, file->value_ptr()); } @@ -523,13 +529,13 @@ static void curl_setopt(HashStringValue: } case CurlOption::CURL_FILE:{ // file-spec curl option - const char *file_spec_cstr=curl_check_file(r.absolute(v.as_string())); + const char *file_spec_cstr=curl_check_file(r.full_disk_path(v.as_string())); res=f_curl_easy_setopt(curl(), opt->id, file_spec_cstr); break; } case CurlOption::CURL_STDERR:{ // verbose output redirection from stderr to file curl option - const char *file_spec_cstr=curl_check_file(r.absolute(v.as_string())); + const char *file_spec_cstr=curl_check_file(r.full_disk_path(v.as_string())); FILE *f_stderr=options().f_stderr=pa_fopen(file_spec_cstr, "wt"); if (f_stderr){ res=f_curl_easy_setopt(curl(), opt->id, f_stderr); @@ -744,7 +750,7 @@ static void _curl_load_action(Request& r case CURLE_SSL_ENGINE_INITFAILED: ex_type = "curl.ssl"; break; case CURLE_WRITE_ERROR: - check_file_size(response.content_length, *new String(options().url)); break; + check_file_size(response.content_length, new String(options().url)); break; default: break; } throw Exception( PA_DEFAULT(ex_type, "curl.fail"), new String(options().url), "%s", f_curl_easy_strerror(res)); @@ -784,7 +790,7 @@ static void _curl_load_action(Request& r VHash* vtables=new VHash; result.fields().put("tables", vtables); - for(Array_iterator i(response.headers); i.has_next(); ){ + for(Array_iterator i(response.headers); i; ){ HTTP_Headers::Header header=i.next(); if(asked_charset)