Skip to content

Commit db60119

Browse files
Merge pull request #70 from Easton97-Jens/codex/refactor-duplicate-helper-functions
Deduplicate JSON backend helper functions into common header
2 parents 2fced75 + 3440e0b commit db60119

File tree

3 files changed

+71
-60
lines changed

3 files changed

+71
-60
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 - 2024 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
#ifndef SRC_REQUEST_BODY_PROCESSOR_JSON_BACKEND_COMMON_H_
17+
#define SRC_REQUEST_BODY_PROCESSOR_JSON_BACKEND_COMMON_H_
18+
19+
#include <string>
20+
#include <string_view>
21+
#include <utility>
22+
23+
#include "src/request_body_processor/json_backend.h"
24+
25+
namespace modsecurity::RequestBodyProcessor::json_backend_common {
26+
27+
inline JsonParseResult makeResult(JsonParseStatus parse_status,
28+
JsonSinkStatus sink_status = JsonSinkStatus::Continue,
29+
std::string detail = "") {
30+
return JsonParseResult{parse_status, sink_status, std::move(detail)};
31+
}
32+
33+
inline JsonParseResult makeResult(JsonParseStatus parse_status,
34+
std::string detail) {
35+
return makeResult(parse_status, JsonSinkStatus::Continue, std::move(detail));
36+
}
37+
38+
inline JsonParseResult stopTraversal(JsonSinkStatus sink_status,
39+
std::string_view location) {
40+
return makeResult(JsonParseStatus::Ok, sink_status,
41+
std::string("JSON traversal stopped while ") + std::string(location)
42+
+ ".");
43+
}
44+
45+
inline JsonParseResult finishSinkCall(JsonSinkStatus sink_status,
46+
std::string_view location) {
47+
if (sink_status != JsonSinkStatus::Continue) {
48+
return stopTraversal(sink_status, location);
49+
}
50+
return makeResult(JsonParseStatus::Ok);
51+
}
52+
53+
} // namespace modsecurity::RequestBodyProcessor::json_backend_common
54+
55+
#endif // SRC_REQUEST_BODY_PROCESSOR_JSON_BACKEND_COMMON_H_

src/request_body_processor/json_backend_jsoncons.cc

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#endif
1919

2020
#include "src/request_body_processor/json_backend.h"
21+
#include "src/request_body_processor/json_backend_common.h"
2122

2223
#include <cctype>
2324
#include <chrono>
@@ -35,23 +36,8 @@
3536

3637
namespace modsecurity::RequestBodyProcessor {
3738
namespace {
38-
39-
JsonParseResult makeResult(JsonParseStatus parse_status,
40-
JsonSinkStatus sink_status = JsonSinkStatus::Continue,
41-
std::string detail = "") {
42-
return JsonParseResult{parse_status, sink_status, std::move(detail)};
43-
}
44-
45-
JsonParseResult makeResult(JsonParseStatus parse_status, std::string detail) {
46-
return makeResult(parse_status, JsonSinkStatus::Continue, std::move(detail));
47-
}
48-
49-
JsonParseResult stopTraversal(JsonSinkStatus sink_status,
50-
std::string_view location) {
51-
return makeResult(JsonParseStatus::Ok, sink_status,
52-
std::string("JSON traversal stopped while ") + std::string(location)
53-
+ ".");
54-
}
39+
using json_backend_common::finishSinkCall;
40+
using json_backend_common::makeResult;
5541

5642
bool isUtf8RelatedError(const std::error_code &error) {
5743
switch (static_cast<jsoncons::json_errc>(error.value())) {
@@ -574,14 +560,6 @@ std::string_view rawNumberFromContext(std::string_view input,
574560
return std::string_view();
575561
}
576562

577-
JsonParseResult emitSink(JsonEventSink *sink, JsonSinkStatus sink_status,
578-
std::string_view location) {
579-
if (sink_status != JsonSinkStatus::Continue) {
580-
return stopTraversal(sink_status, location);
581-
}
582-
return makeResult(JsonParseStatus::Ok);
583-
}
584-
585563
JsonParseResult emitNumberFromRawToken(std::string_view input, JsonEventSink *sink,
586564
RawJsonTokenCursor *token_cursor, jsoncons::staj_event_type event_type,
587565
const jsoncons::ser_context &context, const jsoncons::staj_event &event) {
@@ -599,7 +577,7 @@ JsonParseResult emitNumberFromRawToken(std::string_view input, JsonEventSink *si
599577
JsonSinkStatus::Continue,
600578
"Unable to materialize numeric JSON token from jsoncons backend.");
601579
}
602-
return emitSink(sink, sink->on_number(raw_number), "handling a number");
580+
return finishSinkCall(sink->on_number(raw_number), "handling a number");
603581
}
604582

605583
JsonParseResult decodeStringEventValue(const jsoncons::staj_event &event,
@@ -619,20 +597,20 @@ JsonParseResult emitEvent(std::string_view input, JsonEventSink *sink,
619597

620598
switch (event.event_type()) {
621599
case jsoncons::staj_event_type::begin_object:
622-
return emitSink(sink, sink->on_start_object(), "starting an object");
600+
return finishSinkCall(sink->on_start_object(), "starting an object");
623601
case jsoncons::staj_event_type::end_object:
624-
return emitSink(sink, sink->on_end_object(), "ending an object");
602+
return finishSinkCall(sink->on_end_object(), "ending an object");
625603
case jsoncons::staj_event_type::begin_array:
626-
return emitSink(sink, sink->on_start_array(), "starting an array");
604+
return finishSinkCall(sink->on_start_array(), "starting an array");
627605
case jsoncons::staj_event_type::end_array:
628-
return emitSink(sink, sink->on_end_array(), "ending an array");
606+
return finishSinkCall(sink->on_end_array(), "ending an array");
629607
case jsoncons::staj_event_type::key: {
630608
jsoncons::string_view decoded;
631609
if (JsonParseResult result = decodeStringEventValue(event, context,
632610
&decoded); !result.ok()) {
633611
return result;
634612
}
635-
return emitSink(sink, sink->on_key(std::string_view(decoded.data(),
613+
return finishSinkCall(sink->on_key(std::string_view(decoded.data(),
636614
decoded.size())), "processing an object key");
637615
}
638616
case jsoncons::staj_event_type::string_value: {
@@ -648,24 +626,24 @@ JsonParseResult emitEvent(std::string_view input, JsonEventSink *sink,
648626
&& token_cursor->advanceExactNumber(decoded_number,
649627
&sync_detail)) {
650628
recordJsonconsTokenExactAdvanceStep();
651-
return emitSink(sink, sink->on_number(decoded_number),
629+
return finishSinkCall(sink->on_number(decoded_number),
652630
"handling a number");
653631
}
654632
return emitNumberFromRawToken(input, sink, token_cursor,
655633
jsoncons::staj_event_type::double_value, context, event);
656634
}
657-
return emitSink(sink, sink->on_string(std::string_view(decoded.data(),
635+
return finishSinkCall(sink->on_string(std::string_view(decoded.data(),
658636
decoded.size())), "handling a string");
659637
}
660638
case jsoncons::staj_event_type::null_value:
661-
return emitSink(sink, sink->on_null(), "handling a null value");
639+
return finishSinkCall(sink->on_null(), "handling a null value");
662640
case jsoncons::staj_event_type::bool_value:
663641
{
664642
bool boolean_value = event.get<bool>(error);
665643
if (error) {
666644
return fromJsonconsError(error, context);
667645
}
668-
return emitSink(sink, sink->on_boolean(boolean_value),
646+
return finishSinkCall(sink->on_boolean(boolean_value),
669647
"handling a boolean");
670648
}
671649
case jsoncons::staj_event_type::int64_value:

src/request_body_processor/json_backend_simdjson.cc

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#endif
1919

2020
#include "src/request_body_processor/json_backend.h"
21+
#include "src/request_body_processor/json_backend_common.h"
2122

2223
#include <algorithm>
2324
#include <chrono>
@@ -31,31 +32,8 @@
3132

3233
namespace modsecurity::RequestBodyProcessor {
3334
namespace {
34-
35-
JsonParseResult makeResult(JsonParseStatus parse_status,
36-
JsonSinkStatus sink_status = JsonSinkStatus::Continue,
37-
std::string detail = "") {
38-
return JsonParseResult{parse_status, sink_status, std::move(detail)};
39-
}
40-
41-
JsonParseResult makeResult(JsonParseStatus parse_status, std::string detail) {
42-
return makeResult(parse_status, JsonSinkStatus::Continue, std::move(detail));
43-
}
44-
45-
JsonParseResult stopTraversal(JsonSinkStatus sink_status,
46-
std::string_view location) {
47-
return makeResult(JsonParseStatus::Ok, sink_status,
48-
std::string("JSON traversal stopped while ") + std::string(location)
49-
+ ".");
50-
}
51-
52-
JsonParseResult finishSinkCall(JsonSinkStatus sink_status,
53-
std::string_view location) {
54-
if (sink_status != JsonSinkStatus::Continue) {
55-
return stopTraversal(sink_status, location);
56-
}
57-
return makeResult(JsonParseStatus::Ok);
58-
}
35+
using json_backend_common::finishSinkCall;
36+
using json_backend_common::makeResult;
5937

6038
JsonParseResult fromSimdjsonError(simdjson::error_code error) {
6139
switch (error) {

0 commit comments

Comments
 (0)