added a force_valid_xml option which causes an error when the resulting HTML is not...
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 14 Nov 2008 18:26:24 +0000 (18:26 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 14 Nov 2008 18:26:24 +0000 (18:26 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@11185 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/examples/oils_web.xml.example
Open-ILS/src/perlmods/OpenILS/WWW/EGWeb.pm

index a5ac6b3..afebd8e 100644 (file)
@@ -6,6 +6,11 @@
          E.g. <media_prefix>http://static.example.com/media</media_prefix> -->
     <media_prefix/>
 
+    <!-- If set to true, all output will be parsed as XML before delivery to the client.  
+        If XML parsing fails, the error message, with HTML included, will be output as text/plain.
+        XML parsing adds overhead, so this should only be used for debugging -->
+    <force_valid_xml>false</force_valid_xml>
+
     <!-- Where templates can be found.  Paths will be checked in the order entered here.
          It's possible to override individual or sets of templates by putting them into
          a path in front of the default template path -->
index 14146b9..382db60 100644 (file)
@@ -38,7 +38,7 @@ sub handler {
     $r->content_type('text/html; encoding=utf8');
 
     my $tt = Template->new({
-        OUTPUT => $r,
+        OUTPUT => ($ctx->{force_valid_xml}) ? sub { validate_as_xml($r, @_); } : $r,
         INCLUDE_PATH => $ctx->{template_paths},
     });
 
@@ -50,6 +50,21 @@ sub handler {
     return Apache2::Const::OK;
 }
 
+sub validate_as_xml {
+    my $r = shift;
+    my $data = shift;
+    eval { XML::Simple->new->XMLin($data); };
+    if($@) {
+        my $err = "Invalid XML: $@";
+        $r->log->error($err);
+        $r->content_type('text/plain; encoding=utf8');
+        $r->print("\n$err\n\n$data");
+    } else {
+        $r->print($data);
+    }
+}
+
+
 sub load_context {
     my $r = shift;
     my $cgi = CGI->new;
@@ -120,6 +135,7 @@ sub parse_config {
     $ctx->{media_prefix} = (ref $data->{media_prefix}) ? '' : $data->{media_prefix};
     $ctx->{base_uri} = (ref $data->{base_uri}) ? '' : $data->{base_uri};
     $ctx->{template_paths} = [];
+    $ctx->{force_valid_xml} = ($data->{force_valid_xml} =~ /true/io) ? 1 : 0;
 
     my $tpaths = $data->{template_paths}->{path};
     $tpaths = [$tpaths] unless ref $tpaths;