--- parser3/src/classes/curl.C 2012/03/16 09:24:06 1.13 +++ parser3/src/classes/curl.C 2012/04/22 20:03:02 1.18 @@ -6,8 +6,6 @@ #include "pa_config_includes.h" -#ifdef HAVE_CURL - #include "pa_vmethod_frame.h" #include "pa_request.h" #include "pa_vfile.h" @@ -18,7 +16,7 @@ #include "pa_http.h" #include "ltdl.h" -volatile const char * IDENT_CURL_C="$Id: curl.C,v 1.13 2012/03/16 09:24:06 moko Exp $"; +volatile const char * IDENT_CURL_C="$Id: curl.C,v 1.18 2012/04/22 20:03:02 moko Exp $"; class MCurl: public Methoded { public: @@ -45,7 +43,7 @@ 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"; -const char *dlink(const char *dlopen_file_spec) { +static const char *dlink(const char *dlopen_file_spec) { if(lt_dlinit()) return lt_dlerror(); @@ -78,10 +76,13 @@ public: bool is_text; Charset *charset, *response_charset; struct curl_httppost *f_post; + FILE *stderr; - ParserOptions() : filename(0), content_type(0), is_text(true), charset(0), response_charset(0), f_post(0){} + ParserOptions() : filename(0), content_type(0), is_text(true), charset(0), response_charset(0), f_post(0), stderr(0){} ~ParserOptions() { f_curl_formfree(f_post); + if(stderr) + fclose(stderr); } }; @@ -186,6 +187,7 @@ public: CURL_FORM, CURL_HEADERS, CURL_FILE, + CURL_STDERR, PARSER_LIBRARY, PARSER_NAME, PARSER_CONTENT_TYPE, @@ -209,13 +211,15 @@ public: CURL_OPT(CURL_INT, LOCALPORT); CURL_OPT(CURL_INT, PORT); + CURL_OPT(CURL_INT, VERBOSE); + CURL_OPT(CURL_STDERR, STDERR); + CURL_OPT(CURL_INT, MAXFILESIZE); + CURL_OPT(CURL_INT, HTTPAUTH); CURL_OPT(CURL_STRING, USERPWD); -#ifdef CURLOPT_USERNAME CURL_OPT(CURL_STRING, USERNAME); CURL_OPT(CURL_STRING, PASSWORD); -#endif CURL_OPT(CURL_URLENCODE, USERAGENT); CURL_OPT(CURL_URLENCODE, REFERER); @@ -243,9 +247,7 @@ public: CURL_OPT(CURL_INT, HTTP_TRANSFER_DECODING); CURL_OPT(CURL_INT, MAXREDIRS); -#ifdef CURLOPT_POSTREDIR CURL_OPT(CURL_INT, POSTREDIR); -#endif CURL_OPT(CURL_STRING, RANGE); @@ -276,16 +278,11 @@ public: CURL_OPT(CURL_STRING, SSLENGINE); CURL_OPT(CURL_STRING, SSLENGINE_DEFAULT); -#ifdef CURLOPT_ISSUERCERT CURL_OPT(CURL_FILE, ISSUERCERT); -#endif - -#ifdef CURLOPT_CRLFILE CURL_OPT(CURL_FILE, CRLFILE); -#endif CURL_OPT(CURL_STRING, CAINFO); - CURL_OPT(CURL_STRING, CAPATH); + CURL_OPT(CURL_FILE, CAPATH); CURL_OPT(CURL_INT, SSL_VERIFYPEER); CURL_OPT(CURL_INT, SSL_VERIFYHOST); CURL_OPT(CURL_STRING, SSL_CIPHER_LIST); @@ -360,6 +357,14 @@ static void curl_form(HashStringValue *v } } +static const char *curl_check_file(const String &file_spec){ + const char *file_spec_cstr=file_spec.taint_cstr(String::L_FILE_SPEC); + struct stat finfo; + if(stat(file_spec_cstr, &finfo)==0) + check_safe_mode(finfo, file_spec, file_spec_cstr); + return file_spec_cstr; +} + static void curl_setopt(HashStringValue::key_type key, HashStringValue::value_type value, Request& r) { CurlOption *opt=curl_options->get(key); @@ -417,7 +422,7 @@ static void curl_setopt(HashStringValue: f_curl_formfree(options().f_post); options().f_post = 0; } else { - throw Exception("curl", 0, "%s must be a hash", key.cstr()); + throw Exception("curl", 0, "failed to set option '%s': value must be a hash", key.cstr()); } res=f_curl_easy_setopt(curl(), CURLOPT_HTTPPOST, foptions->f_post); break; @@ -430,8 +435,19 @@ static void curl_setopt(HashStringValue: } case CurlOption::CURL_FILE:{ // file-spec curl option - const char *value_str=r.absolute(v.as_string()).taint_cstr(String::L_FILE_SPEC); - res=f_curl_easy_setopt(curl(), opt->id, value_str); + const char *file_spec_cstr=curl_check_file(r.absolute(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 *stderr=options().stderr=fopen(file_spec_cstr, "wt"); + if (stderr){ + res=f_curl_easy_setopt(curl(), opt->id, stderr); + } else { + throw Exception("curl", 0, "failed to set option '%s': unable to open file '%s'", key.cstr(), file_spec_cstr); + } break; } case CurlOption::PARSER_LIBRARY:{ @@ -439,7 +455,7 @@ static void curl_setopt(HashStringValue: if(fcurl==0){ curl_library=v.as_string().taint_cstr(String::L_FILE_SPEC); } else - throw Exception("curl", 0, "failed to set option '%s': %s", key.cstr(), "already loaded"); + throw Exception("curl", 0, "failed to set option '%s': already loaded", key.cstr()); break; } case CurlOption::PARSER_NAME:{ @@ -546,7 +562,7 @@ static void _curl_load_action(Request& r CURL_SETOPT(CURLOPT_WRITEHEADER, &headers, "curl header buffer"); if((res=f_curl_easy_perform(curl())) != CURLE_OK){ - char *ex_type = 0; + const char *ex_type = 0; switch(res){ case CURLE_OPERATION_TIMEDOUT: ex_type = "curl.timeout"; break; @@ -618,11 +634,3 @@ MCurl::MCurl(): Methoded("curl") { add_native_method("options", Method::CT_STATIC, _curl_options, 1, 1); add_native_method("load", Method::CT_STATIC, _curl_load, 0, 1); } - -#else // HAVE_CURL - -#include "classes.h" -// global variable -DECLARE_CLASS_VAR(curl, 0, 0); // fictive - -#endif // HAVE_CURL