Diff for /parser3/src/main/pa_xml_io.C between versions 1.3 and 1.14

version 1.3, 2003/11/28 09:24:52 version 1.14, 2003/12/01 14:20:50
Line 16  static const char * const IDENT="$Date$" Line 16  static const char * const IDENT="$Date$"
 #include "pa_globals.h"  #include "pa_globals.h"
 #include "pa_request.h"  #include "pa_request.h"
   
   #ifndef DOXYGEN
   struct MemoryStream {
           const char* m_buf;
           size_t m_size;
           size_t m_position;
   
           int read(char* a_buffer, size_t a_size) {
                   size_t left=m_size-m_position;
                   if(!left)
                           return 0;
   
                   size_t to_read=min(a_size, left);
                   memcpy(a_buffer, m_buf+m_position, to_read);
                   m_position+=to_read;
                   return to_read;
           }
   
   };
   #endif
   
   
   #ifdef PA_SAFE_MODE
   static int
   xmlFileMatchSafeMode(const char* file_spec_cstr) {
           if(strstr(file_spec_cstr, "://")) {
                   String* file_spec=new String(file_spec_cstr, true);
                   struct stat finfo;
                   if(stat(file_spec_cstr, &finfo)!=0)
                           throw Exception("file.missing", 
                                   file_spec, 
                                   "stat failed: %s (%d)", 
                                           strerror(errno), errno);
   
                   check_safe_mode(finfo, file_spec, file_spec_cstr);
           }
           return 0;
   }
   #endif
   
   
 /**  /**
  * xmlFileMatchWithLocalhostEqDocumentRoot:   * xmlFileMatchWithLocalhostEqDocumentRoot:
  * filename:  the URI for matching   * filename:  the URI for matching
Line 45  xmlFileMatchLocalhost(const char* filena Line 85  xmlFileMatchLocalhost(const char* filena
  */   */
 static void *  static void *
 xmlFileOpenLocalhost (const char* filename) {  xmlFileOpenLocalhost (const char* filename) {
         //_asm int 3;          Request& r=pa_thread_request();
         FILE *fd;          const char* document_root=r.request_info.document_root;
         const char* document_root=pa_thread_request().request_info.document_root;  
         if(!document_root)          if(!document_root)
                 document_root=".";                  document_root=".";
   
Line 55  xmlFileOpenLocalhost (const char* filena Line 94  xmlFileOpenLocalhost (const char* filena
         path[0]=0;          path[0]=0;
         strcat(path, document_root);          strcat(path, document_root);
         strcat(path, &filename[16]);          strcat(path, &filename[16]);
           
 #ifdef WIN32  
         fd = fopen(path, "rb");  
 #else  
         fd = fopen(path, "r");  
 #endif /* WIN32 */  
         return((void *) fd);  
 }  
   
 /**  
  * xmlFileRead:  
  * @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));  
 }  
   
 /**          if(char *buf=file_read_text(r.charsets, *new String(path), false)) {
  * xmlFileClose:                  MemoryStream *stream=new(UseGC) MemoryStream;
  * @context:  the I/O context                  stream->m_buf=buf;
  *                  stream->m_size=strlen(buf);
  * Close an I/O channel                  return (void *)stream;
  */          } else
 static int                  return 0;
 pa_xmlFileClose (void * context) {  
         return ( ( fclose((FILE *) context) == EOF ) ? -1 : 0 );  
 }  }
   
 //////////////////////////  
   
 #ifndef DOXYGEN  
 struct MemoryStream {  
         const char* m_buf;  
         size_t m_size;  
         size_t m_position;  
   
         int read(char* a_buffer, size_t a_size) {  
                 size_t left=m_size-m_position;  
                 if(!left)  
                         return 0;  
   
                 size_t to_read=min(a_size, left);  
                 memcpy(a_buffer, m_buf, to_read);  
                 m_position+=to_read;  
                 return to_read;  
         }  
   
 };  
 #endif  
   
 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 119  xmlFileMatchMethod(const char* filename) Line 111  xmlFileMatchMethod(const char* filename)
         return(0);          return(0);
 }  }
   
 /// parser://method/param/here -> ^/abc | ./abc  /// parser://method/param/here -> ^MAIN:method[/params/here]
 static void *  static void *
 xmlFileOpenMethod (const char* afilename) {  xmlFileOpenMethod (const char* afilename) {
         //_asm int 3;  
         Request& r=pa_thread_request();          Request& r=pa_thread_request();
   
         char* s=pa_strdup(afilename+9 /*strlen("parser://")*/);          char* s=pa_strdup(afilename+9 /*strlen("parser://")*/);
         const char* method_cstr=lsplit(&s, '/');          const char* method_cstr=lsplit(&s, '/');
         const String* method=new String(method_cstr);          const String* method=new String(method_cstr);
         VString* param=s?new VString(*new String(s)):0;          String::Body param_body("/");  
           if(s)
                   param_body.append_know_length(s, strlen(s));
   
           VString* vparam=new VString(*new String(param_body, String::L_TAINTED));
         {          {
                 Temp_lang temp_lang(r, String::L_XML); // default language: XML                  Temp_lang temp_lang(r, String::L_XML); // default language: XML
                 Request::Execute_nonvirtual_method_result body=                  Request::Execute_nonvirtual_method_result body=
                         r.execute_nonvirtual_method(r.main_class, *method, param, true);                          r.execute_nonvirtual_method(r.main_class, *method, vparam, true);
                 if(body.string) {                  if(body.string) {
                         MemoryStream *stream=new(UseGC) MemoryStream;                          MemoryStream *stream=new(UseGC) MemoryStream;
                         stream->m_buf=body.string->cstr(String::L_UNSPECIFIED);                          stream->m_buf=body.string->cstr(String::L_UNSPECIFIED);
Line 156  pa_xmlFileReadMethod (void * context, // Line 151  pa_xmlFileReadMethod (void * context, //
         return stream.read(buffer, len);          return stream.read(buffer, len);
 }  }
   
 /**  
  * xmlFileClose:  
  * @context:  the I/O context  
  *  
  * Close an I/O channel  
  */  
 static int  static int
 pa_xmlFileCloseMethod (void * /*context*/) {  pa_xmlFileCloseMethod (void * /*context*/) {
         return 0;          return 0;
Line 170  pa_xmlFileCloseMethod (void * /*context* Line 159  pa_xmlFileCloseMethod (void * /*context*
   
   
 void pa_xml_io_init() {  void pa_xml_io_init() {
   #ifdef PA_SAFE_MODE
           // safe mode checker, always fail match, but checks non-"://" there
           xmlRegisterInputCallbacks(
                   xmlFileMatchSafeMode, 0,
                   0, 0);
   #endif
         // 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 -> ^/abc | ./abc          // parser://method/param/here -> ^MAIN:method[/params/here]
         xmlRegisterInputCallbacks(          xmlRegisterInputCallbacks(
                 xmlFileMatchMethod, xmlFileOpenMethod,                  xmlFileMatchMethod, xmlFileOpenMethod,
                 pa_xmlFileReadMethod, pa_xmlFileCloseMethod);                  pa_xmlFileReadMethod, pa_xmlFileCloseMethod);

Removed from v.1.3  
changed lines
  Added in v.1.14


E-mail: