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

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

E-mail: