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