--- parser3/src/targets/cgi/parser3.C 2001/03/23 10:14:36 1.36 +++ parser3/src/targets/cgi/parser3.C 2001/03/23 14:03:28 1.41 @@ -5,40 +5,30 @@ Author: Alexander Petrosyan (http://design.ru/paf) - $Id: parser3.C,v 1.36 2001/03/23 10:14:36 paf Exp $ + $Id: parser3.C,v 1.41 2001/03/23 14:03:28 paf Exp $ */ -#ifdef HAVE_CONFIG_H -# include "pa_config.h" -#endif - +#include "pa_config_includes.h" #ifdef WIN32 # include -# include -#else -# include #endif -//\ifwin32 #include -//#include -//\endifwin32 - #include #include -#include #include +#include "pa_sapi.h" #include "pa_common.h" #include "pa_globals.h" #include "pa_request.h" -Pool pool; // global pool +Pool pool; // global pool [dont describe to doxygen: it confuses it with param names] bool cgi; ///< we were started as CGI? -#if defined(WIN32) && _MSC_VER -// intercept global system errors +#ifdef WIN32 +/// global system errors into parser exceptions converter static LONG WINAPI TopLevelExceptionFilter (struct _EXCEPTION_POINTERS *ExceptionInfo) { char buf[MAX_STRING]; if(ExceptionInfo && ExceptionInfo->ExceptionRecord) { @@ -57,22 +47,22 @@ static LONG WINAPI TopLevelExceptionFilt } #endif -//\if +#ifdef WIN32 static void fix_slashes(char *s) { if(s) for(; *s; s++) if(*s=='\\') *s='/'; } -//\endif - -// service funcs +#endif -static const char *sapi_get_env(Pool& pool, const char *name) { +//@{ +/// SAPI funcs decl +const char *SAPI::get_env(Pool& pool, const char *name) { return getenv(name); } -static uint sapi_read_post(Pool& pool, char *buf, uint max_bytes) { +uint SAPI::read_post(Pool& pool, char *buf, uint max_bytes) { uint read_size=0; do { int chunk_size=read(fileno(stdin), @@ -85,37 +75,34 @@ static uint sapi_read_post(Pool& pool, c return read_size; } -static void sapi_add_header_attribute(Pool& pool, const char *key, const char *value) { +void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) { if(cgi) printf("%s: %s\n", key, value); } /// @todo intelligent cache-control -static void sapi_send_header(Pool& pool) { +void SAPI::send_header(Pool& pool) { if(cgi) { - puts("Expires: Fri, 23 Mar 2001 09:32:23 GMT"); + puts("expires: Fri, 23 Mar 2001 09:32:23 GMT"); // header | body delimiter puts(""); } } -static void sapi_send_body(Pool& pool, const char *buf, size_t size) { +void SAPI::send_body(Pool& pool, const char *buf, size_t size) { stdout_write(buf, size); } +//@} -/// SAPI -SAPI sapi={ - sapi_get_env, - sapi_read_post, - sapi_add_header_attribute, - sapi_send_header, - sapi_send_body -}; - - -// main +/** + main workhorse + @todo + IIS: remove trailing default-document[index.html] from $request.uri. + to do that we need to consult metabase, + wich is tested but seems slow. +*/ int main(int argc, char *argv[]) { umask(2); @@ -149,13 +136,13 @@ int main(int argc, char *argv[]) { bool header_only=request_method && strcasecmp(request_method, "HEAD")==0; PTRY { // global try // must be first in PTRY{}PCATCH -#if defined(WIN32) && _MSC_VER +#ifdef WIN32 SetUnhandledExceptionFilter(&TopLevelExceptionFilter); //TODO: initSocks(); #endif // init global variables - globals_init(pool); + pa_globals_init(pool); if(!filespec_to_process) PTHROW(0, 0, @@ -164,26 +151,55 @@ int main(int argc, char *argv[]) { // Request info Request::Info request_info; - if(cgi) - request_info.document_root=getenv("DOCUMENT_ROOT"); - else { - static char document_root[MAX_STRING]; - strncpy(document_root, filespec_to_process, MAX_STRING); - rsplit(document_root, '/'); rsplit(document_root, '\\');// strip filename - request_info.document_root=document_root; + if(cgi) { + if(const char *env_document_root=getenv("DOCUMENT_ROOT")) + request_info.document_root=env_document_root; + else if(const char *path_info=getenv("PATH_INFO")) { + // 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; + request_info.document_root=buf; + } else + PTHROW(0, 0, + 0, + "CGI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)"); + } else { + static char buf[MAX_STRING]; + strncpy(buf, filespec_to_process, MAX_STRING); + rsplit(buf, '/'); rsplit(buf, '\\');// strip filename + request_info.document_root=buf; } request_info.path_translated=filespec_to_process; request_info.method=request_method; - request_info.query_string=getenv("QUERY_STRING"); - request_info.uri=getenv("REQUEST_URI"); + const char *query_string=getenv("QUERY_STRING"); + request_info.query_string=query_string; + if(const char *env_request_uri=getenv("REQUEST_URI")) + request_info.uri=env_request_uri; + else if (const char *path_info=getenv("PATH_INFO")) { + if(query_string) { + char *reconstructed_uri=(char *)malloc( + strlen(path_info)+1/*'?'*/+ + strlen(query_string)+1/*0*/); + strcpy(reconstructed_uri, path_info); + strcat(reconstructed_uri, "?"); + strcat(reconstructed_uri, query_string); + request_info.uri=reconstructed_uri; + } else + request_info.uri=path_info; + } else + PTHROW(0, 0, + 0, + "CGI: no PATH_INFO defined (in reinventing REQUEST_URI)"); + request_info.content_type=getenv("CONTENT_TYPE"); const char *content_length=getenv("CONTENT_LENGTH"); request_info.content_length=(content_length?atoi(content_length):0); request_info.cookie=getenv("HTTP_COOKIE"); // prepare to process request - Pool request_pool; - Request request(request_pool, + Request request(pool, request_info, cgi ? String::UL_HTML_TYPO : String::UL_NO ); @@ -194,7 +210,7 @@ int main(int argc, char *argv[]) { static char root_auto_path[MAX_STRING]; GetWindowsDirectory(root_auto_path, MAX_STRING); #else - // ~nobody + // ~nobody todo: figure out a better place char *root_auto_path=getenv("HOME"); #endif @@ -210,7 +226,7 @@ int main(int argc, char *argv[]) { header_only); // must be last in PTRY{}PCATCH -#if defined(WIN32) && _MSC_VER +#ifdef WIN32 SetUnhandledExceptionFilter(0); #endif // successful finish @@ -220,17 +236,17 @@ int main(int argc, char *argv[]) { int content_length=strlen(body); // prepare header - sapi_add_header_attribute(pool, "content-type", "text/plain"); + SAPI::add_header_attribute(pool, "content-type", "text/plain"); char content_length_cstr[MAX_NUMBER]; snprintf(content_length_cstr, MAX_NUMBER, "%lu", content_length); - sapi_add_header_attribute(pool, "content-length", content_length_cstr); + SAPI::add_header_attribute(pool, "content-length", content_length_cstr); // send header - sapi_send_header(pool); + SAPI::send_header(pool); // body if(!header_only) - sapi_send_body(pool, body, content_length); + SAPI::send_body(pool, body, content_length); // unsuccessful finish return 1;