std::string WebSessionManager::getId(const std::string& appName, const Poco::Net::HTTPServerRequest& request) { std::string id; std::string name(cookieName(appName)); NameValueCollection cookies; request.getCookies(cookies); NameValueCollection::ConstIterator it = cookies.find(name); if (it != cookies.end()) id = it->second; return id; }
//------------------------------------------------------------------------------ bool ofxWebServerBaseRouteHandler::isValidRequest(const Settings& settings, HTTPServerRequest& request, HTTPServerResponse& response) { string sessionId = ""; // extract cookie from request NameValueCollection cookies; request.getCookies(cookies); NameValueCollection::ConstIterator it = cookies.find(settings.sessionCookieName); if (it != cookies.end()) { sessionId = it->second; } else { sessionId = ofxWebServerSessionManager::generateSessionKey(request); HTTPCookie cookie(settings.sessionCookieName,sessionId); cookie.setPath("/"); // set no age, so it expires @ end of session response.addCookie(cookie); } // TODO: update session manager URI uri(request.getURI()); const string path = uri.getPath(); // just get the path if(settings.requireAuthentication) { if(request.hasCredentials()) { HTTPBasicCredentials credentials(request); const string& user = credentials.getUsername(); const string& pwd = credentials.getPassword(); if(settings.username == credentials.getUsername() && settings.password == credentials.getPassword()) { // add an authentication cookie? return true; } else { response.setStatusAndReason(HTTPResponse::HTTP_UNAUTHORIZED); sendErrorResponse(response); return false; } } else { response.requireAuthentication(settings.realm); response.setContentLength(0); response.send(); return false; } } else { return true; } }
void NameValueCollectionTest::testNameValueCollection() { NameValueCollection nvc; assert (nvc.empty()); assert (nvc.size() == 0); nvc.set("name", "value"); assert (!nvc.empty()); assert (nvc["name"] == "value"); assert (nvc["Name"] == "value"); nvc.set("name2", "value2"); assert (nvc.get("name2") == "value2"); assert (nvc.get("NAME2") == "value2"); assert (nvc.size() == 2); try { std::string value = nvc.get("name3"); fail("not found - must throw"); } catch (NotFoundException&) { } try { std::string value = nvc["name3"]; fail("not found - must throw"); } catch (NotFoundException&) { } assert (nvc.get("name", "default") == "value"); assert (nvc.get("name3", "default") == "default"); assert (nvc.has("name")); assert (nvc.has("name2")); assert (!nvc.has("name3")); nvc.add("name3", "value3"); assert (nvc.get("name3") == "value3"); nvc.add("name3", "value31"); NameValueCollection::ConstIterator it = nvc.find("Name3"); assert (it != nvc.end()); std::string v1 = it->second; assert (it->first == "name3"); ++it; assert (it != nvc.end()); std::string v2 = it->second; assert (it->first == "name3"); assert ((v1 == "value3" && v2 == "value31") || (v1 == "value31" && v2 == "value3")); nvc.erase("name3"); assert (!nvc.has("name3")); assert (nvc.find("name3") == nvc.end()); it = nvc.begin(); assert (it != nvc.end()); ++it; assert (it != nvc.end()); ++it; assert (it == nvc.end()); nvc.clear(); assert (nvc.empty()); assert (nvc.size() == 0); }