Skip to content

Commit

Permalink
Simplify and reduce code duplication in Transaction constructors
Browse files Browse the repository at this point in the history
- Leverage delegating constructor to avoid code duplication between the
  two available Transaction constructors.
  - The constructor without 'id' argument delegates to the one that
    receives it by providing `nullptr` as a value, which is used to
    flag that an id needs to be generated.
- Simplified constructor by removing member initialization where the
  default constructor will be invoked.
  • Loading branch information
eduar-hte committed Sep 3, 2024
1 parent c2a879e commit 55d3483
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 112 deletions.
6 changes: 3 additions & 3 deletions headers/modsecurity/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*
*/

#ifndef HEADERS_MODSECURITY_TRANSACTION_H_
#define HEADERS_MODSECURITY_TRANSACTION_H_

#ifdef __cplusplus
#include <cassert>
#include <ctime>
Expand All @@ -33,9 +36,6 @@
#include <stdlib.h>
#include <stddef.h>

#ifndef HEADERS_MODSECURITY_TRANSACTION_H_
#define HEADERS_MODSECURITY_TRANSACTION_H_

#ifndef __cplusplus
typedef struct ModSecurity_t ModSecurity;
typedef struct Transaction_t Transaction;
Expand Down
120 changes: 11 additions & 109 deletions src/transaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,90 +102,12 @@ namespace modsecurity {
* @endcode
*
*/
Transaction::Transaction(ModSecurity *ms, RulesSet *rules, void *logCbData)
: m_creationTimeStamp(utils::cpu_seconds()),
m_clientIpAddress(""),
m_httpVersion(""),
m_serverIpAddress(""),
m_requestHostName(""),
m_uri(""),
m_uri_no_query_string_decoded(""),
m_ARGScombinedSizeDouble(0),
m_clientPort(0),
m_highestSeverityAction(255),
m_httpCodeReturned(200),
m_serverPort(0),
m_ms(ms),
m_requestBodyType(UnknownFormat),
m_requestBodyProcessor(UnknownFormat),
m_rules(rules),
m_ruleRemoveById(),
m_ruleRemoveByIdRange(),
m_ruleRemoveByTag(),
m_ruleRemoveTargetByTag(),
m_ruleRemoveTargetById(),
m_requestBodyAccess(RulesSet::PropertyNotSetConfigBoolean),
m_auditLogModifier(),
m_ctlAuditEngine(AuditLog::AuditLogStatus::NotSetLogStatus),
m_rulesMessages(),
m_requestBody(),
m_responseBody(),
/* m_id(), */
m_skip_next(0),
m_allowType(modsecurity::actions::disruptive::NoneAllowType),
m_uri_decoded(""),
m_actions(),
m_it(),
m_timeStamp(std::time(NULL)),
m_collections(ms->m_global_collection, ms->m_ip_collection,
ms->m_session_collection, ms->m_user_collection,
ms->m_resource_collection),
m_matched(),
#ifdef WITH_LIBXML2
m_xml(new RequestBodyProcessor::XML(this)),
#else
m_xml(NULL),
#endif
#ifdef WITH_YAJL
m_json(new RequestBodyProcessor::JSON(this)),
#else
m_json(NULL),
#endif
m_secRuleEngine(RulesSetProperties::PropertyNotSetRuleEngine),
m_variableDuration(""),
m_variableEnvs(),
m_variableHighestSeverityAction(""),
m_variableRemoteUser(""),
m_variableTime(""),
m_variableTimeDay(""),
m_variableTimeEpoch(""),
m_variableTimeHour(""),
m_variableTimeMin(""),
m_variableTimeSec(""),
m_variableTimeWDay(""),
m_variableTimeYear(""),
m_logCbData(logCbData),
TransactionAnchoredVariables(this) {
m_id = std::to_string(m_timeStamp) +
std::to_string(modsecurity::utils::generate_transaction_unique_id());

m_variableUrlEncodedError.set("0", 0);
m_variableMscPcreError.set("0", 0);
m_variableMscPcreLimitsExceeded.set("0", 0);

ms_dbg(4, "Initializing transaction");

intervention::clean(&m_it);
}
Transaction::Transaction(ModSecurity *ms, RulesSet *rules, void *logCbData)
: Transaction(ms, rules, nullptr, logCbData) { }

Transaction::Transaction(ModSecurity *ms, RulesSet *rules, char *id, void *logCbData)
: m_creationTimeStamp(utils::cpu_seconds()),
m_clientIpAddress(""),
m_httpVersion(""),
m_serverIpAddress(""),
m_requestHostName(""),
m_uri(""),
m_uri_no_query_string_decoded(""),
m_ARGScombinedSizeDouble(0),
m_clientPort(0),
m_highestSeverityAction(255),
Expand All @@ -195,53 +117,33 @@ Transaction::Transaction(ModSecurity *ms, RulesSet *rules, char *id, void *logCb
m_requestBodyType(UnknownFormat),
m_requestBodyProcessor(UnknownFormat),
m_rules(rules),
m_ruleRemoveById(),
m_ruleRemoveByIdRange(),
m_ruleRemoveByTag(),
m_ruleRemoveTargetByTag(),
m_ruleRemoveTargetById(),
m_requestBodyAccess(RulesSet::PropertyNotSetConfigBoolean),
m_auditLogModifier(),
m_ctlAuditEngine(AuditLog::AuditLogStatus::NotSetLogStatus),
m_rulesMessages(),
m_requestBody(),
m_responseBody(),
m_id(id),
m_skip_next(0),
m_allowType(modsecurity::actions::disruptive::NoneAllowType),
m_uri_decoded(""),
m_actions(),
m_it(),
m_timeStamp(std::time(NULL)),
m_timeStamp(std::time(nullptr)),
m_collections(ms->m_global_collection, ms->m_ip_collection,
ms->m_session_collection, ms->m_user_collection,
ms->m_resource_collection),
m_matched(),
#ifdef WITH_LIBXML2
m_xml(new RequestBodyProcessor::XML(this)),
#else
m_xml(NULL),
m_xml(nullptr),
#endif
#ifdef WITH_YAJL
m_json(new RequestBodyProcessor::JSON(this)),
#else
m_json(NULL),
m_json(nullptr),
#endif
m_secRuleEngine(RulesSetProperties::PropertyNotSetRuleEngine),
m_variableDuration(""),
m_variableEnvs(),
m_variableHighestSeverityAction(""),
m_variableRemoteUser(""),
m_variableTime(""),
m_variableTimeDay(""),
m_variableTimeEpoch(""),
m_variableTimeHour(""),
m_variableTimeMin(""),
m_variableTimeSec(""),
m_variableTimeWDay(""),
m_variableTimeYear(""),
m_logCbData(logCbData),
TransactionAnchoredVariables(this) {
if (id == nullptr) {
m_id = std::to_string(m_timeStamp) +
std::to_string(modsecurity::utils::generate_transaction_unique_id());
} else {
m_id = id;
}

m_variableUrlEncodedError.set("0", 0);
m_variableMscPcreError.set("0", 0);
Expand Down

0 comments on commit 55d3483

Please sign in to comment.