--- parser3/src/classes/curl.C 2017/01/28 15:26:50 1.52 +++ parser3/src/classes/curl.C 2026/04/25 13:38:46 1.78 @@ -1,7 +1,8 @@ /** @file Parser: @b curl parser class. - Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2026 Art. Lebedev Studio (https://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.52 2017/01/28 15:26:50 moko Exp $"; +volatile const char * IDENT_CURL_C="$Id: curl.C,v 1.78 2026/04/25 13:38:46 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); @@ -69,8 +75,7 @@ static const char *dlink(const char *dlo } -class ParserOptions { -public: +struct ParserOptions : public PA_Allocated { // real options const String *filename; const String *content_type; @@ -82,13 +87,19 @@ public: struct curl_httppost *f_post; FILE *f_stderr; - ParserOptions() : filename(0), content_type(0), is_text(true), charset(0), response_charset(0), url(0), f_post(0), f_stderr(0){} + // if response content-length check required + bool no_body; + // stuff to walkaround curl request content-length bugs + bool is_post; + bool has_content_length; + + ParserOptions() : filename(0), content_type(0), is_text(true), charset(0), response_charset(0), url(0), f_post(0), f_stderr(0), no_body(false), is_post(false), has_content_length(false){} ~ParserOptions() { f_curl_formfree(f_post); if(f_stderr) fclose(f_stderr); } - + }; // using TLS instead of keeping variables in request @@ -111,13 +122,17 @@ 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(); - f_curl_easy_setopt(fcurl, CURLOPT_POSTFIELDSIZE, 0); // fix libcurl bug + 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 } + ~Temp_curl() { f_curl_easy_cleanup(fcurl); fcurl = saved_curl; @@ -126,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); } } @@ -153,26 +176,28 @@ static void _curl_session(Request& r, Me } static void _curl_version_action(Request& r, MethodParams& ){ - r.write(*new VString(*new String(f_curl_version(), String::L_TAINTED))); + r.write(*new VString(f_curl_version())); } static void _curl_version(Request& r, MethodParams& params){ fcurl ? _curl_version_action(r, params) : temp_curl(_curl_version_action, r, params); } -class CurlOption { -public: +struct CurlOption : public PA_Allocated{ enum OptionType { CURL_STRING, CURL_URLENCODE, // url-encoded string CURL_URL, CURL_INT, + CURL_NO_BODY, CURL_POST, + CURL_POSTFIELDS, CURL_FORM, CURL_HEADERS, CURL_FILE, CURL_STDERR, + CURL_HTTP_VERSION, PARSER_LIBRARY, PARSER_NAME, PARSER_CONTENT_TYPE, @@ -217,13 +242,13 @@ public: CURL_OPT(CURL_INT, UNRESTRICTED_AUTH); CURL_OPT(CURL_INT, IPRESOLVE); - CURL_OPT(CURL_INT, POST); + CURL_OPT(CURL_POST, POST); CURL_OPT(CURL_INT, HTTPGET); - CURL_OPT(CURL_INT, NOBODY); + CURL_OPT(CURL_NO_BODY, NOBODY); CURL_OPT(CURL_STRING, CUSTOMREQUEST); - CURL_OPT(CURL_POST, POSTFIELDS); // hopefully is safe too - CURL_OPT(CURL_POST, COPYPOSTFIELDS); + CURL_OPT(CURL_POSTFIELDS, POSTFIELDS); // hopefully is safe too + CURL_OPT(CURL_POSTFIELDS, COPYPOSTFIELDS); CURL_OPT(CURL_FORM, HTTPPOST); CURL_OPT(CURL_HEADERS, HTTPHEADER); @@ -277,6 +302,7 @@ public: CURL_OPT(CURL_STRING, SSL_CIPHER_LIST); CURL_OPT(CURL_INT, SSL_SESSIONID_CACHE); CURL_OPT(CURL_INT, SSLVERSION); + CURL_OPT(CURL_HTTP_VERSION, HTTP_VERSION); PARSER_OPT(PARSER_LIBRARY, "library"); PARSER_OPT(PARSER_NAME, "name"); @@ -288,13 +314,13 @@ public: } *curl_options=0; -class CurlInfo { -public: +struct CurlInfo : public PA_Allocated{ enum OptionType { CURL_STRING, CURL_INT, - CURL_DOUBLE + CURL_DOUBLE, + CURL_HTTP_VERSION }; CURLINFO id; @@ -302,36 +328,41 @@ public: CurlInfo(CURLINFO aid, OptionType atype): id(aid), type(atype) {} }; -class CurlInfoHash: public HashString { +class CurlInfoHash: public OrderedHashString { public: CurlInfoHash() { #define CURL_INF(type, name) put(str_lower(#name),new CurlInfo(CURLINFO_##name, CurlInfo::type)); + CURL_INF(CURL_STRING, SCHEME); + CURL_INF(CURL_HTTP_VERSION, HTTP_VERSION); + CURL_INF(CURL_STRING, EFFECTIVE_URL); + CURL_INF(CURL_STRING, CONTENT_TYPE); + CURL_INF(CURL_INT, RESPONSE_CODE); + CURL_INF(CURL_INT, OS_ERRNO); + + CURL_INF(CURL_DOUBLE, NAMELOOKUP_TIME); CURL_INF(CURL_DOUBLE, APPCONNECT_TIME); + CURL_INF(CURL_DOUBLE, PRETRANSFER_TIME); + CURL_INF(CURL_DOUBLE, STARTTRANSFER_TIME); CURL_INF(CURL_DOUBLE, CONNECT_TIME); + CURL_INF(CURL_DOUBLE, TOTAL_TIME); + CURL_INF(CURL_DOUBLE, CONTENT_LENGTH_DOWNLOAD); CURL_INF(CURL_DOUBLE, CONTENT_LENGTH_UPLOAD); - CURL_INF(CURL_STRING, CONTENT_TYPE); - CURL_INF(CURL_STRING, EFFECTIVE_URL); CURL_INF(CURL_INT, HEADER_SIZE); - CURL_INF(CURL_INT, HTTPAUTH_AVAIL); - CURL_INF(CURL_DOUBLE, NAMELOOKUP_TIME); + CURL_INF(CURL_INT, REQUEST_SIZE); + CURL_INF(CURL_DOUBLE, SIZE_DOWNLOAD); + CURL_INF(CURL_DOUBLE, SIZE_UPLOAD); + CURL_INF(CURL_DOUBLE, SPEED_DOWNLOAD); + CURL_INF(CURL_DOUBLE, SPEED_UPLOAD); + CURL_INF(CURL_INT, NUM_CONNECTS); - CURL_INF(CURL_INT, OS_ERRNO); - CURL_INF(CURL_DOUBLE, PRETRANSFER_TIME); CURL_INF(CURL_STRING, PRIMARY_IP); + CURL_INF(CURL_INT, HTTPAUTH_AVAIL); CURL_INF(CURL_INT, PROXYAUTH_AVAIL); CURL_INF(CURL_INT, REDIRECT_COUNT); CURL_INF(CURL_DOUBLE, REDIRECT_TIME); CURL_INF(CURL_STRING, REDIRECT_URL); - CURL_INF(CURL_INT, REQUEST_SIZE); - CURL_INF(CURL_INT, RESPONSE_CODE); - CURL_INF(CURL_DOUBLE, SIZE_DOWNLOAD); - CURL_INF(CURL_DOUBLE, SIZE_UPLOAD); - CURL_INF(CURL_DOUBLE, SPEED_DOWNLOAD); - CURL_INF(CURL_DOUBLE, SPEED_UPLOAD); CURL_INF(CURL_INT, SSL_VERIFYRESULT); - CURL_INF(CURL_DOUBLE, STARTTRANSFER_TIME); - CURL_INF(CURL_DOUBLE, TOTAL_TIME); } } *curl_infos=0; @@ -380,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, @@ -390,7 +421,7 @@ static void curl_form(HashStringValue *v CURLFORM_CONTENTTYPE, fvalue->fields().get("content-type")->as_string().taint_cstr(String::L_URI), CURLFORM_END); } else { - throw Exception("curl", new String(i.key(), String::L_TAINTED), "is %s, form option value can be string, table or file only", i.value()->type()); + throw Exception("curl", new String(i.key(), String::L_TAINTED), "is %s, form option value can be string, table or file only", i.value()->type()); } } } @@ -403,6 +434,28 @@ static const char *curl_check_file(const return file_spec_cstr; } +static long curl_http_version(const String &name){ + if(name.is_empty()) return CURL_HTTP_VERSION_NONE; + + if(name == "1.0") return CURL_HTTP_VERSION_1_0; + if(name == "1.1") return CURL_HTTP_VERSION_1_1; + if(name == "2") return CURL_HTTP_VERSION_2; + if(name == "2.0") return CURL_HTTP_VERSION_2_0; + + const char *sname = str_upper(name.cstr()); + if(!strcmp(sname,"2TLS")) return CURL_HTTP_VERSION_2TLS; + if(!strcmp(sname,"2ONLY")) return CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE; + throw Exception("curl", &name, "invalid http_version option value"); +} + +static const char *curl_http_version_name(long value){ + if(value == CURL_HTTP_VERSION_NONE) return "none"; + if(value == CURL_HTTP_VERSION_1_0) return "1.0"; + if(value == CURL_HTTP_VERSION_1_1) return "1.1"; + if(value == CURL_HTTP_VERSION_2) return "2"; + throw Exception("curl", 0, "invalid http version '%d' in info", value); +} + static void curl_setopt(HashStringValue::key_type key, HashStringValue::value_type value, Request& r) { CurlOption *opt=curl_options->get(key); @@ -440,16 +493,31 @@ static void curl_setopt(HashStringValue: res=f_curl_easy_setopt(curl(), opt->id, value_int); break; } + case CurlOption::CURL_NO_BODY:{ + // integer curl option + long value_int=(long)v.as_double(); + res=f_curl_easy_setopt(curl(), opt->id, value_int); + options().no_body=value_int != 0; + break; + } case CurlOption::CURL_POST:{ + // integer curl option + long value_int=(long)v.as_double(); + res=f_curl_easy_setopt(curl(), opt->id, value_int); + options().is_post=value_int != 0; + break; + } + case CurlOption::CURL_POSTFIELDS:{ // http post curl option if(v.get_string()){ 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()); } + options().has_content_length=true; break; } case CurlOption::CURL_FORM:{ @@ -457,10 +525,12 @@ static void curl_setopt(HashStringValue: if(value_hash){ curl_form(value_hash, r); } else { - f_curl_formfree(options().f_post); + if(options().f_post) + f_curl_formfree(options().f_post); options().f_post = 0; } res=f_curl_easy_setopt(curl(), CURLOPT_HTTPPOST, foptions->f_post); + options().has_content_length=true; break; } case CurlOption::CURL_HEADERS:{ @@ -471,14 +541,14 @@ 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())); - FILE *f_stderr=options().f_stderr=fopen(file_spec_cstr, "wt"); + 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); } else { @@ -486,10 +556,18 @@ static void curl_setopt(HashStringValue: } break; } + case CurlOption::CURL_HTTP_VERSION:{ + // http protocol version name curl option + long value_int=curl_http_version(v.as_string()); + res=f_curl_easy_setopt(curl(), opt->id, value_int); + break; + } case CurlOption::PARSER_LIBRARY:{ // '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; @@ -540,13 +618,12 @@ static void _curl_options(Request& r, Me #define CURL_GETINFO(arg) \ if((res=f_curl_easy_getinfo(curl(), info->id, &arg)) != CURLE_OK){ \ - throw Exception("curl", 0, "failed to get %s info: %s", key.cstr(), f_curl_easy_strerror(res)); \ + if (fail_on_error) \ + throw Exception("curl", 0, "failed to get %s info: %s", key.cstr(), f_curl_easy_strerror(res)); \ + return 0; \ } -static Value *curl_getinfo(const String::Body &key, CurlInfo *info=0) { - if(info==0 && !(info=curl_infos->get(key))) - throw Exception("curl", 0, "called with invalid parameter '%s'", key.cstr()); - +static Value *curl_getinfo(const String::Body &key, CurlInfo *info, bool fail_on_error=false) { CURLcode res; switch (info->type){ case CurlInfo::CURL_STRING:{ @@ -564,6 +641,11 @@ static Value *curl_getinfo(const String: CURL_GETINFO(d); return new VDouble(d); } + case CurlInfo::CURL_HTTP_VERSION:{ + long l=0; + CURL_GETINFO(l); + return new VString(curl_http_version_name(l)); + } } return VVoid::get(); } @@ -573,25 +655,29 @@ static void _curl_info(Request& r, Metho curl_infos=new CurlInfoHash(); if(params.count()==1){ const String &name=params.as_string(0, "name must be string"); - r.write(*curl_getinfo(name)); + CurlInfo *info=curl_infos->get(name); + if(info==0) + throw Exception("curl", 0, "called with invalid parameter '%s'", name.cstr()); + r.write(*curl_getinfo(name, info, true)); } else { VHash& result=*new VHash; for(CurlInfoHash::Iterator i(*curl_infos); i; i.next() ){ - result.get_hash()->put(i.key(), curl_getinfo(i.key(), i.value())); + Value *value=curl_getinfo(i.key(), i.value()); + if(value) + result.get_hash()->put(i.key(), value); } r.write(result); } } - class Curl_buffer{ public: char *buf; size_t length; size_t buf_size; - ResponseHeaders& headers; + HTTP_Headers& headers; - Curl_buffer(ResponseHeaders& aheaders) : buf((char *)pa_malloc_atomic(MAX_STRING)), length(0), buf_size(MAX_STRING-1), headers(aheaders){} + Curl_buffer(HTTP_Headers& aheaders) : buf((char *)pa_malloc_atomic(MAX_STRING)), length(0), buf_size(MAX_STRING-1), headers(aheaders){} void resize(size_t size){ buf_size=size; @@ -614,7 +700,7 @@ static int curl_writer(char *data, size_ return size; } -static int curl_header(char *data, size_t size, size_t nmemb, ResponseHeaders *result){ +static int curl_header(char *data, size_t size, size_t nmemb, HTTP_Headers *result){ if(result == 0) return 0; @@ -626,7 +712,7 @@ static int curl_header(char *data, size_ result->clear(); } else { result->add_header(header); - if(result->content_length>pa_file_size_limit) + if(result->content_length>pa_file_size_limit && !options().no_body) return 0; } } @@ -645,7 +731,7 @@ static void _curl_load_action(Request& r CURLcode res; // we need a container for headers as VFile fields can be put only after VFile.set - ResponseHeaders response; + HTTP_Headers response; CURL_SETOPT(CURLOPT_HEADERFUNCTION, curl_header, "curl header function"); CURL_SETOPT(CURLOPT_WRITEHEADER, &response, "curl header buffer"); @@ -653,7 +739,14 @@ static void _curl_load_action(Request& r CURL_SETOPT(CURLOPT_WRITEFUNCTION, curl_writer, "curl writer function"); CURL_SETOPT(CURLOPT_WRITEDATA, &body, "curl write buffer"); - if((res=f_curl_easy_perform(curl())) != CURLE_OK){ + if(options().is_post && !options().has_content_length){ + // libcurl bug walkaround. Prior to 7.38 (Debian Jessie) curl passed Content-length: -1 + // after that no Content-length header is passed, that hangs request to nginx. + CURL_SETOPT(CURLOPT_POSTFIELDSIZE, 0, "post content-length"); + } + + ALTER_EXCEPTION_SOURCE(res=f_curl_easy_perform(curl()), new String(options().url)); + if(res != CURLE_OK){ const char *ex_type = 0; switch(res){ case CURLE_OPERATION_TIMEDOUT: @@ -671,10 +764,10 @@ 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"), 0, "%s", f_curl_easy_strerror(res)); + throw Exception( PA_DEFAULT(ex_type, "curl.fail"), new String(options().url), "%s", f_curl_easy_strerror(res)); } // assure trailing zero @@ -705,14 +798,14 @@ 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(); ){ - ResponseHeaders::Header header=i.next(); + for(Array_iterator i(response.headers); i; ){ + HTTP_Headers::Header header=i.next(); if(asked_charset) header.transcode(*asked_charset, r.charsets.source()); @@ -725,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); }