@@ -43,10 +43,20 @@ bool ValidateSchema::evaluate(Transaction *transaction,
43
43
const RuleWithActions *rule,
44
44
const bpstd::string_view &str,
45
45
RuleMessage *ruleMessage) {
46
- int rc;
47
46
48
- m_parserCtx = xmlSchemaNewParserCtxt (m_resource.c_str ());
49
- if (m_parserCtx == NULL ) {
47
+ if (transaction->m_xml ->m_data .doc == NULL ) {
48
+ ms_dbg_a (transaction, 4 , " XML document tree could not be found for " \
49
+ " schema validation." );
50
+ return true ;
51
+ }
52
+ if (transaction->m_xml ->m_data .well_formed != 1 ) {
53
+ ms_dbg_a (transaction, 4 , " XML: Schema validation failed because " \
54
+ " content is not well formed." );
55
+ return true ;
56
+ }
57
+
58
+ xmlSchemaParserCtxtPtr parserCtx = xmlSchemaNewParserCtxt (m_resource.c_str ());
59
+ if (parserCtx == NULL ) {
50
60
std::stringstream err;
51
61
err << " XML: Failed to load Schema from file: " ;
52
62
err << m_resource;
@@ -58,18 +68,18 @@ bool ValidateSchema::evaluate(Transaction *transaction,
58
68
return true ;
59
69
}
60
70
61
- xmlSchemaSetParserErrors (m_parserCtx ,
71
+ xmlSchemaSetParserErrors (parserCtx ,
62
72
(xmlSchemaValidityErrorFunc)error_load,
63
73
(xmlSchemaValidityWarningFunc)warn_load, &m_err);
64
74
65
- xmlThrDefSetGenericErrorFunc (m_parserCtx ,
75
+ xmlThrDefSetGenericErrorFunc (parserCtx ,
66
76
null_error);
67
77
68
- xmlSetGenericErrorFunc (m_parserCtx ,
78
+ xmlSetGenericErrorFunc (parserCtx ,
69
79
null_error);
70
80
71
- m_schema = xmlSchemaParse (m_parserCtx );
72
- if (m_schema == NULL ) {
81
+ xmlSchemaPtr schema = xmlSchemaParse (parserCtx );
82
+ if (schema == NULL ) {
73
83
std::stringstream err;
74
84
err << " XML: Failed to load Schema: " ;
75
85
err << m_resource;
@@ -78,60 +88,40 @@ bool ValidateSchema::evaluate(Transaction *transaction,
78
88
err << " " << m_err;
79
89
}
80
90
ms_dbg_a (transaction, 4 , err.str ());
81
- xmlSchemaFreeParserCtxt (m_parserCtx );
91
+ xmlSchemaFreeParserCtxt (parserCtx );
82
92
return true ;
83
93
}
84
94
85
- m_validCtx = xmlSchemaNewValidCtxt (m_schema );
86
- if (m_validCtx == NULL ) {
95
+ xmlSchemaValidCtxtPtr validCtx = xmlSchemaNewValidCtxt (schema );
96
+ if (validCtx == NULL ) {
87
97
std::stringstream err (" XML: Failed to create validation context." );
88
98
if (m_err.empty () == false ) {
89
99
err << " " << m_err;
90
100
}
91
101
ms_dbg_a (transaction, 4 , err.str ());
102
+ xmlSchemaFree (schema);
103
+ xmlSchemaFreeParserCtxt (parserCtx);
92
104
return true ;
93
105
}
94
106
95
107
/* Send validator errors/warnings to msr_log */
96
- xmlSchemaSetValidErrors (m_validCtx ,
108
+ xmlSchemaSetValidErrors (validCtx ,
97
109
(xmlSchemaValidityErrorFunc)error_runtime,
98
110
(xmlSchemaValidityWarningFunc)warn_runtime, transaction);
99
111
100
- if (transaction->m_xml ->m_data .doc == NULL ) {
101
- ms_dbg_a (transaction, 4 , " XML document tree could not be found for " \
102
- " schema validation." );
103
- return true ;
104
- }
105
-
106
- if (transaction->m_xml ->m_data .well_formed != 1 ) {
107
- ms_dbg_a (transaction, 4 , " XML: Schema validation failed because " \
108
- " content is not well formed." );
109
- return true ;
110
- }
111
-
112
- /* Make sure there were no other generic processing errors */
113
- /*
114
- if (msr->msc_reqbody_error) {
115
- ms_dbg_a(t, 4, "XML: Schema validation could not proceed due to previous"
116
- " processing errors.");
117
- return true;
118
- }
119
- */
112
+ int rc = xmlSchemaValidateDoc (validCtx, transaction->m_xml ->m_data .doc );
120
113
121
- rc = xmlSchemaValidateDoc (m_validCtx, transaction->m_xml ->m_data .doc );
114
+ xmlSchemaFreeValidCtxt (validCtx);
115
+ xmlSchemaFree (schema);
116
+ xmlSchemaFreeParserCtxt (parserCtx);
122
117
if (rc != 0 ) {
123
118
ms_dbg_a (transaction, 4 , " XML: Schema validation failed." );
124
- xmlSchemaFree (m_schema);
125
- xmlSchemaFreeParserCtxt (m_parserCtx);
126
119
return true ; /* No match. */
120
+ } else {
121
+ ms_dbg_a (transaction, 4 , " XML: Successfully validated payload against " \
122
+ " Schema: " + m_resource);
123
+ return false ;
127
124
}
128
-
129
- ms_dbg_a (transaction, 4 , " XML: Successfully validated payload against " \
130
- " Schema: " + m_resource);
131
- xmlSchemaFree (m_schema);
132
- xmlSchemaFreeParserCtxt (m_parserCtx);
133
-
134
- return false ;
135
125
}
136
126
137
127
#endif
0 commit comments