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

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

E-mail: