Diff for /parser3/src/main/pa_common.C between versions 1.143.2.21.2.5 and 1.143.2.21.2.8

version 1.143.2.21.2.5, 2003/03/20 09:41:46 version 1.143.2.21.2.8, 2003/03/21 13:42:31
Line 167  char* file_read_text(Charset& charset, Line 167  char* file_read_text(Charset& charset,
                                          HashStringValue* params/*, HashStringValue* * out_fields*/) {                                           HashStringValue* params/*, HashStringValue* * out_fields*/) {
         File_read_result file=          File_read_result file=
                 file_read(charset, file_spec, true, params, fail_on_read_problem);                  file_read(charset, file_spec, true, params, fail_on_read_problem);
         return file.success?file.data:0;          return file.success?file.str:0;
 }  }
   
 #ifdef PA_HTTP  #ifdef PA_HTTP
Line 191  static int http_read_responseString& res Line 191  static int http_read_responseString& res
         int result=0;          int result=0;
         ssize_t EOLat=0;          ssize_t EOLat=0;
         while(true) {          while(true) {
                 char *buf=new char[MAX_STRING];                   char *buf=new(PointerFreeGC) char[MAX_STRING]; 
                 ssize_t size=recv(sock, buf, MAX_STRING, 0);                   ssize_t size=recv(sock, buf, MAX_STRING, 0); 
                 if(size<=0)                  if(size<=0)
                         break;                          break;
Line 214  static int http_read_responseString& res Line 214  static int http_read_responseString& res
         else          else
                 throw Exception("http.response",                  throw Exception("http.response",
                         0,                          0,
                         "bad response from host - no status found (size=%lu)", response.size());                           "bad response from host - no status found (size=%lu)", response.length()); 
 }  }
   
 /* ********************** request *************************** */  /* ********************** request *************************** */
Line 305  struct Http_pass_header_info { Line 305  struct Http_pass_header_info {
 static void http_pass_header(HashStringValue::key_type key, HashStringValue::value_type value,   static void http_pass_header(HashStringValue::key_type key, HashStringValue::value_type value, 
                                                          Http_pass_header_info *info) {                                                           Http_pass_header_info *info) {
     *(info->request)<<*key<<": "      *(info->request)<<*key<<": "
                 << *attributed_meaning_to_string(*info->value, String::UL_HTTP_HEADER, false)                  << *attributed_meaning_to_string(*info->value, String::L_HTTP_HEADER, false)
                 <<"\n";                   <<"\n"; 
                   
         if(*key->change_case(*info->*info->charset, String::CC_UPPER)=="USER-AGENT")          if(*key->change_case(*info->*info->charset, String::CC_UPPER)=="USER-AGENT")
Line 326  static File_read_http_result file_read_h Line 326  static File_read_http_result file_read_h
         const char* method(0);          const char* method(0);
         int timeout=2;          int timeout=2;
         bool fail_on_status_ne_200=true;          bool fail_on_status_ne_200=true;
         ValuePtr vheaders(0);          Value* vheaders(0);
   
         const String& connect_string(new String());          const String& connect_string(new String());
         // not in ^sql{... UL_SQL ...} spirit, but closer to ^file::load one          // not in ^sql{... L_SQL ...} spirit, but closer to ^file::load one
         connect_string->append(*file_spec, String::UL_URI); // tainted pieces -> URI pieces          connect_string->append(*file_spec, String::L_URI); // tainted pieces -> URI pieces
   
         const char* connect_string_cstr=connect_string->cstr(String::UL_UNSPECIFIED);           const char* connect_string_cstr=connect_string->cstr(String::L_UNSPECIFIED); 
         const char* current=connect_string_cstr;          const char* current=connect_string_cstr;
         if(strncmp(current, "http://", 7)!=0)          if(strncmp(current, "http://", 7)!=0)
                 throw Exception(0,                   throw Exception(0, 
Line 349  static File_read_http_result file_read_h Line 349  static File_read_http_result file_read_h
   
         if(options) {          if(options) {
                 int valid_options=0;                  int valid_options=0;
                 if(ValuePtr vmethod=options->get(http_method_name)) {                  if(Value* vmethod=options->get(http_method_name)) {
                         valid_options++;                          valid_options++;
                         method=vmethod->as_string(&pool)->cstr();                          method=vmethod->as_string()->cstr();
                 }                  }
                 if(ValuePtr vtimeout=options->get(http_timeout_name)) {                  if(Value* vtimeout=options->get(http_timeout_name)) {
                         valid_options++;                          valid_options++;
                         timeout=vtimeout->as_int();                           timeout=vtimeout->as_int(); 
                 }                  }
                 if(vheaders=options->get(http_headers_name)) {                  if(vheaders=options->get(http_headers_name)) {
                         valid_options++;                          valid_options++;
                 }                  }
                 if(ValuePtr vany_status=options->get(http_any_status_name)) {                  if(Value* vany_status=options->get(http_any_status_name)) {
                         valid_options++;                          valid_options++;
                         fail_on_status_ne_200=!vany_status->as_bool();                           fail_on_status_ne_200=!vany_status->as_bool(); 
                 }                  }
Line 395  static File_read_http_result file_read_h Line 395  static File_read_http_result file_read_h
                   
         //sending request          //sending request
         String response;          String response;
         const char* request_cstr=request.cstr(String::UL_UNSPECIFIED);          const char* request_cstr=request.cstr(String::L_UNSPECIFIED);
         int status_code=http_request(response,          int status_code=http_request(response,
                 connect_string, host, port, request_cstr,                   connect_string, host, port, request_cstr, 
                 timeout, fail_on_status_ne_200);                   timeout, fail_on_status_ne_200); 
Line 408  static File_read_http_result file_read_h Line 408  static File_read_http_result file_read_h
                         "bad response from host - no headers found");                           "bad response from host - no headers found"); 
         }          }
         const String& header_block=response.mid(0, pos);           const String& header_block=response.mid(0, pos); 
         const String& body=response.mid(pos+4, response.size());           const String& body=response.mid(pos+4, response.length()); 
                   
         ArrayString aheaders;          ArrayString aheaders;
         result.headers=HashStringValue* (new HashStringValue);           result.headers=HashStringValue* (new HashStringValue); 
Line 425  static File_read_http_result file_read_h Line 425  static File_read_http_result file_read_h
                                                   
                 result.headers->put(                  result.headers->put(
                         line->mid(0, pos)->change_case(charset, String::CC_UPPER),                           line->mid(0, pos)->change_case(charset, String::CC_UPPER), 
                         ValuePtr(new VString(line->mid(pos+2, line->size()))));                           Value*(new VString(line->mid(pos+2, line->length())))); 
         }          }
   
         // output response          // output response
         result.data=new char[1/*as text puts terminating zero later*/+(result.size=body->size())];          result.str=new(PointerFreeGC) char[1/*as text puts terminating zero later*/+(result.length=body->length())];
         body->store_to(result.data, String::UL_AS_IS);          body->store_to(result.str, String::L_AS_IS);
         result.headers->put(file_status_name, ValuePtr(new VInt(status_code)));           result.headers->put(file_status_name, Value*(new VInt(status_code))); 
         return result;          return result;
 }  }
   
Line 449  static void file_read_action( Line 449  static void file_read_action(
                                                          void *context) {                                                           void *context) {
         File_read_action_info& info=*static_cast<File_read_action_info *>(context);           File_read_action_info& info=*static_cast<File_read_action_info *>(context); 
         if(size_t to_read_size=(size_t)finfo.st_size) {           if(size_t to_read_size=(size_t)finfo.st_size) { 
                 *info.data=new char[to_read_size+(as_text?1:0)];                   *info.data=new(PointerFreeGC) char[to_read_size+(as_text?1:0)]; 
                 *info.data_size=(size_t)read(f, *info.data, to_read_size);                   *info.data_size=(size_t)read(f, *info.data, to_read_size); 
   
                 if(ssize_t(*info.data_size)<0 || *info.data_size>to_read_size)                  if(ssize_t(*info.data_size)<0 || *info.data_size>to_read_size)
Line 459  static void file_read_action( Line 459  static void file_read_action(
                                         *info.data_size, to_read_size);                                           *info.data_size, to_read_size); 
         } else { // empty file          } else { // empty file
                 if(as_text) {                  if(as_text) {
                         *info.data=new char[1];                           *info.data=new(PointerFreeGC) char[1]; 
                         *(char*)(*info.data)=0;                          *(char*)(*info.data)=0;
                 } else                   } else 
                         *info.data=0;                          *info.data=0;
Line 476  File_read_result file_read(Charset& char Line 476  File_read_result file_read(Charset& char
                 // fail on read problem                  // fail on read problem
                 File_read_http_result http=file_read_http(charset, file_spec, params);                  File_read_http_result http=file_read_http(charset, file_spec, params);
                 result.success=true;                  result.success=true;
                 result.data=http.data;                  result.str=http.data;
                 result.size=http.size;                  result.length=http.size;
                 result.headers=http.headers;                   result.headers=http.headers; 
         } else {          } else {
 #endif  #endif
                 File_read_action_info info={&result.data, &result.size};                   File_read_action_info info={&result.str, &result.length}; 
                 result.success=file_read_action_under_lock(file_spec,                   result.success=file_read_action_under_lock(file_spec, 
                         "read", file_read_action, &info,                           "read", file_read_action, &info, 
                         as_text, fail_on_read_problem);                           as_text, fail_on_read_problem); 
Line 491  File_read_result file_read(Charset& char Line 491  File_read_result file_read(Charset& char
   
         if(result.success && as_text) {          if(result.success && as_text) {
                 // UTF-8 signature: EF BB BF                  // UTF-8 signature: EF BB BF
                 if(result.size>=3) {                  if(result.length>=3) {
                         char *in=(char *)result.data;                          char *in=(char *)result.str;
                         if((in[0] == '\xEF') && (in[1] == '\xBB') &&                          if((in[0] == '\xEF') && (in[1] == '\xBB') &&
                                 (in[2] == '\xBF')) {                                  (in[2] == '\xBF')) {
                                 result.data=in+3; result.size-=3;// skip prefix                                  result.str=in+3; result.length-=3;// skip prefix
                         }                          }
                 }                  }
   
                 fix_line_breaks((char *)(result.data), result.size);                   fix_line_breaks((char *)(result.str), result.length); 
                 // note: after fixing                  // note: after fixing
                 ((char*&)(result.data))[result.size]=0;                  ((char*&)(result.str))[result.length]=0;
         }          }
   
         return result;          return result;
Line 511  bool file_read_action_under_lock(const S Line 511  bool file_read_action_under_lock(const S
                                 const char* action_name, File_read_action action, void *context,                                   const char* action_name, File_read_action action, void *context, 
                                 bool as_text,                                   bool as_text, 
                                 bool fail_on_read_problem) {                                  bool fail_on_read_problem) {
         const char* fname=file_spec.cstr(String::UL_FILE_SPEC);           const char* fname=file_spec.cstr(String::L_FILE_SPEC); 
         int f;          int f;
   
         // first open, next stat:          // first open, next stat:
Line 571  static void create_dir_for_file(const St Line 571  static void create_dir_for_file(const St
         size_t pos_after=1;          size_t pos_after=1;
         int pos_before;          int pos_before;
         while((pos_before=file_spec.pos("/", pos_after))>=0) {          while((pos_before=file_spec.pos("/", pos_after))>=0) {
                 mkdir(file_spec.mid(0, pos_before).cstr(String::UL_FILE_SPEC), 0775);                   mkdir(file_spec.mid(0, pos_before).cstr(String::L_FILE_SPEC), 0775); 
                 pos_after=pos_before+1;                  pos_after=pos_before+1;
         }          }
 }  }
Line 583  bool file_write_action_under_lock( Line 583  bool file_write_action_under_lock(
                                 bool do_append,                                   bool do_append, 
                                 bool do_block,                                   bool do_block, 
                                 bool fail_on_lock_problem) {                                  bool fail_on_lock_problem) {
         const char* fname=file_spec.cstr(String::UL_FILE_SPEC);           const char* fname=file_spec.cstr(String::L_FILE_SPEC); 
         int f;          int f;
         if(access(fname, W_OK)!=0) // no          if(access(fname, W_OK)!=0) // no
                 create_dir_for_file(file_spec);                   create_dir_for_file(file_spec); 
Line 630  bool file_write_action_under_lock( Line 630  bool file_write_action_under_lock(
   
 #ifndef DOXYGEN  #ifndef DOXYGEN
 struct File_write_action_info {  struct File_write_action_info {
         const char* data; size_t size;          const char* str; size_t length;
 };   }; 
 #endif  #endif
 static void file_write_action(int f, void *context) {  static void file_write_action(int f, void *context) {
         File_write_action_info& info=*static_cast<File_write_action_info *>(context);           File_write_action_info& info=*static_cast<File_write_action_info *>(context); 
         if(info.size) {          if(info.length) {
                 int written=write(f, info.data, info.size);                   int written=write(f, info.str, info.length); 
                 if(written<0)                  if(written<0)
                         throw Exception(0,                           throw Exception(0, 
                                 0,                                   0, 
Line 662  static void rmdir(const String& file_spe Line 662  static void rmdir(const String& file_spe
         if((pos_before=file_spec.pos("/", pos_after))>=0)          if((pos_before=file_spec.pos("/", pos_after))>=0)
                 rmdir(file_spec, pos_before+1);                   rmdir(file_spec, pos_before+1); 
                   
         rmdir(file_spec.mid(0, pos_after-1/* / */).cstr(String::UL_FILE_SPEC));           rmdir(file_spec.mid(0, pos_after-1/* / */).cstr(String::L_FILE_SPEC)); 
 }  }
 bool file_delete(const String& file_spec, bool fail_on_read_problem) {  bool file_delete(const String& file_spec, bool fail_on_read_problem) {
         const char* fname=file_spec.cstr(String::UL_FILE_SPEC);           const char* fname=file_spec.cstr(String::L_FILE_SPEC); 
         if(unlink(fname)!=0)          if(unlink(fname)!=0)
                 if(fail_on_read_problem)                  if(fail_on_read_problem)
                         throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,                           throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, 
Line 679  bool file_delete(const String& file_spec Line 679  bool file_delete(const String& file_spec
         return true;          return true;
 }  }
 void file_move(const String& old_spec, const String& new_spec) {  void file_move(const String& old_spec, const String& new_spec) {
         const char* old_spec_cstr=old_spec.cstr(String::UL_FILE_SPEC);           const char* old_spec_cstr=old_spec.cstr(String::L_FILE_SPEC); 
         const char* new_spec_cstr=new_spec.cstr(String::UL_FILE_SPEC);           const char* new_spec_cstr=new_spec.cstr(String::L_FILE_SPEC); 
                   
         create_dir_for_file(new_spec);           create_dir_for_file(new_spec); 
   
Line 703  bool entry_exists(const char* fname, str Line 703  bool entry_exists(const char* fname, str
 }  }
   
 bool entry_exists(const String& file_spec) {  bool entry_exists(const String& file_spec) {
         const char* fname=file_spec.cstr(String::UL_FILE_SPEC);           const char* fname=file_spec.cstr(String::L_FILE_SPEC); 
         return entry_exists(fname, 0);           return entry_exists(fname, 0); 
 }  }
   
 static bool entry_readable(const String& file_spec, bool need_dir) {  static bool entry_readable(const String& file_spec, bool need_dir) {
         char* fname=file_spec.cstrm(String::UL_FILE_SPEC);           char* fname=file_spec.cstrm(String::L_FILE_SPEC); 
         if(need_dir) {          if(need_dir) {
                 size_t size=strlen(fname);                   size_t size=strlen(fname); 
                 while(size) {                  while(size) {
Line 739  const String* file_readable(String& path Line 739  const String* file_readable(String& path
         return file_readable(result)?&result:0;          return file_readable(result)?&result:0;
 }  }
 bool file_executable(const String& file_spec) {  bool file_executable(const String& file_spec) {
     return access(file_spec.cstr(String::UL_FILE_SPEC), X_OK)==0;      return access(file_spec.cstr(String::L_FILE_SPEC), X_OK)==0;
 }  }
   
 bool file_stat(const String& file_spec,   bool file_stat(const String& file_spec, 
Line 748  bool file_stat(const String& file_spec, Line 748  bool file_stat(const String& file_spec,
                            time_t& rmtime,                              time_t& rmtime, 
                            time_t& rctime,                              time_t& rctime, 
                            bool fail_on_read_problem) {                             bool fail_on_read_problem) {
         const char* fname=file_spec.cstr(String::UL_FILE_SPEC);           const char* fname=file_spec.cstr(String::L_FILE_SPEC); 
     struct stat finfo;      struct stat finfo;
         if(stat(fname, &finfo)!=0)          if(stat(fname, &finfo)!=0)
                 if(fail_on_read_problem)                  if(fail_on_read_problem)
Line 842  size_t stdout_write(const void *buf, siz Line 842  size_t stdout_write(const void *buf, siz
 }  }
   
 char* unescape_chars(const char* cp, int len) {  char* unescape_chars(const char* cp, int len) {
         char* s=new char[len + 1];           char* s=new(PointerFreeGC) char[len + 1]; 
         enum EscapeState {          enum EscapeState {
                 EscapeRest,                   EscapeRest, 
                 EscapeFirst,                   EscapeFirst, 

Removed from v.1.143.2.21.2.5  
changed lines
  Added in v.1.143.2.21.2.8


E-mail: