--- parser3/src/main/pa_request.C 2020/12/07 23:25:20 1.398 +++ parser3/src/main/pa_request.C 2020/12/21 14:12:57 1.409 @@ -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) */ @@ -25,6 +25,7 @@ #include "pa_charset.h" #include "pa_charsets.h" #include "pa_cache_managers.h" +#include "pa_http.h" #include "pa_vmail.h" #include "pa_vform.h" #include "pa_vcookie.h" @@ -33,7 +34,7 @@ #include "pa_vconsole.h" #include "pa_vdate.h" -volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.398 2020/12/07 23:25:20 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.409 2020/12/21 14:12:57 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H; // consts @@ -103,6 +104,7 @@ static const String file_size_limit_name static const String lock_wait_timeout_name("lock_wait_timeout"); static const String httpd_name("HTTPD"); static const String httpd_timeout_name("timeout"); +static const String httpd_mode_name("mode"); static const String conf_method_name("conf"); static const String post_process_method_name("postprocess"); @@ -127,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), @@ -328,6 +329,13 @@ void Request::configure_admin(VStateless if(pa_httpd_timeout==0) pa_httpd_timeout=INT_MAX; }, "HTTPD.%s must be int"); + if(httpd) + if(Value* option=httpd->get_element( httpd_mode_name)) { + if(option->get_junction()) + throw Exception(PARSER_RUNTIME, 0, "$main:HTTPD:mode must be string"); + HTTPD_Server::set_mode(option->as_string()); + } + // configure method_frame options // until someone with less privileges have overriden them methoded_array().configure_admin(*this); @@ -371,6 +379,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 @@ -396,6 +432,9 @@ void Request::configure() { */ void Request::core(const char* config_filespec, bool header_only, const String &amain_method_name) { + VFile* body_file=NULL; + bool as_attachment=false; + try { // loading config if(config_filespec) @@ -421,7 +460,7 @@ void Request::core(const char* config_fi // 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) @@ -429,8 +468,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); @@ -438,90 +475,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) { @@ -743,62 +754,109 @@ static void parse_range(const String* s, while(*p){ r.start = UNSET; r.end = UNSET; + + while(*p==' ' || *p=='\t') p++; + if(*p >= '0' && *p <= '9'){ - r.start = pa_atoul(p); - while(*p>='0' && *p<='9') ++p; + const char *s=p; + while(*p>='0' && *p<='9') p++; + r.start = pa_atoul(pa_strdup(s,p-s)); } + + while(*p==' ' || *p=='\t') p++; + if(*p++ != '-') break; + + while(*p==' ' || *p=='\t') p++; + if(*p >= '0' && *p <= '9'){ - r.end = pa_atoul(p); - while(*p>='0' && *p<='9') ++p; + const char *s=p; + while(*p>='0' && *p<='9') p++; + r.end = pa_atoul(pa_strdup(s,p-s)); } - if(*p == ',') ++p; + + while(*p==' ' || *p=='\t') p++; + + if(*p) + if(*p++ != ',') break; + ar += r; } } -static void output_pieces(Request& r, bool header_only, const String& filename, uint64_t content_length, Value& date, bool add_last_modified) { - SAPI::add_header_attribute(r.sapi_info, "accept-ranges", "bytes"); +struct Send_range_action_info { + Request *r; + uint64_t offset; + uint64_t part_length; +}; + +static void send_range(struct stat& /*finfo*/, int f, const String& /*file_spec*/, void *context){ + Send_range_action_info &info = *(Send_range_action_info*)context; + + pa_lseek(f, info.offset, SEEK_SET); const size_t BUFSIZE = 128*0x400; char buf[BUFSIZE]; + do{ + size_t to_read = info.part_length < BUFSIZE ? (size_t)info.part_length : BUFSIZE; + size_t to_write = file_block_read(f, buf, to_read); + + if(to_write == 0) + break; + + size_t size = SAPI::send_body(info.r->sapi_info, buf, to_write); + if(size != to_write) + break; + + info.part_length -= to_write; + } while (info.part_length); +} + +static void output_pieces(Request& r, bool header_only, const String& filename, uint64_t content_length, Value& date, bool add_last_modified) { + SAPI::add_header_attribute(r.sapi_info, "accept-ranges", "bytes"); + const char *range = SAPI::Env::get(r.sapi_info, "HTTP_RANGE"); uint64_t offset=0; uint64_t part_length=content_length; - if(range){ + + if(range && content_length){ Array ar; parse_range(new String(range), ar); int count = ar.count(); if(count == 1){ Range &rg = ar.get_ref(0); - if(rg.start == UNSET && rg.end == UNSET){ - SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "416 Requested Range Not Satisfiable"); - return; - } + + if(rg.start == UNSET && rg.end == UNSET) + return SAPI::send_error(r.sapi_info, "", "416"); + if(rg.start == UNSET && rg.end != UNSET){ + if(rg.end > content_length) + rg.end = content_length; rg.start = content_length - rg.end; - rg.end = content_length; - offset += rg.start; - part_length = rg.end-rg.start; - }else if(rg.start != UNSET && rg.end == UNSET){ + 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"); rg.end = content_length-1; - offset += rg.start; - part_length -= rg.start; - } - if(part_length == 0){ - SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "204 No Content"); - return; + } else { + if(rg.start >= content_length || rg.start > rg.end) + return SAPI::send_error(r.sapi_info, "", "416"); + if(rg.end >= content_length) + rg.end = content_length-1; } - SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "206 Partial Content"); - snprintf(buf, BUFSIZE, "bytes %.15g-%.15g/%.15g", (double)rg.start, (double)rg.end, (double)content_length); + + offset = rg.start; + part_length = rg.end-rg.start+1; + + char buf[MAX_STRING]; + snprintf(buf, MAX_STRING, "bytes %.15g-%.15g/%.15g", (double)rg.start, (double)rg.end, (double)content_length); + SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "206"); SAPI::add_header_attribute(r.sapi_info, "content-range", buf); - }else if(count != 0){ - SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "501 Not Implemented"); - return; + } else { + return SAPI::send_error(r.sapi_info, count ? "Multiple ranges not supported" : "Invalid range", count ? "501" : "400"); } } - SAPI::add_header_attribute(r.sapi_info, HTTP_CONTENT_LENGTH, format((double)part_length, "%.15g")); if(add_last_modified) @@ -806,22 +864,9 @@ static void output_pieces(Request& r, bo SAPI::send_header(r.sapi_info); - const String& filespec=r.full_disk_path(filename); - if(!header_only){ - do{ - size_t to_read = part_length < BUFSIZE ? (size_t)part_length : BUFSIZE; - File_read_result read_result=file_read_binary(filespec, true /*fail on problem*/, buf, offset, to_read); - to_read=read_result.length; - if(to_read == 0) - break; - offset += to_read; - - size_t size = SAPI::send_body(r.sapi_info, read_result.str, to_read); - if(size != to_read) - break; - part_length -= to_read; - } while (part_length); + Send_range_action_info info = { &r, offset, part_length}; + file_read_action_under_lock(r.full_disk_path(filename), "send", send_range, &info); } } @@ -839,7 +884,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(); @@ -885,7 +930,7 @@ void Request::output_result(VFile* body_ if(!vdate) vdate=new VDate((pa_time_t)mtime); - output_pieces(*this, header_only, *sfile, (size_t)content_length, *vdate, info.add_last_modified); + output_pieces(*this, header_only, *sfile, content_length, *vdate, info.add_last_modified); } else { if(body_file_content_type) if(HashStringValue *hash=body_file_content_type->get_hash())