--- parser3/src/main/pa_request.C 2020/12/15 12:16:04 1.403 +++ parser3/src/main/pa_request.C 2021/01/02 10:40:08 1.413 @@ -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.413 2021/01/02 10:40:08 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H; // consts @@ -129,8 +129,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 +233,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()); } @@ -380,6 +386,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 +438,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 +460,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 +476,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 +483,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) { @@ -830,7 +840,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 +861,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"); } } @@ -881,7 +892,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();