Skip to content

Commit 743e739

Browse files
Merge pull request #69 from Easton97-Jens/codex/reduce-duplicated-lines-in-specific-files
Reduce duplicated control-flow and error handling in JSON backends
2 parents 1916516 + b91820f commit 743e739

2 files changed

Lines changed: 40 additions & 20 deletions

File tree

src/request_body_processor/json_backend_jsoncons.cc

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,16 @@ JsonParseResult emitNumberFromRawToken(std::string_view input, JsonEventSink *si
602602
return emitSink(sink, sink->on_number(raw_number), "handling a number");
603603
}
604604

605+
JsonParseResult decodeStringEventValue(const jsoncons::staj_event &event,
606+
const jsoncons::ser_context &context, jsoncons::string_view *decoded) {
607+
std::error_code error;
608+
*decoded = event.get<jsoncons::string_view>(error);
609+
if (error) {
610+
return fromJsonconsError(error, context);
611+
}
612+
return makeResult(JsonParseStatus::Ok);
613+
}
614+
605615
JsonParseResult emitEvent(std::string_view input, JsonEventSink *sink,
606616
RawJsonTokenCursor *token_cursor, const jsoncons::staj_event &event,
607617
const jsoncons::ser_context &context) {
@@ -617,17 +627,19 @@ JsonParseResult emitEvent(std::string_view input, JsonEventSink *sink,
617627
case jsoncons::staj_event_type::end_array:
618628
return emitSink(sink, sink->on_end_array(), "ending an array");
619629
case jsoncons::staj_event_type::key: {
620-
jsoncons::string_view decoded = event.get<jsoncons::string_view>(error);
621-
if (error) {
622-
return fromJsonconsError(error, context);
630+
jsoncons::string_view decoded;
631+
if (JsonParseResult result = decodeStringEventValue(event, context,
632+
&decoded); !result.ok()) {
633+
return result;
623634
}
624635
return emitSink(sink, sink->on_key(std::string_view(decoded.data(),
625636
decoded.size())), "processing an object key");
626637
}
627638
case jsoncons::staj_event_type::string_value: {
628-
jsoncons::string_view decoded = event.get<jsoncons::string_view>(error);
629-
if (error) {
630-
return fromJsonconsError(error, context);
639+
jsoncons::string_view decoded;
640+
if (JsonParseResult result = decodeStringEventValue(event, context,
641+
&decoded); !result.ok()) {
642+
return result;
631643
}
632644
if (isNumericStringEvent(event)) {
633645
std::string sync_detail;
@@ -713,6 +725,11 @@ JsonParseResult parseDocumentWithJsoncons(const std::string &input,
713725
std::chrono::duration_cast<std::chrono::nanoseconds>(
714726
std::chrono::steady_clock::now() - event_loop_start).count()));
715727
};
728+
const auto finish_with_event_loop = [&record_event_loop](
729+
JsonParseResult result) {
730+
record_event_loop();
731+
return result;
732+
};
716733
#else
717734
RawJsonTokenCursor token_cursor(input);
718735
#endif
@@ -721,15 +738,16 @@ JsonParseResult parseDocumentWithJsoncons(const std::string &input,
721738
if (JsonParseResult result = emitEvent(input, sink, &token_cursor,
722739
cursor.current(), cursor.context()); !result.ok()) {
723740
#ifdef MSC_JSON_AUDIT_INSTRUMENTATION
724-
record_event_loop();
741+
return finish_with_event_loop(result);
725742
#endif
726743
return result;
727744
}
728745

729746
cursor.next(error);
730747
if (error) {
731748
#ifdef MSC_JSON_AUDIT_INSTRUMENTATION
732-
record_event_loop();
749+
return finish_with_event_loop(
750+
fromJsonconsError(error, cursor.context()));
733751
#endif
734752
return fromJsonconsError(error, cursor.context());
735753
}
@@ -738,7 +756,8 @@ JsonParseResult parseDocumentWithJsoncons(const std::string &input,
738756
cursor.check_done(error);
739757
if (error) {
740758
#ifdef MSC_JSON_AUDIT_INSTRUMENTATION
741-
record_event_loop();
759+
return finish_with_event_loop(fromJsonconsError(error,
760+
cursor.context()));
742761
#endif
743762
return fromJsonconsError(error, cursor.context());
744763
}

src/request_body_processor/json_backend_simdjson.cc

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -511,28 +511,29 @@ JsonParseResult parsePreparedDocumentWithSimdjson(
511511
return walker.walk(&document);
512512
}
513513

514-
} // namespace
515-
516-
JsonParseResult parseDocumentWithSimdjson(std::string &input,
517-
JsonEventSink *sink, const JsonBackendParseOptions &options) {
514+
JsonParseResult validateSinkAndParsePreparedDocument(
515+
simdjson::padded_string_view input, JsonEventSink *sink,
516+
const JsonBackendParseOptions &options) {
518517
if (sink == nullptr) {
519518
return makeResult(JsonParseStatus::InternalError,
520519
JsonSinkStatus::InternalError, "JSON event sink is null.");
521520
}
522521

522+
return parsePreparedDocumentWithSimdjson(input, sink, options);
523+
}
524+
525+
} // namespace
526+
527+
JsonParseResult parseDocumentWithSimdjson(std::string &input,
528+
JsonEventSink *sink, const JsonBackendParseOptions &options) {
523529
PreparedSimdjsonInput prepared = prepareMutableSimdjsonInput(&input);
524-
return parsePreparedDocumentWithSimdjson(prepared.view, sink, options);
530+
return validateSinkAndParsePreparedDocument(prepared.view, sink, options);
525531
}
526532

527533
JsonParseResult parseDocumentWithSimdjson(const std::string &input,
528534
JsonEventSink *sink, const JsonBackendParseOptions &options) {
529-
if (sink == nullptr) {
530-
return makeResult(JsonParseStatus::InternalError,
531-
JsonSinkStatus::InternalError, "JSON event sink is null.");
532-
}
533-
534535
PreparedSimdjsonInput prepared = prepareConstSimdjsonInput(input);
535-
return parsePreparedDocumentWithSimdjson(prepared.view, sink, options);
536+
return validateSinkAndParsePreparedDocument(prepared.view, sink, options);
536537
}
537538

538539
} // namespace modsecurity::RequestBodyProcessor

0 commit comments

Comments
 (0)