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

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

E-mail: