--- parser3/src/main/pa_request.C 2020/11/10 22:42:26 1.388 +++ parser3/src/main/pa_request.C 2020/11/23 22:45:11 1.394 @@ -33,7 +33,7 @@ #include "pa_vconsole.h" #include "pa_vdate.h" -volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.388 2020/11/10 22:42:26 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.394 2020/11/23 22:45:11 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H; // consts @@ -44,6 +44,7 @@ const char* DEFAULT_CONTENT_TYPE="text/h const uint LOOP_LIMIT=20000; const uint EXECUTE_RECOURSION_LIMIT=1000; +const uint HTTPD_TIMEOUT=4; const size_t FILE_SIZE_LIMIT=512*1024*1024; // defines for globals @@ -75,6 +76,7 @@ static const String origin_key(ORIGIN_KE int pa_loop_limit=LOOP_LIMIT; int pa_execute_recoursion_limit=EXECUTE_RECOURSION_LIMIT; +int pa_httpd_timeout=HTTPD_TIMEOUT; size_t pa_file_size_limit=FILE_SIZE_LIMIT; // defines for statics @@ -86,6 +88,7 @@ size_t pa_file_size_limit=FILE_SIZE_LIMI #define LIMITS_NAME "LIMITS" #define LOOP_LIMIT_NAME "max_loop" #define RECOURSION_LIMIT_NAME "max_recoursion" +#define HTTPD_TIMEOUT_NAME "httpd_timeout" #define FILE_SIZE_LIMIT_NAME "max_file_size" #define LOCK_WAIT_TIMEOUT_NAME "lock_wait_timeout" #define CONF_METHOD_NAME "conf" @@ -106,6 +109,7 @@ static const String prototype_name(PROTO static const String limits_name(LIMITS_NAME); static const String loop_limit_name(LOOP_LIMIT_NAME); static const String recoursion_limit_name(RECOURSION_LIMIT_NAME); +static const String httpd_timeout_name(HTTPD_TIMEOUT_NAME); static const String file_size_limit_name(FILE_SIZE_LIMIT_NAME); static const String lock_wait_timeout_name(LOCK_WAIT_TIMEOUT_NAME); @@ -121,6 +125,7 @@ static const String body_name_upper(BODY static const String content_type_name_upper(HTTP_CONTENT_TYPE_UPPER); static const String content_disposition_name_upper(CONTENT_DISPOSITION_UPPER); +static const String content_disposition_inline(CONTENT_DISPOSITION_INLINE); static const String content_disposition_attachment(CONTENT_DISPOSITION_ATTACHMENT); // defines @@ -305,6 +310,16 @@ void Request::configure_admin(VStateless throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":LIMITS." RECOURSION_LIMIT_NAME " must be int"); } + pa_httpd_timeout=HTTPD_TIMEOUT; + if(limits) + if(Value* httpd_timeout=limits->get_element(httpd_timeout_name)) { + if(httpd_timeout->is_evaluated_expr()) { + pa_httpd_timeout=httpd_timeout->as_int(); + if(pa_httpd_timeout==0) pa_httpd_timeout=INT_MAX; + } else + throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":LIMITS." HTTPD_TIMEOUT_NAME " must be int"); + } + pa_file_size_limit=FILE_SIZE_LIMIT; if(limits) if(Value* file_size_limit=limits->get_element(file_size_limit_name)) { @@ -397,7 +412,7 @@ void Request::configure() { @test log stack trace */ -void Request::core(const char* config_filespec, bool header_only) { +void Request::core(const char* config_filespec, bool header_only, const String &amain_method_name) { try { // loading config if(config_filespec) @@ -408,7 +423,8 @@ void Request::core(const char* config_fi try { // compile requested file - use_file_directly(*new String(request_info.path_translated, String::L_TAINTED), true, true /* load auto.p files */); + if(request_info.path_translated) + use_file_directly(*new String(request_info.path_translated, String::L_TAINTED), true, true /* load auto.p files */); configure(); } catch(...) { configure(); // configure anyway, useful in @unhandled_exception [say, if they would want to mail by SMTP something] @@ -416,9 +432,9 @@ void Request::core(const char* config_fi } // execute @main[] - const String* body_string=execute_method(main_class, main_method_name); + const String* body_string=amain_method_name.is_empty() ? &String::Empty : execute_method(main_class, amain_method_name); if(!body_string) - throw Exception(PARSER_RUNTIME, 0, "'" MAIN_METHOD_NAME "' method not found"); + throw Exception(PARSER_RUNTIME, 0, "'%s' method not found", amain_method_name.cstr()); // extract response body Value* body_value=response.fields().get(download_name_upper); // $response:download? @@ -672,17 +688,17 @@ const String& Request::full_disk_path(co String& result=*new String(pa_strdup(request_info.document_root)); result << relative_name; return result; - } else - if(relative_name.pos("://")!=STRING_NOT_FOUND // something like "http://xxx" + } + if(relative_name.pos("://")!=STRING_NOT_FOUND // something like "http://xxx" #ifdef WIN32 - || relative_name.pos(":")==1 // DRIVE: - || relative_name.starts_with("\\\\") // UNC1 - || relative_name.starts_with("//") // UNC2 + || relative_name.pos(":")==1 // DRIVE: + || relative_name.starts_with("\\\\") // UNC1 + || relative_name.starts_with("//") // UNC2 #endif - ) - return relative_name; - else - return relative(request_info.path_translated, relative_name); + ) + return relative_name; + + return relative(request_info.path_translated ? request_info.path_translated : request_info.document_root, relative_name); } #ifndef DOXYGEN @@ -831,35 +847,31 @@ void Request::output_result(VFile* body_ // header: cookies cookie.output_result(sapi_info); - // _file_ content-type might be specified - Value* body_file_content_type=body_file->fields().get(content_type_name); + // $.file and $.name (when body is real vfile) + Value* vfile = body_file->fields().get(response_body_file_name); + Value* vname = body_file->fields().get(name_name); + + const String* sfile = vfile ? &vfile->as_string() : NULL; + const String* sname = vname ? &vname->as_string() : NULL; + + if(sname && *sname == NONAME_DAT) + sname = NULL; // Content-Disposition - Value* vfile_name=body_file->fields().get(name_name); - if(!vfile_name) { - vfile_name=body_file->fields().get(response_body_file_name); - if(vfile_name) { - char* name_cstr=vfile_name->as_string().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) { - if(as_attachment) { - VHash& hash=*new VHash(); - hash.hash().put(value_name, new VString(content_disposition_attachment)); - hash.hash().put(content_disposition_filename_name, new VString(*new String(sfile_name, String::L_HTTP_HEADER))); - response.fields().put(content_disposition_name_upper, &hash); - } + const String* disposition_name = sname ? sname->is_empty() ? NULL : sname : sfile; + if(disposition_name) { + VHash& hash=*new VHash(); + hash.hash().put(value_name, new VString(as_attachment ? content_disposition_attachment : content_disposition_inline)); + hash.hash().put(content_disposition_filename_name, new VString(*new String(*disposition_name, String::L_HTTP_HEADER))); + response.fields().put(content_disposition_name_upper, &hash); + } - if(!body_file_content_type) - body_file_content_type=new VString(mime_type_of(sfile_name.cstr())); - } + // _file_ content-type might be specified + Value* body_file_content_type = body_file->fields().get(content_type_name); + if(!body_file_content_type){ + const String* scontent_name = sname && !sname->is_empty() ? sname : sfile; + if(scontent_name) + body_file_content_type = new VString(mime_type_of(scontent_name->cstr())); } // set Content-Type @@ -875,12 +887,11 @@ void Request::output_result(VFile* body_ Add_header_attribute_info info(*this); response.fields().for_each(add_header_attribute, &info); - if(Value* vresponse_body_file=body_file->fields().get(response_body_file_name)) { + if(sfile) { // $response:[download|body][$.file[filespec]] -- optput specified file - const String& sresponse_body_file=vresponse_body_file->as_string(); uint64_t content_length=0; time_t atime=0, mtime=0, ctime=0; - file_stat(full_disk_path(sresponse_body_file), content_length, atime, mtime, ctime); + file_stat(full_disk_path(*sfile), content_length, atime, mtime, ctime); VDate* vdate=0; if(Value* v=body_file->fields().get("mdate")) { @@ -892,7 +903,7 @@ void Request::output_result(VFile* body_ if(!vdate) vdate=new VDate((pa_time_t)mtime); - output_pieces(*this, header_only, sresponse_body_file, (size_t)content_length, *vdate, info.add_last_modified); + output_pieces(*this, header_only, *sfile, (size_t)content_length, *vdate, info.add_last_modified); } else { if(body_file_content_type) if(HashStringValue *hash=body_file_content_type->get_hash())