--- parser3/src/classes/curl.C 2010/05/16 00:23:00 1.4 +++ parser3/src/classes/curl.C 2011/11/23 12:17:22 1.12 @@ -8,7 +8,7 @@ #ifdef HAVE_CURL -static const char * const IDENT_INET_C="$Date: 2010/05/16 00:23:00 $"; +static const char * const IDENT_INET_C="$Date: 2011/11/23 12:17:22 $"; #include "pa_vmethod_frame.h" #include "pa_request.h" @@ -17,23 +17,18 @@ static const char * const IDENT_INET_C=" #include "pa_vstring.h" #include "pa_vtable.h" #include "pa_common.h" -#include "ltdl.h" +#include "pa_http.h" +#include "ltdl.h" class MCurl: public Methoded { public: MCurl(); - -public: // Methoded - bool used_directly() { return true; } }; // global variables 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; @@ -78,7 +73,7 @@ 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; @@ -91,13 +86,9 @@ public: }; -// using thread local variables instead of keeping them in request -// not necessary for cgi version -#ifdef WIN32 -#define __thread __declspec(thread) -#endif -__thread CURL *fcurl = 0; -__thread ParserOptions *foptions = 0; +// using TLS instead of keeping variables in request +THREAD_LOCAL CURL *fcurl = 0; +THREAD_LOCAL ParserOptions *foptions = 0; static CURL *curl(){ if(!fcurl) @@ -117,8 +108,8 @@ class Temp_curl { ParserOptions *saved_options; public: Temp_curl() : saved_curl(fcurl), saved_options(foptions){ - fcurl = f_curl_easy_init(); - foptions = new ParserOptions(); + fcurl = f_curl_easy_init(); + foptions = new ParserOptions(); f_curl_easy_setopt(fcurl, CURLOPT_POSTFIELDSIZE, 0); // fix libcurl bug } ~Temp_curl() { @@ -161,7 +152,7 @@ static void _curl_session(Request& r, Me temp_curl(_curl_session_action, r, params); } -static void _curl_version_action(Request& r, MethodParams& params){ +static void _curl_version_action(Request& r, MethodParams& ){ r.write_no_lang(*new VString(*new String(f_curl_version(), String::L_TAINTED))); } @@ -252,7 +243,9 @@ 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); @@ -321,9 +314,9 @@ static struct curl_slist *curl_headers(H for(HashStringValue::Iterator i(*value_hash); i; i.next() ){ String header = - String(capitalize(i.key().cstr()), String::L_URI) + String(pa_http_safe_header_name(capitalize(i.key().cstr())), String::L_AS_IS) << ": " - << String(i.value()->as_string(), String::L_URI); + << String(i.value()->as_string(), String::L_HTTP_HEADER); slist=f_curl_slist_append(slist, curl_urlencode(header, r)); } @@ -391,9 +384,9 @@ static void curl_setopt(HashStringValue: } case CurlOption::CURL_URL:{ // url-encoded string curl_url option - const String url = v.as_string(); - if(!url.starts_with("http://") && !url.starts_with("https://")) - throw Exception("curl", 0, "failed to set option '%s': invalid url scheme '%s'", key.cstr(), url.cstr()); + const String url = v.as_string(); + if(!url.starts_with("http://") && !url.starts_with("https://")) + throw Exception("curl", 0, "failed to set option '%s': invalid url scheme '%s'", key.cstr(), url.cstr()); const char *value_str=curl_urlencode(url, r); res=f_curl_easy_setopt(curl(), opt->id, value_str); break; @@ -420,12 +413,12 @@ static void curl_setopt(HashStringValue: HashStringValue *value_hash = v.get_hash(); if(value_hash){ curl_form(value_hash, r); - } else if(v.get_string()->is_empty()){ + } else if(v.get_string()->is_empty()){ f_curl_formfree(options().f_post); options().f_post = 0; } else { throw Exception("curl", 0, "%s must be a hash", key.cstr()); - } + } res=f_curl_easy_setopt(curl(), CURLOPT_HTTPPOST, foptions->f_post); break; } @@ -451,7 +444,7 @@ static void curl_setopt(HashStringValue: } 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:{ @@ -461,7 +454,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:{ @@ -470,7 +463,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; } @@ -487,7 +480,7 @@ static void _curl_options(Request& r, Me if(HashStringValue* options=params.as_no_junction(0, OPTIONS_MUST_NOT_BE_CODE).get_hash()){ options->for_each(curl_setopt, r); } else - throw Exception("curl", 0, "options must be hash"); + throw Exception("curl", 0, OPTIONS_MUST_BE_HASH); } @@ -576,15 +569,14 @@ 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); - Charset *remote_charset = ct_header.is_empty() ? 0 : detect_charset(ct_header.trim(String::TRIM_BOTH, " \t\n\r").cstr()); - Charset *asked_charset = options().response_charset ? options().response_charset : (remote_charset ? remote_charset : options().charset); + Charset *asked_charset = options().response_charset; + if (asked_charset == 0){ + Charset *remote_charset = ct_header.is_empty() ? 0 : detect_charset(ct_header.trim(String::TRIM_BOTH, " \t\n\r").cstr()); + asked_charset = remote_charset ? remote_charset : options().charset; + } if(options().is_text && asked_charset != 0){ String::C c=Charset::transcode(String::C(body.buf, body.length), *asked_charset, r.charsets.source()); @@ -592,7 +584,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;