--- parser3/src/main/pa_request.C 2004/07/30 10:02:31 1.268 +++ parser3/src/main/pa_request.C 2007/10/22 16:38:36 1.289 @@ -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/07/30 10:02:31 $"; +static const char * const IDENT_REQUEST_C="$Date: 2007/10/22 16:38:36 $"; #include "pa_sapi.h" #include "pa_common.h" @@ -36,7 +36,7 @@ static const char * const IDENT_REQUEST_ // consts -const char* UNHANDLED_EXCEPTION_METHOD_NAME="unhandled_exception"; +#define UNHANDLED_EXCEPTION_METHOD_NAME "unhandled_exception" /// content type of exception response, when no @MAIN:exception handler defined const char* UNHANDLED_EXCEPTION_CONTENT_TYPE="text/plain"; @@ -53,21 +53,21 @@ 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_transfer_encoding_name(CONTENT_TRANSFER_ENCODING_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 +78,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 +90,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 +109,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 +127,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 +164,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 +206,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 +222,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"); } @@ -232,6 +234,59 @@ void Request::configure_admin(VStateless methoded_array().configure_admin(*this); } +const char* Request::get_exception_cstr( + const Exception& e, + Request::Exception_details& details) { +#define PA_URI_FORMAT "%s: " +#define PA_ORIGIN_FILE_POS_FORMAT "%s(%d:%d): " +#define PA_SOURCE_FORMAT "'%s' " +#define PA_COMMENT_TYPE_FORMAT "%s [%s]" + char* result=new(PointerFreeGC) char[MAX_STRING]; + + if(details.problem_source) { // do we know the guy? + if(details.trace) { // do whe know where he came from? + Operation::Origin origin=details.trace.origin(); + snprintf(result, MAX_STRING, + PA_URI_FORMAT + PA_ORIGIN_FILE_POS_FORMAT + PA_SOURCE_FORMAT PA_COMMENT_TYPE_FORMAT, + request_info.uri, + file_list[origin.file_no].cstr(), 1+origin.line, 1+origin.col, + details.problem_source->cstr(), + e.comment(), e.type() + ); + } else + snprintf(result, MAX_STRING, + PA_URI_FORMAT + PA_SOURCE_FORMAT PA_COMMENT_TYPE_FORMAT, + request_info.uri, + details.problem_source->cstr(), + e.comment(), e.type() + ); + } else + snprintf(result, MAX_STRING, + PA_URI_FORMAT + PA_COMMENT_TYPE_FORMAT, + request_info.uri, + e.comment(), e.type() + ); + + 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 @@ -251,6 +306,12 @@ struct timeval mt[10]; gettimeofday(&mt[0],NULL); #endif try { + // filling cookies + cookie.fill_fields(request_info); + + // filling mail received + mail.fill_received(*this); + // loading config if(config_filespec) { const String& filespec=*new String(config_filespec); @@ -292,31 +353,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; - - // filling cookies - cookie.fill_fields(request_info); - - // filling mail received - mail.fill_received(*this); + 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 @@ -325,7 +375,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"); @@ -341,6 +391,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)) @@ -387,40 +444,9 @@ t[9]-t[3] } catch(const Exception& e) { // request handling problem try { // we're returning not result, but error explanation -#define PA_URI_FORMAT "%s: " -#define PA_ORIGIN_FILE_POS_FORMAT "%s(%d:%d): " -#define PA_SOURCE_FORMAT "'%s' " -#define PA_COMMENT_TYPE_FORMAT "%s [%s]" - // log the beast - Request::Exception_details details=get_details(e); - if(details.problem_source) { // do we know the guy? - if(details.trace) { // do whe know where he came from? - Operation::Origin origin=details.trace.origin(); - SAPI::log(sapi_info, - PA_URI_FORMAT - PA_ORIGIN_FILE_POS_FORMAT - PA_SOURCE_FORMAT PA_COMMENT_TYPE_FORMAT, - request_info.uri, - file_list[origin.file_no].cstr(), 1+origin.line, 1+origin.col, - details.problem_source->cstr(), - e.comment(), e.type() - ); - } else - SAPI::log(sapi_info, - PA_URI_FORMAT - PA_SOURCE_FORMAT PA_COMMENT_TYPE_FORMAT, - request_info.uri, - details.problem_source->cstr(), - e.comment(), e.type() - ); - } else - SAPI::log(sapi_info, - PA_URI_FORMAT - PA_COMMENT_TYPE_FORMAT, - request_info.uri, - e.comment(), e.type() - ); + Request::Exception_details details=get_details(e); + const char* exception_cstr=get_exception_cstr(e, details); // reset language to default flang=fdefault_lang; @@ -439,39 +465,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); -// future $response:body= -// execute ^unhandled_exception[exception;stack] -body_string=&execute_method(frame, *method).as_string(); + // $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(); icstr()); - if(const char* comment=e.comment(true)) - printed+=snprintf(buf+printed, MAX_STRING-printed, "%s", comment); - if(const char* type=e.type(true)) - printed+=snprintf(buf+printed, MAX_STRING-printed, " type: %s", type); - // future $response:content-type response.fields().put(content_type_name, new VString(*new String(UNHANDLED_EXCEPTION_CONTENT_TYPE))); // future $response:body - body_string=new String(buf); + body_string=new String(exception_cstr); } VString body_vstring(*body_string); VFile* body_file=body_vstring.as_vfile(String::L_UNSPECIFIED, &charsets); + // 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); + } + // ERROR. write it out output_result(body_file, header_only, false); - } catch(const Exception& e) { - throw Exception(e.type(), - e.problem_source(), - "in request exception handler: %s", - e.comment()); + + } 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, + "in %s", + exception_cstr); } } } @@ -535,25 +564,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"); } @@ -649,139 +678,6 @@ static void add_header_attribute( info->add_last_modified = false; } -/* - if(v = opts->get("mdate")){ - ++valid_options; - if(Value* vdate=v->as(VDATE_TYPE, false)) - date=static_cast(vdate); - else throw Exception("parser.runtime", 0, "mdate must be a date"); - } - if(v = opts->get("disposition")){ - ++valid_options; - if(!v->is("string")) throw Exception("parser.runtime", 0, "disposition must be a string"); - disposition = v->get_string()->cstr(); - if(strcmp(disposition, "inline") && strcmp(disposition, "attachment")) throw Exception("parser.runtime", 0, "disposition can be only 'inline' or 'attachment'"); - } - if(valid_options != opts->count()) - throw Exception("parser.runtime", 0, "invalid option passed"); - } - - auto_file f = fopen(c_from_file_name, "rb"); - if(f == 0) - throw Exception("parser.runtime", 0, "Can't open file"); - - if(fseek(f, 0, SEEK_END)!=0) - throw Exception("parser.runtime", 0, "Can't seek file"); - - size_t file_length = (size_t)ftell(f); - if(file_length == (size_t)-1) - throw Exception("parser.runtime", 0, "can't get file size"); - if(file_length <= offset) - throw Exception("parser.runtime", 0, "offset too big"); - - size_t content_length = file_length-offset; - if(limit != (size_t)-1) - content_length = limit ar; - parse_range(new String(range), ar); - size_t count = ar.count(); - if(count == 1){ - Range &rg = ar.get_ref(0); - if(rg.start == (size_t)-1 && rg.end == (size_t)-1){ - SAPI::add_header_attribute(r.sapi_info, "status", "416 Requested Range Not Satisfiable"); - return; - } - if(rg.start == (size_t)-1 && rg.end != (size_t)-1){ - rg.start = content_length - rg.end; - rg.end = content_length; - offset += rg.start; - part_length = rg.end-rg.start; - }else if(rg.start != (size_t)-1 && rg.end == (size_t)-1){ - rg.end = content_length-1; - offset += rg.start; - part_length -= rg.start; - } - if(part_length == 0){ - SAPI::add_header_attribute(r.sapi_info, "status", "204 No Content"); - return; - } - SAPI::add_header_attribute(r.sapi_info, "status", "206 Partial Content"); - snprintf((char*)buf, BUFSIZE, "bytes %u-%u/%u", rg.start, rg.end, content_length); - SAPI::add_header_attribute(r.sapi_info, "Content-Range", (char*)buf); - }else if(count != 0){ - SAPI::add_header_attribute(r.sapi_info, "status", "501 Not Implemented"); - return; - } - } - - fseek(f, offset, SEEK_SET); - snprintf((char*)buf, BUFSIZE, "%u", part_length); - SAPI::add_header_attribute(r.sapi_info, "Content-Length", (char*)buf); - - if(info.add_content_disposition && disposition){ - const char *fname = 0; - if(to_file_name){ - fname = to_file_name->as_string().cstr(); - }else{ - const char *fname = c_from_file_name; - const char *p1 = strrchr(fname, '/'); - const char *p2 = strrchr(fname, '\\'); - if(p1 || p2) - fname = max(p1, p2)+1; - } - - snprintf((char*)buf, BUFSIZE, "%s; filename=\"%s\"", disposition, fname); - SAPI::add_header_attribute(r.sapi_info, "Content-Disposition", (char*)buf); - } - if(info.add_content_type) - SAPI::add_header_attribute(r.sapi_info, "Content-Type", r.mime_type_of(c_from_file_name).cstr()); - if(info.add_last_modified){ - if(date == 0){ - struct stat st; - if(stat(c_from_file_name, &st)!=0) throw Exception("parser.runtime", 0, "can't get file stat"); - date = new VDate(st.st_mtime); - } - const String &s = attributed_meaning_to_string(*date, String::L_AS_IS, true); - SAPI::add_header_attribute(r.sapi_info, "Last-Modified", s.cstr()); - } - r.cookie.output_result(r.sapi_info); - SAPI::send_header(r.sapi_info); - - const char* request_method=getenv("REQUEST_METHOD"); - bool header_only=request_method && strcasecmp(request_method, "HEAD")==0; - size_t sent = 0; - if(!header_only){ - size_t to_read = 0; - size_t size = 0; - do{ - to_read = part_lengthfields().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); @@ -963,7 +864,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()) @@ -982,7 +883,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); @@ -1007,7 +908,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"); } @@ -1016,19 +917,15 @@ const String& Request::mime_type_of(cons } #ifdef XML -GdomeDOMString_auto_ptr Request::transcode(const String& s) { +xmlChar* Request::transcode(const String& s) { return charsets.source().transcode(s); } -GdomeDOMString_auto_ptr Request::transcode(const String::Body s) { +xmlChar* Request::transcode(const String::Body s) { return charsets.source().transcode(s); } -const String& Request::transcode(GdomeDOMString* 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 @@ -1039,13 +936,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;