Skip to content

Commit 5c70617

Browse files
Refactor JSONAdapter parse method to use parseImpl
1 parent a12f9f5 commit 5c70617

1 file changed

Lines changed: 15 additions & 24 deletions

File tree

src/request_body_processor/json_adapter.cc

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ JsonParseResult normalizeResult(JsonParseResult result) {
5353
return result;
5454
}
5555

56-
} // namespace
57-
58-
JsonParseResult JSONAdapter::parse(std::string &input,
56+
template <typename StringType>
57+
JsonParseResult parseImpl(StringType &input,
5958
JsonEventSink *sink,
60-
const JsonBackendParseOptions &options [[maybe_unused]]) const {
59+
const JsonBackendParseOptions &options [[maybe_unused]],
60+
const JSONAdapter *adapter) {
6161
if (sink == nullptr) {
6262
return makeResult(JsonParseStatus::InternalError,
6363
JsonSinkStatus::InternalError, "JSON event sink is null.");
@@ -68,37 +68,28 @@ JsonParseResult JSONAdapter::parse(std::string &input,
6868
}
6969

7070
#if defined(MSC_JSON_BACKEND_SIMDJSON)
71-
return normalizeResult(parseDocumentWithSimdjson(input, sink, options));
71+
return normalizeResult(adapter->parseDocumentWithSimdjson(input, sink, options));
7272
#elif defined(MSC_JSON_BACKEND_JSONCONS)
73-
return normalizeResult(parseDocumentWithJsoncons(input, sink, options));
73+
return normalizeResult(adapter->parseDocumentWithJsoncons(input, sink, options));
7474
#else
7575
return makeResult(JsonParseStatus::InternalError,
7676
JsonSinkStatus::InternalError,
7777
"ModSecurity was built without a selected JSON backend.");
7878
#endif
7979
}
8080

81-
JsonParseResult JSONAdapter::parse(const std::string &input,
81+
} // namespace
82+
83+
JsonParseResult JSONAdapter::parse(std::string &input,
8284
JsonEventSink *sink,
8385
const JsonBackendParseOptions &options [[maybe_unused]]) const {
84-
if (sink == nullptr) {
85-
return makeResult(JsonParseStatus::InternalError,
86-
JsonSinkStatus::InternalError, "JSON event sink is null.");
87-
}
88-
89-
if (input.empty()) {
90-
return makeResult(JsonParseStatus::Ok);
91-
}
86+
return parseImpl(input, sink, options, this);
87+
}
9288

93-
#if defined(MSC_JSON_BACKEND_SIMDJSON)
94-
return normalizeResult(parseDocumentWithSimdjson(input, sink, options));
95-
#elif defined(MSC_JSON_BACKEND_JSONCONS)
96-
return normalizeResult(parseDocumentWithJsoncons(input, sink, options));
97-
#else
98-
return makeResult(JsonParseStatus::InternalError,
99-
JsonSinkStatus::InternalError,
100-
"ModSecurity was built without a selected JSON backend.");
101-
#endif
89+
JsonParseResult JSONAdapter::parse(const std::string &input,
90+
JsonEventSink *sink,
91+
const JsonBackendParseOptions &options [[maybe_unused]]) const {
92+
return parseImpl(input, sink, options, this);
10293
}
10394

10495
} // namespace modsecurity::RequestBodyProcessor

0 commit comments

Comments
 (0)