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

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

E-mail: