Commit 4c8c76ec authored by Tien-Thinh Nguyen's avatar Tien-Thinh Nguyen

Replace Curl.cpp/hpp by ausf_client.cpp/hpp

parent f2918401
...@@ -19,9 +19,8 @@ ...@@ -19,9 +19,8 @@
* contact@openairinterface.org * contact@openairinterface.org
*/ */
/*! \file rijndael.cpp /*! \file sha256.cpp
\brief \brief
\brief Based on https://github.com/OPENAIRINTERFACE/openair-hss
\author Jian Yang, Fengjiao He, Hongxin Wang \author Jian Yang, Fengjiao He, Hongxin Wang
\company \company
\date 2020 \date 2020
......
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
#include <typeinfo> #include <typeinfo>
#include <map> #include <map>
#include "ausf_config.hpp" #include "ausf_config.hpp"
#include "curl.hpp" #include "ausf_client.hpp"
#include "ProblemDetails.h" #include "ProblemDetails.h"
using namespace config; using namespace config;
...@@ -223,7 +223,7 @@ void DefaultApiImpl::ue_authentications_auth_ctx_id5g_aka_confirmation_put( ...@@ -223,7 +223,7 @@ void DefaultApiImpl::ue_authentications_auth_ctx_id5g_aka_confirmation_put(
confirmResultInfo["authRemovalInd"] = false; confirmResultInfo["authRemovalInd"] = false;
cout << confirmResultInfo.dump() << endl; cout << confirmResultInfo.dump() << endl;
Curl::curl_http_client( ausf_client::curl_http_client(
udmUri, Method, confirmResultInfo.dump(), Response); udmUri, Method, confirmResultInfo.dump(), Response);
} }
} }
...@@ -304,7 +304,7 @@ void DefaultApiImpl::ue_authentications_post( ...@@ -304,7 +304,7 @@ void DefaultApiImpl::ue_authentications_post(
Logger::ausf_server().info("received normal authInfo from amf"); Logger::ausf_server().info("received normal authInfo from amf");
} }
Curl::curl_http_client(udmUri, Method, AuthInfo.dump(), Response); ausf_client::curl_http_client(udmUri, Method, AuthInfo.dump(), Response);
Logger::ausf_server().error("response: %s", Response.c_str()); Logger::ausf_server().error("response: %s", Response.c_str());
......
...@@ -31,7 +31,6 @@ add_library (AUSF STATIC ...@@ -31,7 +31,6 @@ add_library (AUSF STATIC
ausf_app.cpp ausf_app.cpp
ausf_client.cpp ausf_client.cpp
ausf_config.cpp ausf_config.cpp
curl.cpp
) )
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
\brief \brief
\author Tien-Thinh NGUYEN \author Tien-Thinh NGUYEN
\company Eurecom \company Eurecom
\date 2020 \date 2021
\email: Tien-Thinh.Nguyen@eurecom.fr \email: Tien-Thinh.Nguyen@eurecom.fr
*/ */
...@@ -50,11 +50,11 @@ ausf_client* ausf_client_inst = nullptr; ...@@ -50,11 +50,11 @@ ausf_client* ausf_client_inst = nullptr;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ausf_app::ausf_app(const std::string& config_file) { ausf_app::ausf_app(const std::string& config_file) {
// logger::ausf_server().startup("Starting..."); Logger::ausf_app().startup("Starting...");
// logger::ausf_server().startup("Started"); Logger::ausf_app().startup("Started");
} }
ausf_app::~ausf_app() { ausf_app::~ausf_app() {
// logger::ausf_server().debug("Delete AUSF_APP instance..."); Logger::ausf_app().debug("Delete AUSF_APP instance...");
} }
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
\brief \brief
\author Tien-Thinh NGUYEN \author Tien-Thinh NGUYEN
\company Eurecom \company Eurecom
\date 2020 \date 2021
\email: Tien-Thinh.Nguyen@eurecom.fr \email: Tien-Thinh.Nguyen@eurecom.fr
*/ */
......
...@@ -41,9 +41,11 @@ ...@@ -41,9 +41,11 @@
using namespace Pistache::Http; using namespace Pistache::Http;
using namespace Pistache::Http::Mime; using namespace Pistache::Http::Mime;
using namespace oai::ausf::app; using namespace oai::ausf::app;
using namespace config;
using json = nlohmann::json; using json = nlohmann::json;
extern ausf_client* ausf_client_inst; extern ausf_client* ausf_client_inst;
extern ausf_config ausf_cfg;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// To read content of the response from NF // To read content of the response from NF
...@@ -59,5 +61,154 @@ ausf_client::ausf_client() {} ...@@ -59,5 +61,154 @@ ausf_client::ausf_client() {}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ausf_client::~ausf_client() { ausf_client::~ausf_client() {
Logger::ausf_server().debug("Delete AUSF Client instance..."); Logger::ausf_app().debug("Delete AUSF Client instance...");
}
void ausf_client::curl_http_client(
std::string remoteUri, std::string Method, std::string msgBody,
std::string& Response) {
Logger::ausf_server().info("Send HTTP message with body %s", msgBody.c_str());
uint32_t str_len = msgBody.length();
char* body_data = (char*) malloc(str_len + 1);
memset(body_data, 0, str_len + 1);
memcpy((void*) body_data, (void*) msgBody.c_str(), str_len);
curl_global_init(CURL_GLOBAL_ALL);
CURL* curl = curl_easy_init();
if (curl) {
CURLcode res = {};
struct curl_slist* headers = nullptr;
if (!Method.compare("POST") || !Method.compare("PUT") ||
!Method.compare("PATCH")) {
std::string content_type = "Content-Type: application/json";
headers = curl_slist_append(headers, content_type.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
}
curl_easy_setopt(curl, CURLOPT_URL, remoteUri.c_str());
if (!Method.compare("POST"))
curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1);
else if (!Method.compare("PUT"))
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
else if (!Method.compare("DELETE"))
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
else if (!Method.compare("PATCH"))
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
else
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, CURL_TIMEOUT_MS);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1);
curl_easy_setopt(curl, CURLOPT_INTERFACE, ausf_cfg.sbi.if_name.c_str());
Logger::ausf_server().info(
"[CURL] request sent by interface " + ausf_cfg.sbi.if_name);
// Response information.
long httpCode = {0};
std::unique_ptr<std::string> httpData(new std::string());
std::unique_ptr<std::string> httpHeaderData(new std::string());
// Hook up data handling function.
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get());
curl_easy_setopt(curl, CURLOPT_HEADERDATA, httpHeaderData.get());
if (!Method.compare("POST") || !Method.compare("PUT") ||
!Method.compare("PATCH")) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, msgBody.length());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body_data);
}
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
// get the response
std::string response = *httpData.get();
std::string json_data_response = "";
std::string resMsg = "";
bool is_response_ok = true;
Logger::ausf_server().info("Get response with httpcode (%d)", httpCode);
if (httpCode == 0) {
Logger::ausf_server().info(
"Cannot get response when calling %s", remoteUri.c_str());
// free curl before returning
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return;
}
nlohmann::json response_data = {};
if (httpCode != 200 && httpCode != 201 && httpCode != 204) {
is_response_ok = false;
if (response.size() < 1) {
Logger::ausf_server().info("There's no content in the response");
// TODO: send context response error
return;
}
Logger::ausf_server().info("Wrong response code");
return;
}
else { // httpCode = 200 || httpCode = 201 || httpCode = 204
/*
//store location of the created context
std::string header_response = *httpHeaderData.get();
std::string CRLF = "\r\n";
std::size_t location_pos = header_response.find("Location");
if (location_pos != std::string::npos)
{
std::size_t crlf_pos = header_response.find(CRLF, location_pos);
if (crlf_pos != std::string::npos)
{
std::string location = header_response.substr(location_pos + 10,
crlf_pos - (location_pos + 10)); printf("Location of the created SMF
context: %s", location.c_str());
}
}
try
{
response_data = nlohmann::json::parse(response);
}
catch (nlohmann::json::exception &e)
{
printf("Could not get Json content from the response");
//Set the default Cause
response_data["error"]["cause"] = "504 Gateway Timeout";
}*/
Response = *httpData.get();
}
if (!is_response_ok) {
try {
response_data = nlohmann::json::parse(json_data_response);
} catch (nlohmann::json::exception& e) {
Logger::ausf_server().info(
"Could not get Json content from the response");
// Set the default Cause
response_data["error"]["cause"] = "504 Gateway Timeout";
}
Logger::ausf_server().info(
"Get response with jsonData: %s", json_data_response.c_str());
std::string cause = response_data["error"]["cause"];
Logger::ausf_server().info("Call Network Function services failure");
Logger::ausf_server().info("Cause value: %s", cause.c_str());
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
if (body_data) {
free(body_data);
body_data = NULL;
}
fflush(stdout);
} }
...@@ -34,6 +34,9 @@ ...@@ -34,6 +34,9 @@
#include <curl/curl.h> #include <curl/curl.h>
#include "logger.hpp"
#include "ausf_config.hpp"
namespace oai { namespace oai {
namespace ausf { namespace ausf {
namespace app { namespace app {
...@@ -45,6 +48,10 @@ class ausf_client { ...@@ -45,6 +48,10 @@ class ausf_client {
virtual ~ausf_client(); virtual ~ausf_client();
ausf_client(ausf_client const&) = delete; ausf_client(ausf_client const&) = delete;
static void curl_http_client(
std::string remoteUri, std::string Method, std::string msgBody,
std::string& Response);
}; };
} // namespace app } // namespace app
} // namespace ausf } // namespace ausf
......
...@@ -46,7 +46,6 @@ extern "C" { ...@@ -46,7 +46,6 @@ extern "C" {
} }
using namespace libconfig; using namespace libconfig;
// using namespace amf_application;
namespace config { namespace config {
......
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
*file except in compliance with the License. You may obtain a copy of the
*License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file curl.cpp
\brief
\author Fengjiao He, BUPT
\date 2021
\email: contact@openairinterface.org
*/
#include "curl.hpp"
using namespace config;
extern ausf_config ausf_cfg;
std::size_t callback(
const char* in, std::size_t size, std::size_t num, std::string* out) {
const std::size_t totalBytes(size * num);
out->append(in, totalBytes);
return totalBytes;
}
void Curl::curl_http_client(
std::string remoteUri, std::string Method, std::string msgBody,
std::string& Response) {
Logger::ausf_server().info("Send HTTP message with body %s", msgBody.c_str());
uint32_t str_len = msgBody.length();
char* body_data = (char*) malloc(str_len + 1);
memset(body_data, 0, str_len + 1);
memcpy((void*) body_data, (void*) msgBody.c_str(), str_len);
curl_global_init(CURL_GLOBAL_ALL);
CURL* curl = curl_easy_init();
if (curl) {
CURLcode res = {};
struct curl_slist* headers = nullptr;
if (!Method.compare("POST") || !Method.compare("PUT") ||
!Method.compare("PATCH")) {
std::string content_type = "Content-Type: application/json";
headers = curl_slist_append(headers, content_type.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
}
curl_easy_setopt(curl, CURLOPT_URL, remoteUri.c_str());
if (!Method.compare("POST"))
curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1);
else if (!Method.compare("PUT"))
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
else if (!Method.compare("DELETE"))
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
else if (!Method.compare("PATCH"))
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
else
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, CURL_TIMEOUT_MS);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1);
curl_easy_setopt(curl, CURLOPT_INTERFACE, ausf_cfg.sbi.if_name.c_str());
Logger::ausf_server().info(
"[CURL] request sent by interface " + ausf_cfg.sbi.if_name);
// Response information.
long httpCode = {0};
std::unique_ptr<std::string> httpData(new std::string());
std::unique_ptr<std::string> httpHeaderData(new std::string());
// Hook up data handling function.
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get());
curl_easy_setopt(curl, CURLOPT_HEADERDATA, httpHeaderData.get());
if (!Method.compare("POST") || !Method.compare("PUT") ||
!Method.compare("PATCH")) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, msgBody.length());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body_data);
}
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
// get the response
std::string response = *httpData.get();
std::string json_data_response = "";
std::string resMsg = "";
bool is_response_ok = true;
Logger::ausf_server().info("Get response with httpcode (%d)", httpCode);
if (httpCode == 0) {
Logger::ausf_server().info(
"Cannot get response when calling %s", remoteUri.c_str());
// free curl before returning
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return;
}
nlohmann::json response_data = {};
if (httpCode != 200 && httpCode != 201 && httpCode != 204) {
is_response_ok = false;
if (response.size() < 1) {
Logger::ausf_server().info("There's no content in the response");
// TODO: send context response error
return;
}
Logger::ausf_server().info("Wrong response code");
return;
}
else { // httpCode = 200 || httpCode = 201 || httpCode = 204
/*
//store location of the created context
std::string header_response = *httpHeaderData.get();
std::string CRLF = "\r\n";
std::size_t location_pos = header_response.find("Location");
if (location_pos != std::string::npos)
{
std::size_t crlf_pos = header_response.find(CRLF, location_pos);
if (crlf_pos != std::string::npos)
{
std::string location = header_response.substr(location_pos + 10,
crlf_pos - (location_pos + 10)); printf("Location of the created SMF
context: %s", location.c_str());
}
}
try
{
response_data = nlohmann::json::parse(response);
}
catch (nlohmann::json::exception &e)
{
printf("Could not get Json content from the response");
//Set the default Cause
response_data["error"]["cause"] = "504 Gateway Timeout";
}*/
Response = *httpData.get();
}
if (!is_response_ok) {
try {
response_data = nlohmann::json::parse(json_data_response);
} catch (nlohmann::json::exception& e) {
Logger::ausf_server().info(
"Could not get Json content from the response");
// Set the default Cause
response_data["error"]["cause"] = "504 Gateway Timeout";
}
Logger::ausf_server().info(
"Get response with jsonData: %s", json_data_response.c_str());
std::string cause = response_data["error"]["cause"];
Logger::ausf_server().info("Call Network Function services failure");
Logger::ausf_server().info("Cause value: %s", cause.c_str());
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
if (body_data) {
free(body_data);
body_data = NULL;
}
fflush(stdout);
}
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
*file except in compliance with the License. You may obtain a copy of the
*License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file curl.hpp
\brief
\date 2020
\email: contact@openairinterface.org
*/
#ifndef _CURL_H_
#define _CURL_H_
// extern "C"{
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <string>
#include <unistd.h>
//}
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include "bstrlib.h"
#include "logger.hpp"
#include "ausf_config.hpp"
#define CURL_TIMEOUT_MS 100L
class Curl {
public:
/****** curl function ********/
static void curl_http_client(
std::string remoteUri, std::string Method, std::string msgBody,
std::string& Response);
private:
};
#endif
...@@ -70,6 +70,7 @@ static const std::vector<std::string> patch_op_type_e2str = { ...@@ -70,6 +70,7 @@ static const std::vector<std::string> patch_op_type_e2str = {
"ADD", "REMOVE", "REPLACE", "MOVE", "COPY", "TEST", "UNKNOWN"}; "ADD", "REMOVE", "REPLACE", "MOVE", "COPY", "TEST", "UNKNOWN"};
#define NF_CURL_TIMEOUT_MS 1000L #define NF_CURL_TIMEOUT_MS 1000L
#define CURL_TIMEOUT_MS 100L
#define MAX_WAIT_MSECS 20000 // 1 second #define MAX_WAIT_MSECS 20000 // 1 second
......
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