Merge pull request #6207 from Icinga/fix/nscp-api-multiple-params

Fix multiple parameter problems for check_nscp_api
This commit is contained in:
Michael Friedrich 2018-04-09 10:07:04 +02:00 committed by GitHub
commit f2ce5b549c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 4 deletions

View File

@ -205,6 +205,11 @@ void Url::SetQuery(const std::map<String, std::vector<String> >& query)
m_Query = query;
}
void Url::SetArrayFormatUseBrackets(bool useBrackets)
{
m_ArrayFormatUseBrackets = useBrackets;
}
void Url::AddQueryElement(const String& name, const String& value)
{
auto it = m_Query.find(name);
@ -272,8 +277,11 @@ String Url::Format(bool onlyPathAndQuery, bool printCredentials) const
temp += "&";
temp += key;
if (m_ArrayFormatUseBrackets) {
if (kv.second.size() > 1)
temp += "[]";
}
if (!s.IsEmpty())
temp += "=" + Utility::EscapeString(s, ACQUERY_ENCODE, false);

View File

@ -65,6 +65,7 @@ public:
void SetPort(const String& port);
void SetPath(const std::vector<String>& path);
void SetQuery(const std::map<String, std::vector<String> >& query);
void SetArrayFormatUseBrackets(bool useBrackets = true);
void AddQueryElement(const String& name, const String& query);
void SetQueryElements(const String& name, const std::vector<String>& query);
@ -78,6 +79,7 @@ private:
String m_Port;
std::vector<String> m_Path;
std::map<String, std::vector<String> > m_Query;
bool m_ArrayFormatUseBrackets;
String m_Fragment;
bool ParseScheme(const String& scheme);

View File

@ -17,7 +17,7 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#define VERSION "1.0.0"
#define VERSION "1.0.1"
#include "remote/httpclientconnection.hpp"
#include "remote/httprequest.hpp"
@ -88,9 +88,13 @@ static Dictionary::Ptr QueryEndpoint(const String& host, const String& port, con
// Url() will call Utillity::UnescapeString() which will thrown an exception if it finds a lonely %
req->RequestUrl = new Url(endpoint);
// NSClient++ uses `time=1m&time=5m` instead of `time[]=1m&time[]=5m`
req->RequestUrl->SetArrayFormatUseBrackets(false);
req->AddHeader("password", password);
if (l_Debug)
std::cout << "Sending request to 'https://" << host << ":" << port << req->RequestUrl->Format() << "'\n";
std::cout << "Sending request to 'https://" << host << ":" << port << req->RequestUrl->Format(false, false) << "'\n";
// Submits the request. The 'ResultHttpCompletionCallback' is called once the HttpRequest receives an answer,
// which then sets 'ready' to true

View File

@ -95,6 +95,14 @@ BOOST_AUTO_TEST_CASE(format)
url = new Url("/");
BOOST_CHECK(url->Format(false, false) == "/");
url = new Url("https://nsclient:8443/query/check_cpu?time%5B%5D=1m&time=5m&time%5B%5D=15m");
url->SetArrayFormatUseBrackets(false);
BOOST_CHECK(url2 = new Url(url->Format(false, false)));
url = new Url("https://icinga2/query?a[]=1&a[]=2&a[]=3");
url->SetArrayFormatUseBrackets(true);
BOOST_CHECK(url2 = new Url(url->Format(false, false)));
}
BOOST_AUTO_TEST_CASE(illegal_legal_strings)