Diff for /parser3/src/classes/curl.C between versions 1.26 and 1.54

version 1.26, 2013/04/21 21:24:53 version 1.54, 2017/02/07 22:00:25
Line 1 Line 1
 /** @file  /** @file
         Parser: @b curl parser class.          Parser: @b curl parser class.
   
         Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)          Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)
 */  */
   
 #include "pa_config_includes.h"  #include "pa_config_includes.h"
Line 11 Line 11
 #include "pa_vfile.h"  #include "pa_vfile.h"
 #include "pa_charsets.h"  #include "pa_charsets.h"
 #include "pa_vstring.h"  #include "pa_vstring.h"
   #include "pa_vdate.h"
 #include "pa_vtable.h"  #include "pa_vtable.h"
 #include "pa_common.h"  #include "pa_common.h"
 #include "pa_http.h"   #include "pa_http.h" 
Line 25  public: Line 26  public:
   
 // global variables  // global variables
   
 DECLARE_CLASS_VAR(curl, new MCurl, 0);  DECLARE_CLASS_VAR(curl, new MCurl);
   
 #include "curl.h"  #include "curl.h"
   
Line 44  typedef void (*t_curl_formfree)(struct c Line 45  typedef void (*t_curl_formfree)(struct c
 #define DLINK(name) GLINK(name) if(!f_##name) return "function " #name " was not found";  #define DLINK(name) GLINK(name) if(!f_##name) return "function " #name " was not found";
                                   
 static const char *dlink(const char *dlopen_file_spec) {  static const char *dlink(const char *dlopen_file_spec) {
         if(lt_dlinit())          pa_dlinit();
                 return lt_dlerror();  
   
         lt_dlhandle handle=lt_dlopen(dlopen_file_spec);          lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
   
Line 71  static const char *dlink(const char *dlo Line 71  static const char *dlink(const char *dlo
   
 class ParserOptions {  class ParserOptions {
 public:  public:
           // real options
         const String *filename;          const String *filename;
         const String *content_type;          const String *content_type;
         bool is_text;          bool is_text;
         Charset *charset, *response_charset;          Charset *charset, *response_charset;
   
           // stuff for internal use
           const char *url;
         struct curl_httppost *f_post;          struct curl_httppost *f_post;
         FILE *f_stderr;          FILE *f_stderr;
   
         ParserOptions() : filename(0), content_type(0), is_text(true), charset(0), response_charset(0), f_post(0), f_stderr(0){}          ParserOptions() : filename(0), content_type(0), is_text(true), charset(0), response_charset(0), url(0), f_post(0), f_stderr(0){}
         ~ParserOptions() {          ~ParserOptions() {
                 f_curl_formfree(f_post);                  f_curl_formfree(f_post);
                 if(f_stderr)                  if(f_stderr)
Line 112  public: Line 116  public:
                 fcurl = f_curl_easy_init();                  fcurl = f_curl_easy_init();
                 foptions = new ParserOptions();                  foptions = new ParserOptions();
                 f_curl_easy_setopt(fcurl, CURLOPT_POSTFIELDSIZE, 0); // fix libcurl bug                  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
         }          }
         ~Temp_curl() {          ~Temp_curl() {
                 f_curl_easy_cleanup(fcurl);                  f_curl_easy_cleanup(fcurl);
Line 122  public: Line 127  public:
 };  };
   
 bool curl_linked = false;  bool curl_linked = false;
 #ifdef WIN32  
 const char *curl_library="libcurl.dll";  
 #else  
 const char *curl_library="libcurl.so";  
 #endif  
   
 const char *curl_status = 0;  const char *curl_status = 0;
   const char *curl_library="libcurl" LT_MODULE_EXT;
   
 static void temp_curl(void (*action)(Request&, MethodParams&), Request& r, MethodParams& params){  static void temp_curl(void (*action)(Request&, MethodParams&), Request& r, MethodParams& params){
         if(!curl_linked){          if(!curl_linked)
                 curl_linked=true;  
                 curl_status=dlink(curl_library);                  curl_status=dlink(curl_library);
         }  
   
         if(curl_status == 0){          if(curl_status == 0){
                   curl_linked=true;
                 Temp_curl temp_curl;                  Temp_curl temp_curl;
                 action(r,params);                  action(r,params);
         } else {          } else {
Line 154  static void _curl_session(Request& r, Me Line 153  static void _curl_session(Request& r, Me
 }  }
   
 static void _curl_version_action(Request& r, MethodParams& ){  static void _curl_version_action(Request& r, MethodParams& ){
         r.write_no_lang(*new VString(*new String(f_curl_version(), String::L_TAINTED)));          r.write(*new VString(*new String(f_curl_version(), String::L_TAINTED)));
 }  }
   
 static void _curl_version(Request& r, MethodParams& params){  static void _curl_version(Request& r, MethodParams& params){
         fcurl ? _curl_version_action(r, params) : temp_curl(_curl_version_action, r, params);          fcurl ? _curl_version_action(r, params) : temp_curl(_curl_version_action, r, params);
 }  }
   
 static char *str_lower(const char *str){  
         char *result=pa_strdup(str);  
         for(char* c=result; *c; c++)  
                 *c=(char)tolower((unsigned char)*c);  
         return result;  
 }  
   
 static char *str_upper(const char *str){  
         char *result=pa_strdup(str);  
         for(char* c=result; *c; c++)  
                 *c=(char)toupper((unsigned char)*c);  
         return result;  
 }  
   
 class CurlOption {  class CurlOption {
 public:  public:
   
Line 230  public: Line 215  public:
   
                 CURL_OPT(CURL_INT, FOLLOWLOCATION);                  CURL_OPT(CURL_INT, FOLLOWLOCATION);
                 CURL_OPT(CURL_INT, UNRESTRICTED_AUTH);                  CURL_OPT(CURL_INT, UNRESTRICTED_AUTH);
                   CURL_OPT(CURL_INT, IPRESOLVE);
   
                 CURL_OPT(CURL_INT, POST);                  CURL_OPT(CURL_INT, POST);
                 CURL_OPT(CURL_INT, HTTPGET);                  CURL_OPT(CURL_INT, HTTPGET);
Line 290  public: Line 276  public:
                 CURL_OPT(CURL_INT, SSL_VERIFYHOST);                  CURL_OPT(CURL_INT, SSL_VERIFYHOST);
                 CURL_OPT(CURL_STRING, SSL_CIPHER_LIST);                  CURL_OPT(CURL_STRING, SSL_CIPHER_LIST);
                 CURL_OPT(CURL_INT, SSL_SESSIONID_CACHE);                  CURL_OPT(CURL_INT, SSL_SESSIONID_CACHE);
                   CURL_OPT(CURL_INT, SSLVERSION);
   
                 PARSER_OPT(PARSER_LIBRARY, "library");                  PARSER_OPT(PARSER_LIBRARY, "library");
                 PARSER_OPT(PARSER_NAME, "name");                  PARSER_OPT(PARSER_NAME, "name");
Line 301  public: Line 288  public:
   
 } *curl_options=0;  } *curl_options=0;
   
   class CurlInfo {
   public:
   
           enum OptionType {
                   CURL_STRING,
                   CURL_INT,
                   CURL_DOUBLE
           };
   
           CURLINFO id;
           OptionType type;
           CurlInfo(CURLINFO aid, OptionType atype): id(aid), type(atype) {}
   };
   
   class CurlInfoHash: public HashString<CurlInfo*> {
   public:
           CurlInfoHash() {
   #define CURL_INF(type, name) put(str_lower(#name),new CurlInfo(CURLINFO_##name, CurlInfo::type));
                   CURL_INF(CURL_DOUBLE, APPCONNECT_TIME);
                   CURL_INF(CURL_DOUBLE, CONNECT_TIME);
                   CURL_INF(CURL_DOUBLE, CONTENT_LENGTH_DOWNLOAD);
                   CURL_INF(CURL_DOUBLE, CONTENT_LENGTH_UPLOAD);
                   CURL_INF(CURL_STRING, CONTENT_TYPE);
                   CURL_INF(CURL_STRING, EFFECTIVE_URL);
                   CURL_INF(CURL_INT, HEADER_SIZE);
                   CURL_INF(CURL_INT, HTTPAUTH_AVAIL);
                   CURL_INF(CURL_DOUBLE, NAMELOOKUP_TIME);
                   CURL_INF(CURL_INT, NUM_CONNECTS);
                   CURL_INF(CURL_INT, OS_ERRNO);
                   CURL_INF(CURL_DOUBLE, PRETRANSFER_TIME);
                   CURL_INF(CURL_STRING, PRIMARY_IP);
                   CURL_INF(CURL_INT, PROXYAUTH_AVAIL);
                   CURL_INF(CURL_INT, REDIRECT_COUNT);
                   CURL_INF(CURL_DOUBLE, REDIRECT_TIME);
                   CURL_INF(CURL_STRING, REDIRECT_URL);
                   CURL_INF(CURL_INT, REQUEST_SIZE);
                   CURL_INF(CURL_INT, RESPONSE_CODE);
                   CURL_INF(CURL_DOUBLE, SIZE_DOWNLOAD);
                   CURL_INF(CURL_DOUBLE, SIZE_UPLOAD);
                   CURL_INF(CURL_DOUBLE, SPEED_DOWNLOAD);
                   CURL_INF(CURL_DOUBLE, SPEED_UPLOAD);
                   CURL_INF(CURL_INT, SSL_VERIFYRESULT);
                   CURL_INF(CURL_DOUBLE, STARTTRANSFER_TIME);
                   CURL_INF(CURL_DOUBLE, TOTAL_TIME);
           }
   
   } *curl_infos=0;
   
 static const char *curl_urlencode(const String &s, Request& r){  static const char *curl_urlencode(const String &s, Request& r){
         if(options().charset){          if(options().charset){
                 Temp_client_charset temp(r.charsets, *options().charset);                  Temp_client_charset temp(r.charsets, *options().charset);
Line 363  static void curl_form(HashStringValue *v Line 398  static void curl_form(HashStringValue *v
 static const char *curl_check_file(const String &file_spec){  static const char *curl_check_file(const String &file_spec){
         const char *file_spec_cstr=file_spec.taint_cstr(String::L_FILE_SPEC);          const char *file_spec_cstr=file_spec.taint_cstr(String::L_FILE_SPEC);
         struct stat finfo;          struct stat finfo;
         if(stat(file_spec_cstr, &finfo)==0)          if(pa_stat(file_spec_cstr, &finfo)==0)
                 check_safe_mode(finfo, file_spec, file_spec_cstr);                  check_safe_mode(finfo, file_spec, file_spec_cstr);
         return file_spec_cstr;          return file_spec_cstr;
 }  }
Line 375  static void curl_setopt(HashStringValue: Line 410  static void curl_setopt(HashStringValue:
                 throw Exception("curl", 0, "called with invalid option '%s'", key.cstr());                  throw Exception("curl", 0, "called with invalid option '%s'", key.cstr());
   
         CURLcode res = CURLE_OK;          CURLcode res = CURLE_OK;
         Value &v=r.process_to_value(*value);          Value &v=r.process(*value);
   
         switch (opt->type){          switch (opt->type){
                 case CurlOption::CURL_STRING:{                  case CurlOption::CURL_STRING:{
Line 395  static void curl_setopt(HashStringValue: Line 430  static void curl_setopt(HashStringValue:
                         const String url = v.as_string();                          const String url = v.as_string();
                         if(!url.starts_with("http://") && !url.starts_with("https://"))                          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());                                  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);                          options().url=curl_urlencode(url, r);
                         res=f_curl_easy_setopt(curl(), opt->id, value_str);                          res=f_curl_easy_setopt(curl(), opt->id, options().url);
                         break;                          break;
                 }                  }
                 case CurlOption::CURL_INT:{                  case CurlOption::CURL_INT:{
Line 418  static void curl_setopt(HashStringValue: Line 453  static void curl_setopt(HashStringValue:
                         break;                          break;
                 }                  }
                 case CurlOption::CURL_FORM:{                  case CurlOption::CURL_FORM:{
                         HashStringValue *value_hash = v.get_hash();                          HashStringValue *value_hash = v.as_hash("failed to set option 'httppost': value");
                         if(value_hash){                          if(value_hash){
                                 curl_form(value_hash, r);                                  curl_form(value_hash, r);
                         } else if(v.get_string()->is_empty()){                          } else {
                                 f_curl_formfree(options().f_post);                                  f_curl_formfree(options().f_post);
                                 options().f_post = 0;                                  options().f_post = 0;
                         } else {  
                                 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);                          res=f_curl_easy_setopt(curl(), CURLOPT_HTTPPOST, foptions->f_post);
                         break;                          break;
                 }                  }
                 case CurlOption::CURL_HEADERS:{                  case CurlOption::CURL_HEADERS:{
                         // http headers curl option                          // http headers curl option
                         HashStringValue *value_hash=v.get_hash();                          HashStringValue *value_hash=v.as_hash("failed to set option 'httpheader': value");
                         res=f_curl_easy_setopt(curl(), opt->id, value_hash ? curl_headers(value_hash, r) : 0);                          res=f_curl_easy_setopt(curl(), opt->id, value_hash ? curl_headers(value_hash, r) : 0);
                         break;                          break;
                 }                  }
Line 445  static void curl_setopt(HashStringValue: Line 478  static void curl_setopt(HashStringValue:
                 case CurlOption::CURL_STDERR:{                  case CurlOption::CURL_STDERR:{
                         // verbose output redirection from stderr to file curl option                          // verbose output redirection from stderr to file curl option
                         const char *file_spec_cstr=curl_check_file(r.absolute(v.as_string()));                          const char *file_spec_cstr=curl_check_file(r.absolute(v.as_string()));
                         FILE *f_stderr=options().f_stderr=fopen(file_spec_cstr, "wt");                          FILE *f_stderr=options().f_stderr=pa_fopen(file_spec_cstr, "wt");
                         if (f_stderr){                          if (f_stderr){
                                 res=f_curl_easy_setopt(curl(), opt->id, f_stderr);                                  res=f_curl_easy_setopt(curl(), opt->id, f_stderr);
                         } else {                          } else {
Line 455  static void curl_setopt(HashStringValue: Line 488  static void curl_setopt(HashStringValue:
                 }                  }
                 case CurlOption::PARSER_LIBRARY:{                  case CurlOption::PARSER_LIBRARY:{
                         // 'library' parser option                          // 'library' parser option
                         if(fcurl==0){                          if(!curl_linked){
                                 curl_library=v.as_string().taint_cstr(String::L_FILE_SPEC);                                  curl_library=v.as_string().taint_cstr(String::L_FILE_SPEC);
                         } else                           } else 
                                 throw Exception("curl", 0, "failed to set option '%s': already loaded", key.cstr());                                  throw Exception("curl", 0, "failed to set option '%s': already loaded", key.cstr());
Line 477  static void curl_setopt(HashStringValue: Line 510  static void curl_setopt(HashStringValue:
                         break;                          break;
                 }                  }
                 case CurlOption::PARSER_CHARSET:{                  case CurlOption::PARSER_CHARSET:{
                         // 'charset' parser option                          // 'charset' parser option should be processed before other options
                         options().charset=&::charsets.get(v.as_string().change_case(r.charsets.source(), String::CC_UPPER));  
                         break;                          break;
                 }                  }
                 case CurlOption::PARSER_RESPONSE_CHARSET:{                  case CurlOption::PARSER_RESPONSE_CHARSET:{
                         // 'response-charset' parser option                          // 'response-charset' parser option
                         options().response_charset=&::charsets.get(v.as_string().change_case(r.charsets.source(), String::CC_UPPER));                          options().response_charset=&pa_charsets.get(v.as_string());
                         break;                          break;
                 }                  }
         }          }
Line 496  static void _curl_options(Request& r, Me Line 528  static void _curl_options(Request& r, Me
         if(curl_options==0)          if(curl_options==0)
                 curl_options=new CurlOptionHash();                  curl_options=new CurlOptionHash();
   
         if(HashStringValue* options=params.as_hash(0))          if(HashStringValue* options_hash=params.as_hash(0)){
                 options->for_each<Request&>(curl_setopt, r);                  if(Value* value=options_hash->get("charset")){
                           // charset should be handled first as params may require transcode
                           Value &v=r.process(*value);
                           options().charset=&pa_charsets.get(v.as_string());
                   }
                   options_hash->for_each<Request&>(curl_setopt, r);
           }
   }
   
   #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)); \
           }
   
   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());
   
           CURLcode res;
           switch (info->type){
                   case CurlInfo::CURL_STRING:{
                           char *str=0;
                           CURL_GETINFO(str);
                           return new VString(str ? *new String(pa_strdup(str), String::L_TAINTED) : String::Empty);
                   }
                   case CurlInfo::CURL_INT:{
                           long l=0;
                           CURL_GETINFO(l);
                           return new VInt(l);
                   }
                   case CurlInfo::CURL_DOUBLE:{
                           double d=0;
                           CURL_GETINFO(d);
                           return new VDouble(d);
                   }
           }
           return VVoid::get();
   }
   
   static void _curl_info(Request& r, MethodParams& params){
           if(curl_infos==0)
                   curl_infos=new CurlInfoHash();
           if(params.count()==1){
                   const String &name=params.as_string(0, "name must be string");
                   r.write(*curl_getinfo(name));
           } 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()));
                   }
                   r.write(result);
           }
 }  }
   
   
Line 506  public: Line 589  public:
         char *buf;          char *buf;
         size_t length;          size_t length;
         size_t buf_size;          size_t buf_size;
           ResponseHeaders& headers;
   
         Curl_buffer() : buf((char *)pa_malloc(MAX_STRING+1)), length(0), buf_size(MAX_STRING){}          Curl_buffer(ResponseHeaders& aheaders) : buf((char *)pa_malloc_atomic(MAX_STRING)), length(0), buf_size(MAX_STRING-1), headers(aheaders){}
   
           void resize(size_t size){
                   buf_size=size;
                   buf=(char *)pa_realloc(buf, size+1);
           }
 };  };
   
 static int curl_writer(char *data, size_t size, size_t nmemb, Curl_buffer *result){  static int curl_writer(char *data, size_t size, size_t nmemb, Curl_buffer *result){
Line 516  static int curl_writer(char *data, size_ Line 605  static int curl_writer(char *data, size_
   
         size=size*nmemb;          size=size*nmemb;
         if(size>0){          if(size>0){
                 if(result->length + size >= result->buf_size){                  size_t buf_required = result->length + size;
                         result->buf_size = result->buf_size*2 + size;                  if(buf_required > result->buf_size)
                         result->buf = (char *)pa_realloc(result->buf, result->buf_size+1);                          result->resize(buf_required <= result->headers.content_length ? (size_t)result->headers.content_length : result->buf_size*2 + size);
                 }  
                 memcpy(result->buf+result->length, data, size);                  memcpy(result->buf+result->length, data, size);
                 result->length += size;                  result->length += size;
         }          }
         return size;          return size;
 }  }
   
 class Curl_response {  static int curl_header(char *data, size_t size, size_t nmemb, ResponseHeaders *result){
 public:  
         HASH_STRING<char*> headers;  
         Array<char*> cookies;  
 };  
   
 static int curl_header(char *data, size_t size, size_t nmemb, Curl_response *result){  
         if(result == 0)          if(result == 0)
                 return 0;                  return 0;
   
         size=size*nmemb;          size=size*nmemb;
         if(size>0){          if(size>0){
                 char *line=pa_strdup(data, size);                  char *header=pa_strdup(data, size);
                 char *value=lsplit(line,':');                  if(!pa_strncasecmp(header, "HTTP/") && !strchr(header, ':')){
                 if(value && *line){                          // response code, clearing possible headers from previous requests
                         // we need only headers, not the response code                          result->clear();
                         const char* HEADER_NAME=str_upper(line);                  } else {
                         result->headers.put(HEADER_NAME, value);                          result->add_header(header);
                         if(strcmp(HEADER_NAME, "SET-COOKIE")==0)                          if(result->content_length>pa_file_size_limit)
                                 result->cookies+=value;                                  return 0;
                 }                  }
         }          }
         return size;          return size;
Line 562  static void _curl_load_action(Request& r Line 644  static void _curl_load_action(Request& r
   
         CURLcode res;          CURLcode res;
   
         Curl_buffer body;  
         CURL_SETOPT(CURLOPT_WRITEFUNCTION, curl_writer, "curl writer function");  
         CURL_SETOPT(CURLOPT_WRITEDATA, &body, "curl write buffer");  
   
         // we need a container for headers as VFile fields can be put only after VFile.set          // we need a container for headers as VFile fields can be put only after VFile.set
         Curl_response response;          ResponseHeaders response;
         CURL_SETOPT(CURLOPT_HEADERFUNCTION, curl_header, "curl header function");          CURL_SETOPT(CURLOPT_HEADERFUNCTION, curl_header, "curl header function");
         CURL_SETOPT(CURLOPT_WRITEHEADER, &response, "curl header buffer");          CURL_SETOPT(CURLOPT_WRITEHEADER, &response, "curl header buffer");
   
           Curl_buffer body(response);
           CURL_SETOPT(CURLOPT_WRITEFUNCTION, curl_writer, "curl writer function");
           CURL_SETOPT(CURLOPT_WRITEDATA, &body, "curl write buffer");
   
         if((res=f_curl_easy_perform(curl())) != CURLE_OK){          if((res=f_curl_easy_perform(curl())) != CURLE_OK){
                 const char *ex_type = 0;                   const char *ex_type = 0; 
                 switch(res){                  switch(res){
Line 588  static void _curl_load_action(Request& r Line 670  static void _curl_load_action(Request& r
                         case CURLE_SSL_CACERT:                          case CURLE_SSL_CACERT:
                         case CURLE_SSL_ENGINE_INITFAILED:                          case CURLE_SSL_ENGINE_INITFAILED:
                                 ex_type = "curl.ssl"; break;                                  ex_type = "curl.ssl"; break;
                           case CURLE_WRITE_ERROR:
                                   check_file_size(response.content_length, *new String(options().url)); break;
                           default: break;
                 }                  }
                 throw Exception( ex_type ? ex_type : "curl.fail", 0, "%s", f_curl_easy_strerror(res));                  throw Exception( PA_DEFAULT(ex_type, "curl.fail"), 0, "%s", f_curl_easy_strerror(res));
         }          }
   
         // assure trailing zero          // assure trailing zero
Line 597  static void _curl_load_action(Request& r Line 682  static void _curl_load_action(Request& r
   
         VFile& result=*new VFile;          VFile& result=*new VFile;
   
         String::Body ct_header = response.headers.get(HTTP_CONTENT_TYPE_UPPER);  
         Charset *asked_charset = options().response_charset;          Charset *asked_charset = options().response_charset;
         if (asked_charset == 0){          if (!asked_charset && !response.content_type.is_empty())
                 Charset *remote_charset = ct_header.is_empty() ? 0 : detect_charset(ct_header.trim(String::TRIM_BOTH, " \t\n\r").cstr());                  asked_charset=detect_charset(response.content_type.cstr());
                 asked_charset = remote_charset ? remote_charset : options().charset;  
         }          if(options().is_text)
                   asked_charset=pa_charsets.checkBOM(body.buf, body.length, asked_charset);
   
           if (!asked_charset)
                   asked_charset = options().charset;
   
         if(options().is_text && asked_charset != 0){          if(options().is_text && asked_charset != 0){
                 String::C c=Charset::transcode(String::C(body.buf, body.length), *asked_charset, r.charsets.source());                  String::C c=Charset::transcode(String::C(body.buf, body.length), *asked_charset, r.charsets.source());
Line 610  static void _curl_load_action(Request& r Line 698  static void _curl_load_action(Request& r
                 body.length=c.length;                  body.length=c.length;
         }          }
   
         result.set(true/*tainted*/, options().is_text, body.buf, body.length, options().filename          const String *content_type = PA_DEFAULT(options().content_type, response.content_type.is_empty() ? 0 : new String(response.content_type, String::L_TAINTED));
                                 , options().content_type ? new VString(*options().content_type) : 0, &r);          const String *filename = PA_DEFAULT(options().filename, new String(options().url));
   
           result.set(true/*tainted*/, options().is_text, body.buf, body.length, filename, content_type ? new VString(*content_type) : 0, &r);
   
         long http_status = 0;          long http_status = 0;
         if(f_curl_easy_getinfo(curl(), CURLINFO_RESPONSE_CODE, &http_status) == CURLE_OK){          if(f_curl_easy_getinfo(curl(), CURLINFO_RESPONSE_CODE, &http_status) == CURLE_OK){
                 result.fields().put("status", new VInt(http_status));                  result.fields().put("status", new VInt(http_status));
         }          }
   
         for(HASH_STRING<char *>::Iterator i(response.headers); i; i.next() ){          VHash* vtables=new VHash;
                 String::Body HEADER_NAME=i.key();                 result.fields().put("tables", vtables);
                 String::Body value=i.value();  
                 if(asked_charset){  
                         HEADER_NAME=Charset::transcode(HEADER_NAME, *asked_charset, r.charsets.source());  
                         value=Charset::transcode(value, *asked_charset, r.charsets.source());  
                 }  
                 result.fields().put(HEADER_NAME, new VString(*new String(value.trim(String::TRIM_BOTH, " \t\n\r"), String::L_TAINTED)));  
         }  
   
         // filling $.cookies          for(Array_iterator<ResponseHeaders::Header> i(response.headers); i.has_next(); ){
         Table* tcookies=0;                  ResponseHeaders::Header header=i.next();
   
         for(Array_iterator<char*> i(response.cookies); i.has_next(); ){  
                 if(!tcookies){  
                         Table::columns_type columns=new ArrayString(1);  
                         *columns+=new String("value");  
                         tcookies=new Table(columns);  
                 }  
                 String::Body value=i.next();  
                 if(asked_charset)                  if(asked_charset)
                         value=Charset::transcode(value, *asked_charset, r.charsets.source());                          header.transcode(*asked_charset, r.charsets.source());
                 ArrayString& row=*new ArrayString(1);  
                 row+=new String(value.trim(String::TRIM_BOTH, " \t\n\r"), String::L_TAINTED);                  String &header_value=*new String(header.value, String::L_TAINTED);
                 *tcookies+=&row;  
                   tables_update(vtables->hash(), header.name, header_value);
                   result.fields().put(header.name, new VString(header_value));
         }          }
   
         if(tcookies)          // filling $.cookies
                 result.fields().put(HTTP_COOKIES_NAME, new VTable(parse_cookies(r, tcookies)));          if(Value *vcookies=vtables->hash().get("SET-COOKIE"))
                   result.fields().put(HTTP_COOKIES_NAME, new VTable(parse_cookies(r, vcookies->get_table())));
   
         r.write_no_lang(result);          r.write(result);
 }  }
   
 static void _curl_load(Request& r, MethodParams& params){  static void _curl_load(Request& r, MethodParams& params){
Line 659  MCurl::MCurl(): Methoded("curl") { Line 739  MCurl::MCurl(): Methoded("curl") {
         add_native_method("session", Method::CT_STATIC, _curl_session, 1, 1);          add_native_method("session", Method::CT_STATIC, _curl_session, 1, 1);
         add_native_method("version", Method::CT_STATIC, _curl_version, 0, 0);          add_native_method("version", Method::CT_STATIC, _curl_version, 0, 0);
         add_native_method("options", Method::CT_STATIC, _curl_options, 1, 1);          add_native_method("options", Method::CT_STATIC, _curl_options, 1, 1);
           add_native_method("info", Method::CT_STATIC, _curl_info, 0, 1);
         add_native_method("load", Method::CT_STATIC, _curl_load, 0, 1);          add_native_method("load", Method::CT_STATIC, _curl_load, 0, 1);
 }  }

Removed from v.1.26  
changed lines
  Added in v.1.54


E-mail: