|
|
| version 1.7, 2003/11/28 09:36:26 | version 1.27, 2010/11/24 00:44:09 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: plugins to xml library, controlling i/o; implementation | Parser: plugins to xml library, controlling i/o; implementation |
| Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| Line 13 static const char * const IDENT="$Date$" | Line 13 static const char * const IDENT="$Date$" |
| #include "libxslt/extensions.h" | #include "libxslt/extensions.h" |
| #include "pa_threads.h" | |
| #include "pa_globals.h" | #include "pa_globals.h" |
| #include "pa_request.h" | #include "pa_request.h" |
| /** | THREAD_LOCAL HashStringBool* xml_dependencies = NULL; |
| * xmlFileMatchWithLocalhostEqDocumentRoot: | |
| * filename: the URI for matching | |
| * | |
| * check if the URI matches an HTTP one | |
| * | |
| * Returns 1 if matches, 0 otherwise | |
| */ | |
| static int | |
| xmlFileMatchLocalhost(const char* filename) { | |
| if (!strncmp(filename, "http://localhost", 16)) | |
| return(1); | |
| return(0); | |
| } | |
| /** | static void add_dependency(const String::Body url) { |
| * xmlFileOpenHttpLocalhost : | if(xml_dependencies) // do we need to monitor now? |
| * filename: the URI for matching | xml_dependencies->put(url, true); |
| * | |
| * http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc | |
| * | |
| * input from FILE *, supports compressed input | |
| * if filename is " " then the standard input is used | |
| * | |
| * Returns an I/O context or NULL in case of error | |
| */ | |
| static void * | |
| xmlFileOpenLocalhost (const char* filename) { | |
| FILE *fd; | |
| const char* document_root=pa_thread_request().request_info.document_root; | |
| if(!document_root) | |
| document_root="."; | |
| char path[MAX_STRING]; | |
| path[0]=0; | |
| strcat(path, document_root); | |
| strcat(path, &filename[16]); | |
| #ifdef WIN32 | |
| fd = fopen(path, "rb"); | |
| #else | |
| fd = fopen(path, "r"); | |
| #endif /* WIN32 */ | |
| return((void *) fd); | |
| } | } |
| /** | void pa_xmlStartMonitoringDependencies() { |
| * xmlFileRead: | xml_dependencies=new HashStringBool; |
| * @context: the I/O context | |
| * @buffer: where to drop data | |
| * @len: number of bytes to write | |
| * | |
| * Read @len bytes to @buffer from the I/O channel. | |
| * | |
| * Returns the number of bytes written | |
| */ | |
| static int | |
| pa_xmlFileRead (void * context, char * buffer, int len) { | |
| return(fread(&buffer[0], 1, len, (FILE *) context)); | |
| } | } |
| /** | void pa_xmlStopMonitoringDependencies() { |
| * xmlFileClose: | xml_dependencies=NULL; |
| * @context: the I/O context | |
| * | |
| * Close an I/O channel | |
| */ | |
| static int | |
| pa_xmlFileClose (void * context) { | |
| return ( ( fclose((FILE *) context) == EOF ) ? -1 : 0 ); | |
| } | } |
| ////////////////////////// | HashStringBool* pa_xmlGetDependencies() { |
| HashStringBool* result=xml_dependencies; | |
| xml_dependencies=NULL; | |
| return result; | |
| } | |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| struct MemoryStream { | struct MemoryStream { |
| Line 103 struct MemoryStream { | Line 50 struct MemoryStream { |
| return 0; | return 0; |
| size_t to_read=min(a_size, left); | size_t to_read=min(a_size, left); |
| memcpy(a_buffer, m_buf, to_read); | memcpy(a_buffer, m_buf+m_position, to_read); |
| m_position+=to_read; | m_position+=to_read; |
| return to_read; | return to_read; |
| } | } |
| Line 111 struct MemoryStream { | Line 58 struct MemoryStream { |
| }; | }; |
| #endif | #endif |
| static void * | |
| xmlFileOpen_ReadIntoStream (const char* do_not_store_filename, bool adjust_path_to_root_from_document_root=false) { | |
| #ifdef PA_SAFE_MODE | |
| //copied from libxml/catalog.c | |
| # define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog" | |
| // disable attempts to consult default catalog [usually, that file belongs to other user/group] | |
| if(strcmp(do_not_store_filename, XML_XML_DEFAULT_CATALOG)==0) | |
| return 0; | |
| #endif | |
| Request& r=pa_thread_request(); | |
| char adjust_buf[MAX_STRING]; | |
| if(adjust_path_to_root_from_document_root) { | |
| const char* document_root=r.request_info.document_root; | |
| if(!document_root) | |
| document_root="."; | |
| adjust_buf[0]=0; | |
| strcat(adjust_buf, document_root); | |
| strcat(adjust_buf, &do_not_store_filename[16]); | |
| do_not_store_filename=adjust_buf; | |
| } else | |
| if(!strstr(do_not_store_filename, "http://")) | |
| if(strstr(do_not_store_filename, "file://")){ | |
| do_not_store_filename+=7/*strlen("file://")*/; | |
| #ifdef WIN32 | |
| if( | |
| do_not_store_filename[0]=='/' | |
| && do_not_store_filename[1] | |
| && do_not_store_filename[2]==':' | |
| && do_not_store_filename[3]=='/' | |
| ){ | |
| // skip leading slash for absolute path file:///C:/path/to/file | |
| do_not_store_filename++; | |
| } | |
| #endif | |
| } else if(*do_not_store_filename && do_not_store_filename[1]!=':' && strstr(do_not_store_filename, "://")) { | |
| pa_xmlStopMonitoringDependencies(); | |
| return 0; // plug out [do not handle other prefixes] | |
| } | |
| const char* can_store_filename=pa_strdup(do_not_store_filename); | |
| add_dependency(can_store_filename); | |
| const char *buf; | |
| try { | |
| buf=file_load_text(r, *new String(can_store_filename), | |
| true/*fail_on_read_problem*/, | |
| 0/*params*/, | |
| false/*don't transcode result because it must be fit with @encoding value!*/); | |
| } catch(const Exception& e) { | |
| if(strcmp(e.type(), "file.missing")==0) | |
| return 0; // let the library try that and report an error properly | |
| buf=e.comment(); | |
| } catch(...) { | |
| buf="xmlFileOpen_ReadIntoStream: unknown error"; | |
| } | |
| MemoryStream* stream=new(UseGC) MemoryStream; | |
| stream->m_buf=buf; | |
| stream->m_size=strlen(buf); | |
| return (void *)stream; | |
| } | |
| static int | |
| xmlFileMatchMonitor(const char* /*file_spec_cstr*/) { | |
| return 1; // always intercept, causing xmlFileOpenMonitor to be called | |
| } | |
| static void * | |
| xmlFileOpenMonitor(const char* filename) { | |
| return xmlFileOpen_ReadIntoStream(filename); // handles localfile case, else returns 0 | |
| } | |
| /** | |
| * xmlFileMatchWithLocalhostEqDocumentRoot: | |
| * filename: the URI for matching | |
| * | |
| * check if the URI matches an HTTP one | |
| * | |
| * Returns 1 if matches, 0 otherwise | |
| */ | |
| static int | |
| xmlFileMatchLocalhost(const char* filename) { | |
| if (!strncmp(filename, "http://localhost", 16)) | |
| return(1); | |
| return(0); | |
| } | |
| /** | |
| * xmlFileOpenLocalhost: | |
| * filename: the URI for matching | |
| * | |
| * http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc | |
| * | |
| * Returns an I/O context or NULL in case of error | |
| */ | |
| static void * | |
| xmlFileOpenLocalhost (const char* filename) { | |
| return xmlFileOpen_ReadIntoStream(filename, true/*adjust path to root from document_root*/); | |
| } | |
| static int | static int |
| xmlFileMatchMethod(const char* filename) { | xmlFileMatchMethod(const char* filename) { |
| if (!strncmp(filename, "parser://", 9 /*strlen("parser://"), and check xmlFileOpenMethod*/)) | if (!strncmp(filename, "parser://", 9 /*strlen("parser://"), and check xmlFileOpenMethod*/)) |
| Line 121 xmlFileMatchMethod(const char* filename) | Line 170 xmlFileMatchMethod(const char* filename) |
| /// parser://method/param/here -> ^MAIN:method[/params/here] | /// parser://method/param/here -> ^MAIN:method[/params/here] |
| static void * | static void * |
| xmlFileOpenMethod (const char* afilename) { | xmlFileOpenMethod (const char* afilename) { |
| Request& r=pa_thread_request(); | const char* buf; |
| try { | |
| char* s=pa_strdup(afilename+9 /*strlen("parser://")*/); | Request& r=pa_thread_request(); |
| const char* method_cstr=lsplit(&s, '/'); | |
| const String* method=new String(method_cstr); | char* s=pa_strdup(afilename+9 /*strlen("parser://")*/); |
| String::Body param_body("/"); | const char* method_cstr=lsplit(&s, '/'); |
| if(s) | const String* method=new String(method_cstr); |
| param_body.append_know_length(s, strlen(s)); | String::Body param_body("/"); |
| if(s) | |
| VString* vparam=new VString(*new String(param_body, String::L_TAINTED)); | param_body.append_know_length(s, strlen(s)); |
| { | |
| Temp_lang temp_lang(r, String::L_XML); // default language: XML | VString* vparam=new VString(*new String(param_body, String::L_TAINTED)); |
| Request::Execute_nonvirtual_method_result body= | { |
| r.execute_nonvirtual_method(r.main_class, *method, vparam, true); | Temp_lang temp_lang(r, String::L_XML); // default language: XML |
| if(body.string) { | Request::Execute_nonvirtual_method_result body= |
| MemoryStream *stream=new(UseGC) MemoryStream; | r.execute_nonvirtual_method(r.main_class, *method, vparam, true); |
| stream->m_buf=body.string->cstr(String::L_UNSPECIFIED); | if(body.string) { |
| stream->m_size=strlen(stream->m_buf); | buf=body.string->untaint_cstr(r.flang); |
| return (void*)stream; | } else |
| throw Exception(0, | |
| new String(afilename), | |
| "'%s' method not found in %s class", | |
| method_cstr, MAIN_CLASS_NAME); | |
| } | } |
| } catch(const Exception& e) { | |
| buf=e.comment(); | |
| } catch(...) { | |
| buf="xmlFileOpenLocalhost: unknown error"; | |
| } | } |
| MemoryStream* stream=new(UseGC) MemoryStream; | |
| throw Exception(0, | stream->m_buf=buf; |
| new String(afilename), | stream->m_size=strlen(buf); |
| "'%s' method not found in %s class", | return (void *)stream; |
| method_cstr, MAIN_CLASS_NAME); | |
| } | } |
| /** | |
| * pa_xmlFileReadMethod: | |
| * @context: the I/O context | |
| * @buffer: where to drop data | |
| * @len: number of bytes to write | |
| * | |
| * Read @len bytes to @buffer from the I/O channel. | |
| * | |
| * Returns the number of bytes written | |
| */ | |
| static int | static int |
| pa_xmlFileReadMethod (void * context, //< MemoryStream actually | pa_xmlFileReadMethod (void * context, //< MemoryStream actually |
| char * buffer, int len) | char * buffer, int len) |
| Line 166 pa_xmlFileCloseMethod (void * /*context* | Line 232 pa_xmlFileCloseMethod (void * /*context* |
| void pa_xml_io_init() { | void pa_xml_io_init() { |
| // file open monitorer [for xslt cacher] | |
| // safe mode checker, always fail match, but checks non-"://" there | |
| xmlRegisterInputCallbacks( | |
| xmlFileMatchMonitor, xmlFileOpenMonitor, | |
| pa_xmlFileReadMethod, pa_xmlFileCloseMethod); | |
| // http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc | // http://localhost/abc -> $ENV{DOCUMENT_ROOT}/abc | ./abc |
| xmlRegisterInputCallbacks( | xmlRegisterInputCallbacks( |
| xmlFileMatchLocalhost, xmlFileOpenLocalhost, | xmlFileMatchLocalhost, xmlFileOpenLocalhost, |
| pa_xmlFileRead, pa_xmlFileClose); | pa_xmlFileReadMethod, pa_xmlFileCloseMethod); |
| // parser://method/param/here -> ^MAIN:method[/params/here] | // parser://method/param/here -> ^MAIN:method[/params/here] |
| xmlRegisterInputCallbacks( | xmlRegisterInputCallbacks( |