Annotation of parser3/src/main/pa_xml_io.C, revision 1.38
1.1 paf 1: /** @file
2: Parser: plugins to xml library, controlling i/o; implementation
3:
1.33 moko 4: Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)
1.1 paf 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.38 ! moko 12: volatile const char * IDENT_PA_XML_IO_C="$Id: pa_xml_io.C,v 1.37 2019/11/05 19:35:00 moko Exp $" IDENT_PA_XML_IO_H;
1.1 paf 13:
14: #include "libxslt/extensions.h"
15:
1.17 paf 16: #include "pa_threads.h"
1.1 paf 17: #include "pa_globals.h"
18: #include "pa_request.h"
19:
1.27 moko 20: THREAD_LOCAL HashStringBool* xml_dependencies = NULL;
1.17 paf 21:
22: static void add_dependency(const String::Body url) {
1.27 moko 23: if(xml_dependencies) // do we need to monitor now?
24: xml_dependencies->put(url, true);
1.17 paf 25: }
26:
27: void pa_xmlStartMonitoringDependencies() {
1.27 moko 28: xml_dependencies=new HashStringBool;
1.17 paf 29: }
30:
31: void pa_xmlStopMonitoringDependencies() {
1.27 moko 32: xml_dependencies=NULL;
1.17 paf 33: }
34:
35: HashStringBool* pa_xmlGetDependencies() {
1.27 moko 36: HashStringBool* result=xml_dependencies;
37: xml_dependencies=NULL;
38: return result;
1.17 paf 39: }
40:
1.8 paf 41: #ifndef DOXYGEN
1.34 moko 42: struct MemoryStream : public PA_Allocated {
1.8 paf 43: const char* m_buf;
44: size_t m_size;
45: size_t m_position;
46:
1.36 moko 47: MemoryStream(const char *a_buf) : m_buf(a_buf), m_size(strlen(m_buf)), m_position(0) {}
48:
1.8 paf 49: int read(char* a_buffer, size_t a_size) {
50: size_t left=m_size-m_position;
51: if(!left)
52: return 0;
53:
54: size_t to_read=min(a_size, left);
1.14 paf 55: memcpy(a_buffer, m_buf+m_position, to_read);
1.8 paf 56: m_position+=to_read;
57: return to_read;
58: }
59:
60: };
61: #endif
62:
1.38 ! moko 63: static int xmlFileMatchMonitor(const char* /*file_spec_cstr*/) {
! 64: return 1; // always intercept, causing xmlFileOpenMonitor to be called
! 65: }
! 66:
! 67: /**
! 68: * xmlFileOpenLocalhost:
! 69: * filename: the URI for matching
! 70: *
! 71: * http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
! 72: *
! 73: * Returns an I/O context or NULL in case of error
! 74: */
! 75: static void *xmlFileOpenMonitor(const char* afilename) {
1.18 paf 76: #ifdef PA_SAFE_MODE
77: //copied from libxml/catalog.c
78: # define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
79: // disable attempts to consult default catalog [usually, that file belongs to other user/group]
1.37 moko 80: if(strcmp(afilename, XML_XML_DEFAULT_CATALOG)==0)
1.18 paf 81: return 0;
82: #endif
83:
1.15 paf 84: Request& r=pa_thread_request();
1.35 moko 85: char adjust_buf[MAX_STRING];
1.38 ! moko 86: if(!strncmp(afilename, "http://localhost", 16)) {
1.15 paf 87: const char* document_root=r.request_info.document_root;
88: if(!document_root)
89: document_root=".";
90:
91: adjust_buf[0]=0;
92: strcat(adjust_buf, document_root);
1.37 moko 93: strcat(adjust_buf, &afilename[16]);
94: afilename=adjust_buf;
1.15 paf 95: } else
1.37 moko 96: if(!strstr(afilename, "http://")) {
97: if(strstr(afilename, "file://")) {
98: afilename+=7 /*strlen("file://")*/;
1.26 misha 99: #ifdef WIN32
1.37 moko 100: if(afilename[0]=='/' && afilename[1] && afilename[2]==':' && afilename[3]=='/') {
1.26 misha 101: // skip leading slash for absolute path file:///C:/path/to/file
1.37 moko 102: afilename++;
1.26 misha 103: }
104: #endif
1.37 moko 105: } else if(afilename[0] && afilename[1]!=':' && strstr(afilename, "://")) {
1.20 misha 106: pa_xmlStopMonitoringDependencies();
107: return 0; // plug out [do not handle other prefixes]
108: }
1.30 moko 109: }
1.17 paf 110:
1.37 moko 111: const char* can_store_filename=pa_strdup(afilename);
1.17 paf 112: add_dependency(can_store_filename);
1.15 paf 113:
114: const char *buf;
115: try {
1.23 misha 116: buf=file_load_text(r, *new String(can_store_filename),
1.35 moko 117: true /*fail_on_read_problem*/,
118: 0 /*params*/,
119: false /*don't transcode result because it must be fit with @encoding value!*/);
1.15 paf 120: } catch(const Exception& e) {
1.17 paf 121: if(strcmp(e.type(), "file.missing")==0)
122: return 0; // let the library try that and report an error properly
123:
1.15 paf 124: buf=e.comment();
125: } catch(...) {
1.17 paf 126: buf="xmlFileOpen_ReadIntoStream: unknown error";
1.15 paf 127: }
1.36 moko 128: return (void *)new MemoryStream(buf);
1.15 paf 129: }
1.9 paf 130:
1.35 moko 131: static int xmlFileMatchMethod(const char* filename) {
132: return !strncmp(filename, "parser://", 9 /*strlen("parser://"), and check xmlFileOpenMethod*/);
1.2 paf 133: }
134:
1.5 paf 135: /// parser://method/param/here -> ^MAIN:method[/params/here]
1.35 moko 136: static void *xmlFileOpenMethod (const char* afilename) {
1.15 paf 137: const char* buf;
138: try {
139: Request& r=pa_thread_request();
140:
141: char* s=pa_strdup(afilename+9 /*strlen("parser://")*/);
142: const char* method_cstr=lsplit(&s, '/');
143: const String* method=new String(method_cstr);
144: String::Body param_body("/");
145: if(s)
146: param_body.append_know_length(s, strlen(s));
147:
148: VString* vparam=new VString(*new String(param_body, String::L_TAINTED));
1.32 moko 149: Request::Execute_nonvirtual_method_result body=r.execute_nonvirtual_method(r.main_class, *method, vparam, true);
150: if(body.string) {
151: buf=body.string->untaint_cstr(String::L_XML);
152: } else
153: throw Exception(0, new String(afilename), "'%s' method not found in %s class", method_cstr, MAIN_CLASS_NAME);
1.15 paf 154: } catch(const Exception& e) {
155: buf=e.comment();
156: } catch(...) {
157: buf="xmlFileOpenLocalhost: unknown error";
1.2 paf 158: }
1.36 moko 159: return (void *)new MemoryStream(buf);
1.2 paf 160: }
161:
1.15 paf 162: /**
163: * pa_xmlFileReadMethod:
164: * @context: the I/O context
165: * @buffer: where to drop data
166: * @len: number of bytes to write
167: *
168: * Read @len bytes to @buffer from the I/O channel.
169: *
170: * Returns the number of bytes written
171: */
1.35 moko 172: static int pa_xmlFileReadMethod (void* context, char* buffer, int len){
1.2 paf 173: MemoryStream& stream=*static_cast<MemoryStream*>(context);
174: return stream.read(buffer, len);
175: }
176:
1.35 moko 177: static int pa_xmlFileCloseMethod (void* /*context*/) {
1.2 paf 178: return 0;
179: }
180:
1.1 paf 181: void pa_xml_io_init() {
1.17 paf 182: // file open monitorer [for xslt cacher]
1.9 paf 183: // safe mode checker, always fail match, but checks non-"://" there
1.35 moko 184: xmlRegisterInputCallbacks(xmlFileMatchMonitor, xmlFileOpenMonitor, pa_xmlFileReadMethod, pa_xmlFileCloseMethod);
1.17 paf 185:
1.37 moko 186: // parser://method/param/here -> ^MAIN:method[/params/here] - should be last to be called first
1.35 moko 187: xmlRegisterInputCallbacks(xmlFileMatchMethod, xmlFileOpenMethod, pa_xmlFileReadMethod, pa_xmlFileCloseMethod);
1.1 paf 188: }
189:
190: #endif
E-mail: