--- parser3/src/classes/curl.C 2020/11/10 22:42:24 1.65 +++ parser3/src/classes/curl.C 2025/05/26 00:52:15 1.76 @@ -1,7 +1,8 @@ /** @file Parser: @b curl parser class. - Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2024 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.65 2020/11/10 22:42:24 moko Exp $"; +volatile const char * IDENT_CURL_C="$Id: curl.C,v 1.76 2025/05/26 00:52:15 moko Exp $"; class MCurl: public Methoded { public: @@ -44,15 +45,20 @@ typedef void (*t_curl_formfree)(struct c #define GLINK(name) f_##name=(t_##name)lt_dlsym(handle, #name); #define DLINK(name) GLINK(name) if(!f_##name) return "function " #name " was not found"; -static const char *dlink(const char *dlopen_file_spec) { +static const char *dlink(char *dlopen_file_spec) { pa_dlinit(); - lt_dlhandle handle=lt_dlopen(dlopen_file_spec); + lt_dlhandle handle; + do { + char *next=lsplit(dlopen_file_spec, ','); + handle=lt_dlopen(dlopen_file_spec); + dlopen_file_spec=next; + } while (!handle && dlopen_file_spec); 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 +122,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 } @@ -131,20 +141,28 @@ public: } }; +#ifdef WIN32 +#define CURL_LIBRARY "libcurl" LT_MODULE_EXT +#else +#define CURL_LIBRARY "libcurl" LT_MODULE_EXT ",libcurl" LT_MODULE_EXT ".4" +#endif + bool curl_linked = false; const char *curl_status = 0; -const char *curl_library="libcurl" LT_MODULE_EXT; + +const char *curl_library=CURL_LIBRARY; static void temp_curl(void (*action)(Request&, MethodParams&), Request& r, MethodParams& params){ if(!curl_linked) - curl_status=dlink(curl_library); + curl_status=dlink(pa_strdup(curl_library)); if(curl_status == 0){ curl_linked=true; 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, CURL_LIBRARY) ? "" : " (before use, 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 +411,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 +513,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()); } @@ -548,6 +566,8 @@ static void curl_setopt(HashStringValue: // 'library' parser option if(!curl_linked){ curl_library=v.as_string().taint_cstr(String::L_FILE_SPEC); + if(!curl_library[0]) + curl_library=CURL_LIBRARY; } else throw Exception("curl", 0, "failed to set option '%s': already loaded", key.cstr()); break; @@ -778,13 +798,13 @@ static void _curl_load_action(Request& r long http_status = 0; if(f_curl_easy_getinfo(curl(), CURLINFO_RESPONSE_CODE, &http_status) == CURLE_OK){ - result.fields().put("status", new VInt(http_status)); + HASH_PUT_CSTR(result.fields(), "status", new VInt(http_status)); } VHash* vtables=new VHash; - result.fields().put("tables", vtables); + HASH_PUT_CSTR(result.fields(), "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) @@ -798,7 +818,7 @@ static void _curl_load_action(Request& r // filling $.cookies if(Value *vcookies=vtables->hash().get("SET-COOKIE")) - result.fields().put(HTTP_COOKIES_NAME, new VTable(parse_cookies(r, vcookies->get_table()))); + HASH_PUT_CSTR(result.fields(), HTTP_COOKIES_NAME, new VTable(parse_cookies(r, vcookies->get_table()))); r.write(result); }