--- parser3/src/targets/cgi/parser3.C 2001/03/23 10:14:36 1.36 +++ parser3/src/targets/cgi/parser3.C 2001/03/23 11:07:07 1.39 @@ -5,7 +5,7 @@ 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.39 2001/03/23 11:07:07 paf Exp $ */ #ifdef HAVE_CONFIG_H @@ -30,6 +30,7 @@ #include #include +#include "pa_sapi.h" #include "pa_common.h" #include "pa_globals.h" #include "pa_request.h" @@ -37,7 +38,7 @@ Pool pool; // global pool bool cgi; ///< we were started as CGI? -#if defined(WIN32) && _MSC_VER +#if _MSC_VER // intercept global system errors static LONG WINAPI TopLevelExceptionFilter (struct _EXCEPTION_POINTERS *ExceptionInfo) { char buf[MAX_STRING]; @@ -66,13 +67,13 @@ static void fix_slashes(char *s) { } //\endif -// service funcs - -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,13 +86,13 @@ 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"); @@ -100,22 +101,14 @@ static void sapi_send_header(Pool& pool) } } -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 +#include int main(int argc, char *argv[]) { umask(2); @@ -149,13 +142,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 +#if _MSC_VER SetUnhandledExceptionFilter(&TopLevelExceptionFilter); //TODO: initSocks(); #endif // init global variables - globals_init(pool); + pa_globals_init(pool); if(!filespec_to_process) PTHROW(0, 0, @@ -164,13 +157,25 @@ 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")) { + // under IIS for sure + static char buf[MAX_STRING]; + size_t len=strlen(filespec_to_process)-strlen(path_info); + strncpy(buf, filespec_to_process, min(MAX_STRING-1, 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; @@ -182,8 +187,7 @@ int main(int argc, char *argv[]) { 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 ); @@ -210,7 +214,7 @@ int main(int argc, char *argv[]) { header_only); // must be last in PTRY{}PCATCH -#if defined(WIN32) && _MSC_VER +#if _MSC_VER SetUnhandledExceptionFilter(0); #endif // successful finish @@ -220,17 +224,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;