--- parser3/src/classes/curl.C 2011/11/23 12:17:22 1.12 +++ parser3/src/classes/curl.C 2012/06/15 11:54:18 1.22 @@ -1,15 +1,11 @@ /** @file Parser: @b curl parser class. - Copyright(c) 2001-2009 ArtLebedev Group(http://www.artlebedev.com) + Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) */ #include "pa_config_includes.h" -#ifdef HAVE_CURL - -static const char * const IDENT_INET_C="$Date: 2011/11/23 12:17:22 $"; - #include "pa_vmethod_frame.h" #include "pa_request.h" #include "pa_vfile.h" @@ -20,6 +16,8 @@ static const char * const IDENT_INET_C=" #include "pa_http.h" #include "ltdl.h" +volatile const char * IDENT_CURL_C="$Id: curl.C,v 1.22 2012/06/15 11:54:18 moko Exp $"; + class MCurl: public Methoded { public: MCurl(); @@ -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 *f_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), f_stderr(0){} ~ParserOptions() { f_curl_formfree(f_post); + if(f_stderr) + fclose(f_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 *f_stderr=options().f_stderr=fopen(file_spec_cstr, "wt"); + if (f_stderr){ + res=f_curl_easy_setopt(curl(), opt->id, f_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:{ @@ -477,10 +493,8 @@ static void _curl_options(Request& r, Me if(curl_options==0) curl_options=new CurlOptionHash(); - if(HashStringValue* options=params.as_no_junction(0, OPTIONS_MUST_NOT_BE_CODE).get_hash()){ + if(HashStringValue* options=params.as_hash(0)) options->for_each(curl_setopt, r); - } else - throw Exception("curl", 0, OPTIONS_MUST_BE_HASH); } @@ -546,7 +560,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; @@ -584,11 +598,8 @@ static void _curl_load_action(Request& r body.length=c.length; } - result.set(true /*tainted*/, body.buf, body.length, options().filename - , options().content_type ? new VString(*options().content_type) : 0 - , &r); - result.set_mode(options().is_text); - + result.set(true/*tainted*/, options().is_text, body.buf, body.length, options().filename + , options().content_type ? new VString(*options().content_type) : 0, &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)); @@ -618,11 +629,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