--- parser3/src/classes/curl.C 2017/11/26 21:24:07 1.56 +++ parser3/src/classes/curl.C 2017/11/29 19:05:47 1.58 @@ -17,7 +17,7 @@ #include "pa_http.h" #include "ltdl.h" -volatile const char * IDENT_CURL_C="$Id: curl.C,v 1.56 2017/11/26 21:24:07 moko Exp $"; +volatile const char * IDENT_CURL_C="$Id: curl.C,v 1.58 2017/11/29 19:05:47 moko Exp $"; class MCurl: public Methoded { public: @@ -81,7 +81,11 @@ struct ParserOptions : public PA_Allocat 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){} + // stuff to walkaround curl 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), is_post(false), has_content_length(false){} ~ParserOptions() { f_curl_formfree(f_post); if(f_stderr) @@ -114,8 +118,6 @@ public: Temp_curl() : saved_curl(fcurl), saved_options(foptions){ fcurl = f_curl_easy_init(); foptions = new ParserOptions(); - if(curl_is_old_and_buggy(f_curl_version())) - f_curl_easy_setopt(fcurl, CURLOPT_POSTFIELDSIZE, 0); // fix libcurl bug f_curl_easy_setopt(fcurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); // avoid ipv6 by default } @@ -125,11 +127,6 @@ public: delete foptions; foptions = saved_options; } - - static bool curl_is_old_and_buggy(const char *version){ - if(strncmp(version,"libcurl/7.",10)) return false; - return atoi(version+10)<38; // 7.38 in Debian Jessie is first known non-buggy version - } }; bool curl_linked = false; @@ -174,6 +171,7 @@ struct CurlOption : public PA_Allocated{ CURL_URL, CURL_INT, CURL_POST, + CURL_POSTFIELDS, CURL_FORM, CURL_HEADERS, CURL_FILE, @@ -223,13 +221,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_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); @@ -340,7 +338,6 @@ public: CURL_INF(CURL_DOUBLE, STARTTRANSFER_TIME); CURL_INF(CURL_DOUBLE, TOTAL_TIME); CURL_INF(CURL_HTTP_VERSION, HTTP_VERSION); - CURL_INF(CURL_INT, PROTOCOL); CURL_INF(CURL_STRING, SCHEME); } @@ -473,6 +470,13 @@ static void curl_setopt(HashStringValue: 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 ) @@ -482,6 +486,7 @@ static void curl_setopt(HashStringValue: 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:{ @@ -489,10 +494,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:{ @@ -578,13 +585,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:{ @@ -616,17 +622,21 @@ 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)); } 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(), true); + if(value) + result.get_hash()->put(i.key(), value); } r.write(result); } } - class Curl_buffer{ public: char *buf; @@ -696,6 +706,12 @@ 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(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"); + } + if((res=f_curl_easy_perform(curl())) != CURLE_OK){ const char *ex_type = 0; switch(res){