--- parser3/src/classes/curl.C 2011/01/07 23:57:04 1.11 +++ parser3/src/classes/curl.C 2012/04/20 20:02:03 1.16 @@ -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/01/07 23:57:04 $"; - #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.16 2012/04/20 20:02:03 moko Exp $"; + class MCurl: public Methoded { public: MCurl(); @@ -29,9 +27,6 @@ public: DECLARE_CLASS_VAR(curl, new MCurl, 0); -// from file.C -extern bool is_text_mode(const String& mode); - #include "curl.h" typedef CURL *(*t_curl_easy_init)(); t_curl_easy_init f_curl_easy_init; @@ -48,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(); @@ -76,15 +71,18 @@ const char *dlink(const char *dlopen_fil class ParserOptions { public: - const char *filename; + const String *filename; const String *content_type; 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); } }; @@ -189,6 +187,7 @@ public: CURL_FORM, CURL_HEADERS, CURL_FILE, + CURL_STDERR, PARSER_LIBRARY, PARSER_NAME, PARSER_CONTENT_TYPE, @@ -212,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); @@ -246,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); @@ -279,13 +278,8 @@ 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); @@ -420,7 +414,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; @@ -437,17 +431,28 @@ static void curl_setopt(HashStringValue: res=f_curl_easy_setopt(curl(), opt->id, value_str); break; } + case CurlOption::CURL_STDERR:{ + // verbose output redirection from stderr to file curl option + const char *value_str=r.absolute(v.as_string()).taint_cstr(String::L_FILE_SPEC); + FILE *stderr=options().stderr=fopen(value_str, "at"); + 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(), value_str); + } + break; + } case CurlOption::PARSER_LIBRARY:{ // 'library' parser option 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:{ // 'name' parser option - options().filename=v.as_string().taint_cstr(String::L_FILE_SPEC); + options().filename=&v.as_string(); break; } case CurlOption::PARSER_CONTENT_TYPE:{ @@ -457,7 +462,7 @@ static void curl_setopt(HashStringValue: } case CurlOption::PARSER_MODE:{ // 'mode' parser option - options().is_text=is_text_mode(v.as_string()); + options().is_text=VFile::is_text_mode(v.as_string()); break; } case CurlOption::PARSER_CHARSET:{ @@ -466,7 +471,7 @@ static void curl_setopt(HashStringValue: break; } case CurlOption::PARSER_RESPONSE_CHARSET:{ - // 'charset' parser option + // 'response-charset' parser option options().response_charset=&::charsets.get(v.as_string().change_case(r.charsets.source(), String::CC_UPPER)); break; } @@ -549,7 +554,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; @@ -572,10 +577,6 @@ static void _curl_load_action(Request& r // assure trailing zero body.buf[body.length]=0; - Value* vcontent_type= - options().content_type ? new VString(*options().content_type) : - options().filename ? new VString(r.mime_type_of(options().filename)) : 0; - VFile& result=*new VFile; String::Body ct_header = headers.get(HTTP_CONTENT_TYPE_UPPER); @@ -591,7 +592,9 @@ static void _curl_load_action(Request& r body.length=c.length; } - result.set(true /*tainted*/, body.buf, body.length, options().filename, vcontent_type); + 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); long http_status = 0; @@ -623,11 +626,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