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