Annotation of parser3/src/main/pa_globals.C, revision 1.114

1.15      paf         1: /** @file
1.16      paf         2:        Parser: globals.
                      3: 
1.112     paf         4:        Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.113     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.16      paf         6: 
1.114   ! paf         7:        $Id: pa_globals.C,v 1.113 2002/02/08 08:30:16 paf Exp $
1.1       paf         8: */
                      9: 
1.102     paf        10: #include "pa_config_includes.h"
                     11: 
                     12: #ifdef XML
                     13: //#include "libxml/parser.h"
                     14: //#include "libxslt/xslt.h"
                     15: //#include "libxslt/libxslt.h"
                     16: #include "libxslt/extensions.h"
                     17: #include "libxslt/xsltutils.h"
1.103     paf        18: extern "C" {
1.102     paf        19: #include "libexslt/exslt.h"
1.103     paf        20: };
1.102     paf        21: #endif
                     22: 
1.1       paf        23: #include "pa_globals.h"
1.32      paf        24: #include "pa_string.h"
                     25: #include "pa_hash.h"
1.42      paf        26: #include "pa_sql_driver_manager.h"
1.77      parser     27: #include "pa_dictionary.h"
1.100     paf        28: #include "pa_stylesheet_manager.h"
1.83      parser     29: #include "pa_sapi.h"
1.90      paf        30: #include "pa_cache_managers.h"
1.95      paf        31: #include "pa_charsets.h"
                     32: #include "pa_charset.h"
1.101     paf        33: #include "pa_threads.h"
1.84      parser     34: 
1.86      paf        35: #ifdef DB2
1.83      parser     36: #include "pa_db_manager.h"
1.84      parser     37: #endif
1.1       paf        38: 
1.8       paf        39: String *content_type_name;
1.75      parser     40: String *charset_name;
1.8       paf        41: String *body_name;
1.13      paf        42: String *value_name;
1.14      paf        43: String *expires_name;
                     44: String *path_name;
1.17      paf        45: String *name_name;
                     46: String *size_name;
                     47: String *text_name;
1.8       paf        48: 
1.1       paf        49: String *exception_method_name;
1.38      paf        50: String *post_process_method_name;
1.1       paf        51: 
1.39      paf        52: String *content_disposition_name;
                     53: String *content_disposition_filename_name;
                     54: 
1.1       paf        55: String *unnamed_name;
                     56: 
                     57: String *auto_method_name;
                     58: String *main_method_name;
                     59: 
                     60: String *main_class_name;
                     61: 
1.6       paf        62: String *result_var_name;
1.64      parser     63: String *match_var_name;
1.41      paf        64: String *string_pre_match_name;
1.40      paf        65: String *string_match_name;
1.41      paf        66: String *string_post_match_name;
1.6       paf        67: 
1.79      parser     68: String *charsets_name;
1.36      paf        69: String *mime_types_name;
                     70: String *vfile_mime_type_name;
1.63      parser     71: String *origins_mode_name;
1.67      parser     72: 
1.69      parser     73: String *class_path_name;
                     74: 
1.67      parser     75: String *switch_data_name;
                     76: String *case_default_value;
                     77: 
1.70      parser     78: String *sql_limit_name;
                     79: String *sql_offset_name;
                     80: String *sql_default_name;
                     81: 
1.95      paf        82: String *charset_UTF8_name;
                     83: 
1.71      parser     84: String *hash_default_element_name;
1.42      paf        85: 
1.1       paf        86: Hash *untaint_lang_name2enum;
                     87: 
1.95      paf        88: Charset *utf8_charset;
1.32      paf        89: 
1.5       paf        90: short hex_value[0x100];
1.111     paf        91: 
                     92: #ifdef XML
                     93: GdomeDOMImplementation *domimpl;
                     94: #endif
1.5       paf        95: 
                     96: static void setup_hex_value() {
1.68      parser     97:        memset(hex_value, 0, sizeof(hex_value));
1.5       paf        98:        hex_value['0'] = 0;     
                     99:        hex_value['1'] = 1;     
                    100:        hex_value['2'] = 2;     
                    101:        hex_value['3'] = 3;     
                    102:        hex_value['4'] = 4;     
                    103:        hex_value['5'] = 5;     
                    104:        hex_value['6'] = 6;     
                    105:        hex_value['7'] = 7;     
                    106:        hex_value['8'] = 8;     
                    107:        hex_value['9'] = 9;
                    108:        hex_value['A'] = 10;
                    109:        hex_value['B'] = 11;
                    110:        hex_value['C'] = 12;
                    111:        hex_value['D'] = 13;
                    112:        hex_value['E'] = 14;
                    113:        hex_value['F'] = 15;
                    114:        hex_value['a'] = 10;
                    115:        hex_value['b'] = 11;
                    116:        hex_value['c'] = 12;
                    117:        hex_value['d'] = 13;
                    118:        hex_value['e'] = 14;
                    119:        hex_value['f'] = 15;
                    120: }
1.1       paf       121: 
1.99      paf       122: #ifdef XML
1.101     paf       123: 
                    124: const int MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS=10;
                    125: 
                    126: struct XML_Generic_error_info {
                    127:        pa_thread_t thread_id;
                    128:        char *message;
                    129: } xml_generic_error_infos[MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS];
                    130: 
                    131: XML_Generic_error_info *xml_generic_error_info(pa_thread_t thread_id) {
                    132:        for(int i=0; i<MAX_CONCURRENT_XML_GENERIC_ERROR_THREADS; i++) {
                    133:                XML_Generic_error_info *p=xml_generic_error_infos+i;
                    134:                if(p->thread_id==thread_id)
                    135:                        return p;
                    136:        }
                    137:        return 0;
                    138: }
                    139: 
1.99      paf       140: static void
1.101     paf       141: xmlParserGenericErrorFunc(void *ctx, const char *msg, ...) { 
                    142:     pa_thread_t thread_id=pa_get_thread_id();
                    143: 
                    144:        // infinitely looking for free slot to fill it
                    145:        while(true) {
                    146:                SYNCHRONIZED;  // find+fill blocked
                    147: 
                    148:                // first try to get existing for this thread_id
                    149:                XML_Generic_error_info *p=xml_generic_error_info(thread_id);
                    150:                if(!p) { // occupy empty one
                    151:                        p=xml_generic_error_info(0);
                    152:                        if(!p) // wait for empty for it to appear
                    153:                                continue;
                    154:                }
                    155: 
1.102     paf       156:                p->thread_id=thread_id;
1.101     paf       157:                size_t offset=p->message?strlen(p->message):0;
                    158:                p->message=(char *)realloc(p->message, offset+MAX_STRING);
                    159:                if(!p->message)
                    160:                        SAPI::die(
                    161:                                "out of memory in 'xmlParserGenericErrorFunc', failed to reallocate to %u bytes", 
                    162:                                offset+MAX_STRING);
                    163:                
                    164:                va_list args;
                    165:                va_start(args, msg);
                    166:                vsnprintf(p->message+offset, MAX_STRING, msg, args);
                    167:                va_end(args);
                    168: 
                    169:                break;
                    170:        }
                    171: }
                    172: 
1.102     paf       173: bool xmlHaveGenericErrors() {
                    174:     pa_thread_t thread_id=pa_get_thread_id();
                    175: 
                    176:        SYNCHRONIZED;  // find blocked
                    177: 
                    178:        return xml_generic_error_info(thread_id)!=0;
                    179: }
                    180: 
1.101     paf       181: const char *xmlGenericErrors() {
                    182:     pa_thread_t thread_id=pa_get_thread_id();
                    183: 
                    184:        SYNCHRONIZED;  // find+free blocked
                    185: 
                    186:        XML_Generic_error_info *p=xml_generic_error_info(thread_id);
                    187:        if(!p) // no errors for our thread_id registered
                    188:                return 0;
                    189: 
                    190:        const char *result=p->message;
                    191: 
                    192:        // free slot up 
                    193:        memset(p, 0, sizeof(*p));
                    194: 
                    195:        // it is up to caller to free it
                    196:        return result;
1.99      paf       197: }
1.110     paf       198: 
                    199: /**
                    200:  * xmlFileMatchWithLocalhostEqDocumentRoot:
                    201:  * filename:  the URI for matching
                    202:  *
                    203:  * check if the URI matches an HTTP one
                    204:  *
                    205:  * Returns 1 if matches, 0 otherwise
                    206:  */
                    207: static int
                    208: xmlFileMatchLocalhost(const char *filename) {
                    209:     if (!strncmp(filename, "http://localhost", 16))
                    210:        return(1);
                    211:     return(0);
                    212: }
                    213: 
                    214: 
                    215: /**
                    216:  * xmlFileOpenHttpLocalhost :
                    217:  * filename:  the URI for matching
                    218:  *
                    219:  * http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
                    220:  *
                    221:  * input from FILE *, supports compressed input
                    222:  * if filename is " " then the standard input is used
                    223:  *
                    224:  * Returns an I/O context or NULL in case of error
                    225:  */
                    226: static void *
                    227: xmlFileOpenLocalhost (const char *filename) {
                    228:     FILE *fd;
                    229:     const char* documentRoot;
                    230:     char path[1000];
                    231: 
                    232:        path[0]=0;
                    233:        strcat(path, (documentRoot=getenv("DOCUMENT_ROOT"))?documentRoot:".");
                    234:        strcat(path, &filename[16]);
                    235: 
                    236: #ifdef WIN32
                    237:     fd = fopen(path, "rb");
                    238: #else
                    239:     fd = fopen(path, "r");
                    240: #endif /* WIN32 */
                    241:     return((void *) fd);
                    242: }
                    243: 
                    244: /**
                    245:  * xmlFileRead:
                    246:  * @context:  the I/O context
                    247:  * @buffer:  where to drop data
                    248:  * @len:  number of bytes to write
                    249:  *
                    250:  * Read @len bytes to @buffer from the I/O channel.
                    251:  *
                    252:  * Returns the number of bytes written
                    253:  */
                    254: static int
                    255: xmlFileRead (void * context, char * buffer, int len) {
                    256:     return(fread(&buffer[0], 1,  len, (FILE *) context));
                    257: }
                    258: 
                    259: /**
                    260:  * xmlFileWrite:
                    261:  * @context:  the I/O context
                    262:  * @buffer:  where to drop data
                    263:  * @len:  number of bytes to write
                    264:  *
                    265:  * Write @len bytes from @buffer to the I/O channel.
                    266:  *
                    267:  * Returns the number of bytes written
                    268:  */
                    269: static int
                    270: xmlFileWrite (void * context, const char * buffer, int len) {
                    271:     return(fwrite(&buffer[0], 1,  len, (FILE *) context));
                    272: }
                    273: 
                    274: /**
                    275:  * xmlFileClose:
                    276:  * @context:  the I/O context
                    277:  *
                    278:  * Close an I/O channel
                    279:  */
                    280: static int
                    281: xmlFileClose (void * context) {
                    282:     return ( ( fclose((FILE *) context) == EOF ) ? -1 : 0 );
                    283: }
                    284: 
1.99      paf       285: #endif
                    286: 
1.83      parser    287: void pa_globals_destroy(void *) {
                    288:        try {
1.96      paf       289: #ifdef XML
                    290:                GdomeException exc;
                    291:                gdome_di_unref (domimpl, &exc);
                    292: #endif
1.93      paf       293:                if(cache_managers)
                    294:                        cache_managers->~Cache_managers();
1.95      paf       295: 
                    296:                charsets->~Charsets();
1.83      parser    297:                
                    298:        } catch(const Exception& e) {
                    299:                SAPI::die("pa_globals_destroy failed: %s", e.comment());
                    300:        }
                    301: }
                    302: 
1.108     paf       303: /// @test hint on one should call this for each thread xmlSubstituteEntitiesDefault(1);
1.27      paf       304: void pa_globals_init(Pool& pool) {
1.83      parser    305:        pool.register_cleanup(pa_globals_destroy, 0);
                    306: 
1.32      paf       307:        #undef NEW
                    308:        #define NEW new(pool)
                    309: 
1.5       paf       310:        // hex value
                    311:        setup_hex_value();
                    312: 
1.1       paf       313:        // names
1.32      paf       314:        content_type_name=NEW String(pool, CONTENT_TYPE_NAME);
1.75      parser    315:        charset_name=NEW String(pool, CHARSET_NAME);
1.32      paf       316:        body_name=NEW String(pool, BODY_NAME);
                    317:        value_name=NEW String(pool, VALUE_NAME);
                    318:        expires_name=NEW String(pool, EXPIRES_NAME);
                    319:        path_name=NEW String(pool, PATH_NAME);
                    320:        name_name=NEW String(pool, NAME_NAME);
                    321:        size_name=NEW String(pool, SIZE_NAME);
                    322:        text_name=NEW String(pool, TEXT_NAME);
                    323: 
                    324:        exception_method_name=NEW String(pool, EXCEPTION_METHOD_NAME);
1.38      paf       325:        post_process_method_name=NEW String(pool, POST_PROCESS_METHOD_NAME);
1.39      paf       326: 
                    327:        content_disposition_name=NEW String(pool, CONTENT_DISPOSITION_NAME);
                    328:        content_disposition_filename_name=NEW String(pool, CONTENT_DISPOSITION_FILENAME_NAME);
1.32      paf       329: 
                    330:        unnamed_name=NEW String(pool, UNNAMED_NAME);
                    331: 
                    332:        auto_method_name=NEW String(pool, AUTO_METHOD_NAME);
                    333:        main_method_name=NEW String(pool, MAIN_METHOD_NAME);
                    334: 
                    335:        main_class_name=NEW String(pool, MAIN_CLASS_NAME);
1.6       paf       336: 
1.32      paf       337:        result_var_name=NEW String(pool, RESULT_VAR_NAME);
1.64      parser    338:        match_var_name=NEW String(pool, MATCH_VAR_NAME);
1.41      paf       339:        string_pre_match_name=NEW String(pool, STRING_PRE_MATCH_NAME);
1.40      paf       340:        string_match_name=NEW String(pool, STRING_MATCH_NAME);
1.41      paf       341:        string_post_match_name=NEW String(pool, STRING_POST_MATCH_NAME);
1.6       paf       342: 
1.4       paf       343: 
1.79      parser    344:        charsets_name=NEW String(pool, CHARSETS_NAME);
1.36      paf       345:        mime_types_name=NEW String(pool, MIME_TYPES_NAME);
                    346:        vfile_mime_type_name=NEW String(pool, VFILE_MIME_TYPE_NAME);
1.63      parser    347:        origins_mode_name=NEW String(pool, ORIGINS_MODE_NAME);
1.69      parser    348: 
                    349:        class_path_name=NEW String(pool, CLASS_PATH_NAME);
1.66      parser    350: 
                    351:        //^switch ^case
                    352:        switch_data_name=NEW String(pool, SWITCH_DATA_NAME);
                    353:        case_default_value=NEW String(pool, CASE_DEFAULT_VALUE);
1.70      parser    354: 
                    355:        // sql
                    356:        sql_limit_name=NEW String(pool, SQL_LIMIT_NAME);
                    357:        sql_offset_name=NEW String(pool, SQL_OFFSET_NAME);
                    358:        sql_default_name=NEW String(pool, SQL_DEFAULT_NAME);
1.71      parser    359: 
1.95      paf       360:        // charsets
                    361:        charset_UTF8_name=NEW String(pool, CHARSET_UTF8_NAME);
                    362: 
1.71      parser    363:        // hash
                    364:        hash_default_element_name=NEW String(pool, HASH_DEFAULT_ELEMENT_NAME);
1.70      parser    365: 
1.66      parser    366: 
1.1       paf       367:        // hashes
1.32      paf       368:        untaint_lang_name2enum=NEW Hash(pool);
1.87      paf       369:        #define ULN(cstr, LANG) \
                    370:                untaint_lang_name2enum->put(*NEW String(pool, cstr), (int)String::UL_##LANG);
                    371:        ULN("as-is", AS_IS);
                    372:        ULN("file-spec", FILE_SPEC);
                    373:        ULN("http-header", HTTP_HEADER);
                    374:        ULN("mail-header", MAIL_HEADER);
                    375:        ULN("uri", URI);
                    376:        ULN("table", TABLE);
                    377:        ULN("sql", SQL);
                    378:        ULN("js", JS);
                    379:        ULN("xml", XML);
                    380:        ULN("html", HTML);
1.92      paf       381:        ULN("optimized-html", HTML|String::UL_OPTIMIZE_BIT);
1.1       paf       382: 
1.95      paf       383:        // charsets
                    384:        charsets=NEW Charsets(pool);
                    385:        charsets->put(*charset_UTF8_name, 
                    386:                utf8_charset=NEW Charset(pool, *charset_UTF8_name, 0/*no file=system*/));
1.88      paf       387: 
1.101     paf       388: 
1.88      paf       389:        // Status registration, must be initialized before all registrants
1.90      paf       390:        cache_managers=NEW Cache_managers(pool);
1.42      paf       391: 
1.73      parser    392:        // SQL driver manager
1.90      paf       393:        cache_managers->put(*NEW String(pool, "sql"), 
                    394:                SQL_driver_manager=NEW SQL_Driver_manager(pool));
1.74      parser    395: 
1.86      paf       396: #ifdef DB2
1.83      parser    397:        // DB driver manager
1.90      paf       398:        cache_managers->put(*NEW String(pool, "db"), 
                    399:                DB_manager=NEW DB_Manager(pool));
1.83      parser    400: #endif
                    401: 
1.76      parser    402: #ifdef XML
1.96      paf       403:        // initializing xml libs
                    404: 
                    405:        /* First I get a DOMImplementation reference */
                    406:        domimpl = gdome_di_mkref ();
                    407:     /*
                    408:      * Register the EXSLT extensions and the test module
                    409:      */
                    410:     exsltRegisterAll();
                    411:     xsltRegisterTestModule();
                    412:     xmlDefaultSAXHandlerInit();
                    413:     /*
                    414:      * disable CDATA from being built in the document tree
                    415:      */
1.101     paf       416:     // never added yet  xmlDefaultSAXHandler.cdataBlock = NULL;
1.99      paf       417: 
                    418:        /*
                    419:         * Initialization function for the XML parser.
                    420:         * This is not reentrant. Call once before processing in case of
                    421:         * use in multithreaded programs.
                    422:        */
                    423:        xmlInitParser();
1.107     paf       424: 
                    425:        // 1. this is needed for proper parsing of stylesheets
                    426:        // there were a situation where honest entity ruined innocent xpath compilation
                    427:        // doc says "you sould turn it on on stylesheet load" without deepening into details
                    428:        // 2. when dom tree with entites goes under transform text nodes 
                    429:        // got [erroreosly] cut on first entity occurance
1.109     paf       430:        // --
1.107     paf       431:        // that is why this is:
                    432:        xmlSubstituteEntitiesDefault(1);
1.100     paf       433:        
                    434:        // Bit in the loadsubset context field to tell to do ID/REFs lookups 
                    435:        xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
                    436:        // Bit in the loadsubset context field to tell to do complete the elements attributes lists 
                    437:        // with the ones defaulted from the DTDs 
                    438:     //never added yet xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
1.99      paf       439: 
1.104     paf       440: //regretfully this not only replaces entities on parse, but also on generate   xmlSubstituteEntitiesDefault(1);
1.105     paf       441:        // never switched this on xmlIndentTreeOutput=1;
1.104     paf       442: 
1.101     paf       443:        memset(xml_generic_error_infos, 0, sizeof(xml_generic_error_infos));
                    444:        xmlSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.102     paf       445:        xsltSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.105     paf       446: //     FILE *f=fopen("y:\\xslt.log", "wt");
                    447: //     xsltSetGenericDebugFunc(f/*stderr*/, 0);
1.110     paf       448: 
                    449:        // http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
                    450:        xmlRegisterInputCallbacks(
                    451:                xmlFileMatchLocalhost, xmlFileOpenLocalhost,
                    452:                xmlFileRead, xmlFileClose);
1.96      paf       453: 
                    454:        // XSLT stylesheet manager
1.90      paf       455:        cache_managers->put(*NEW String(pool, "stylesheet"), 
                    456:                stylesheet_manager=NEW Stylesheet_manager(pool));
1.76      parser    457: #endif
1.1       paf       458: }
1.76      parser    459: 
                    460: #if defined(XML) && defined(_MSC_VER)
1.96      paf       461: #      define XML_LIBS "/parser3project/win32xml"
                    462: #      pragma comment(lib, XML_LIBS "/glib/lib/libglib-1.3-11.lib")
1.76      parser    463: #      ifdef _DEBUG
1.114   ! paf       464: #              pragma comment(lib, XML_LIBS "/libxml2-x.x.x/win32/dsp/libxml2_so_debug/libxml2.lib")
        !           465: #              pragma comment(lib, XML_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_so_debug/libexslt.lib")
        !           466: #              pragma comment(lib, XML_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_so_debug/libxslt.lib")
        !           467: #              pragma comment(lib, XML_LIBS "/gdome2-x.x.x/win32/dsp/Debug/libgdome.lib")
1.76      parser    468: #      else
1.114   ! paf       469: #              pragma comment(lib, XML_LIBS "/libxml2-x.x.x/win32/dsp/libxml2_so_release/libxml2.lib")
        !           470: #              pragma comment(lib, XML_LIBS "/libxslt-x.x.x/win32/dsp/libexslt_so_release/libexslt.lib")
        !           471: #              pragma comment(lib, XML_LIBS "/libxslt-x.x.x/win32/dsp/libxslt_so_release/libxslt.lib")
        !           472: #              pragma comment(lib, XML_LIBS "/gdome2-x.x.x/win32/dsp/Release/libgdome.lib")
1.76      parser    473: #      endif
                    474: #endif
1.85      paf       475: 
                    476: 
1.86      paf       477: #if defined(DB2) && defined(_MSC_VER)
                    478: #      define LIBDB2_WIN32_BUILD "/parser3project/win32db/lib"
1.85      paf       479: #      ifdef _DEBUG
1.86      paf       480: #              pragma comment(lib, LIBDB2_WIN32_BUILD "/debug/libdb.lib")
1.85      paf       481: #      else
1.86      paf       482: #              pragma comment(lib, LIBDB2_WIN32_BUILD "/release/libdb.lib")
1.85      paf       483: #      endif
                    484: #endif

E-mail: