From 65f6c25469167e76c5f059ecace3a0a034ea29dc Mon Sep 17 00:00:00 2001 From: Chris Cormack Date: Fri, 20 Sep 2013 09:25:32 +1200 Subject: [PATCH] Making sure we validate our xml before trying to handle it --- lib/NCIP.pm | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/lib/NCIP.pm b/lib/NCIP.pm index 8f9a3e8..5031f6f 100644 --- a/lib/NCIP.pm +++ b/lib/NCIP.pm @@ -53,11 +53,6 @@ sub process_request { #bail out for now } - -#my $response = " Hello There

Hello You Big JERK!

Who would take this book seriously if the first eaxample didn't say \"hello world\"? "; - - #return $response; - warn $request_type; my $handler = NCIP::Handler->new($request_type); return $handler->handle($xml); } @@ -79,21 +74,18 @@ sub handle_initiation { if ($dom) { # should check validity with validate at this point - # if ( $self->validate($dom) ) { - my $request_type = $self->parse_request($dom); - - # do whatever we should do to initiate, then hand back request_type - if ($request_type) { - return $request_type; + if ( $self->validate($dom) ) { + my $request_type = $self->parse_request($dom); + # do whatever we should do to initiate, then hand back request_type + if ($request_type) { + return $request_type; + } + } + else { + warn "Not valid xml"; + # not valid throw error + return; } - - # } - # else { - # warn "Not valid xml"; - # not valid throw error - # return; - # } - } else { return; @@ -103,15 +95,21 @@ sub handle_initiation { sub validate { # this should perhaps be in it's own module - my $self = shift; - my $dom = shift; - my $validity = $dom->is_valid(); + my $self = shift; + my $dom = shift; + try { + $dom->validate(); + } + catch { + warn "Bad xml, caught error: $_"; + return; + } # we could validate against the dtd here, might be good? # my $dtd = XML::LibXML::Dtd->parse_string($dtd_str); - # my $validity = $dom->is_valid($dtd); + # $dom->validate($dtd); # perhaps we could check the ncip version and validate that too - return $validity; + return 1; } sub parse_request { @@ -129,9 +127,9 @@ sub parse_request { } else { warn "Invalid XML"; - return 0; + return; } - return 0; + return; } 1; -- 2.43.2