Diff for /parser3/src/main/pa_request.C between versions 1.394 and 1.431

version 1.394, 2020/11/23 22:45:11 version 1.431, 2024/11/10 12:57:17
Line 1 Line 1
 /** @file  /** @file
         Parser: request class main part. @see compile.C and execute.C.          Parser: request class main part. @see compile.C and execute.C.
   
         Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)          Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)
         Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)          Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
 */  */
   
 #include "pa_sapi.h"  #include "pa_sapi.h"
Line 25 Line 25
 #include "pa_charset.h"  #include "pa_charset.h"
 #include "pa_charsets.h"  #include "pa_charsets.h"
 #include "pa_cache_managers.h"  #include "pa_cache_managers.h"
   #include "pa_http.h"
 #include "pa_vmail.h"  #include "pa_vmail.h"
 #include "pa_vform.h"  #include "pa_vform.h"
 #include "pa_vcookie.h"  #include "pa_vcookie.h"
Line 32 Line 33
 #include "pa_vmemory.h"  #include "pa_vmemory.h"
 #include "pa_vconsole.h"  #include "pa_vconsole.h"
 #include "pa_vdate.h"  #include "pa_vdate.h"
   #include "pa_varray.h"
   
 volatile const char * IDENT_PA_REQUEST_C="$Id$" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H;  volatile const char * IDENT_PA_REQUEST_C="$Id$" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H;
   
Line 42  volatile const char * IDENT_PA_REQUEST_C Line 44  volatile const char * IDENT_PA_REQUEST_C
 /// content type of response when no $MAIN:defaults.content-type defined  /// content type of response when no $MAIN:defaults.content-type defined
 const char* DEFAULT_CONTENT_TYPE="text/html";  const char* DEFAULT_CONTENT_TYPE="text/html";
   
 const uint LOOP_LIMIT=20000;  const uint LOOP_LIMIT=100000;
 const uint EXECUTE_RECOURSION_LIMIT=1000;  const uint ARRAY_LIMIT=1000000;
   const uint EXECUTE_RECURSION_LIMIT=1000;
 const uint HTTPD_TIMEOUT=4;  const uint HTTPD_TIMEOUT=4;
 const size_t FILE_SIZE_LIMIT=512*1024*1024;  const size_t FILE_SIZE_LIMIT=512*1024*1024;
   
Line 75  const String exception_handled_part_name Line 78  const String exception_handled_part_name
 static const String origin_key(ORIGIN_KEY);  static const String origin_key(ORIGIN_KEY);
   
 int pa_loop_limit=LOOP_LIMIT;  int pa_loop_limit=LOOP_LIMIT;
 int pa_execute_recoursion_limit=EXECUTE_RECOURSION_LIMIT;  int pa_array_limit=ARRAY_LIMIT;
   int pa_execute_recursion_limit=EXECUTE_RECURSION_LIMIT;
 int pa_httpd_timeout=HTTPD_TIMEOUT;  int pa_httpd_timeout=HTTPD_TIMEOUT;
 size_t pa_file_size_limit=FILE_SIZE_LIMIT;  size_t pa_file_size_limit=FILE_SIZE_LIMIT;
   
 // defines for statics  // defines for statics
   
 #define CHARSETS_NAME "CHARSETS"  
 #define MIME_TYPES_NAME "MIME-TYPES"  #define MIME_TYPES_NAME "MIME-TYPES"
 #define STRICT_VARS_NAME "STRICT-VARS"  
 #define PROTOTYPE_NAME "OBJECT-PROTOTYPE"  
 #define LIMITS_NAME "LIMITS"  
 #define LOOP_LIMIT_NAME "max_loop"  
 #define RECOURSION_LIMIT_NAME "max_recoursion"  
 #define HTTPD_TIMEOUT_NAME "httpd_timeout"  
 #define FILE_SIZE_LIMIT_NAME "max_file_size"  
 #define LOCK_WAIT_TIMEOUT_NAME "lock_wait_timeout"  
 #define CONF_METHOD_NAME "conf"  
 #define POST_PROCESS_METHOD_NAME "postprocess"  
 #define CLASS_PATH_NAME "CLASS_PATH"  #define CLASS_PATH_NAME "CLASS_PATH"
 #define RESPONSE_BODY_FILE_NAME "file"  
   
 #define DOWNLOAD_NAME_UPPER "DOWNLOAD"  #define DOWNLOAD_NAME_UPPER "DOWNLOAD"
 #define BODY_NAME_UPPER "BODY"  #define BODY_NAME_UPPER "BODY"
   
 // statics  // statics
   
 static const String charsets_name(CHARSETS_NAME);  
 static const String main_class_name(MAIN_CLASS_NAME);  static const String main_class_name(MAIN_CLASS_NAME);
 static const String mime_types_name(MIME_TYPES_NAME);  static const String mime_types_name(MIME_TYPES_NAME);
 static const String strict_vars_name(STRICT_VARS_NAME);  
 static const String prototype_name(PROTOTYPE_NAME);  
 static const String limits_name(LIMITS_NAME);  
 static const String loop_limit_name(LOOP_LIMIT_NAME);  
 static const String recoursion_limit_name(RECOURSION_LIMIT_NAME);  
 static const String httpd_timeout_name(HTTPD_TIMEOUT_NAME);  
 static const String file_size_limit_name(FILE_SIZE_LIMIT_NAME);  
 static const String lock_wait_timeout_name(LOCK_WAIT_TIMEOUT_NAME);  
   
 static const String conf_method_name(CONF_METHOD_NAME);  
 static const String post_process_method_name(POST_PROCESS_METHOD_NAME);  
 static const String class_path_name(CLASS_PATH_NAME);  static const String class_path_name(CLASS_PATH_NAME);
 static const String response_body_file_name(RESPONSE_BODY_FILE_NAME);  
   static const String charsets_name("CHARSETS");
   static const String strict_vars_name("STRICT-VARS");
   static const String prototype_name("OBJECT-PROTOTYPE");
   static const String getter_protected_name("CLASS-GETTER-PROTECTED");
   static const String locals_name("LOCALS");
   static const String limits_name("LIMITS");
   static const String loop_limit_name("max_loop");
   static const String array_limit_name("max_array_size");
   static const String recursion_limit_name("max_recursion");
   static const String file_size_limit_name("max_file_size");
   static const String lock_wait_timeout_name("lock_wait_timeout");
   static const String httpd_name("HTTPD");
   static const String httpd_timeout_name("timeout");
   static const String httpd_mode_name("mode");
   
   static const String conf_method_name("conf");
   static const String post_process_method_name("postprocess");
   static const String response_body_file_name("file");
   
 static const String download_name_upper(DOWNLOAD_NAME_UPPER);  static const String download_name_upper(DOWNLOAD_NAME_UPPER);
 static const String body_name_upper(BODY_NAME_UPPER);  static const String body_name_upper(BODY_NAME_UPPER);
Line 128  static const String content_disposition_ Line 126  static const String content_disposition_
 static const String content_disposition_inline(CONTENT_DISPOSITION_INLINE);  static const String content_disposition_inline(CONTENT_DISPOSITION_INLINE);
 static const String content_disposition_attachment(CONTENT_DISPOSITION_ATTACHMENT);  static const String content_disposition_attachment(CONTENT_DISPOSITION_ATTACHMENT);
   
   const String content_disposition_filename_name(CONTENT_DISPOSITION_FILENAME_NAME);
   const String content_disposition_filename_name_asterisk("filename*");
   
 // defines  // defines
   
 #define CHARSET_NAME_UPPER "CHARSET"  #define CHARSET_NAME_UPPER "CHARSET"
Line 137  static const String content_disposition_ Line 138  static const String content_disposition_
 VStateless_class& VClassMAIN_create();  VStateless_class& VClassMAIN_create();
   
 //  //
 Request::Request(SAPI_Info& asapi_info, Request_info& arequest_info,   Request::Request(SAPI_Info& asapi_info, Request_info& arequest_info, String::Language adefault_lang):
                                  String::Language adefault_lang):  
         // private          // private
         anti_endless_execute_recoursion(0),          anti_endless_execute_recursion(0),
   
         // public          // public
         allow_class_replace(false),          allow_class_replace(false),
Line 242  VStateless_class* Request::get_class(con Line 242  VStateless_class* Request::get_class(con
         return result;          return result;
 }  }
   
   VStateless_class& Request::get_class_ref(const String& name){
           VStateless_class* result=get_class(name);
           if(!result)
                   throw Exception(PARSER_RUNTIME, &name, "class is undefined");
           return *result;
   }
   
   bool Request::add_class(const char* atype, VStateless_class *aclass){
           if(!allow_class_replace){
                   if(!classes().put_dont_replace(atype, aclass))
                           return true;
                   if(strcmp(atype, VARRAY_TYPE))
                           return false;
           }
           classes().put(atype, aclass);
           return true;
   }
   
 static void load_charset(HashStringValue::key_type akey, HashStringValue::value_type avalue, Request_charsets* charsets) {  static void load_charset(HashStringValue::key_type akey, HashStringValue::value_type avalue, Request_charsets* charsets) {
         pa_charsets.load_charset(*charsets, akey, avalue->as_string());          pa_charsets.load_charset(*charsets, akey, avalue->as_string());
 }  }
   
   
   #define CONF_OPTION(config, name, code, exception_name)         \
           if(config)                                              \
                   if(Value* option=config->get_element(name)) {   \
                           if(option->is_evaluated_expr()) {       \
                                   code;                           \
                           } else                                  \
                                   throw Exception(PARSER_RUNTIME, 0, "$MAIN:" exception_name, name.cstr()); \
                   }
   
 void Request::configure_admin(VStateless_class& conf_class) {  void Request::configure_admin(VStateless_class& conf_class) {
         if(configure_admin_done)          if(configure_admin_done)
                 throw Exception(PARSER_RUNTIME, 0, "parser already configured");                  throw Exception(PARSER_RUNTIME, 0, "parser already configured");
Line 264  void Request::configure_admin(VStateless Line 292  void Request::configure_admin(VStateless
                         if(HashStringValue* charsets=vcharsets->get_hash())                          if(HashStringValue* charsets=vcharsets->get_hash())
                                 charsets->for_each<Request_charsets*>(load_charset, &this->charsets);                                  charsets->for_each<Request_charsets*>(load_charset, &this->charsets);
                         else                          else
                                 throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":" CHARSETS_NAME " must be hash");                                  throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be hash", charsets_name.cstr());
                 }                  }
         }          }
   
Line 274  void Request::configure_admin(VStateless Line 302  void Request::configure_admin(VStateless
                 if(strict_vars->is_bool())                  if(strict_vars->is_bool())
                         VVoid::strict_vars=strict_vars->as_bool();                          VVoid::strict_vars=strict_vars->as_bool();
                 else                  else
                         throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":" STRICT_VARS_NAME " must be bool");                          throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", strict_vars_name.cstr());
         }          }
 #endif  #endif
   
Line 284  void Request::configure_admin(VStateless Line 312  void Request::configure_admin(VStateless
                 if(prototype->is_bool())                  if(prototype->is_bool())
                         VClass::prototype=prototype->as_bool();                          VClass::prototype=prototype->as_bool();
                 else                  else
                         throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":" PROTOTYPE_NAME " must be bool");                          throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", prototype_name.cstr());
         }          }
 #endif  #endif
   
         Value* limits=conf_class.get_element(limits_name);  #ifdef CLASS_GETTER_UNPROTECTED
           VClass::getter_protected=true;
           if(Value* getter_protected=conf_class.get_element(getter_protected_name)) {
                   if(getter_protected->is_bool())
                           VClass::getter_protected=getter_protected->as_bool();
                   else
                           throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", getter_protected_name.cstr());
           }
   #endif
   
         pa_loop_limit=LOOP_LIMIT;          VStateless_class::gall_vars_local=false;
         if(limits)          if(Value* locals=conf_class.get_element(locals_name)) {
                 if(Value* loop_limit=limits->get_element(loop_limit_name)) {                  if(locals->is_bool()){
                         if(loop_limit->is_evaluated_expr()) {                          VStateless_class::gall_vars_local=locals->as_bool();
                                 pa_loop_limit=loop_limit->as_int();                          main_class.set_all_vars_local();
                                 if(pa_loop_limit==0) pa_loop_limit=INT_MAX;                  } else
                         } else                          throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", locals_name.cstr());
                                 throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":LIMITS." LOOP_LIMIT_NAME " must be int");          }
                 }  
   
         pa_execute_recoursion_limit=EXECUTE_RECOURSION_LIMIT;          Value* limits=conf_class.get_element(limits_name);
         if(limits)  
                 if(Value* recoursion_limit=limits->get_element(recoursion_limit_name)) {  
                         if(recoursion_limit->is_evaluated_expr()) {  
                                 pa_execute_recoursion_limit=recoursion_limit->as_int();  
                                 if(pa_execute_recoursion_limit==0) pa_execute_recoursion_limit=INT_MAX;  
                         } else  
                                 throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":LIMITS." RECOURSION_LIMIT_NAME " must be int");  
                 }  
   
         pa_httpd_timeout=HTTPD_TIMEOUT;          pa_loop_limit=LOOP_LIMIT;
         if(limits)          CONF_OPTION(limits, loop_limit_name, {
                 if(Value* httpd_timeout=limits->get_element(httpd_timeout_name)) {                  pa_loop_limit=option->as_int();
                         if(httpd_timeout->is_evaluated_expr()) {                  if(pa_loop_limit==0) pa_loop_limit=INT_MAX;
                                 pa_httpd_timeout=httpd_timeout->as_int();          }, "LIMITS.%s must be int");
                                 if(pa_httpd_timeout==0) pa_httpd_timeout=INT_MAX;  
                         } else          pa_array_limit=ARRAY_LIMIT;
                                 throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":LIMITS." HTTPD_TIMEOUT_NAME " must be int");          CONF_OPTION(limits, array_limit_name, {
                 }                  pa_array_limit=option->as_int();
                   if(pa_array_limit==0) pa_array_limit=INT_MAX;
           }, "LIMITS.%s must be int");
   
           pa_execute_recursion_limit=EXECUTE_RECURSION_LIMIT;
           CONF_OPTION(limits, recursion_limit_name, {
                   pa_execute_recursion_limit=option->as_int();
                   if(pa_execute_recursion_limit==0) pa_execute_recursion_limit=INT_MAX;
           }, "LIMITS.%s must be int");
   
         pa_file_size_limit=FILE_SIZE_LIMIT;          pa_file_size_limit=FILE_SIZE_LIMIT;
         if(limits)          CONF_OPTION(limits, file_size_limit_name, {
                 if(Value* file_size_limit=limits->get_element(file_size_limit_name)) {                  double limit=option->as_double();
                         if(file_size_limit->is_evaluated_expr()) {                  if(limit >= (double)SSIZE_MAX)
                                 double limit=file_size_limit->as_double();                          throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %.15g", file_size_limit_name.cstr(), (double)SSIZE_MAX);
                                 if(limit >= (double)SSIZE_MAX)                  pa_file_size_limit=(size_t)limit;
                                         throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":LIMITS." FILE_SIZE_LIMIT_NAME " must be less then %.15g", (double)SSIZE_MAX);                  if(pa_file_size_limit==0)
                                 pa_file_size_limit=(size_t)limit;                          pa_file_size_limit=SSIZE_MAX;
                                 if(pa_file_size_limit==0) pa_file_size_limit=SSIZE_MAX;          }, "LIMITS.%s must be number");
                         } else  
                                 throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":LIMITS." FILE_SIZE_LIMIT_NAME " must be number");  
                 }  
   
         pa_lock_attempts=PA_LOCK_ATTEMPTS;          pa_lock_attempts=PA_LOCK_ATTEMPTS;
         if(limits)          CONF_OPTION(limits, lock_wait_timeout_name, {
                 if(Value* lock_wait_timeout=limits->get_element(lock_wait_timeout_name)) {                  double limit=option->as_double();
                         if(lock_wait_timeout->is_evaluated_expr()) {                  if(limit >= 3600*24)
                                 double limit=lock_wait_timeout->as_double();                          throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %d", lock_wait_timeout_name.cstr(), 3600*24);
                                 if(limit >= 3600*24)                  pa_lock_attempts=(unsigned int)(limit*2)+1;
                                         throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":LIMITS." LOCK_WAIT_TIMEOUT_NAME " must be less then %d", 3600*24);          }, "LIMITS.%s must be number");
                                 pa_lock_attempts=(unsigned int)(limit*2)+1;  
                         } else          Value* httpd=conf_class.get_element(httpd_name);
                                 throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":LIMITS." LOCK_WAIT_TIMEOUT_NAME " must be number");  
           pa_httpd_timeout=HTTPD_TIMEOUT;
           CONF_OPTION(httpd, httpd_timeout_name, {
                   pa_httpd_timeout=option->as_int();
                   if(pa_httpd_timeout==0) pa_httpd_timeout=INT_MAX;
           }, "HTTPD.%s must be int");
   
           if(httpd)
                   if(Value* option=httpd->get_element( httpd_mode_name)) {
                           if(option->get_junction())
                                   throw Exception(PARSER_RUNTIME, 0, "$MAIN:HTTPD:mode must be string");
                           HTTPD_Server::set_mode(option->as_string());
                 }                  }
   
         // configure method_frame options          // configure method_frame options
Line 388  const char* Request::get_exception_cstr( Line 431  const char* Request::get_exception_cstr(
         return result;          return result;
 }  }
   
   Table &Request::Exception_trace::table(Request &r){
           // $stack[^table::create{name   file    lineno  colno}]
           Table::columns_type stack_trace_columns(new ArrayString);
           *stack_trace_columns+=new String("name");
           *stack_trace_columns+=new String("file");
           *stack_trace_columns+=new String("lineno");
           *stack_trace_columns+=new String("colno");
           Table& stack_trace=*new Table(stack_trace_columns);
   
           if(!is_empty()/*signed!*/)
                   for(size_t i=bottom_index(); i<top_index(); i++) {
                           Trace trace=get(i);
                           Table::element_type row(new ArrayString);
   
                           *row+=trace.name(); // name column
                           Operation::Origin origin=trace.origin();
                           if(origin.file_no) {
                                   *row+=new String(r.file_list[origin.file_no], String::L_TAINTED); // 'file' column
                                   *row+=new String(pa_uitoa(1+origin.line), String::L_CLEAN); // 'lineno' column
                                   *row+=new String(pa_uitoa(1+origin.col), String::L_CLEAN); // 'colno' column
                           }
                           stack_trace+=row;
                   }
   
           return stack_trace;
   }
   
 void Request::configure() {  void Request::configure() {
         // configure admin options if not configured yet          // configure admin options if not configured yet
         if(!configure_admin_done)          if(!configure_admin_done)
Line 399  void Request::configure() { Line 469  void Request::configure() {
         // $MAIN:MIME-TYPES          // $MAIN:MIME-TYPES
         if(Value* element=main_class.get_element(mime_types_name))          if(Value* element=main_class.get_element(mime_types_name))
                 if(Table *table=element->get_table())                  if(Table *table=element->get_table())
                         mime_types=table;                                                 mime_types=table;
 }  }
   
 /**  /**
         load MAIN class, execute @main.          load MAIN class, execute @main.
         MAIN class consists of all the auto.p files we'd manage to find          MAIN class consists of all the auto.p files we'd manage to find
Line 412  void Request::configure() { Line 483  void Request::configure() {
         @test log stack trace          @test log stack trace
   
 */  */
 void Request::core(const char* config_filespec, bool header_only, const String &amain_method_name) {  void Request::core(const char* config_filespec, bool header_only, const String &amain_method_name, const String* amain_class_name) {
         try {          try {
                 // loading config                  // loading config
                 if(config_filespec)                  if(config_filespec)
Line 431  void Request::core(const char* config_fi Line 502  void Request::core(const char* config_fi
                         rethrow;                          rethrow;
                 }                  }
   
                   VStateless_class& main = amain_class_name ? get_class_ref(*amain_class_name) : main_class;
                 // execute @main[]                  // execute @main[]
                 const String* body_string=amain_method_name.is_empty() ? &String::Empty : execute_method(main_class, amain_method_name);                  const String* body_string=amain_method_name.is_empty() ? &String::Empty : execute_method(main, amain_method_name);
                 if(!body_string)                  if(!body_string)
                         throw Exception(PARSER_RUNTIME, 0, "'%s' method not found", amain_method_name.cstr());                          throw Exception(PARSER_RUNTIME, &amain_method_name, "method not found in class %s", main.type());
   
                 // extract response body                  // extract response body
                 Value* body_value=response.fields().get(download_name_upper); // $response:download?                  Value* body_value=response.fields().get(download_name_upper); // $response:download?
Line 446  void Request::core(const char* config_fi Line 518  void Request::core(const char* config_fi
   
                 // @postprocess                  // @postprocess
                 if(const Method *method=main_class.get_method(post_process_method_name)) {                  if(const Method *method=main_class.get_method(post_process_method_name)) {
                         // preparing to pass parameters to  
                         //      @postprocess[data]  
                         METHOD_FRAME_ACTION(*method, 0 /*no parent*/, main_class, {                          METHOD_FRAME_ACTION(*method, 0 /*no parent*/, main_class, {
                                 frame.store_params(&body_value, 1);                                  frame.store_params(&body_value, 1);
                                 call(frame);                                  call(frame);
Line 455  void Request::core(const char* config_fi Line 525  void Request::core(const char* config_fi
                         });                          });
                 }                  }
   
                 VFile* body_file=body_value->as_vfile(flang, &charsets);                  // can throw exceptions while handling $response:download[]
                   output_result(body_value->as_vfile(flang, &charsets), header_only, as_attachment);
                 // OK. write out the result  
                 output_result(body_file, header_only, as_attachment);  
   
         } catch(const Exception& e) { // request handling problem          } catch(const Exception& e) { // request handling problem
                 try {  
                 // we're returning not result, but error explanation  
   
                   // we're returning not result, but error explanation
                 Request::Exception_details details=get_details(e);                  Request::Exception_details details=get_details(e);
                 const char* exception_cstr=get_exception_cstr(e, details);                  const char* exception_cstr=get_exception_cstr(e, details);
   
                 // reset language to default                  // reset language to default
                 flang=fdefault_lang;                  flang=fdefault_lang;
                   
                 // reset response                  // reset response
                 response.fields().clear();                  response.fields().clear();
                   SAPI::clear_headers(sapi_info);
   
                 // this is what we'd return in $response:body                  // this is what we'd return in $response:body
                 const String* body_string=0;                  const String* body_string=0;
   
                 // maybe we'd be lucky enough as to report an error                  try {
                 // in a gracefull way...                          // maybe we'd be lucky enough as to report an error in a gracefull way...
                 if(const Method *method=main_class.get_method(*new String(UNHANDLED_EXCEPTION_METHOD_NAME))) {                          if(const Method *method=main_class.get_method(*new String(UNHANDLED_EXCEPTION_METHOD_NAME))) {
                         // preparing to pass parameters to                                   // preparing parameters to @unhandled_exception[exception;stack]
                         //      @unhandled_exception[exception;stack]  
                                   Table& stack_trace=exception_trace.table(*this);
                         // $stack[^table::create{name   file    lineno  colno}]                                  exception_trace.clear(); // forget all about previous life, in case there would be error inside of this method, error handled would not be mislead by old stack contents (see extract_origin)
                         Table::columns_type stack_trace_columns(new ArrayString);  
                         *stack_trace_columns+=new String("name");                                  Value *params[]={&details.vhash, new VTable(&stack_trace)};
                         *stack_trace_columns+=new String("file");                                  METHOD_FRAME_ACTION(*method, 0 /*no caller*/, main_class, {
                         *stack_trace_columns+=new String("lineno");                                          frame.store_params(params, 2);
                         *stack_trace_columns+=new String("colno");                                          call(frame);
                         Table& stack_trace=*new Table(stack_trace_columns);                                          body_string=&frame.result().as_string();
                         if(!exception_trace.is_empty()/*signed!*/)                                   });
                                 for(size_t i=exception_trace.bottom_index(); i<exception_trace.top_index(); i++) {                          }
                                         Trace trace=exception_trace.get(i);  
                                         Table::element_type row(new ArrayString);  
   
                                         *row+=trace.name(); // name column  
                                         Operation::Origin origin=trace.origin();  
                                         if(origin.file_no) {  
                                                 *row+=new String(file_list[origin.file_no], String::L_TAINTED); // 'file' column  
                                                 *row+=new String(String::Body::Format(1+origin.line), String::L_CLEAN); // 'lineno' column  
                                                 *row+=new String(String::Body::Format(1+origin.col), String::L_CLEAN); // 'colno' column  
                                         }  
                                         stack_trace+=row;  
                                 }  
   
                         // future $response:body=  
                         //   execute ^unhandled_exception[exception;stack]  
                         exception_trace.clear(); // forget all about previous life, in case there would be error inside of this method, error handled  would not be mislead by old stack contents (see extract_origin)  
   
                         Value *params[]={&details.vhash, new VTable(&stack_trace)};                          // conditionally log it
                         METHOD_FRAME_ACTION(*method, 0 /*no caller*/, main_class, {                          Value* vhandled=details.vhash.hash().get(exception_handled_part_name);
                                 frame.store_params(params, 2);                          if(!vhandled || !vhandled->as_bool()) {
                                 call(frame);                                  SAPI::log(sapi_info, "%s", exception_cstr);
                                 body_string=&frame.result().as_string();                          }
                         });                  } catch(const Exception& e) { // exception in @unhandled_exception
                 }                          Request::Exception_details details=get_details(e);
                                           // logging both initial and new exceptions
                 // conditionally log it  
                 Value* vhandled=details.vhash.hash().get(exception_handled_part_name);  
                 if(!vhandled || !vhandled->as_bool()) {  
                         SAPI::log(sapi_info, "%s", exception_cstr);                          SAPI::log(sapi_info, "%s", exception_cstr);
                           SAPI::log(sapi_info, "Exception in @unhandled_exception at %s", get_exception_cstr(e, details));
                           SAPI::send_error(sapi_info, "Exception in @unhandled_exception, details are available in Parser error log.");
                           return;
                 }                  }
   
                 if(body_string) {  // could report an error beautifully?                  if(body_string) {  // could report an error beautifully?
                         VString body_vstring(*body_string);                          VString body_vstring(*body_string);
                         VFile* body_file=body_vstring.as_vfile(flang, &charsets);                          output_result(body_vstring.as_vfile(flang, &charsets), header_only, false);
                         // write it out the error  
                         output_result(body_file, header_only, false);  
                 } else {                  } else {
                         // doing that ugly                          // doing that ugly
                         SAPI::send_error(sapi_info, exception_cstr, !strcmp(e.type(), "file.missing") ? "404" : "500");                          SAPI::send_error(sapi_info, exception_cstr, strcmp(e.type(), "file.missing") ? "500" : "404");
                 }  
   
                 } catch(const Exception& e) { // exception in unhandled exception  
                         Request::Exception_details details=get_details(e);  
                         const char* exception_cstr=get_exception_cstr(e, details);  
                         // unconditionally log the beast in exception handler  
                         throw Exception(0, 0, "Unhandled exception in %s", exception_cstr);  
                 }                  }
         }          }
 }  }
Line 620  void Request::use_file(const String& fil Line 661  void Request::use_file(const String& fil
                         } else                          } else
                                 throw Exception(PARSER_RUNTIME, 0, "$" CLASS_PATH_NAME " must be string or table");                                  throw Exception(PARSER_RUNTIME, 0, "$" CLASS_PATH_NAME " must be string or table");
                         if(!filespec)                          if(!filespec)
                                 throw Exception(PARSER_RUNTIME, &file_name, "not found along $" MAIN_CLASS_NAME ":" CLASS_PATH_NAME);                                  throw Exception(PARSER_RUNTIME, &file_name, "not found along $MAIN:" CLASS_PATH_NAME);
                 } else                   } else 
                         throw Exception(PARSER_RUNTIME, &file_name, "usage failed - no $" MAIN_CLASS_NAME  ":" CLASS_PATH_NAME " were specified");                          throw Exception(PARSER_RUNTIME, &file_name, "usage failed - no $MAIN:" CLASS_PATH_NAME " were specified");
         }          }
   
         use_file_directly(*filespec, true, with_auto_p);          use_file_directly(*filespec, true, with_auto_p);
Line 668  void Request::use_buf(VStateless_class& Line 709  void Request::use_buf(VStateless_class&
                         configure_admin(cclass/*, executed.method->name*/);                          configure_admin(cclass/*, executed.method->name*/);
   
                 // locate and execute possible @auto[] static                  // locate and execute possible @auto[] static
                 execute_method_if_exists(cclass, auto_method_name, vfilespec);                  execute_auto_method_if_exists(cclass, auto_method_name, vfilespec);
   
                 cclass.enable_default_setter();                  cclass.enable_default_setter();
         }          }
Line 689  const String& Request::full_disk_path(co Line 730  const String& Request::full_disk_path(co
                 result << relative_name;                  result << relative_name;
                 return result;                  return result;
         }          }
         if(relative_name.pos("://")!=STRING_NOT_FOUND // something like "http://xxx"          if(relative_name.starts_with("http://") || relative_name.starts_with("parser://")
 #ifdef WIN32  #ifdef WIN32
                 || relative_name.pos(":")==1  // DRIVE:                  || relative_name.pos(":")==1  // DRIVE:
                 || relative_name.starts_with("\\\\") // UNC1                  || relative_name.starts_with("\\\\") // UNC1
Line 732  static void output_sole_piece(Request& r Line 773  static void output_sole_piece(Request& r
                 output=Charset::transcode(output, r.charsets.source(), r.charsets.client());                  output=Charset::transcode(output, r.charsets.source(), r.charsets.client());
   
         // prepare header: Content-Length          // prepare header: Content-Length
         SAPI::add_header_attribute(r.sapi_info, HTTP_CONTENT_LENGTH, format(output.length, "%u"));          SAPI::add_header_attribute(r.sapi_info, HTTP_CONTENT_LENGTH, pa_uitoa(output.length));
   
         // send header          // send header
         SAPI::send_header(r.sapi_info);          SAPI::send_headers(r.sapi_info);
                   
         // send body          // send body
         if(!header_only)          if(!header_only)
Line 745  static void output_sole_piece(Request& r Line 786  static void output_sole_piece(Request& r
 #ifndef DOXYGEN  #ifndef DOXYGEN
 struct Range  struct Range
 {  {
         size_t start;          uint64_t start;
         size_t end;          uint64_t end;
 };  };
 #endif  #endif
   
   #define UNSET ((uint64_t)-1)
   
 static void parse_range(const String* s, Array<Range> &ar) {  static void parse_range(const String* s, Array<Range> &ar) {
         const char *p = s->cstr();          const char *p = s->cstr();
         if(s->starts_with("bytes="))          if(s->starts_with("bytes="))
                 p += 6;                  p += 6;
         Range r;          Range r;
         while(*p){          while(*p){
                 r.start = (size_t)-1;                  r.start = UNSET;
                 r.end = (size_t)-1;                  r.end = UNSET;
   
                   while(*p==' ' || *p=='\t') p++;
   
                 if(*p >= '0' && *p <= '9'){                  if(*p >= '0' && *p <= '9'){
                         r.start = atol(p);                          const char *n=p;
                         while(*p>='0' && *p<='9') ++p;                          while(*p>='0' && *p<='9') p++;
                           r.start = pa_atoul(pa_strdup(n, p-n));
                 }                  }
   
                   while(*p==' ' || *p=='\t') p++;
   
                 if(*p++ != '-') break;                  if(*p++ != '-') break;
   
                   while(*p==' ' || *p=='\t') p++;
   
                 if(*p >= '0' && *p <= '9'){                  if(*p >= '0' && *p <= '9'){
                         r.end = atol(p);                          const char *n=p;
                         while(*p>='0' && *p<='9') ++p;                          while(*p>='0' && *p<='9') p++;
                           r.end = pa_atoul(pa_strdup(n, p-n));
                 }                  }
                 if(*p == ',') ++p;  
                   while(*p==' ' || *p=='\t') p++;
   
                   if(*p)
                           if(*p++ != ',') break;
   
                 ar += r;                  ar += r;
         }          }
 }  }
   
 static void output_pieces(Request& r, bool header_only, const String& filename, size_t content_length, Value& date, bool add_last_modified) {  struct Send_range_action_info {
         SAPI::add_header_attribute(r.sapi_info, "accept-ranges", "bytes");          Request *r;
           uint64_t offset;
           uint64_t part_length;
   };
   
   static void send_range(struct stat& /*finfo*/, int f, const String& /*file_spec*/, void *context){
           Send_range_action_info &info = *(Send_range_action_info*)context;
   
           SAPI::send_headers(info.r->sapi_info);
           pa_lseek(f, info.offset, SEEK_SET);
   
         const size_t BUFSIZE = 128*0x400;          const size_t BUFSIZE = 128*0x400;
         char buf[BUFSIZE];          char buf[BUFSIZE];
           do{
                   size_t to_read = info.part_length < BUFSIZE ? (size_t)info.part_length : BUFSIZE;
                   size_t to_write = file_block_read(f, buf, to_read);
   
                   if(to_write == 0)
                           break;
   
                   size_t size = SAPI::send_body(info.r->sapi_info, buf, to_write);
                   if(size != to_write)
                           break;
   
                   info.part_length -= to_write;
           } while (info.part_length);
   }
   
   static void output_pieces(Request& r, bool header_only, const String& filename, uint64_t content_length, Value& date, bool add_last_modified) {
           SAPI::add_header_attribute(r.sapi_info, "accept-ranges", "bytes");
   
         const char *range = SAPI::Env::get(r.sapi_info, "HTTP_RANGE");          const char *range = SAPI::Env::get(r.sapi_info, "HTTP_RANGE");
         size_t offset=0;          uint64_t offset=0;
         size_t part_length=content_length;          uint64_t part_length=content_length;
         if(range){  
           if(range && content_length){
                 Array<Range> ar;                  Array<Range> ar;
                 parse_range(new String(range), ar);                  parse_range(new String(range), ar);
                 size_t count = ar.count();                  int count = ar.count();
                 if(count == 1){                  if(count == 1){
                         Range &rg = ar.get_ref(0);                          Range &rg = ar.get_ref(0);
                         if(rg.start == (size_t)-1 && rg.end == (size_t)-1){  
                                 SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "416 Requested Range Not Satisfiable");                          if(rg.start == UNSET && rg.end == UNSET)
                                 return;                                  return SAPI::send_error(r.sapi_info, "", "416");
                         }  
                         if(rg.start == (size_t)-1 && rg.end != (size_t)-1){                          if(rg.start == UNSET && rg.end != UNSET){
                                   if(rg.end > content_length)
                                           rg.end = content_length;
                                 rg.start = content_length - rg.end;                                  rg.start = content_length - rg.end;
                                 rg.end = content_length;                                  rg.end =  content_length-1;
                                 offset += rg.start;                          } else if(rg.start != UNSET && rg.end == UNSET){
                                 part_length = rg.end-rg.start;                                  if(rg.start >= content_length)
                         }else if(rg.start != (size_t)-1 && rg.end == (size_t)-1){                                          return SAPI::send_error(r.sapi_info, "", "416");
                                 rg.end = content_length-1;                                  rg.end = content_length-1;
                                 offset += rg.start;                          } else {
                                 part_length -= rg.start;                                  if(rg.start >= content_length || rg.start > rg.end)
                         }                                          return SAPI::send_error(r.sapi_info, "", "416");
                         if(part_length == 0){                                  if(rg.end >= content_length)
                                 SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "204 No Content");                                          rg.end = content_length-1;
                                 return;  
                         }                          }
                         SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "206 Partial Content");  
                         snprintf(buf, BUFSIZE, "bytes %u-%u/%u", rg.start, rg.end, content_length);                          offset = rg.start;
                           part_length = rg.end-rg.start+1;
   
                           char buf[MAX_STRING];
                           snprintf(buf, MAX_STRING, "bytes %.15g-%.15g/%.15g", (double)rg.start, (double)rg.end, (double)content_length);
                           SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "206");
                         SAPI::add_header_attribute(r.sapi_info, "content-range", buf);                          SAPI::add_header_attribute(r.sapi_info, "content-range", buf);
                 }else if(count != 0){                  } else {
                         SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "501 Not Implemented");                          return SAPI::send_error(r.sapi_info, count ? "Multiple ranges are not supported" : "Invalid range", count ? "501" : "400");
                         return;  
                 }                  }
         }          }
   
           SAPI::add_header_attribute(r.sapi_info, HTTP_CONTENT_LENGTH, pa_uitoa(part_length));
         SAPI::add_header_attribute(r.sapi_info, HTTP_CONTENT_LENGTH, format(part_length, "%u"));  
   
         if(add_last_modified)          if(add_last_modified)
                 SAPI::add_header_attribute(r.sapi_info, "last-modified", attributed_meaning_to_string(date, String::L_AS_IS, true).cstr());                  SAPI::add_header_attribute(r.sapi_info, "last-modified", attributed_meaning_to_string(date, String::L_AS_IS, true).cstr());
   
         SAPI::send_header(r.sapi_info);          if(header_only){
                   SAPI::send_headers(r.sapi_info);
         const String& filespec=r.full_disk_path(filename);          } else {
                   Send_range_action_info info = { &r, offset, part_length};
         size_t sent = 0;                  file_read_action_under_lock(r.full_disk_path(filename), "send", send_range, &info);
         if(!header_only){  
                 size_t to_read = 0;  
                 size_t size = 0;  
                 do{  
                         to_read = part_length < BUFSIZE ? part_length : BUFSIZE;  
                         File_read_result read_result=file_read_binary(filespec, true /*fail on problem*/, buf, offset, to_read);  
                         to_read=read_result.length;  
                         if(to_read == 0)  
                                 break;  
                         offset += to_read;  
   
                         size = SAPI::send_body(r.sapi_info, read_result.str, to_read);  
                         sent += size;  
                         if(size != to_read)  
                                 break;  
                         part_length -= to_read;  
                 }while(part_length);  
         }          }
 }  }
   
Line 857  void Request::output_result(VFile* body_ Line 932  void Request::output_result(VFile* body_
         if(sname && *sname == NONAME_DAT)          if(sname && *sname == NONAME_DAT)
                 sname = NULL;                  sname = NULL;
   
         // Content-Disposition          // Content-Disposition, use $.name[<empty>] to avoid
         const String* disposition_name = sname ? sname->is_empty() ? NULL : sname : sfile;          const String* disposition_name = sname ? sname->is_empty() ? NULL : sname : sfile;
         if(disposition_name) {          if(disposition_name) {
                   String& filename = *new String(pa_filename(disposition_name->cstr()), String::L_URI);
                   String& filename_asterisk = *new String(charsets.client().NAME(), String::L_CLEAN) << (*new String("''")) << filename;
   
                 VHash& hash=*new VHash();                  VHash& hash=*new VHash();
                 hash.hash().put(value_name, new VString(as_attachment ? content_disposition_attachment : content_disposition_inline));                  hash.hash().put(value_name, new VString(as_attachment ? content_disposition_attachment : content_disposition_inline));
                 hash.hash().put(content_disposition_filename_name, new VString(*new String(*disposition_name, String::L_HTTP_HEADER)));                  hash.hash().put(content_disposition_filename_name, new VString(filename));
                   hash.hash().put(content_disposition_filename_name_asterisk, new VString(filename_asterisk));
                 response.fields().put(content_disposition_name_upper, &hash);                  response.fields().put(content_disposition_name_upper, &hash);
         }          }
   
Line 895  void Request::output_result(VFile* body_ Line 974  void Request::output_result(VFile* body_
   
                 VDate* vdate=0;                  VDate* vdate=0;
                 if(Value* v=body_file->fields().get("mdate")) {                  if(Value* v=body_file->fields().get("mdate")) {
                         if(Value* vdatep=v->as(VDATE_TYPE))                          vdate=dynamic_cast<VDate*>(v);
                                 vdate=static_cast<VDate*>(vdatep);                          if(!vdate)
                         else   
                                 throw Exception(PARSER_RUNTIME, 0, "mdate must be a date");                                  throw Exception(PARSER_RUNTIME, 0, "mdate must be a date");
                 }                  }
                 if(!vdate)                  if(!vdate)
                         vdate=new VDate((pa_time_t)mtime);                          vdate=new VDate((pa_time_t)mtime);
   
                 output_pieces(*this, header_only, *sfile, (size_t)content_length, *vdate, info.add_last_modified);                  output_pieces(*this, header_only, *sfile, content_length, *vdate, info.add_last_modified);
         } else {          } else {
                 if(body_file_content_type)                  if(body_file_content_type)
                         if(HashStringValue *hash=body_file_content_type->get_hash())                          if(HashStringValue *hash=body_file_content_type->get_hash())
Line 926  const String& Request::mime_type_of(cons Line 1004  const String& Request::mime_type_of(cons
                                 if(const String* result=mime_types->item(1))                                  if(const String* result=mime_types->item(1))
                                         return *result;                                          return *result;
                                 else                                  else
                                         throw Exception(PARSER_RUNTIME, 0, MIME_TYPES_NAME  " table column elements must not be empty");                                          throw Exception(PARSER_RUNTIME, 0, MIME_TYPES_NAME " table column elements must not be empty");
                         }                          }
                 }                  }
   

Removed from v.1.394  
changed lines
  Added in v.1.431


E-mail: