--- parser3/src/targets/cgi/parser3.C 2001/08/31 15:35:34 1.99 +++ parser3/src/targets/cgi/parser3.C 2001/10/13 17:42:52 1.121 @@ -2,10 +2,10 @@ 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.121 2001/10/13 17:42:52 parser Exp $ */ -static const char *RCSId="$Id: parser3.C,v 1.99 2001/08/31 15:35:34 parser Exp $"; #include "pa_config_includes.h" @@ -20,14 +20,39 @@ static const char *RCSId="$Id: parser3.C #include "pa_socks.h" #include "pa_version.h" -#ifdef WIN32 -# define EOL "\r\n" -#else -# define EOL "\n" +#ifdef XML +#include #endif //#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 @@ -82,7 +107,12 @@ 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"); @@ -94,7 +124,7 @@ void SAPI::log(Pool& pool, const char *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) { @@ -132,7 +162,7 @@ void SAPI::send_body(Pool& pool, const v // char *full_file_spec(char *file_name) { - if(!strchr(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); @@ -150,16 +180,11 @@ char *full_file_spec(char *file_name) { 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") || @@ -170,16 +195,22 @@ int main(int argc, char *argv[]) { if(!cgi) { if(argc<2) { printf( - "Parser/%s Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)"EOL - "Author: Alexander Petrosyan (http://design.ru/paf)"EOL - EOL - "Usage: %s "EOL, + "Parser/%s Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)\n" + "Author: Alexander Petrosyan (http://design.ru/paf)\n" + "\n" + "Usage: %s \n", PARSER_VERSION, argv0?argv0:"parser3"); return 1; } } +#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); @@ -193,10 +224,20 @@ int main(int argc, char *argv[]) { #ifdef WIN32 SetUnhandledExceptionFilter(&TopLevelExceptionFilter); #endif - // init socks init_socks(pool); +#ifdef XML + /** + * Initialize Xerces and Xalan. + * + * Should be called only once per process before making + * any other API calls. + */ + //_asm int 3; + XalanInitialize(); +#endif + // init global classes init_methoded_array(pool); // init global variables @@ -231,7 +272,7 @@ int main(int argc, char *argv[]) { 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) { @@ -239,7 +280,7 @@ int main(int argc, char *argv[]) { request_info.uri=env_request_uri; else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) if(query_string) { - char *reconstructed_uri=(char *)malloc( + char *reconstructed_uri=(char *)pool.malloc( strlen(path_info)+1/*'?'*/+ strlen(query_string)+1/*0*/); strcpy(reconstructed_uri, path_info); @@ -278,29 +319,43 @@ int main(int argc, char *argv[]) { ); // 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, argv0, MAX_STRING-1); site_auto_path[MAX_STRING-1]=0; // filespec of my binary + // @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_auto_path, '/') || - rsplit(site_auto_path, '\\'))) { // strip filename + rsplit(site_config_path, '/') || + rsplit(site_config_path, '\\'))) { // strip filename // no path, just filename - site_auto_path[0]='.'; site_auto_path[1]=0; + 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); // @@ -315,12 +370,9 @@ int main(int argc, char *argv[]) { #ifdef WIN32 SetUnhandledExceptionFilter(0); #endif - // - if(!cgi) - SAPI::send_body(pool, EOL, strlen(EOL)); // successful finish - return 0; + result=0; } PCATCH(e) { // global problem // must be first in PCATCH{} #ifdef WIN32 @@ -350,12 +402,15 @@ int main(int argc, char *argv[]) { if(!header_only) SAPI::send_body(pool, body, content_length); - // - if(!cgi) - SAPI::send_body(pool, EOL, strlen(EOL)); - // unsuccessful finish - return 1; + result=1; } PEND_CATCH + +#ifndef WIN32 + // + if(!cgi) + SAPI::send_body(pool, "\n", 1); +#endif + return result; }