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

1.27      paf         1: /** @file
                      2:        Parser: scripting and CGI main.
                      3: 
1.155     paf         4:        Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.154     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.27      paf         6: 
1.173   ! paf         7:        $Id: parser3.C,v 1.172 2002/04/16 08:41:05 paf Exp $
1.1       paf         8: */
                      9: 
1.40      paf        10: #include "pa_config_includes.h"
1.3       paf        11: 
1.139     paf        12: #if _MSC_VER
1.131     paf        13: #      include <new.h>
1.148     paf        14: #      include <crtdbg.h>
1.151     paf        15: #else
                     16: #      include "pa_config_paths.h"
1.3       paf        17: #endif
1.27      paf        18: 
1.37      paf        19: #include "pa_sapi.h"
1.76      paf        20: #include "classes.h"
1.24      paf        21: #include "pa_common.h"
1.2       paf        22: #include "pa_request.h"
1.57      paf        23: #include "pa_socks.h"
1.68      paf        24: #include "pa_version.h"
1.125     parser     25: #include "pool_storage.h"
1.69      paf        26: 
1.149     paf        27: #ifdef WIN32
                     28: #      include <windows.h>
1.120     parser     29: #endif
                     30: 
1.158     paf        31: //#define DEBUG_POOL_MALLOC
1.162     paf        32: //#define DEBUG_STRING_APPENDS_VS_EXPANDS
1.160     paf        33: 
                     34: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
                     35: extern ulong 
                     36:        string_piece_appends, 
                     37:        wcontext_result_size,
                     38:        total_alloc_size,
                     39:        string_string_shortcut_economy;
                     40: #endif
1.84      parser     41: 
1.109     parser     42: // consts
1.113     parser     43: 
                     44: extern const char *main_RCSIds[];
1.116     parser     45: #ifdef USE_SMTP
1.113     parser     46: extern const char *smtp_RCSIds[];
1.114     parser     47: #endif
1.113     parser     48: extern const char *gd_RCSIds[];
                     49: extern const char *classes_RCSIds[];
                     50: extern const char *types_RCSIds[];
1.115     parser     51: extern const char *parser3_RCSIds[];
1.113     parser     52: const char **RCSIds[]={
                     53:        main_RCSIds,
1.116     parser     54: #ifdef USE_SMTP
1.113     parser     55:        smtp_RCSIds,
1.114     parser     56: #endif
1.113     parser     57:        gd_RCSIds,
                     58:        classes_RCSIds,
                     59:        types_RCSIds,
1.115     parser     60:        parser3_RCSIds,
1.113     parser     61:        0
                     62: };
1.84      parser     63: 
1.164     paf        64: const char *PARSER_ROOT_CONFIG_ENV_NAME="HTTP_PARSER_ROOT_CONFIG";
                     65: const char *PARSER_SITE_CONFIG_ENV_NAME="HTTP_PARSER_SITE_CONFIG";
1.159     paf        66: 
1.42      paf        67: /// IIS refuses to read bigger chunks
                     68: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M 
                     69: 
1.45      paf        70: const char *argv0;
1.125     parser     71: Pool_storage pool_storage;
                     72: Pool pool(&pool_storage); // global pool [dont describe to doxygen: it confuses it with param names]
1.27      paf        73: bool cgi; ///< we were started as CGI?
1.5       paf        74: 
1.46      paf        75: // SAPI
1.86      parser     76: 
1.124     parser     77: static void log(const char *fmt, va_list args) {
1.61      paf        78:        bool opened;
                     79:        FILE *f=0;
                     80: 
                     81:        if(argv0) {
                     82:                // beside by binary
                     83:                char file_spec[MAX_STRING];
1.98      parser     84:                strncpy(file_spec, argv0, MAX_STRING-1);  file_spec[MAX_STRING-1]=0; // filespec of my binary
1.61      paf        85:                rsplit(file_spec, '/');  rsplit(file_spec, '\\');// strip filename
                     86:                strcat(file_spec, "/parser3.log");
                     87:                f=fopen(file_spec, "at");
                     88:        }
                     89:        opened=f!=0;
                     90:        if(!opened)
                     91:                f=stderr;
                     92: 
                     93:        // prefix
                     94:        time_t t=time(0);
1.171     paf        95:        if(const char *stamp=ctime(&t)) { // never saw that
1.173   ! paf        96:                if(size_t len=strlen(stamp)) // saw once stamp being =""
1.172     paf        97:                        fprintf(f, "[%.*s] ", len-1, stamp);
1.171     paf        98:        }
1.61      paf        99:        // message
1.117     parser    100: 
                    101:        char buf[MAX_STRING];
                    102:        size_t size=vsnprintf(buf, MAX_STRING, fmt, args);
                    103:        remove_crlf(buf, buf+size);
                    104: 
                    105:        fwrite(buf, size, 1, f);
1.61      paf       106:        // newline
                    107:        fprintf(f, "\n");
                    108: 
                    109:        if(opened)
                    110:                fclose(f);
1.85      parser    111:        else
                    112:                fflush(f);
1.124     parser    113: }
                    114: 
                    115: // appends to parser3.log located beside my binary if openable, to stderr otherwize
                    116: void SAPI::log(Pool& , const char *fmt, ...) {
                    117:     va_list args;
                    118:        va_start(args,fmt);
                    119:        ::log(fmt, args);
                    120:        va_end(args);
                    121: }
                    122: 
                    123: void SAPI::die(const char *fmt, ...) {
1.137     paf       124: #ifdef DEBUG_POOL_MALLOC
                    125:        extern void log_pool_stats(Pool& pool);
                    126:        log_pool_stats(pool);
                    127: #endif
                    128: 
1.144     paf       129:     va_list args;
                    130:        va_start(args,fmt);
1.138     paf       131:        // log
                    132: 
                    133:        // logging is more important than user 
                    134:        // she can cancel download, we'd get SIG_PIPE, 
                    135:        // nothing would be logged then
1.124     parser    136:        ::log(fmt, args);
                    137: 
1.138     paf       138:        // inform user
                    139: 
1.134     paf       140:        char body[MAX_STRING];
1.138     paf       141:        int content_length=vsnprintf(body, MAX_STRING, fmt, args);
1.134     paf       142: 
1.144     paf       143:        va_end(args);
                    144: 
1.134     paf       145:        // prepare header
                    146:        // let's be honest, that's bad we couldn't produce valid output
                    147:        SAPI::add_header_attribute(pool, "status", "500");
                    148:        SAPI::add_header_attribute(pool, "content-type", "text/plain");
                    149:        char content_length_cstr[MAX_NUMBER];
1.168     paf       150:        snprintf(content_length_cstr, sizeof(content_length_cstr), "%u", content_length);
1.134     paf       151:        SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
                    152: 
                    153:        // send header
                    154:        SAPI::send_header(pool);
                    155: 
                    156:        // body
                    157:        SAPI::send_body(pool, body, content_length);
                    158: 
1.124     parser    159:        exit(1);
1.61      paf       160: }
                    161: 
1.122     parser    162: const char *SAPI::get_env(Pool& , const char *name) {
1.109     parser    163:        return getenv(name);
1.28      paf       164: }
                    165: 
1.122     parser    166: size_t SAPI::read_post(Pool& , char *buf, size_t max_bytes) {
1.59      paf       167:        size_t read_size=0;
1.12      paf       168:        do {
1.36      paf       169:                int chunk_size=read(fileno(stdin), 
1.42      paf       170:                        buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.129     paf       171:                if(chunk_size<=0)
1.12      paf       172:                        break;
                    173:                read_size+=chunk_size;
                    174:        } while(read_size<max_bytes);
                    175: 
                    176:        return read_size;
1.10      paf       177: }
                    178: 
1.122     parser    179: void SAPI::add_header_attribute(Pool& , const char *key, const char *value) {
1.68      paf       180:        if(cgi)
1.20      paf       181:                printf("%s: %s\n", key, value);
1.19      paf       182: }
                    183: 
1.56      paf       184: /// @todo intelligent cache-control
1.122     parser    185: void SAPI::send_header(Pool& ) {
1.33      paf       186:        if(cgi) {
1.147     paf       187: //             puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33      paf       188: 
                    189:                // header | body  delimiter
1.20      paf       190:                puts("");
1.33      paf       191:        }
1.30      paf       192: }
1.20      paf       193: 
1.122     parser    194: void SAPI::send_body(Pool& , const void *buf, size_t size) {
1.19      paf       195:        stdout_write(buf, size);
1.58      paf       196: }
                    197: 
1.97      parser    198: //
                    199: 
1.166     paf       200: void full_file_spec(const char *file_name, char *buf, size_t buf_size) {
1.167     paf       201:        if(file_name)
                    202:                if(file_name[0]=='/' 
                    203: #ifdef WIN32
                    204:                        || (file_name[0] && file_name[1]==':')
                    205: #endif
                    206:                        ) 
                    207:                        strncpy(buf, file_name, buf_size);
                    208:                else {
                    209:                        char cwd[MAX_STRING];  getcwd(cwd, MAX_STRING);
                    210:                        snprintf(buf, buf_size, "%s/%s", cwd, file_name);
                    211:                }
                    212:        else
                    213:                buf[0]=0;
1.166     paf       214: #ifdef WIN32
                    215:        back_slashes_to_slashes(buf);
                    216: #endif
1.97      parser    217: }
                    218: 
1.40      paf       219: /**
1.122     parser    220: main workhorse
1.19      paf       221: 
1.122     parser    222:   @todo 
1.40      paf       223:                IIS: remove trailing default-document[index.html] from $request.uri.
                    224:                to do that we need to consult metabase,
                    225:                wich is tested but seems slow.
1.144     paf       226:                IIS5 todo find out proper 'illegal call' check 
1.40      paf       227: */
1.122     parser    228: void real_parser_handler(
                    229:                                        const char *filespec_to_process,
                    230:                                        const char *request_method, bool header_only) {
                    231:        // init socks
                    232:        init_socks(pool);
                    233:        
                    234:        // init global classes
                    235:        init_methoded_array(pool);
                    236:        // init global variables
                    237:        pa_globals_init(pool);
                    238:        
                    239:        if(!filespec_to_process)
1.144     paf       240:                SAPI::die("Parser/%s", PARSER_VERSION);
1.122     parser    241:        
                    242:        // Request info
                    243:        Request::Info request_info;
1.166     paf       244:        char document_root_buf[MAX_STRING];
1.122     parser    245:        if(cgi) {
                    246:                if(const char *env_document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"))
                    247:                        request_info.document_root=env_document_root;
                    248:                else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
                    249:                        // IIS
1.166     paf       250:                        size_t len=min(sizeof(document_root_buf)-1, strlen(filespec_to_process)-strlen(path_info));
                    251:                        memcpy(document_root_buf, filespec_to_process, len); document_root_buf[len]=0;
                    252:                        request_info.document_root=document_root_buf;
1.122     parser    253:                } else
1.165     paf       254:                        throw Exception("parser.runtime",
                    255:                                0,
                    256:                                "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.122     parser    257:        } else {
1.166     paf       258:                full_file_spec("", document_root_buf, sizeof(document_root_buf));
                    259:                request_info.document_root=document_root_buf;
1.122     parser    260:        }
                    261:        request_info.path_translated=filespec_to_process;
                    262:        request_info.method=request_method ? request_method : "GET";
                    263:        const char *query_string=SAPI::get_env(pool, "QUERY_STRING");
                    264:        request_info.query_string=query_string;
                    265:        if(cgi) {
                    266:                if(const char *env_request_uri=SAPI::get_env(pool, "REQUEST_URI"))
                    267:                        request_info.uri=env_request_uri;
                    268:                else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO"))
                    269:                        if(query_string) {
                    270:                                char *reconstructed_uri=(char *)pool.malloc(
                    271:                                        strlen(path_info)+1/*'?'*/+
                    272:                                        strlen(query_string)+1/*0*/);
                    273:                                strcpy(reconstructed_uri, path_info);
                    274:                                strcat(reconstructed_uri, "?");
                    275:                                strcat(reconstructed_uri, query_string);
                    276:                                request_info.uri=reconstructed_uri;
                    277:                        } else
                    278:                                request_info.uri=path_info;
                    279:                        else
1.165     paf       280:                                throw Exception("parser.runtime",
                    281:                                        0,
                    282:                                        "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)");
1.145     paf       283:                        
                    284: #ifndef WIN32
                    285:                        // they've changed this under IIS5.
1.122     parser    286:                        if(const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME")) {
                    287:                                size_t script_name_len=strlen(script_name);
                    288:                                size_t uri_len=strlen(request_info.uri);
                    289:                                if(strncmp(request_info.uri,script_name, script_name_len)==0 &&
                    290:                                        script_name_len != uri_len) // under IIS they are the same
1.144     paf       291:                                        SAPI::die("CGI: illegal call");
1.122     parser    292:                        }
1.145     paf       293: #endif
1.122     parser    294:        } else
                    295:                request_info.uri=0;
                    296:        
                    297:        request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE");
                    298:        const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH");
                    299:        request_info.content_length=(content_length?atoi(content_length):0);
                    300:        request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
                    301:        request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
                    302:        
                    303:        // prepare to process request
                    304:        Request request(pool,
                    305:                request_info,
1.143     paf       306: #ifdef _DEBUG
                    307:                String::UL_HTML|String::UL_OPTIMIZE_BIT
                    308: #else
                    309:                cgi ? String::UL_HTML|String::UL_OPTIMIZE_BIT : String::UL_AS_IS
                    310: #endif
                    311:                ,
1.130     paf       312:                true /* status_allowed */);
1.122     parser    313:        
                    314:        // some root-controlled location
1.169     paf       315:        const char *root_config_filespec_cstr;
                    316:        char root_config_filespec_buf[MAX_STRING];
1.159     paf       317:        if(const char *root_config_by_env=getenv(PARSER_ROOT_CONFIG_ENV_NAME))
1.169     paf       318:                root_config_filespec_cstr=root_config_by_env;
1.159     paf       319:        else {
1.152     paf       320: #ifdef ROOT_CONFIG_DIR
1.169     paf       321:                root_config_filespec_cstr=ROOT_CONFIG_DIR "/" CONFIG_FILE_NAME;
1.122     parser    322: #else
                    323: #      ifdef WIN32
1.159     paf       324:                // c:\windows
                    325:                char windows_dir[MAX_STRING];
                    326:                GetWindowsDirectory(windows_dir, MAX_STRING);
                    327: 
1.169     paf       328:                
                    329:                snprintf(root_config_filespec_buf, MAX_STRING, 
1.159     paf       330:                        "%s/%s", 
                    331:                        windows_dir, CONFIG_FILE_NAME);
                    332:                
1.169     paf       333:                root_config_filespec_cstr=root_config_filespec_buf;
1.122     parser    334: #      else
                    335: #error must be compiled either configure/make or MSVC++
                    336: #      endif
                    337: #endif
1.159     paf       338:        }
1.122     parser    339:        
1.169     paf       340:        const char *site_config_filespec_cstr;
                    341:        char site_config_filespec_buf[MAX_STRING];
1.164     paf       342:        if(const char *site_config_by_env=getenv(PARSER_SITE_CONFIG_ENV_NAME))
1.169     paf       343:                site_config_filespec_cstr=site_config_by_env;
1.164     paf       344:        else {
1.122     parser    345:        // beside by binary
                    346:        // @todo full path, not ./!
1.164     paf       347:                char beside_binary_path[MAX_STRING];
                    348:                strncpy(beside_binary_path, argv0, MAX_STRING-1);  beside_binary_path[MAX_STRING-1]=0; // filespec of my binary
                    349:                if(!(
                    350:                        rsplit(beside_binary_path, '/') || 
                    351:                        rsplit(beside_binary_path, '\\'))) { // strip filename
                    352:                        // no path, just filename
                    353:                        beside_binary_path[0]='.'; beside_binary_path[1]=0;
                    354:                }
1.169     paf       355:                snprintf(site_config_filespec_buf, MAX_STRING, 
1.164     paf       356:                        "%s/%s", 
                    357:                        beside_binary_path, CONFIG_FILE_NAME);
1.169     paf       358:                site_config_filespec_cstr=site_config_filespec_buf;
1.122     parser    359:        }
                    360:        
                    361:        // process the request
                    362:        request.core(
1.170     paf       363:                root_config_filespec_cstr, false /*fail_on_read_problem*/,
                    364:                site_config_filespec_cstr, false /*fail_on_read_problem*/,
1.122     parser    365:                header_only);
                    366:        
                    367:        //
                    368:        done_socks();
                    369:        
                    370: #ifdef DEBUG_POOL_MALLOC
                    371:        extern void log_pool_stats(Pool& pool);
                    372:        log_pool_stats(pool);
                    373: #endif
1.160     paf       374: 
                    375: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
                    376:        SAPI::log(pool, 
1.161     paf       377:                "string piece appends=%lu, wcontext_result_size=%lu, string_string_shortcut_economy_closer=%lu, total_alloc_size=%lu", 
1.160     paf       378:                string_piece_appends,
                    379:                wcontext_result_size,
                    380:                string_string_shortcut_economy,
                    381:                total_alloc_size);
                    382: #endif
                    383: 
1.122     parser    384: }
                    385: 
                    386: void call_real_parser_handler__do_SEH(
                    387:                                                                 const char *filespec_to_process,
                    388:                                                                 const char *request_method, bool header_only) {
1.133     paf       389: #if _MSC_VER && !defined(_DEBUG)
1.122     parser    390:        LPEXCEPTION_POINTERS system_exception=0;
                    391:        __try {
                    392: #endif
                    393:                real_parser_handler(
                    394:                        filespec_to_process,
                    395:                        request_method, header_only);
                    396:                
1.133     paf       397: #if _MSC_VER && !defined(_DEBUG)
1.122     parser    398:        } __except (
                    399:                (system_exception=GetExceptionInformation()), 
                    400:                EXCEPTION_EXECUTE_HANDLER) {
                    401:                
                    402:                if(system_exception)
                    403:                        if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.165     paf       404:                                throw Exception(0,
                    405:                                        0,
                    406:                                        "Exception 0x%08X at 0x%08X", er->ExceptionCode,  er->ExceptionAddress);
1.122     parser    407:                        else
1.165     paf       408:                                throw Exception(0, 0, "Exception <no exception record>");
1.122     parser    409:                        else
1.165     paf       410:                                throw Exception(0, 0, "Exception <no exception information>");
1.122     parser    411:        }
                    412: #endif
                    413: }
                    414: 
1.139     paf       415: #if _MSC_VER
1.135     paf       416: int failed_new(size_t size) {
                    417:        SAPI::die("out of memory in 'new', failed to allocated %u bytes", size);
                    418:        return 0; // not reached
1.131     paf       419: }
1.135     paf       420: #endif
1.131     paf       421: 
1.135     paf       422: #ifdef HAVE_SET_NEW_HANDLER
1.134     paf       423: void failed_new() {
1.135     paf       424:     SAPI::die("out of memory in 'new'");
1.131     paf       425: }
                    426: #endif
                    427: 
1.5       paf       428: int main(int argc, char *argv[]) {
1.144     paf       429: //     _asm int 3;
1.45      paf       430:        argv0=argv[0];
                    431: 
1.32      paf       432:        umask(2);
                    433: 
1.3       paf       434:        // were we started as CGI?
1.146     paf       435:        cgi=
1.109     parser    436:                getenv("SERVER_SOFTWARE") || 
                    437:                getenv("SERVER_NAME") || 
                    438:                getenv("GATEWAY_INTERFACE") || 
                    439:                getenv("REQUEST_METHOD");
1.5       paf       440:        
1.10      paf       441:        if(!cgi) {
                    442:                if(argc<2) {
1.69      paf       443:                        printf(
1.155     paf       444:                                "Parser/%s Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)\n"
1.154     paf       445:                                "Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\n"
1.100     parser    446:                                "\n"
                    447:                                "Usage: %s <file>\n",
1.69      paf       448:                                PARSER_VERSION, 
                    449:                                argv0?argv0:"parser3");
1.67      paf       450:                        return 1;
1.10      paf       451:                }
                    452:        }
                    453: 
1.100     parser    454: #ifdef WIN32
                    455:        setmode(fileno(stdin), _O_BINARY);
                    456:        setmode(fileno(stdout), _O_BINARY);
                    457:        setmode(fileno(stderr), _O_BINARY);
                    458: #endif
                    459: 
1.139     paf       460: #if _MSC_VER
1.138     paf       461:        _set_new_handler(failed_new);
1.148     paf       462: 
                    463: #ifdef _DEBUG
                    464:        // Get current flag
                    465:        int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
                    466: 
                    467:        // Turn on leak-checking bit
                    468:        tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
                    469: 
                    470:        // Set flag to the new value
                    471:        _CrtSetDbgFlag( tmpFlag );
                    472: //     _CrtSetBreakAlloc(471);
                    473: 
                    474: #endif
                    475: 
1.138     paf       476: #endif
                    477: 
                    478: #ifdef HAVE_SET_NEW_HANDLER
                    479:        std::set_new_handler(failed_new);
                    480: #endif
                    481: 
1.166     paf       482:        char *raw_filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
                    483:        char filespec_to_process[MAX_STRING];
                    484:        full_file_spec(raw_filespec_to_process, filespec_to_process, sizeof(filespec_to_process));
1.10      paf       485: 
1.109     parser    486:        const char *request_method=getenv("REQUEST_METHOD");
1.35      paf       487:        bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131     paf       488: 
1.122     parser    489:        try { // global try
                    490:                call_real_parser_handler__do_SEH(
                    491:                        filespec_to_process,
                    492:                        request_method, header_only);
                    493:        } catch(const Exception& e) { // global problem 
1.44      paf       494:                // don't allocate anything on pool here:
                    495:                //   possible pool' exception not catch-ed now
                    496:                //   and there could be out-of-memory exception
1.43      paf       497: 
1.144     paf       498:                SAPI::die("exception in request exception handler: %s", e.comment());
1.134     paf       499: #ifndef _DEBUG
1.131     paf       500:        } catch(...) { 
1.134     paf       501:                SAPI::die("<unknown exception>");
1.133     paf       502: #endif
1.16      paf       503:        }
1.122     parser    504:        
1.109     parser    505: 
                    506: #ifndef WIN32
                    507:        // 
                    508:        if(!cgi)
                    509:                SAPI::send_body(pool, "\n", 1);
                    510: #endif
1.156     paf       511: //_asm int 3;
1.134     paf       512:        return 0;
1.1       paf       513: }

E-mail: