Annotation of parser3/src/main/pa_xml_io.C, revision 1.37
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.37 ! moko 12: volatile const char * IDENT_PA_XML_IO_C="$Id: pa_xml_io.C,v 1.36 2019/11/05 19:25:51 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.37 ! moko 63: static void * xmlFileOpen_ReadIntoStream (const char* afilename, bool adjust_path_to_root_from_document_root=false) {
1.18 paf 64: #ifdef PA_SAFE_MODE
65: //copied from libxml/catalog.c
66: # define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
67: // disable attempts to consult default catalog [usually, that file belongs to other user/group]
1.37 ! moko 68: if(strcmp(afilename, XML_XML_DEFAULT_CATALOG)==0)
1.18 paf 69: return 0;
70: #endif
71:
1.15 paf 72: Request& r=pa_thread_request();
1.35 moko 73: char adjust_buf[MAX_STRING];
1.15 paf 74: if(adjust_path_to_root_from_document_root) {
75: const char* document_root=r.request_info.document_root;
76: if(!document_root)
77: document_root=".";
78:
79: adjust_buf[0]=0;
80: strcat(adjust_buf, document_root);
1.37 ! moko 81: strcat(adjust_buf, &afilename[16]);
! 82: afilename=adjust_buf;
1.15 paf 83: } else
1.37 ! moko 84: if(!strstr(afilename, "http://")) {
! 85: if(strstr(afilename, "file://")) {
! 86: afilename+=7 /*strlen("file://")*/;
1.26 misha 87: #ifdef WIN32
1.37 ! moko 88: if(afilename[0]=='/' && afilename[1] && afilename[2]==':' && afilename[3]=='/') {
1.26 misha 89: // skip leading slash for absolute path file:///C:/path/to/file
1.37 ! moko 90: afilename++;
1.26 misha 91: }
92: #endif
1.37 ! moko 93: } else if(afilename[0] && afilename[1]!=':' && strstr(afilename, "://")) {
1.20 misha 94: pa_xmlStopMonitoringDependencies();
95: return 0; // plug out [do not handle other prefixes]
96: }
1.30 moko 97: }
1.17 paf 98:
1.37 ! moko 99: const char* can_store_filename=pa_strdup(afilename);
1.17 paf 100: add_dependency(can_store_filename);
1.15 paf 101:
102: const char *buf;
103: try {
1.23 misha 104: buf=file_load_text(r, *new String(can_store_filename),
1.35 moko 105: true /*fail_on_read_problem*/,
106: 0 /*params*/,
107: false /*don't transcode result because it must be fit with @encoding value!*/);
1.15 paf 108: } catch(const Exception& e) {
1.17 paf 109: if(strcmp(e.type(), "file.missing")==0)
110: return 0; // let the library try that and report an error properly
111:
1.15 paf 112: buf=e.comment();
113: } catch(...) {
1.17 paf 114: buf="xmlFileOpen_ReadIntoStream: unknown error";
1.15 paf 115: }
1.36 moko 116: return (void *)new MemoryStream(buf);
1.15 paf 117: }
1.9 paf 118:
1.35 moko 119: static int xmlFileMatchMonitor(const char* /*file_spec_cstr*/) {
1.17 paf 120: return 1; // always intercept, causing xmlFileOpenMonitor to be called
1.15 paf 121: }
1.35 moko 122:
123: static void *xmlFileOpenMonitor(const char* filename) {
1.15 paf 124: return xmlFileOpen_ReadIntoStream(filename); // handles localfile case, else returns 0
1.9 paf 125: }
126:
1.1 paf 127: /**
128: * xmlFileMatchWithLocalhostEqDocumentRoot:
129: * filename: the URI for matching
130: *
131: * check if the URI matches an HTTP one
132: *
133: * Returns 1 if matches, 0 otherwise
134: */
1.35 moko 135: static int xmlFileMatchLocalhost(const char* filename) {
136: return !strncmp(filename, "http://localhost", 16);
1.1 paf 137: }
138:
139: /**
1.15 paf 140: * xmlFileOpenLocalhost:
1.1 paf 141: * filename: the URI for matching
142: *
143: * http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
144: *
145: * Returns an I/O context or NULL in case of error
146: */
1.35 moko 147: static void *xmlFileOpenLocalhost(const char* filename) {
1.15 paf 148: return xmlFileOpen_ReadIntoStream(filename, true/*adjust path to root from document_root*/);
1.1 paf 149: }
150:
1.35 moko 151: static int xmlFileMatchMethod(const char* filename) {
152: return !strncmp(filename, "parser://", 9 /*strlen("parser://"), and check xmlFileOpenMethod*/);
1.2 paf 153: }
154:
1.5 paf 155: /// parser://method/param/here -> ^MAIN:method[/params/here]
1.35 moko 156: static void *xmlFileOpenMethod (const char* afilename) {
1.15 paf 157: const char* buf;
158: try {
159: Request& r=pa_thread_request();
160:
161: char* s=pa_strdup(afilename+9 /*strlen("parser://")*/);
162: const char* method_cstr=lsplit(&s, '/');
163: const String* method=new String(method_cstr);
164: String::Body param_body("/");
165: if(s)
166: param_body.append_know_length(s, strlen(s));
167:
168: VString* vparam=new VString(*new String(param_body, String::L_TAINTED));
1.32 moko 169: Request::Execute_nonvirtual_method_result body=r.execute_nonvirtual_method(r.main_class, *method, vparam, true);
170: if(body.string) {
171: buf=body.string->untaint_cstr(String::L_XML);
172: } else
173: throw Exception(0, new String(afilename), "'%s' method not found in %s class", method_cstr, MAIN_CLASS_NAME);
1.15 paf 174: } catch(const Exception& e) {
175: buf=e.comment();
176: } catch(...) {
177: buf="xmlFileOpenLocalhost: unknown error";
1.2 paf 178: }
1.36 moko 179: return (void *)new MemoryStream(buf);
1.2 paf 180: }
181:
1.15 paf 182: /**
183: * pa_xmlFileReadMethod:
184: * @context: the I/O context
185: * @buffer: where to drop data
186: * @len: number of bytes to write
187: *
188: * Read @len bytes to @buffer from the I/O channel.
189: *
190: * Returns the number of bytes written
191: */
1.35 moko 192: static int pa_xmlFileReadMethod (void* context, char* buffer, int len){
1.2 paf 193: MemoryStream& stream=*static_cast<MemoryStream*>(context);
194: return stream.read(buffer, len);
195: }
196:
1.35 moko 197: static int pa_xmlFileCloseMethod (void* /*context*/) {
1.2 paf 198: return 0;
199: }
200:
1.1 paf 201: void pa_xml_io_init() {
1.17 paf 202: // file open monitorer [for xslt cacher]
1.9 paf 203: // safe mode checker, always fail match, but checks non-"://" there
1.35 moko 204: xmlRegisterInputCallbacks(xmlFileMatchMonitor, xmlFileOpenMonitor, pa_xmlFileReadMethod, pa_xmlFileCloseMethod);
1.17 paf 205:
1.1 paf 206: // http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc
1.35 moko 207: xmlRegisterInputCallbacks(xmlFileMatchLocalhost, xmlFileOpenLocalhost, pa_xmlFileReadMethod, pa_xmlFileCloseMethod);
1.2 paf 208:
1.37 ! moko 209: // parser://method/param/here -> ^MAIN:method[/params/here] - should be last to be called first
1.35 moko 210: xmlRegisterInputCallbacks(xmlFileMatchMethod, xmlFileOpenMethod, pa_xmlFileReadMethod, pa_xmlFileCloseMethod);
1.1 paf 211: }
212:
213: #endif
E-mail: