--- parser3/src/targets/cgi/parser3.C 2001/04/23 13:29:53 1.71 +++ parser3/src/targets/cgi/parser3.C 2001/10/09 14:30:19 1.119 @@ -2,10 +2,9 @@ Parser: scripting and CGI main. Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) - $Id: parser3.C,v 1.71 2001/04/23 13:29:53 paf Exp $ + $Id: parser3.C,v 1.119 2001/10/09 14:30:19 parser Exp $ */ #include "pa_config_includes.h" @@ -15,12 +14,42 @@ #endif #include "pa_sapi.h" +#include "classes.h" #include "pa_common.h" -#include "pa_globals.h" #include "pa_request.h" #include "pa_socks.h" #include "pa_version.h" +//#define DEBUG_POOL_MALLOC + +// consts + +extern const char *main_RCSIds[]; +#ifdef USE_SMTP +extern const char *smtp_RCSIds[]; +#endif +extern const char *gd_RCSIds[]; +extern const char *classes_RCSIds[]; +extern const char *types_RCSIds[]; +extern const char *parser3_RCSIds[]; +#ifdef XML +extern const char *xalan_patched_RCSIds[]; +#endif +const char **RCSIds[]={ + main_RCSIds, +#ifdef USE_SMTP + smtp_RCSIds, +#endif + gd_RCSIds, + classes_RCSIds, + types_RCSIds, + parser3_RCSIds, +#ifdef XML + xalan_patched_RCSIds, +#endif + 0 +}; + /// IIS refuses to read bigger chunks const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M @@ -49,7 +78,8 @@ static LONG WINAPI TopLevelExceptionFilt #endif // SAPI -// appends to parser3.log located beside my binary + +// appends to parser3.log located beside my binary if openable, to stderr otherwize void SAPI::log(Pool& pool, const char *fmt, ...) { bool opened; FILE *f=0; @@ -57,7 +87,7 @@ void SAPI::log(Pool& pool, const char *f if(argv0) { // beside by binary char file_spec[MAX_STRING]; - strncpy(file_spec, argv0, MAX_STRING); // filespec of my binary + strncpy(file_spec, argv0, MAX_STRING-1); file_spec[MAX_STRING-1]=0; // filespec of my binary rsplit(file_spec, '/'); rsplit(file_spec, '\\');// strip filename strcat(file_spec, "/parser3.log"); f=fopen(file_spec, "at"); @@ -73,17 +103,24 @@ void SAPI::log(Pool& pool, const char *f // message va_list args; va_start(args,fmt); - vfprintf(f, fmt, args); + + char buf[MAX_STRING]; + size_t size=vsnprintf(buf, MAX_STRING, fmt, args); + remove_crlf(buf, buf+size); + + fwrite(buf, size, 1, f); va_end(args); // newline fprintf(f, "\n"); if(opened) fclose(f); + else + fflush(f); } const char *SAPI::get_env(Pool& pool, const char *name) { - return getenv(name); + return getenv(name); } size_t SAPI::read_post(Pool& pool, char *buf, size_t max_bytes) { @@ -118,6 +155,18 @@ void SAPI::send_body(Pool& pool, const v stdout_write(buf, size); } +// + +char *full_file_spec(char *file_name) { + if(file_name && !strchr(file_name, '/')) { + static char cwd[MAX_STRING]; getcwd(cwd, MAX_STRING); + static char buf[MAX_STRING]; + snprintf(buf, MAX_STRING, "%s/%s", cwd, file_name); + return buf; + } + return file_name; +} + /** main workhorse @@ -127,16 +176,11 @@ void SAPI::send_body(Pool& pool, const v wich is tested but seems slow. */ int main(int argc, char *argv[]) { + int result; argv0=argv[0]; umask(2); -#ifdef WIN32 - setmode(fileno(stdin), _O_BINARY); - setmode(fileno(stdout), _O_BINARY); - setmode(fileno(stderr), _O_BINARY); -#endif - // were we started as CGI? cgi= getenv("SERVER_SOFTWARE") || @@ -157,10 +201,17 @@ int main(int argc, char *argv[]) { } } +#ifdef WIN32 + setmode(fileno(stdin), _O_BINARY); + setmode(fileno(stdout), _O_BINARY); + setmode(fileno(stderr), _O_BINARY); +#endif + char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1]; #ifdef WIN32 back_slashes_to_slashes(filespec_to_process); #endif + filespec_to_process=full_file_spec(filespec_to_process); const char *request_method=getenv("REQUEST_METHOD"); bool header_only=request_method && strcasecmp(request_method, "HEAD")==0; @@ -173,6 +224,8 @@ int main(int argc, char *argv[]) { // init socks init_socks(pool); + // init global classes + init_methoded_array(pool); // init global variables pa_globals_init(pool); @@ -190,8 +243,7 @@ int main(int argc, char *argv[]) { // IIS size_t len=strlen(filespec_to_process)-strlen(path_info); char *buf=(char *)pool.malloc(len+1); - strncpy(buf, filespec_to_process, len); - buf[len]=0; + memcpy(buf, filespec_to_process, len); buf[len]=0; request_info.document_root=buf; } else PTHROW(0, 0, @@ -199,14 +251,14 @@ int main(int argc, char *argv[]) { "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)"); } else { static char buf[MAX_STRING]; - strncpy(buf, filespec_to_process, MAX_STRING); + strncpy(buf, filespec_to_process, MAX_STRING-1); buf[MAX_STRING-1]=0; if(rsplit(buf, '/') || rsplit(buf, '\\')) // strip filename request_info.document_root=buf; else request_info.document_root=""; } request_info.path_translated=filespec_to_process; - request_info.method=request_method; + request_info.method=request_method ? request_method : "GET"; const char *query_string=SAPI::get_env(pool, "QUERY_STRING"); request_info.query_string=query_string; if(cgi) { @@ -228,12 +280,15 @@ int main(int argc, char *argv[]) { 0, "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)"); - const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME"); - if(script_name && - strncmp(request_info.uri,script_name, strlen(script_name))==0) - PTHROW(0, 0, - 0, - "CGI: illegal call"); + if(const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME")) { + size_t script_name_len=strlen(script_name); + size_t uri_len=strlen(request_info.uri); + if(strncmp(request_info.uri,script_name, script_name_len)==0 && + script_name_len != uri_len) // under IIS they are the same + PTHROW(0, 0, + 0, + "CGI: illegal call"); + } } else request_info.uri=0; @@ -246,39 +301,64 @@ int main(int argc, char *argv[]) { // prepare to process request Request request(pool, request_info, - cgi ? String::UL_HTML_TYPO : String::UL_AS_IS + cgi ? String::UL_USER_HTML : String::UL_AS_IS ); // some root-controlled location -#ifdef WIN32 - // c:\windows - static char root_auto_path[MAX_STRING]; - GetWindowsDirectory(root_auto_path, MAX_STRING); +#ifdef SYSCONFDIR + const char *root_config_filespec=SYSCONFDIR "/" CONFIG_FILE_NAME; #else - // ~nobody todo: figure out a better place - const char *root_auto_path=SAPI::get_env(pool, "HOME"); +# ifdef WIN32 + // c:\windows + char root_config_path[MAX_STRING]; + GetWindowsDirectory(root_config_path, MAX_STRING); + + char root_config_filespec[MAX_STRING]; + snprintf(root_config_filespec, MAX_STRING, + "%s/%s", + root_config_path, CONFIG_FILE_NAME); +# else + #error must be compiled either configure/make or MSVC++ +# endif #endif // beside by binary - static char site_auto_path[MAX_STRING]; - strncpy(site_auto_path, argv[0], MAX_STRING); // filespec of my binary - rsplit(site_auto_path, '/'); rsplit(site_auto_path, '\\');// strip filename + // @todo full path, not ./! + static char site_config_path[MAX_STRING]; + strncpy(site_config_path, argv0, MAX_STRING-1); site_config_path[MAX_STRING-1]=0; // filespec of my binary + if(!( + rsplit(site_config_path, '/') || + rsplit(site_config_path, '\\'))) { // strip filename + // no path, just filename + site_config_path[0]='.'; site_config_path[1]=0; + } + + char site_config_filespec[MAX_STRING]; + snprintf(site_config_filespec, MAX_STRING, + "%s/%s", + site_config_path, CONFIG_FILE_NAME); // process the request request.core( - root_auto_path, false, - site_auto_path, false, + root_config_filespec, false, + site_config_filespec, false, header_only); // done_socks(); +#ifdef DEBUG_POOL_MALLOC + extern void log_pool_stats(Pool& pool); + log_pool_stats(pool); +#endif + // must be last in PTRY{}PCATCH #ifdef WIN32 SetUnhandledExceptionFilter(0); #endif + // successful finish - return 0; + result=0; } PCATCH(e) { // global problem // must be first in PCATCH{} #ifdef WIN32 @@ -309,7 +389,14 @@ int main(int argc, char *argv[]) { SAPI::send_body(pool, body, content_length); // unsuccessful finish - return 1; + result=1; } PEND_CATCH + +#ifndef WIN32 + // + if(!cgi) + SAPI::send_body(pool, "\n", 1); +#endif + return result; }