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