--- parser3/src/main/pa_request.C 2020/12/15 12:16:04 1.403 +++ parser3/src/main/pa_request.C 2021/01/02 23:01:11 1.415 @@ -1,7 +1,7 @@ /** @file 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-2020 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ @@ -34,7 +34,7 @@ #include "pa_vconsole.h" #include "pa_vdate.h" -volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.403 2020/12/15 12:16:04 moko Exp $" 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: pa_request.C,v 1.415 2021/01/02 23:01:11 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H; // consts @@ -97,6 +97,8 @@ static const String class_path_name(CLAS 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 recoursion_limit_name("max_recoursion"); @@ -129,8 +131,7 @@ static const String content_disposition_ VStateless_class& VClassMAIN_create(); // -Request::Request(SAPI_Info& asapi_info, Request_info& arequest_info, - String::Language adefault_lang): +Request::Request(SAPI_Info& asapi_info, Request_info& arequest_info, String::Language adefault_lang): // private anti_endless_execute_recoursion(0), @@ -234,6 +235,13 @@ VStateless_class* Request::get_class(con 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; +} + static void load_charset(HashStringValue::key_type akey, HashStringValue::value_type avalue, Request_charsets* charsets) { pa_charsets.load_charset(*charsets, akey, avalue->as_string()); } @@ -245,7 +253,7 @@ static void load_charset(HashStringValue if(option->is_evaluated_expr()) { \ code; \ } else \ - throw Exception(PARSER_RUNTIME, 0, "$main:" exception_name, name.cstr()); \ + throw Exception(PARSER_RUNTIME, 0, "$MAIN:" exception_name, name.cstr()); \ } void Request::configure_admin(VStateless_class& conf_class) { @@ -266,7 +274,7 @@ void Request::configure_admin(VStateless if(HashStringValue* charsets=vcharsets->get_hash()) charsets->for_each(load_charset, &this->charsets); else - throw Exception(PARSER_RUNTIME, 0, "$main:%s must be hash", charsets_name.cstr()); + throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be hash", charsets_name.cstr()); } } @@ -276,7 +284,7 @@ void Request::configure_admin(VStateless if(strict_vars->is_bool()) VVoid::strict_vars=strict_vars->as_bool(); else - throw Exception(PARSER_RUNTIME, 0, "$main:%s must be bool", strict_vars_name.cstr()); + throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", strict_vars_name.cstr()); } #endif @@ -286,10 +294,29 @@ void Request::configure_admin(VStateless if(prototype->is_bool()) VClass::prototype=prototype->as_bool(); else - throw Exception(PARSER_RUNTIME, 0, "$main:%s must be bool", prototype_name.cstr()); + throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", prototype_name.cstr()); } #endif +#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 + + VStateless_class::gall_vars_local=false; + if(Value* locals=conf_class.get_element(locals_name)) { + if(locals->is_bool()){ + VStateless_class::gall_vars_local=locals->as_bool(); + main_class.set_all_vars_local(); + } else + throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", locals_name.cstr()); + } + Value* limits=conf_class.get_element(limits_name); pa_loop_limit=LOOP_LIMIT; @@ -308,7 +335,7 @@ void Request::configure_admin(VStateless CONF_OPTION(limits, file_size_limit_name, { double limit=option->as_double(); if(limit >= (double)SSIZE_MAX) - throw Exception(PARSER_RUNTIME, 0, "$main:LIMITS.%s must be less then %.15g", file_size_limit_name.cstr(), (double)SSIZE_MAX); + throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less then %.15g", file_size_limit_name.cstr(), (double)SSIZE_MAX); pa_file_size_limit=(size_t)limit; if(pa_file_size_limit==0) pa_file_size_limit=SSIZE_MAX; @@ -318,7 +345,7 @@ void Request::configure_admin(VStateless CONF_OPTION(limits, lock_wait_timeout_name, { double limit=option->as_double(); if(limit >= 3600*24) - throw Exception(PARSER_RUNTIME, 0, "$main:LIMITS.%s must be less then %d", lock_wait_timeout_name.cstr(), 3600*24); + throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less then %d", lock_wait_timeout_name.cstr(), 3600*24); pa_lock_attempts=(unsigned int)(limit*2)+1; }, "LIMITS.%s must be number"); @@ -333,7 +360,7 @@ void Request::configure_admin(VStateless 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"); + throw Exception(PARSER_RUNTIME, 0, "$MAIN:HTTPD:mode must be string"); HTTPD_Server::set_mode(option->as_string()); } @@ -380,6 +407,33 @@ const char* Request::get_exception_cstr( 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(); iget_table()) mime_types=table; } + /** load MAIN class, execute @main. MAIN class consists of all the auto.p files we'd manage to find @@ -404,7 +459,10 @@ void Request::configure() { @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) { + VFile* body_file=NULL; + bool as_attachment=false; + try { // loading config if(config_filespec) @@ -423,14 +481,15 @@ void Request::core(const char* config_fi rethrow; } + VStateless_class& main = amain_class_name ? get_class_ref(*amain_class_name) : main_class; // 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) - 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 Value* body_value=response.fields().get(download_name_upper); // $response:download? - bool as_attachment=body_value!=0; + as_attachment=body_value!=0; if(!body_value) body_value=response.fields().get(body_name_upper); // $response:body if(!body_value) @@ -438,8 +497,6 @@ void Request::core(const char* config_fi // @postprocess 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, { frame.store_params(&body_value, 1); call(frame); @@ -447,90 +504,64 @@ void Request::core(const char* config_fi }); } - VFile* body_file=body_value->as_vfile(flang, &charsets); - - // OK. write out the result - output_result(body_file, header_only, as_attachment); + body_file=body_value->as_vfile(flang, &charsets); } 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); - const char* exception_cstr=get_exception_cstr(e, details); + Request::Exception_details details=get_details(e); + const char* exception_cstr=get_exception_cstr(e, details); - // reset language to default - flang=fdefault_lang; - - // reset response - response.fields().clear(); - - // this is what we'd return in $response:body - const String* body_string=0; - - // 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))) { - // preparing to pass parameters to - // @unhandled_exception[exception;stack] - - // $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(!exception_trace.is_empty()/*signed!*/) - for(size_t i=exception_trace.bottom_index(); ias_bool()) { + SAPI::log(sapi_info, "%s", exception_cstr); + } - Value *params[]={&details.vhash, new VTable(&stack_trace)}; - METHOD_FRAME_ACTION(*method, 0 /*no caller*/, main_class, { - frame.store_params(params, 2); - call(frame); - body_string=&frame.result().as_string(); - }); - } - - // 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); - } + if(body_string) { // could report an error beautifully? + VString body_vstring(*body_string); - if(body_string) { // could report an error beautifully? - VString body_vstring(*body_string); - VFile* body_file=body_vstring.as_vfile(flang, &charsets); - // write it out the error - output_result(body_file, header_only, false); - } else { - // doing that ugly - SAPI::send_error(sapi_info, exception_cstr, !strcmp(e.type(), "file.missing") ? "404" : "500"); - } + body_file=body_vstring.as_vfile(flang, &charsets); + as_attachment=false; + } else { + // doing that ugly + SAPI::send_error(sapi_info, exception_cstr, !strcmp(e.type(), "file.missing") ? "404" : "500"); + return; + } } 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); + throw Exception(0, 0, "Unhandled exception in %s", get_exception_cstr(e, details)); } } + + // write out the result outside of try as network exceptions should not be handled by parser code. + output_result(body_file, header_only, as_attachment); } uint Request::register_file(String::Body file_spec) { @@ -612,9 +643,9 @@ void Request::use_file(const String& fil } else throw Exception(PARSER_RUNTIME, 0, "$" CLASS_PATH_NAME " must be string or table"); if(!filespec) - throw Exception(PARSER_RUNTIME, &file_name, "not found along $main:" CLASS_PATH_NAME); + throw Exception(PARSER_RUNTIME, &file_name, "not found along $MAIN:" CLASS_PATH_NAME); } else - throw Exception(PARSER_RUNTIME, &file_name, "usage failed - no $main:" 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); @@ -791,6 +822,7 @@ struct Send_range_action_info { 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_header(info.r->sapi_info); pa_lseek(f, info.offset, SEEK_SET); const size_t BUFSIZE = 128*0x400; @@ -830,7 +862,8 @@ static void output_pieces(Request& r, bo if(rg.start == UNSET && rg.end != UNSET){ if(rg.end > content_length) rg.end = content_length; - rg = { content_length - rg.end, content_length-1 }; + rg.start = content_length - rg.end; + rg.end = content_length-1; } else if(rg.start != UNSET && rg.end == UNSET){ if(rg.start >= content_length) return SAPI::send_error(r.sapi_info, "", "416"); @@ -850,7 +883,7 @@ static void output_pieces(Request& r, bo SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "206"); SAPI::add_header_attribute(r.sapi_info, "content-range", buf); } else { - return SAPI::send_error(r.sapi_info, count ? "Multiple ranges not supported" : "Invalid range", count ? "501" : "400"); + return SAPI::send_error(r.sapi_info, count ? "Multiple ranges are not supported" : "Invalid range", count ? "501" : "400"); } } @@ -859,9 +892,9 @@ static void output_pieces(Request& r, bo 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::send_header(r.sapi_info); - - if(!header_only){ + if(header_only){ + SAPI::send_header(r.sapi_info); + } else { Send_range_action_info info = { &r, offset, part_length}; file_read_action_under_lock(r.full_disk_path(filename), "send", send_range, &info); } @@ -881,7 +914,7 @@ void Request::output_result(VFile* body_ if(sname && *sname == NONAME_DAT) sname = NULL; - // Content-Disposition + // Content-Disposition, use $.name[] to avoid const String* disposition_name = sname ? sname->is_empty() ? NULL : sname : sfile; if(disposition_name) { VHash& hash=*new VHash();