Annotation of parser3/src/targets/cgi/parser3.C, revision 1.118

1.27      paf         1: /** @file
                      2:        Parser: scripting and CGI main.
                      3: 
1.43      paf         4:        Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
1.113     parser      5:        Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.27      paf         6: 
1.118   ! parser      7:        $Id: parser3.C,v 1.117 2001/10/08 09:04:08 parser Exp $
1.1       paf         8: */
                      9: 
1.40      paf        10: #include "pa_config_includes.h"
1.3       paf        11: 
                     12: #ifdef WIN32
                     13: #      include <windows.h>
                     14: #endif
1.27      paf        15: 
1.37      paf        16: #include "pa_sapi.h"
1.76      paf        17: #include "classes.h"
1.24      paf        18: #include "pa_common.h"
1.2       paf        19: #include "pa_request.h"
1.57      paf        20: #include "pa_socks.h"
1.68      paf        21: #include "pa_version.h"
1.69      paf        22: 
1.84      parser     23: //#define DEBUG_POOL_MALLOC
                     24: 
1.109     parser     25: // consts
1.113     parser     26: 
                     27: extern const char *main_RCSIds[];
1.116     parser     28: #ifdef USE_SMTP
1.113     parser     29: extern const char *smtp_RCSIds[];
1.114     parser     30: #endif
1.113     parser     31: extern const char *gd_RCSIds[];
                     32: extern const char *classes_RCSIds[];
                     33: extern const char *types_RCSIds[];
1.115     parser     34: extern const char *parser3_RCSIds[];
1.113     parser     35: const char **RCSIds[]={
                     36:        main_RCSIds,
1.116     parser     37: #ifdef USE_SMTP
1.113     parser     38:        smtp_RCSIds,
1.114     parser     39: #endif
1.113     parser     40:        gd_RCSIds,
                     41:        classes_RCSIds,
                     42:        types_RCSIds,
1.115     parser     43:        parser3_RCSIds,
1.118   ! parser     44: #ifdef XML
        !            45:        xalan_patched_RCSIds,
        !            46: #endif
1.113     parser     47:        0
                     48: };
1.84      parser     49: 
1.42      paf        50: /// IIS refuses to read bigger chunks
                     51: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M 
                     52: 
1.45      paf        53: const char *argv0;
1.42      paf        54: Pool pool(0); // global pool [dont describe to doxygen: it confuses it with param names]
1.27      paf        55: bool cgi; ///< we were started as CGI?
1.5       paf        56: 
1.40      paf        57: #ifdef WIN32
                     58: /// global system errors into parser exceptions converter
1.43      paf        59: static LONG WINAPI TopLevelExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo) {
1.5       paf        60:        char buf[MAX_STRING];
                     61:        if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
1.36      paf        62:                struct _EXCEPTION_RECORD *er=ExceptionInfo->ExceptionRecord;
1.46      paf        63:                snprintf(buf, MAX_STRING, "Exception 0x%X at %p", 
1.31      paf        64:                        er->ExceptionCode, 
                     65:                        er->ExceptionAddress);
1.5       paf        66:        } else 
                     67:                strcpy(buf, "Exception <unknown>");
                     68:        
                     69:        PTHROW(0, 0,
                     70:                0,
                     71:                buf);
                     72: 
                     73:        return EXCEPTION_EXECUTE_HANDLER; // never reached
                     74: }
1.27      paf        75: #endif
                     76: 
1.46      paf        77: // SAPI
1.86      parser     78: 
                     79: // appends to parser3.log located beside my binary if openable, to stderr otherwize
1.61      paf        80: void SAPI::log(Pool& pool, const char *fmt, ...) {
                     81:        bool opened;
                     82:        FILE *f=0;
                     83: 
                     84:        if(argv0) {
                     85:                // beside by binary
                     86:                char file_spec[MAX_STRING];
1.98      parser     87:                strncpy(file_spec, argv0, MAX_STRING-1);  file_spec[MAX_STRING-1]=0; // filespec of my binary
1.61      paf        88:                rsplit(file_spec, '/');  rsplit(file_spec, '\\');// strip filename
                     89:                strcat(file_spec, "/parser3.log");
                     90:                f=fopen(file_spec, "at");
                     91:        }
                     92:        opened=f!=0;
                     93:        if(!opened)
                     94:                f=stderr;
                     95: 
                     96:        // prefix
                     97:        time_t t=time(0);
                     98:        const char *stamp=ctime(&t);
                     99:        fprintf(f, "[%.*s] ", strlen(stamp)-1, stamp);
                    100:        // message
                    101:     va_list args;
                    102:        va_start(args,fmt);
1.117     parser    103: 
                    104:        char buf[MAX_STRING];
                    105:        size_t size=vsnprintf(buf, MAX_STRING, fmt, args);
                    106:        remove_crlf(buf, buf+size);
                    107: 
                    108:        fwrite(buf, size, 1, f);
1.61      paf       109:        va_end(args);
                    110:        // newline
                    111:        fprintf(f, "\n");
                    112: 
                    113:        if(opened)
                    114:                fclose(f);
1.85      parser    115:        else
                    116:                fflush(f);
1.61      paf       117: }
                    118: 
1.37      paf       119: const char *SAPI::get_env(Pool& pool, const char *name) {
1.109     parser    120:        return getenv(name);
1.28      paf       121: }
                    122: 
1.59      paf       123: size_t SAPI::read_post(Pool& pool, char *buf, size_t max_bytes) {
                    124:        size_t read_size=0;
1.12      paf       125:        do {
1.36      paf       126:                int chunk_size=read(fileno(stdin), 
1.42      paf       127:                        buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.12      paf       128:                if(chunk_size<0)
                    129:                        break;
                    130:                read_size+=chunk_size;
                    131:        } while(read_size<max_bytes);
                    132: 
                    133:        return read_size;
1.10      paf       134: }
                    135: 
1.37      paf       136: void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
1.68      paf       137:        if(cgi)
1.20      paf       138:                printf("%s: %s\n", key, value);
1.19      paf       139: }
                    140: 
1.56      paf       141: /// @todo intelligent cache-control
1.37      paf       142: void SAPI::send_header(Pool& pool) {
1.33      paf       143:        if(cgi) {
1.55      paf       144:                puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33      paf       145: 
                    146:                // header | body  delimiter
1.20      paf       147:                puts("");
1.33      paf       148:        }
1.30      paf       149: }
1.20      paf       150: 
1.53      paf       151: void SAPI::send_body(Pool& pool, const void *buf, size_t size) {
1.19      paf       152:        stdout_write(buf, size);
1.58      paf       153: }
                    154: 
1.97      parser    155: //
                    156: 
                    157: char *full_file_spec(char *file_name) {
1.108     parser    158:        if(file_name && !strchr(file_name, '/')) {
1.97      parser    159:                static char cwd[MAX_STRING];  getcwd(cwd, MAX_STRING);
                    160:                static char buf[MAX_STRING];
                    161:                snprintf(buf, MAX_STRING, "%s/%s", cwd, file_name);
                    162:                return buf;
                    163:        }
                    164:        return file_name;
                    165: }
                    166: 
1.40      paf       167: /**
                    168:        main workhorse
1.19      paf       169: 
1.56      paf       170:        @todo 
1.40      paf       171:                IIS: remove trailing default-document[index.html] from $request.uri.
                    172:                to do that we need to consult metabase,
                    173:                wich is tested but seems slow.
                    174: */
1.5       paf       175: int main(int argc, char *argv[]) {
1.109     parser    176:        int result;
1.45      paf       177:        argv0=argv[0];
                    178: 
1.32      paf       179:        umask(2);
                    180: 
1.3       paf       181:        // were we started as CGI?
1.20      paf       182:        cgi=
1.109     parser    183:                getenv("SERVER_SOFTWARE") || 
                    184:                getenv("SERVER_NAME") || 
                    185:                getenv("GATEWAY_INTERFACE") || 
                    186:                getenv("REQUEST_METHOD");
1.5       paf       187:        
1.10      paf       188:        if(!cgi) {
                    189:                if(argc<2) {
1.69      paf       190:                        printf(
1.100     parser    191:                                "Parser/%s Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)\n"
                    192:                                "Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)\n"
                    193:                                "\n"
                    194:                                "Usage: %s <file>\n",
1.69      paf       195:                                PARSER_VERSION, 
                    196:                                argv0?argv0:"parser3");
1.67      paf       197:                        return 1;
1.10      paf       198:                }
                    199:        }
                    200: 
1.100     parser    201: #ifdef WIN32
                    202:        setmode(fileno(stdin), _O_BINARY);
                    203:        setmode(fileno(stdout), _O_BINARY);
                    204:        setmode(fileno(stderr), _O_BINARY);
                    205: #endif
                    206: 
1.109     parser    207:        char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36      paf       208: #ifdef WIN32
1.43      paf       209:        back_slashes_to_slashes(filespec_to_process);
1.36      paf       210: #endif
1.97      parser    211:        filespec_to_process=full_file_spec(filespec_to_process);
1.10      paf       212: 
1.109     parser    213:        const char *request_method=getenv("REQUEST_METHOD");
1.35      paf       214:        bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.5       paf       215:        PTRY { // global try
                    216:                // must be first in PTRY{}PCATCH
1.40      paf       217: #ifdef WIN32
1.5       paf       218:                SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
                    219: #endif
1.2       paf       220: 
1.62      paf       221:                // init socks
1.57      paf       222:                init_socks(pool);
                    223: 
1.73      paf       224:                // init global classes
                    225:                init_methoded_array(pool);
1.10      paf       226:                // init global variables
1.37      paf       227:                pa_globals_init(pool);
1.10      paf       228: 
1.13      paf       229:                if(!filespec_to_process)
                    230:                        PTHROW(0, 0,
                    231:                                0,
1.68      paf       232:                                "Parser/%s", PARSER_VERSION);
1.13      paf       233: 
1.10      paf       234:                // Request info
                    235:                Request::Info request_info;
1.39      paf       236:                if(cgi) {
1.51      paf       237:                        if(const char *env_document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"))
1.39      paf       238:                                request_info.document_root=env_document_root;
1.51      paf       239:                        else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
1.40      paf       240:                                // IIS
1.39      paf       241:                                size_t len=strlen(filespec_to_process)-strlen(path_info);
1.40      paf       242:                                char *buf=(char *)pool.malloc(len+1);
1.98      parser    243:                                memcpy(buf, filespec_to_process, len); buf[len]=0;
1.39      paf       244:                                request_info.document_root=buf;
                    245:                        } else
                    246:                                PTHROW(0, 0,
                    247:                                        0,
1.43      paf       248:                                        "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.39      paf       249:                } else {
                    250:                        static char buf[MAX_STRING];
1.98      parser    251:                        strncpy(buf, filespec_to_process, MAX_STRING-1); buf[MAX_STRING-1]=0;
1.70      paf       252:                        if(rsplit(buf, '/') || rsplit(buf, '\\')) // strip filename
                    253:                                request_info.document_root=buf;
                    254:                        else
                    255:                                request_info.document_root="";
1.13      paf       256:                }
1.10      paf       257:                request_info.path_translated=filespec_to_process;
1.107     parser    258:                request_info.method=request_method ? request_method : "GET";
1.51      paf       259:                const char *query_string=SAPI::get_env(pool, "QUERY_STRING");
1.40      paf       260:                request_info.query_string=query_string;
1.68      paf       261:                if(cgi) {
1.51      paf       262:                        if(const char *env_request_uri=SAPI::get_env(pool, "REQUEST_URI"))
1.42      paf       263:                                request_info.uri=env_request_uri;
1.51      paf       264:                        else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO"))
1.42      paf       265:                                if(query_string) {
                    266:                                        char *reconstructed_uri=(char *)malloc(
                    267:                                                strlen(path_info)+1/*'?'*/+
                    268:                                                strlen(query_string)+1/*0*/);
                    269:                                        strcpy(reconstructed_uri, path_info);
                    270:                                        strcat(reconstructed_uri, "?");
                    271:                                        strcat(reconstructed_uri, query_string);
                    272:                                        request_info.uri=reconstructed_uri;
                    273:                                } else
                    274:                                        request_info.uri=path_info;
                    275:                        else
                    276:                                PTHROW(0, 0,
                    277:                                        0,
1.43      paf       278:                                        "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)");
1.68      paf       279: 
1.72      paf       280:                        if(const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME")) {
                    281:                                size_t script_name_len=strlen(script_name);
                    282:                                size_t uri_len=strlen(request_info.uri);
                    283:                                if(strncmp(request_info.uri,script_name, script_name_len)==0 &&
                    284:                                        script_name_len != uri_len) // under IIS they are the same
                    285:                                        PTHROW(0, 0,
                    286:                                                0,
                    287:                                                "CGI: illegal call");
                    288:                        }
1.68      paf       289:                } else
1.42      paf       290:                        request_info.uri=0;
1.40      paf       291: 
1.51      paf       292:                request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE");
                    293:                const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH");
1.10      paf       294:                request_info.content_length=(content_length?atoi(content_length):0);
1.51      paf       295:                request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
                    296:                request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
1.10      paf       297: 
1.5       paf       298:                // prepare to process request
1.38      paf       299:                Request request(pool,
1.10      paf       300:                        request_info,
1.91      parser    301:                        cgi ? String::UL_USER_HTML : String::UL_AS_IS
1.5       paf       302:                        );
                    303:                
                    304:                // some root-controlled location
1.102     parser    305: #ifdef SYSCONFDIR
1.111     parser    306:                const char *root_config_filespec=SYSCONFDIR "/" CONFIG_FILE_NAME;
1.102     parser    307: #else
                    308: #      ifdef WIN32
1.10      paf       309:                // c:\windows
1.111     parser    310:                char root_config_path[MAX_STRING];
                    311:                GetWindowsDirectory(root_config_path, MAX_STRING);
                    312: 
                    313:                char root_config_filespec[MAX_STRING];
                    314:                snprintf(root_config_filespec, MAX_STRING, 
                    315:                        "%s/%s", 
                    316:                        root_config_path, CONFIG_FILE_NAME);
1.101     parser    317: #      else
1.102     parser    318:                #error must be compiled either configure/make or MSVC++
1.101     parser    319: #      endif
1.5       paf       320: #endif
                    321:                
                    322:                // beside by binary
1.111     parser    323:                // @todo full path, not ./!
                    324:                static char site_config_path[MAX_STRING];
                    325:                strncpy(site_config_path, argv0, MAX_STRING-1);  site_config_path[MAX_STRING-1]=0; // filespec of my binary
1.88      parser    326:                if(!(
1.111     parser    327:                        rsplit(site_config_path, '/') || 
                    328:                        rsplit(site_config_path, '\\'))) { // strip filename
1.88      parser    329:                        // no path, just filename
1.111     parser    330:                        site_config_path[0]='.'; site_config_path[1]=0;
1.88      parser    331:                }
1.111     parser    332: 
                    333:                char site_config_filespec[MAX_STRING];
                    334:                snprintf(site_config_filespec, MAX_STRING, 
                    335:                        "%s/%s", 
                    336:                        site_config_path, CONFIG_FILE_NAME);
1.5       paf       337:                
                    338:                // process the request
1.35      paf       339:                request.core(
1.111     parser    340:                        root_config_filespec, false,
                    341:                        site_config_filespec, false,
1.35      paf       342:                        header_only);
1.57      paf       343: 
                    344:                //
                    345:                done_socks();
1.84      parser    346: 
                    347: #ifdef DEBUG_POOL_MALLOC
1.78      parser    348:                extern void log_pool_stats(Pool& pool);
                    349:                log_pool_stats(pool);
1.84      parser    350: #endif
1.16      paf       351: 
1.22      paf       352:                // must be last in PTRY{}PCATCH
1.40      paf       353: #ifdef WIN32
1.5       paf       354:                SetUnhandledExceptionFilter(0);
1.25      paf       355: #endif
1.100     parser    356: 
1.16      paf       357:                // successful finish
1.109     parser    358:                result=0;
1.16      paf       359:        } PCATCH(e) { // global problem 
1.43      paf       360:                // must be first in PCATCH{}
                    361: #ifdef WIN32
                    362:                SetUnhandledExceptionFilter(0);
                    363: #endif
1.44      paf       364:                // don't allocate anything on pool here:
                    365:                //   possible pool' exception not catch-ed now
                    366:                //   and there could be out-of-memory exception
1.43      paf       367: 
1.19      paf       368:                const char *body=e.comment();
1.44      paf       369:                // log it
                    370:                SAPI::log(pool, "exception in request exception handler: %s", body);
                    371: 
                    372:                //
1.19      paf       373:                int content_length=strlen(body);
1.5       paf       374: 
1.35      paf       375:                // prepare header
1.37      paf       376:                SAPI::add_header_attribute(pool, "content-type", "text/plain");
1.19      paf       377:                char content_length_cstr[MAX_NUMBER];
1.60      paf       378:                snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
1.37      paf       379:                SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
1.35      paf       380: 
                    381:                // send header
1.37      paf       382:                SAPI::send_header(pool);
1.19      paf       383: 
                    384:                // body
1.35      paf       385:                if(!header_only)
1.37      paf       386:                        SAPI::send_body(pool, body, content_length);
1.94      parser    387: 
1.16      paf       388:                // unsuccessful finish
1.109     parser    389:                result=1;
1.16      paf       390:        }
                    391:        PEND_CATCH
1.109     parser    392: 
                    393: #ifndef WIN32
                    394:        // 
                    395:        if(!cgi)
                    396:                SAPI::send_body(pool, "\n", 1);
                    397: #endif
                    398:        return result;
1.1       paf       399: }

E-mail: