--- parser3/src/main/pa_request.C 2001/12/15 21:28:21 1.186 +++ parser3/src/main/pa_request.C 2002/04/29 05:59:33 1.205 @@ -1,10 +1,10 @@ /** @file Parser: request class main part. @see compile.C and execute.C. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://paf.design.ru) + Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) - $Id: pa_request.C,v 1.186 2001/12/15 21:28:21 paf Exp $ + $Id: pa_request.C,v 1.205 2002/04/29 05:59:33 paf Exp $ */ #include "pa_sapi.h" @@ -30,14 +30,12 @@ const char *DEFAULT_CONTENT_TYPE="text/h const char *ORIGINS_CONTENT_TYPE="text/plain"; Methoded *MOP_create(Pool&); +// op.C +VHash& exception2vhash(Pool& pool, const Exception& e); static void load_charset(const Hash::Key& akey, Hash::Val *avalue, void *) { - Value& value=*static_cast(avalue); - - if(!charsets->get(akey)) // don't we know that charset? - charsets->put(akey, - new(charsets->pool()) Charset(charsets->pool(), akey, &value.as_string())); + charsets->load_charset(akey, static_cast(avalue)->as_string()); } // @@ -62,13 +60,14 @@ Request::Request(Pool& apool, default_content_type(0), mime_types(0), main_class(0), - connection(0), + fconnection(0), classes_conf(apool), anti_endless_execute_recoursion(0), - trace(apool) + exception_trace(apool) #ifdef RESOURCES_DEBUG - ,sql_connect_time(0),sql_request_time(0) + , sql_connect_time(0),sql_request_time(0) #endif + , self(0), root(0), rcontext(0), wcontext(0) { // default charsets pool().set_source_charset(*utf8_charset); @@ -106,6 +105,17 @@ Request::Request(Pool& apool, classes().put(math.get_class()->base()->name(), &math); } +Request::~Request() { +#ifdef XML + // if for some strange reason xml generic errors failed to be reported, free them up + if(const char *xml_generic_errors=xmlGenericErrors()) { + SAPI::log(pool(), "warning: unreported xmlGenericErrors: %s", xml_generic_errors); + free((void *)xml_generic_errors); + } +#endif +} + + /** load MAIN class, execute @main. MAIN class consists of all the auto.p files we'd manage to find @@ -152,9 +162,9 @@ gettimeofday(&mt[0],NULL); if(Hash *charsets=vcharsets->get_hash(0)) charsets->for_each(load_charset); else - throw Exception(0, 0, - &vcharsets->name(), - "must be hash"); + throw Exception("parser.runtime", + 0, + "$" CHARSETS_NAME " must be hash"); } } // configure root options @@ -228,10 +238,9 @@ gettimeofday(&mt[0],NULL); gettimeofday(&mt[1],NULL); #endif // execute @main[] - const String *body_string=execute_virtual_method( - *main_class, *main_method_name); + const String *body_string=execute_virtual_method(*main_class, *main_method_name); if(!body_string) - throw Exception(0,0, + throw Exception("parser.runtime", 0, "'"MAIN_METHOD_NAME"' method not found"); @@ -248,26 +257,25 @@ gettimeofday(&mt[2],NULL); if(const Method *method=junction->method) { // preparing to pass parameters to // @postprocess[data] - VMethodFrame frame(pool(), value->name(), *junction); + VMethodFrame frame(pool(), method->name, *junction); frame.set_self(*main_class); - frame.store_param(method->name, - &body_vstring_before_post_process); + frame.store_param(&body_vstring_before_post_process); body_vstring_after_post_process= - NEW VString(*execute_method(frame, *method)); + NEW VString(execute_method(frame, *method)); } - bool origins_mode=main_class->get_element(*origins_mode_name)!=0; + bool lorigins_mode=origins_mode(); const VFile *body_file=body_vstring_after_post_process->as_vfile( - String::UL_UNSPECIFIED, origins_mode); + String::UL_UNSPECIFIED, lorigins_mode); // extract response body Value *body_value=static_cast( response.fields().get(*body_name)); if(body_value) // there is some $response.body body_file=body_value->as_vfile(); - else if(origins_mode) + else if(lorigins_mode) response.fields().put(*content_type_name, NEW VString(*NEW String(pool(), ORIGINS_CONTENT_TYPE))); @@ -301,28 +309,29 @@ t[9]-t[3] // we're returning not result, but error explanation try { // log the beast - const String *problem_source=e.problem_source(); - if(problem_source && problem_source->size()) + if(const String *problem_source=e.problem_source()) SAPI::log(pool(), + "%s: " #ifndef NO_STRING_ORIGIN ORIGIN_FILE_LINE_FORMAT": " #endif - "'%s' %s [%s %s]", + "'%s' %s [%s]", + info.uri, #ifndef NO_STRING_ORIGIN problem_source->origin().file?problem_source->origin().file:"global", problem_source->origin().line, #endif problem_source->cstr(), e.comment(), - e.type()?e.type()->cstr():"-", - e.code()?e.code()->cstr():"-" + e.type()?e.type():"-" ); else SAPI::log(pool(), - "%s [%s %s]", + "%s: " + "%s [%s]", + info.uri, e.comment(), - e.type()?e.type()->cstr():"-", - e.code()?e.code()->cstr():"-" + e.type()?e.type():"-" ); // reset language to default @@ -337,75 +346,23 @@ t[9]-t[3] if(main_class) { // we've managed to end up with some main_class // maybe we'd be lucky enough as to report an error // in a gracefull way... - if(Value *value=main_class->get_element(*exception_method_name)) + if(Value *value=main_class->get_element(*unhandled_exception_method_name)) if(Junction *junction=value->get_junction()) if(const Method *method=junction->method) { // preparing to pass parameters to - // @exception[origin;source;comment;type;code;stack] - VMethodFrame frame(pool(), value->name(), *junction); + // @unhandled_exception[exception;stack] + VMethodFrame frame(pool(), method->name, *junction); frame.set_self(*main_class); - const String *problem_source=e.problem_source(); - // origin - Value *origin_value=0; -#ifndef NO_STRING_ORIGIN - if(problem_source && problem_source->size()) { - const Origin& origin=problem_source->origin(); - if(origin.file) { - char *buf=(char *)malloc(MAX_STRING); - size_t buf_size=snprintf(buf, MAX_STRING, ORIGIN_FILE_LINE_FORMAT, - origin.file, 1+origin.line); - origin_value=NEW VString(*NEW String(pool(), - buf, buf_size, true)); - } - } -#endif - frame.store_param(method->name, - origin_value?origin_value:NEW VVoid(pool())); - - // source - Value *source_value=0; - if(problem_source && problem_source->size()) { - String& problem_source_copy=*NEW String(pool()); - problem_source_copy.append(*problem_source, - flang, true); - source_value=NEW VString(problem_source_copy); - } - frame.store_param(method->name, - source_value?source_value:NEW VVoid(pool())); - - // comment - String *comment_value=NEW String(pool(), - e.comment(), 0, true); - frame.store_param(method->name, - NEW VString(*comment_value)); - - // type - Value *type_value; - if(e.type()) { - String& type_copy=*NEW String(pool()); - type_value=NEW VString(type_copy.append(*e.type(), - flang, true)); - } else - type_value=NEW VVoid(pool()); - frame.store_param(method->name, type_value); - - // code - Value *code_value; - if(e.code()) { - String& code_copy=*NEW String(pool()); - code_value=NEW VString(code_copy.append(*e.code(), - flang, true)); - } else - code_value=NEW VVoid(pool()); - frame.store_param(method->name, code_value); - + // $exception + frame.store_param(&exception2vhash(pool(), e)); // $stack[^table::set{name origin}] Array& stack_trace_columns=*NEW Array(pool()); stack_trace_columns+=NEW String(pool(), "name"); - stack_trace_columns+=NEW String(pool(), "origin"); + stack_trace_columns+=NEW String(pool(), "file"); + stack_trace_columns+=NEW String(pool(), "lineno"); Table& stack_trace=*NEW Table(pool(), 0, &stack_trace_columns); - Array_iter tracei(trace); + Array_iter tracei(exception_trace); while(tracei.has_next()) { Array& row=*NEW Array(pool()); @@ -414,20 +371,19 @@ t[9]-t[3] #ifndef NO_STRING_ORIGIN const Origin& origin=name->origin(); if(origin.file) { - char *buf=(char *)malloc(MAX_STRING); - size_t buf_size=snprintf(buf, MAX_STRING, ORIGIN_FILE_LINE_FORMAT, - origin.file, 1+origin.line); - row+=NEW String(pool(), buf, buf_size, true); // origin column + row+=NEW String(pool(), origin.file, 0, true); // file column + char *buf=(char *)malloc(MAX_NUMBER); + size_t buf_size=snprintf(buf, MAX_NUMBER, "%d", 1+origin.line); + row+=NEW String(pool(), buf, buf_size, true); // line column } #endif stack_trace+=&row; } - frame.store_param(method->name, - NEW VTable(pool(), &stack_trace)); + frame.store_param(NEW VTable(pool(), &stack_trace)); // future $response:body= // execute ^exception[origin;source;comment;type;code;stack] - body_string=execute_method(frame, *method); + body_string=&execute_method(frame, *method); } } @@ -437,8 +393,7 @@ t[9]-t[3] // make up result: $origin $source $comment $type $code char *buf=(char *)malloc(MAX_STRING); size_t printed=0; - const String *problem_source=e.problem_source(); - if(problem_source) { + if(const String *problem_source=e.problem_source()) { #ifndef NO_STRING_ORIGIN const Origin& origin=problem_source->origin(); if(origin.file) @@ -452,15 +407,8 @@ t[9]-t[3] } printed+=snprintf(buf+printed, MAX_STRING-printed, "%s", e.comment()); - const String *type=e.type(); - if(type) { - printed+=snprintf(buf+printed, MAX_STRING-printed, " type: %s", - type->cstr()); - const String *code=e.code(); - if(code) - printed+=snprintf(buf+printed, MAX_STRING-printed, ", code: %s", - code->cstr()); - } + if(const char *type=e.type()) + printed+=snprintf(buf+printed, MAX_STRING-printed, " type: %s", type); // future $response:content-type response.fields().put(*content_type_name, @@ -499,27 +447,27 @@ VStateless_class *Request::use_file(cons if(main_class) if(Value *element=main_class->get_element(*class_path_name)) { if(element->is_string()) { - file_spec=file_readable(element->as_string(), file_name); // found at class_path? + file_spec=file_readable(absolute(element->as_string()), file_name); // found at class_path? } else if(Table *table=element->get_table()) { int size=table->size(); for(int i=size; i--; ) { const String& path=*static_cast(table->get(i))->get_string(0); - if(file_spec=file_readable(path, file_name)) + if(file_spec=file_readable(absolute(path), file_name)) break; // found along class_path } } else - throw Exception(0, 0, - &element->name(), - "must be string or table"); + throw Exception("parser.runtime", + 0, + "$" CLASS_PATH_NAME " must be string or table"); if(!file_spec) - throw Exception(0, 0, + throw Exception("parser.runtime", &file_name, "not found along " MAIN_CLASS_NAME ":" CLASS_PATH_NAME); } if(!file_spec) - throw Exception(0, 0, + throw Exception("parser.runtime", &file_name, - "usage failed - no " MAIN_CLASS_NAME ":" CLASS_PATH_NAME " were specified"); + "usage failed - no $" MAIN_CLASS_NAME ":" CLASS_PATH_NAME " were specified"); } char *source=file_read_text(pool(), *file_spec, fail_on_read_problem); @@ -611,11 +559,9 @@ void Request::output_result(const VFile& ); // prepare header: content-length - if(client_content_length) { // useful for redirecting [header "location: http://..."] - char content_length_cstr[MAX_NUMBER]; - snprintf(content_length_cstr, MAX_NUMBER, "%u", client_content_length); - SAPI::add_header_attribute(pool(), "content-length", content_length_cstr); - } + char content_length_cstr[MAX_NUMBER]; + snprintf(content_length_cstr, MAX_NUMBER, "%u", client_content_length); + SAPI::add_header_attribute(pool(), "content-length", content_length_cstr); // send header SAPI::send_header(pool()); @@ -633,7 +579,7 @@ const String& Request::mime_type_of(cons if(const String *result=mime_types->item(1)) return *result; else - throw Exception(0, 0, + throw Exception("parser.runtime", mime_types->origin_string(), "MIME-TYPE table column elements must not be empty"); }