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