Commit 9e8579fc authored by Tien-Thinh Nguyen's avatar Tien-Thinh Nguyen
Browse files

Minor update for Mime Parser (free memory/use static function)

parent 9b707ff3
......@@ -143,7 +143,6 @@ void IndividualSMContextApiImpl::update_sm_context(
Logger::smf_api_server().debug("Got result for promise ID %d", promise_id);
nlohmann::json json_data = {};
mime_parser parser = {};
std::string body = {};
std::string json_format;
......@@ -153,7 +152,7 @@ void IndividualSMContextApiImpl::update_sm_context(
if (sm_context_response.n1_sm_msg_is_set() and
sm_context_response.n2_sm_info_is_set()) {
parser.create_multipart_related_content(
mime_parser::create_multipart_related_content(
body, json_data.dump(), CURL_MIME_BOUNDARY,
sm_context_response.get_n1_sm_message(),
sm_context_response.get_n2_sm_information(), json_format);
......@@ -161,7 +160,7 @@ void IndividualSMContextApiImpl::update_sm_context(
Pistache::Http::Mime::MediaType(
"multipart/related; boundary=" + std::string(CURL_MIME_BOUNDARY)));
} else if (sm_context_response.n1_sm_msg_is_set()) {
parser.create_multipart_related_content(
mime_parser::create_multipart_related_content(
body, json_data.dump(), CURL_MIME_BOUNDARY,
sm_context_response.get_n1_sm_message(),
multipart_related_content_part_e::NAS, json_format);
......@@ -169,7 +168,7 @@ void IndividualSMContextApiImpl::update_sm_context(
Pistache::Http::Mime::MediaType(
"multipart/related; boundary=" + std::string(CURL_MIME_BOUNDARY)));
} else if (sm_context_response.n2_sm_info_is_set()) {
parser.create_multipart_related_content(
mime_parser::create_multipart_related_content(
body, json_data.dump(), CURL_MIME_BOUNDARY,
sm_context_response.get_n2_sm_information(),
multipart_related_content_part_e::NGAP, json_format);
......
......@@ -103,7 +103,6 @@ void SMContextsCollectionApiImpl::post_sm_contexts(
Logger::smf_api_server().debug("Got result for promise ID %d", promise_id);
nlohmann::json json_data = {};
mime_parser parser = {};
std::string json_format = {};
std::string body = {};
......@@ -112,7 +111,7 @@ void SMContextsCollectionApiImpl::post_sm_contexts(
if (sm_context_response.n1_sm_msg_is_set()) { // add N1 container if
// available
parser.create_multipart_related_content(
mime_parser::create_multipart_related_content(
body, json_data.dump(), CURL_MIME_BOUNDARY,
sm_context_response.get_n1_sm_message(),
multipart_related_content_part_e::NAS, json_format);
......
......@@ -420,7 +420,6 @@ void smf_http2_server::update_sm_context_handler(
Logger::smf_api_server().debug("Got result for promise ID %d", promise_id);
nlohmann::json json_data = {};
mime_parser parser = {};
std::string body = {};
header_map h = {};
std::string json_format = {};
......@@ -431,7 +430,7 @@ void smf_http2_server::update_sm_context_handler(
if (sm_context_response.n1_sm_msg_is_set() and
sm_context_response.n2_sm_info_is_set()) {
parser.create_multipart_related_content(
mime_parser::create_multipart_related_content(
body, json_data.dump(), CURL_MIME_BOUNDARY,
sm_context_response.get_n1_sm_message(),
sm_context_response.get_n2_sm_information(), json_format);
......@@ -439,7 +438,7 @@ void smf_http2_server::update_sm_context_handler(
"content-type", header_value{"multipart/related; boundary=" +
std::string(CURL_MIME_BOUNDARY)});
} else if (sm_context_response.n1_sm_msg_is_set()) {
parser.create_multipart_related_content(
mime_parser::create_multipart_related_content(
body, json_data.dump(), CURL_MIME_BOUNDARY,
sm_context_response.get_n1_sm_message(),
multipart_related_content_part_e::NAS, json_format);
......@@ -447,7 +446,7 @@ void smf_http2_server::update_sm_context_handler(
"content-type", header_value{"multipart/related; boundary=" +
std::string(CURL_MIME_BOUNDARY)});
} else if (sm_context_response.n2_sm_info_is_set()) {
parser.create_multipart_related_content(
mime_parser::create_multipart_related_content(
body, json_data.dump(), CURL_MIME_BOUNDARY,
sm_context_response.get_n2_sm_information(),
multipart_related_content_part_e::NGAP, json_format);
......
......@@ -23,6 +23,10 @@
#include "logger.hpp"
#include "conversions.hpp"
extern "C" {
#include "dynamic_memory_check.h"
}
bool mime_parser::parse(const std::string& str) {
std::string CRLF = "\r\n";
Logger::smf_app().debug("Parsing the message with Simple Parser");
......@@ -127,6 +131,10 @@ void mime_parser::create_multipart_related_content(
body.append(CRLF);
body.append(std::string((char*) n2_msg_hex, n2_message.length() / 2) + CRLF);
body.append("--" + boundary + "--" + CRLF);
// free memory
free_wrapper((void**) &n1_msg_hex);
free_wrapper((void**) &n2_msg_hex);
}
//------------------------------------------------------------------------------
......@@ -158,4 +166,7 @@ void mime_parser::create_multipart_related_content(
body.append(CRLF);
body.append(std::string((char*) msg_hex, message.length() / 2) + CRLF);
body.append("--" + boundary + "--" + CRLF);
// free memory
free_wrapper((void**) &msg_hex);
}
......@@ -62,7 +62,7 @@ class mime_parser {
* @param [const std::string&] str: input string
* @return String represents string in hex format
*/
unsigned char* format_string_as_hex(const std::string& str);
static unsigned char* format_string_as_hex(const std::string& str);
/*
* Create HTTP body content for multipart/related message
......@@ -73,7 +73,7 @@ class mime_parser {
* @param [std::string] n2_message: N2 (NGAP) part
* @return void
*/
void create_multipart_related_content(
void static create_multipart_related_content(
std::string& body, const std::string& json_part,
const std::string boundary, const std::string& n1_message,
const std::string& n2_message,
......@@ -88,7 +88,7 @@ class mime_parser {
* @param [uint8_t] content_type: 1 for NAS content, else NGAP content
* @return void
*/
void create_multipart_related_content(
void static create_multipart_related_content(
std::string& body, const std::string& json_part,
const std::string boundary, const std::string& message,
const multipart_related_content_part_e content_type,
......
......@@ -165,7 +165,6 @@ void smf_sbi::send_n1n2_message_transfer_request(
"Send Communication_N1N2MessageTransfer to AMF (HTTP version %d)",
sm_context_res->http_version);
mime_parser parser = {};
std::string n1_message = sm_context_res->res.get_n1_sm_message();
nlohmann::json json_data = {};
std::string body;
......@@ -177,11 +176,11 @@ void smf_sbi::send_n1n2_message_transfer_request(
if (n2_sm_found > 0) {
std::string n2_message = sm_context_res->res.get_n2_sm_information();
// prepare the body content for Curl
parser.create_multipart_related_content(
mime_parser::create_multipart_related_content(
body, json_part, CURL_MIME_BOUNDARY, n1_message, n2_message);
} else {
// prepare the body content for Curl
parser.create_multipart_related_content(
mime_parser::create_multipart_related_content(
body, json_part, CURL_MIME_BOUNDARY, n1_message,
multipart_related_content_part_e::NAS);
}
......@@ -284,7 +283,6 @@ void smf_sbi::send_n1n2_message_transfer_request(
sm_session_modification) {
Logger::smf_sbi().debug("Send Communication_N1N2MessageTransfer to AMF");
mime_parser parser = {};
std::string body;
nlohmann::json json_data = {};
std::string json_part;
......@@ -297,10 +295,10 @@ void smf_sbi::send_n1n2_message_transfer_request(
if (n2_sm_found > 0) {
std::string n2_message =
sm_session_modification->msg.get_n2_sm_information();
parser.create_multipart_related_content(
mime_parser::create_multipart_related_content(
body, json_part, CURL_MIME_BOUNDARY, n1_message, n2_message);
} else {
parser.create_multipart_related_content(
mime_parser::create_multipart_related_content(
body, json_part, CURL_MIME_BOUNDARY, n1_message,
multipart_related_content_part_e::NAS);
}
......@@ -374,7 +372,6 @@ void smf_sbi::send_n1n2_message_transfer_request(
"Send Communication_N1N2MessageTransfer to AMF (Network-initiated "
"Service Request)");
mime_parser parser = {};
std::string n2_message = report_msg->res.get_n2_sm_information();
nlohmann::json json_data = {};
std::string body;
......@@ -386,10 +383,10 @@ void smf_sbi::send_n1n2_message_transfer_request(
if (n1_sm_found > 0) {
std::string n1_message = report_msg->res.get_n1_sm_message();
// prepare the body content for Curl
parser.create_multipart_related_content(
mime_parser::create_multipart_related_content(
body, json_part, CURL_MIME_BOUNDARY, n1_message, n2_message);
} else {
parser.create_multipart_related_content(
mime_parser::create_multipart_related_content(
body, json_part, CURL_MIME_BOUNDARY, n2_message,
multipart_related_content_part_e::NGAP);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment