Unverified Commit aae8130e authored by kharade's avatar kharade

query string parsing added

parent da873b4a
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/thread.hpp> #include <boost/thread.hpp>
#include <boost/thread/future.hpp> #include <boost/thread/future.hpp>
#include <regex>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <string> #include <string>
...@@ -69,10 +70,28 @@ void nrf_http2_server::start() { ...@@ -69,10 +70,28 @@ void nrf_http2_server::start() {
if (request.method().compare("GET") == 0) { if (request.method().compare("GET") == 0) {
nlohmann::json::parse(msg.c_str()).get_to(nFProfile); nlohmann::json::parse(msg.c_str()).get_to(nFProfile);
std::vector<std::string> split_result; std::vector<std::string> split_result;
std::vector<std::string> split_query;
boost::split( boost::split(
split_result, request.uri().path, boost::is_any_of("/")); split_result, request.uri().path, boost::is_any_of("/"));
nfInstanceID = split_result[split_result.size() - 1].c_str(); boost::split(
this->get_nf_instance_handler(nfInstanceID, response); split_query, request.uri().path, boost::is_any_of("?"));
if (split_result.size() == 5) {
// Read the profile of given NF Instance
nfInstanceID = split_result[split_result.size() - 1].c_str();
this->get_nf_instance_handler(nfInstanceID, response);
}
if (split_result.size() == 4) {
// Retrieves a collection of NF Instances
std::string querystring =
split_query[split_query.size() - 1].c_str();
std::string nfType =
util::get_query_param(querystring, "nf-type");
uint32_t limit =
atoi(util::get_query_param(querystring, "limit").c_str());
this->get_nf_instances_handler(nfType, limit, response);
}
} }
if (request.method().compare("PATCH") == 0 && len > 0) { if (request.method().compare("PATCH") == 0 && len > 0) {
std::vector<PatchItem> patchItem; std::vector<PatchItem> patchItem;
...@@ -218,21 +237,15 @@ void nrf_http2_server::get_nf_instance_handler( ...@@ -218,21 +237,15 @@ void nrf_http2_server::get_nf_instance_handler(
} }
void nrf_http2_server::get_nf_instances_handler( void nrf_http2_server::get_nf_instances_handler(
const std::string& nfInstanceID, const response& response) { const std::string& nfType, const uint32_t& limit,
const response& response) {
Logger::nrf_sbi().info( Logger::nrf_sbi().info(
"Got a request to retrieve a collection of NF Instances"); "Got a request to retrieve a collection of NF Instances");
std::string nf_type = {}; if (!nfType.empty())
// if (!nfType.isEmpty()) { Logger::nrf_sbi().debug("\tNF type: %s", nfType.c_str());
// nf_type = nfType.get();
// Logger::nrf_sbi().debug("\tNF type: %s", nf_type.c_str());
// }
uint32_t limit_item = 0; if (limit > 0) Logger::nrf_sbi().debug("\tLimit number of items: %d", limit);
// if (!limit.isEmpty()) {
// limit_item = limit.get();
// Logger::nrf_sbi().debug("\tLimit number of items: %d", limit_item);
// }
int http_code = 0; int http_code = 0;
ProblemDetails problem_details = {}; ProblemDetails problem_details = {};
...@@ -242,7 +255,7 @@ void nrf_http2_server::get_nf_instances_handler( ...@@ -242,7 +255,7 @@ void nrf_http2_server::get_nf_instances_handler(
std::vector<std::string> uris = {}; std::vector<std::string> uris = {};
m_nrf_app->handle_get_nf_instances( m_nrf_app->handle_get_nf_instances(
nf_type, uris, limit_item, http_code, 2, problem_details); nfType, uris, limit, http_code, 2, problem_details);
if (http_code != HTTP_STATUS_CODE_200_OK) { if (http_code != HTTP_STATUS_CODE_200_OK) {
to_json(json_data, problem_details); to_json(json_data, problem_details);
...@@ -255,7 +268,7 @@ void nrf_http2_server::get_nf_instances_handler( ...@@ -255,7 +268,7 @@ void nrf_http2_server::get_nf_instances_handler(
h.emplace( h.emplace(
"location", header_value{ "location", header_value{
m_address + NNRF_NFM_BASE + nrf_cfg.sbi_api_version + m_address + NNRF_NFM_BASE + nrf_cfg.sbi_api_version +
"/nf-instances/" + nfInstanceID}); "/nf-instances/"});
h.emplace("content-type", header_value{content_type}); h.emplace("content-type", header_value{content_type});
response.write_head(http_code, h); response.write_head(http_code, h);
response.end(); response.end();
...@@ -422,7 +435,7 @@ void nrf_http2_server::search_nf_instances_handler( ...@@ -422,7 +435,7 @@ void nrf_http2_server::search_nf_instances_handler(
Logger::nrf_sbi().info( Logger::nrf_sbi().info(
"Got a request to discover the set of NF instances that satisfies a " "Got a request to discover the set of NF instances that satisfies a "
"number of input query parameters"); "number of input query parameters");
// ToDo
std::string target_nf_type = {}; std::string target_nf_type = {};
// if (!targetNfType.isEmpty()) { // if (!targetNfType.isEmpty()) {
// target_nf_type = targetNfType.get(); // target_nf_type = targetNfType.get();
......
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
#define FILE_NRF_HTTP2_SERVER_SEEN #define FILE_NRF_HTTP2_SERVER_SEEN
#include "conversions.hpp" #include "conversions.hpp"
#include "string.hpp"
//#include "nrf.h" //#include "nrf.h"
#include "nrf_app.hpp" #include "nrf_app.hpp"
#include "uint_generator.hpp" #include "uint_generator.hpp"
...@@ -54,7 +56,8 @@ class nrf_http2_server { ...@@ -54,7 +56,8 @@ class nrf_http2_server {
void get_nf_instance_handler( void get_nf_instance_handler(
const std::string& nfInstanceID, const response& response); const std::string& nfInstanceID, const response& response);
void get_nf_instances_handler( void get_nf_instances_handler(
const std::string& nfInstanceID, const response& response); const std::string& nfType, const uint32_t& limit,
const response& response);
void update_instance_handler( void update_instance_handler(
const std::string& nfInstanceID, const std::vector<PatchItem>& patchItem, const std::string& nfInstanceID, const std::vector<PatchItem>& patchItem,
const response& response); const response& response);
...@@ -67,6 +70,7 @@ class nrf_http2_server { ...@@ -67,6 +70,7 @@ class nrf_http2_server {
const std::string& subscriptionID, const response& response); const std::string& subscriptionID, const response& response);
void search_nf_instances_handler( void search_nf_instances_handler(
const SubscriptionData& subscriptionData, const response& response); const SubscriptionData& subscriptionData, const response& response);
void access_token_request_handler( void access_token_request_handler(
const SubscriptionData& subscriptionData, const response& response); const SubscriptionData& subscriptionData, const response& response);
void stop(); void stop();
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <cctype> #include <cctype>
#include <locale> #include <locale>
#include <stdarg.h> #include <stdarg.h>
#include <regex>
template<class T> template<class T>
class Buffer { class Buffer {
...@@ -86,3 +87,16 @@ std::string& util::rtrim(std::string& s) { ...@@ -86,3 +87,16 @@ std::string& util::rtrim(std::string& s) {
std::string& util::trim(std::string& s) { std::string& util::trim(std::string& s) {
return util::ltrim(util::rtrim(s)); return util::ltrim(util::rtrim(s));
} }
// extract query param from given querystring
std::string& util::get_query_param(
std::string& querystring, std::string param) {
std::regex reList("([^=]*)=([^&]*)&?");
std::string tmp = param;
std::for_each(
std::sregex_iterator(querystring.begin(), querystring.end(), reList),
std::sregex_iterator(), [param](std::smatch const& match) {
std::string qs_match_1 = match[1].str();
if (qs_match_1 == param) return match[2].str();
});
}
\ No newline at end of file
...@@ -39,5 +39,7 @@ std::string& ltrim(std::string& s); ...@@ -39,5 +39,7 @@ std::string& ltrim(std::string& s);
std::string& rtrim(std::string& s); std::string& rtrim(std::string& s);
// trim from both ends // trim from both ends
std::string& trim(std::string& s); std::string& trim(std::string& s);
// extract query param from given querystring
std::string& get_query_param(std::string& querystring, std::string param);
} // namespace util } // namespace util
#endif #endif
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