From b1c6065ffc29522776805afbfb08cb132786fdb5 Mon Sep 17 00:00:00 2001 From: aleks-f Date: Tue, 19 Mar 2013 22:51:04 -0500 Subject: [PATCH] GH #75: Poco::Uri addQueryParameter method - added GH #75: Poco::Uri addQueryParameter method --- CHANGELOG | 1 + Foundation/include/Poco/URI.h | 7 +++++++ Foundation/src/URI.cpp | 12 ++++++++++++ Foundation/testsuite/src/URITest.cpp | 7 +++++++ 4 files changed, 27 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 71ef3b3d2..8aca613bd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -34,6 +34,7 @@ Release 1.5.2 (2013-03-??) - fixed GH #130: prefer sysconf over sysctlbyname - fixed GH #131: no timezone global var on OpenBSD - fixed GH #102: Some subprojects don't have x64 solutions for VS 2010 +- added GH #75: Poco::Uri addQueryParameter method Release 1.5.1 (2013-01-11) ========================== diff --git a/Foundation/include/Poco/URI.h b/Foundation/include/Poco/URI.h index 4aa36fde8..9a7a667e3 100644 --- a/Foundation/include/Poco/URI.h +++ b/Foundation/include/Poco/URI.h @@ -175,6 +175,13 @@ public: void setQuery(const std::string& query); /// Sets the query part of the URI. + void addQueryParameter(const std::string& param, const std::string& val = ""); + /// Adds "param=val" to the query; "param" may not be empty. + /// If val is empty, only '=' is appended to the parameter. + /// + /// In addition to regular encoding, function also encodes '&' and '=', + /// if found in param or val. + const std::string& getRawQuery() const; /// Returns the unencoded query part of the URI. diff --git a/Foundation/src/URI.cpp b/Foundation/src/URI.cpp index d0626f03a..23a0de4cc 100644 --- a/Foundation/src/URI.cpp +++ b/Foundation/src/URI.cpp @@ -340,6 +340,18 @@ void URI::setQuery(const std::string& query) } +void URI::addQueryParameter(const std::string& param, const std::string& val) +{ + std::string reserved(RESERVED_QUERY); + reserved += "=&"; + if (_query.empty()) _query.append(1, '?'); + else _query.append(1, '&'); + encode(param, reserved, _query); + _query.append(1, '='); + encode(val, reserved, _query); +} + + std::string URI::getQuery() const { std::string query; diff --git a/Foundation/testsuite/src/URITest.cpp b/Foundation/testsuite/src/URITest.cpp index ca6433754..226678586 100644 --- a/Foundation/testsuite/src/URITest.cpp +++ b/Foundation/testsuite/src/URITest.cpp @@ -764,6 +764,13 @@ void URITest::testOther() assert(uri.getRawQuery() == "q=pony%7eride"); assert(uri.toString() == "http://google.com/search?q=pony%7eride#frag%20ment"); assert(uri.getPathEtc() == "/search?q=pony%7eride#frag%20ment"); + + uri.addQueryParameter("pa=ra&m1"); + assert(uri.getRawQuery() == "q=pony%7eride&pa%3Dra%26m1="); + assert(uri.getQuery() == "q=pony~ride&pa=ra&m1="); + uri.addQueryParameter("pa=ra&m2", "val&ue"); + assert(uri.getRawQuery() == "q=pony%7eride&pa%3Dra%26m1=&pa%3Dra%26m2=val%26ue"); + assert(uri.getQuery() == "q=pony~ride&pa=ra&m1=&pa=ra&m2=val&ue"); }