Annotation of parser3/src/main/pa_xml_io.C, revision 1.11
1.1 paf 1: /** @file
2: Parser: plugins to xml library, controlling i/o; implementation
3:
4: Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
6: */
7:
8: #include "pa_xml_io.h"
9:
10: #ifdef XML
11:
1.11 ! paf 12: static const char * const IDENT="$Date: 2003/11/28 10:41:11 $";
1.1 paf 13:
14: #include "libxslt/extensions.h"
15:
16: #include "pa_globals.h"
17: #include "pa_request.h"
18:
1.8 paf 19: #ifndef DOXYGEN
20: struct MemoryStream {
21: const char* m_buf;
22: size_t m_size;
23: size_t m_position;
24:
25: int read(char* a_buffer, size_t a_size) {
26: size_t left=m_size-m_position;
27: if(!left)
28: return 0;
29:
30: size_t to_read=min(a_size, left);
31: memcpy(a_buffer, m_buf, to_read);
32: m_position+=to_read;
33: return to_read;
34: }
35:
36: };
37: #endif
38:
1.9 paf 39:
40: #ifdef PA_SAFE_MODE
41: static int
42: xmlFileMatchSafeMode(const char* file_spec_cstr) {
1.11 ! paf 43: if(strstr(file_spec_cstr, "://")) {
1.9 paf 44: String* file_spec=new String(file_spec_cstr, true);
45: struct stat finfo;
46: if(stat(file_spec_cstr, &finfo)!=0)
47: throw Exception("file.missing",
48: file_spec,
49: "stat failed: %s (%d)",
50: strerror(errno), errno);
51:
52: check_safe_mode(finfo, file_spec, file_spec_cstr);
53: }
54: return 0;
55: }
56: #endif
57:
58:
1.1 paf 59: /**
60: * xmlFileMatchWithLocalhostEqDocumentRoot:
61: * filename: the URI for matching
62: *
63: * check if the URI matches an HTTP one
64: *
65: * Returns 1 if matches, 0 otherwise
66: */
67: static int
68: xmlFileMatchLocalhost(const char* filename) {
69: if (!strncmp(filename, "http://localhost", 16))
70: return(1);
71: return(0);
72: }
73:
74:
75: /**
76: * xmlFileOpenHttpLocalhost :
77: * filename: the URI for matching
78: *
79: * http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
80: *
81: * input from FILE *, supports compressed input
82: * if filename is " " then the standard input is used
83: *
84: * Returns an I/O context or NULL in case of error
85: */
86: static void *
87: xmlFileOpenLocalhost (const char* filename) {
1.8 paf 88: Request& r=pa_thread_request();
89: const char* document_root=r.request_info.document_root;
1.1 paf 90: if(!document_root)
91: document_root=".";
92:
93: char path[MAX_STRING];
94: path[0]=0;
95: strcat(path, document_root);
96: strcat(path, &filename[16]);
97:
1.8 paf 98: MemoryStream *stream=new(UseGC) MemoryStream;
99: stream->m_buf=file_read_text(r.charsets, *new String(filename), true);
100: stream->m_size=strlen(stream->m_buf);
1.1 paf 101:
1.8 paf 102: return (void *)stream;
1.1 paf 103: }
104:
1.2 paf 105: static int
106: xmlFileMatchMethod(const char* filename) {
1.3 paf 107: if (!strncmp(filename, "parser://", 9 /*strlen("parser://"), and check xmlFileOpenMethod*/))
1.2 paf 108: return(1);
109: return(0);
110: }
111:
1.5 paf 112: /// parser://method/param/here -> ^MAIN:method[/params/here]
1.2 paf 113: static void *
114: xmlFileOpenMethod (const char* afilename) {
115: Request& r=pa_thread_request();
116:
1.3 paf 117: char* s=pa_strdup(afilename+9 /*strlen("parser://")*/);
1.2 paf 118: const char* method_cstr=lsplit(&s, '/');
119: const String* method=new String(method_cstr);
1.4 paf 120: String::Body param_body("/");
121: if(s)
122: param_body.append_know_length(s, strlen(s));
123:
124: VString* vparam=new VString(*new String(param_body, String::L_TAINTED));
1.2 paf 125: {
126: Temp_lang temp_lang(r, String::L_XML); // default language: XML
1.3 paf 127: Request::Execute_nonvirtual_method_result body=
1.4 paf 128: r.execute_nonvirtual_method(r.main_class, *method, vparam, true);
1.3 paf 129: if(body.string) {
1.2 paf 130: MemoryStream *stream=new(UseGC) MemoryStream;
1.3 paf 131: stream->m_buf=body.string->cstr(String::L_UNSPECIFIED);
1.2 paf 132: stream->m_size=strlen(stream->m_buf);
133: return (void*)stream;
134: }
135: }
136:
137: throw Exception(0,
1.3 paf 138: new String(afilename),
139: "'%s' method not found in %s class",
140: method_cstr, MAIN_CLASS_NAME);
1.2 paf 141: }
142:
143: static int
144: pa_xmlFileReadMethod (void * context, //< MemoryStream actually
145: char * buffer, int len)
146: {
147: MemoryStream& stream=*static_cast<MemoryStream*>(context);
148:
149: return stream.read(buffer, len);
150: }
151:
152: static int
153: pa_xmlFileCloseMethod (void * /*context*/) {
154: return 0;
155: }
156:
157:
1.1 paf 158:
159: void pa_xml_io_init() {
1.9 paf 160: #ifdef PA_SAFE_MODE
161: // safe mode checker, always fail match, but checks non-"://" there
162: xmlRegisterInputCallbacks(
163: xmlFileMatchSafeMode, 0,
164: 0, 0);
165: #endif
1.1 paf 166: // http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
167: xmlRegisterInputCallbacks(
168: xmlFileMatchLocalhost, xmlFileOpenLocalhost,
1.8 paf 169: pa_xmlFileReadMethod, pa_xmlFileCloseMethod);
1.2 paf 170:
1.5 paf 171: // parser://method/param/here -> ^MAIN:method[/params/here]
1.2 paf 172: xmlRegisterInputCallbacks(
173: xmlFileMatchMethod, xmlFileOpenMethod,
174: pa_xmlFileReadMethod, pa_xmlFileCloseMethod);
1.1 paf 175: }
176:
177: #endif
E-mail: