Poco::Net::NameValueCollection HTTPUtils::splitAndURLDecode(const std::string& encoded) { // TODO use code from Poco::Net::HTMLForm Poco::Net::NameValueCollection nvc; auto arguments = ofSplitString(encoded, "&", true); for (const auto& argument: arguments) { std::vector<std::string> tokens = ofSplitString(argument, "=", true); if (tokens.size() > 0) { std::string key; std::string value; Poco::URI::decode(tokens[0], key); if (tokens.size() > 1) { Poco::URI::decode(tokens[1], value); } nvc.add(key, value); } } return nvc; }
TestResult testWebSocketWithCookie() { HTTPResponse response; HTTPRequest request(HTTPRequest::HTTP_GET, "/lool/adminws/"); std::unique_ptr<HTTPClientSession> session(helpers::createSession(_uri)); // set cookie assert(_jwtCookie != ""); HTTPCookie cookie("jwt", _jwtCookie); Poco::Net::NameValueCollection nvc; nvc.add("jwt", _jwtCookie); request.setCookies(nvc); bool authorized = true; try { _adminWs = std::make_shared<Poco::Net::WebSocket>(*session, request, response); } catch (const Poco::Net::WebSocketException& exc) { Log::info() << "Admin websocket: Not authorized " << Log::end; authorized = false; } TestResult res = TestResult::TEST_FAILED; if (authorized) res = TestResult::TEST_OK; Log::info(std::string("testWebSocketWithCookie: ") + (res == TestResult::TEST_OK ? "OK" : "FAIL")); return res; }
// Converts the vector of HTTPCookie objects into a NameValueCollection Poco::Net::NameValueCollection RemoteJobManager::getCookies() { Poco::Net::NameValueCollection nvc; std::vector<Poco::Net::HTTPCookie>::const_iterator it = m_cookies.begin(); while (it != m_cookies.end()) { nvc.add((*it).getName(), (*it).getValue()); ++it; } return nvc; }
Poco::Net::NameValueCollection HTTPUtils::splitTextPlainPost(const std::string& textPlain) { Poco::Net::NameValueCollection nvc; std::string result; std::stringstream ss; ss << textPlain; while (std::getline(ss, result)) { std::size_t pos = result.find("="); if (pos != std::string::npos) { std::string key = result.substr(0, pos); std::string value = result.substr(pos + 1); nvc.add(key, value); } } return nvc; }