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

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

E-mail: