--- parser3/src/main/pa_request.C 2004/10/05 10:55:36 1.271 +++ parser3/src/main/pa_request.C 2007/10/22 13:44:59 1.288 @@ -1,11 +1,11 @@ /** @file Parser: request class main part. @see compile.C and execute.C. - Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_REQUEST_C="$Date: 2004/10/05 10:55:36 $"; +static const char * const IDENT_REQUEST_C="$Date: 2007/10/22 13:44:59 $"; #include "pa_sapi.h" #include "pa_common.h" @@ -53,21 +53,20 @@ const char* ORIGINS_CONTENT_TYPE="text/p #define EXCEPTION_TYPE_PART_NAME "type" #define EXCEPTION_SOURCE_PART_NAME "source" #define EXCEPTION_COMMENT_PART_NAME "comment" -#define RESPONSE_BODY_FILE_NAME "file" // globals const String main_method_name(MAIN_METHOD_NAME); const String auto_method_name(AUTO_METHOD_NAME); const String content_disposition_name(CONTENT_DISPOSITION_NAME); -const String content_disposition_value(CONTENT_DISPOSITION_VALUE); +const String content_disposition_inline(CONTENT_DISPOSITION_INLINE); +const String content_disposition_attachment(CONTENT_DISPOSITION_ATTACHMENT); const String content_disposition_filename_name(CONTENT_DISPOSITION_FILENAME_NAME); const String body_name(BODY_NAME); const String exception_type_part_name(EXCEPTION_TYPE_PART_NAME); const String exception_source_part_name(EXCEPTION_SOURCE_PART_NAME); const String exception_comment_part_name(EXCEPTION_COMMENT_PART_NAME); const String exception_handled_part_name(EXCEPTION_HANDLED_PART_NAME); -const String response_body_file_name(RESPONSE_BODY_FILE_NAME); // defines for statics @@ -78,6 +77,7 @@ const String response_body_file_name(RES #define POST_PROCESS_METHOD_NAME "postprocess" #define DOWNLOAD_NAME "download" #define CLASS_PATH_NAME "CLASS_PATH" +#define RESPONSE_BODY_FILE_NAME "file" // statics @@ -89,6 +89,7 @@ static const String conf_method_name(CON static const String post_process_method_name(POST_PROCESS_METHOD_NAME); static const String download_name(DOWNLOAD_NAME); static const String class_path_name(CLASS_PATH_NAME); +static const String response_body_file_name(RESPONSE_BODY_FILE_NAME); // defines @@ -107,7 +108,7 @@ Request::Request(SAPI_Info& asapi_info, wcontext(0), flang(adefault_lang), fconnection(0), - finterrupted(false), + finterrupted(false),fskip(SKIP_NOTHING), // public #ifdef RESOURCES_DEBUG @@ -125,6 +126,7 @@ Request::Request(SAPI_Info& asapi_info, mail(*new VMail), response(*new VResponse(arequest_info, charsets)), cookie(*new VCookie), + console(*new VConsole), // private configure_admin_done(false), @@ -161,8 +163,7 @@ Request::Request(SAPI_Info& asapi_info, // cookie class classes().put(String::Body(COOKIE_CLASS_NAME), &cookie); // console class - classes().put(String::Body(CONSOLE_CLASS_NAME), new VConsole()); - + classes().put(String::Body(CONSOLE_CLASS_NAME), &console); /// methoded // response class classes().put(response.get_class()->name(), &response); @@ -204,7 +205,7 @@ static void load_charset(HashStringValue } void Request::configure_admin(VStateless_class& conf_class) { if(configure_admin_done) - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, "parser already configured"); configure_admin_done=true; @@ -220,9 +221,9 @@ void Request::configure_admin(VStateless if(Value* vcharsets=conf_class.get_element(charsets_name, conf_class, false)) { if(!vcharsets->is_string()) if(HashStringValue* charsets=vcharsets->get_hash()) - charsets->for_each(load_charset, &this->charsets); + charsets->for_each(load_charset, &this->charsets); else - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":" CHARSETS_NAME " must be hash"); } @@ -271,6 +272,20 @@ const char* Request::get_exception_cstr( return result; } + +void Request::configure() { + // configure admin options if not configured yet + if(!configure_admin_done) + configure_admin(main_class); + + // configure not-admin=user options + methoded_array().configure_user(*this); + + // $MAIN:MIME-TYPES + if(Value* element=main_class.get_element(mime_types_name, main_class, false)) + if(Table *table=element->get_table()) + mime_types=table; +} /** load MAIN class, execute @main. MAIN class consists of all the auto.p files we'd manage to find @@ -337,25 +352,20 @@ gettimeofday(&mt[0],NULL); } } - // compile requested file - String& spath_translated=*new String; - spath_translated.append_help_length(request_info.path_translated, 0, String::L_TAINTED); - use_file(main_class, - spath_translated, - 0 /*main_alias*/, - true /*ignore class_path*/); - - // configure method_frame options if not configured yet - if(!configure_admin_done) - configure_admin(main_class); - - // configure not-method_frame=user options - methoded_array().configure_user(*this); - - // $MAIN:MIME-TYPES - if(Value* element=main_class.get_element(mime_types_name, main_class, false)) - if(Table *table=element->get_table()) - mime_types=table; + try { + // compile requested file + String& spath_translated=*new String; + spath_translated.append_help_length(request_info.path_translated, 0, String::L_TAINTED); + use_file(main_class, + spath_translated, + 0 /*main_alias*/, + true /*ignore class_path*/); + + configure(); + } catch(...) { + configure(); // configure anyway, useful in @unhandled_exception [say, if they would want to mail by SMTP something] + rethrow; + } #ifdef RESOURCES_DEBUG //measure:after compile @@ -364,7 +374,7 @@ gettimeofday(&mt[1],NULL); // execute @main[] const String* body_string=execute_virtual_method(main_class, main_method_name); if(!body_string) - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, "'"MAIN_METHOD_NAME"' method not found"); @@ -380,6 +390,13 @@ gettimeofday(&mt[2],NULL); body_value=response.fields().get(body_name); // $response:body if(!body_value) body_value=new VString(*body_string); // just result of ^main[] + // ensure that body_value has no just L_TAINTED parts left + if(body_value->is_string()) + { + String& untainted=*new String(); + untainted.append(*body_value->get_string(), flang); + body_value=new VString(untainted); + } // @postprocess if(Value* value=main_class.get_element(post_process_method_name, main_class, false)) @@ -429,8 +446,6 @@ t[9]-t[3] Request::Exception_details details=get_details(e); const char* exception_cstr=get_exception_cstr(e, details); - // log the beast - SAPI::log(sapi_info, "%s", exception_cstr); // reset language to default flang=fdefault_lang; @@ -449,39 +464,41 @@ t[9]-t[3] false)) { if(Junction* junction=value->get_junction()) { if(const Method *method=junction->method) { -// preparing to pass parameters to -// @unhandled_exception[exception;stack] -VMethodFrame frame(/*method->name, */ *junction, 0/*no caller*/); -frame.set_self(main_class); - -// $exception -frame.store_param(details.vhash); -// $stack[^table::set{name origin}] -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(); iname, */ *junction, 0/*no caller*/); + frame.set_self(main_class); + + // $exception + frame.store_param(details.vhash); + // $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); + } + // ERROR. write it out output_result(body_file, header_only, false); - } catch(const Exception& e) { + + } 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 + SAPI::log(sapi_info, "%s", exception_cstr); throw Exception(0, 0, @@ -537,25 +563,25 @@ void Request::use_file(VStateless_class& file_spec=0; if(Value* element=main_class.get_element(class_path_name, main_class, false)) { if(element->is_string()) { - file_spec=file_readable(absolute(element->as_string()), file_name); // found at class_path? + file_spec=file_exist(absolute(element->as_string()), file_name); // found at class_path? } else if(Table *table=element->get_table()) { int size=table->count(); for(int i=size; i--; ) { const String& path=*(*table->get(i))[0]; - if((file_spec=file_readable(absolute(path), file_name))) + if((file_spec=file_exist(absolute(path), file_name))) break; // found along class_path } } else - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, "$" CLASS_PATH_NAME " must be string or table"); if(!file_spec) - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, &file_name, "not found along " MAIN_CLASS_NAME ":" CLASS_PATH_NAME); } if(!file_spec) - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, &file_name, "usage failed - no $" MAIN_CLASS_NAME ":" CLASS_PATH_NAME " were specified"); } @@ -798,22 +824,25 @@ void Request::output_result(VFile* body_ Value* vfile_name=body_file->fields().get(name_name); if(!vfile_name) { vfile_name=body_file->fields().get(response_body_file_name); - const String& sfile_name=vfile_name->as_string(); + if(vfile_name) + { + const String& sfile_name=vfile_name->as_string(); - char* name_cstr=sfile_name.cstrm(); - if(char *after_slash=rsplit(name_cstr, '\\')) - name_cstr=after_slash; - if(char *after_slash=rsplit(name_cstr, '/')) - name_cstr=after_slash; - vfile_name=new VString(*new String(name_cstr)); + char* name_cstr=sfile_name.cstrm(); + if(char *after_slash=rsplit(name_cstr, '\\')) + name_cstr=after_slash; + if(char *after_slash=rsplit(name_cstr, '/')) + name_cstr=after_slash; + vfile_name=new VString(*new String(name_cstr)); + } } if(vfile_name) { const String& sfile_name=vfile_name->as_string(); if(sfile_name!=NONAME_DAT) { VHash& hash=*new VHash(); HashStringValue &h=hash.hash(); - if(as_attachment) - h.put(value_name, new VString(content_disposition_value)); + h.put(value_name, new VString( as_attachment ? content_disposition_attachment : content_disposition_inline )); + h.put(content_disposition_filename_name, vfile_name); response.fields().put(content_disposition_name, &hash); @@ -834,7 +863,7 @@ void Request::output_result(VFile* body_ // prepare header: $response:fields without :body Add_header_attribute_info info(*this); - response.fields().for_each(add_header_attribute, &info); + response.fields().for_each(add_header_attribute, &info); if(body_file_content_type) if(HashStringValue *hash=body_file_content_type->get_hash()) @@ -853,7 +882,7 @@ void Request::output_result(VFile* body_ if(Value* vdatep=v->as(VDATE_TYPE, false)) vdate=static_cast(vdatep); else - throw Exception("parser.runtime", 0, "mdate must be a date"); + throw Exception(PARSER_RUNTIME, 0, "mdate must be a date"); } if(!vdate) vdate=new VDate(mtime); @@ -878,7 +907,7 @@ const String& Request::mime_type_of(cons if(const String* result=mime_types->item(1)) return *result; else - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, MIME_TYPES_NAME " table column elements must not be empty"); } @@ -887,19 +916,15 @@ const String& Request::mime_type_of(cons } #ifdef XML -GdomeDOMString_auto_ptr Request::transcode(const String& s) { - return charsets.source().transcode(s); -} - -GdomeDOMString_auto_ptr Request::transcode(const String::Body s) { +xmlChar* Request::transcode(const String& s) { return charsets.source().transcode(s); } -const String& Request::transcode(GdomeDOMString* s) { +xmlChar* Request::transcode(const String::Body s) { return charsets.source().transcode(s); } -const String& Request::transcode(xmlChar* s) { +const String& Request::transcode(const xmlChar* s) { return charsets.source().transcode(s); } #endif @@ -910,13 +935,15 @@ const Request::Trace Request::Exception_ if(!is_empty()) { result=bottom_value(); if(!problem_source) { // we don't know who trigged the bug? - problem_source=result.name(); // consider the stack-top-guy did that + problem_source=result.name(); // consider the stack-top-guy (we usually know source of next-from-throw-point exception) did that fbottom++; } else if(result.name()==problem_source) // it is that same guy? fbottom++; // throw away that trace - // else stack top contains not us, - // leaving result intact - // it would help ^throw + else { + // stack top contains not us, + // leaving result intact + // it would help ^throw + } } return result;