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

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

E-mail: