From 1617790e484b373d970b764cc99413dd7209b0f3 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Fri, 13 Apr 2018 00:09:31 +0200 Subject: [PATCH] [DEV] add querry element in the request --- enet/Http.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++- enet/Http.hpp | 10 +++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/enet/Http.cpp b/enet/Http.cpp index d24d826..920103f 100644 --- a/enet/Http.cpp +++ b/enet/Http.cpp @@ -10,6 +10,7 @@ #include #include #include +#include extern "C" { #include } @@ -471,7 +472,14 @@ void enet::Http::getHeader() { etk::from_string(valueType, listLineOne[0]); m_requestHeader.setType(valueType); // get URI: - m_requestHeader.setUri(listLineOne[1]); + etk::String basicUri = listLineOne[1]; + size_t pos = basicUri.find('?'); + if (pos == etk::String::npos) { + m_requestHeader.setUri(listLineOne[1]); + } else { + m_requestHeader.setUri(basicUri.extract(0, pos)); + m_requestHeader.setQuery(pourcentUriDecode(basicUri.extract(pos+1))); + } // Get http version: enum enet::HTTPProtocol valueProtocol; etk::from_string(valueProtocol, listLineOne[2]); @@ -649,6 +657,14 @@ etk::String enet::HttpHeader::getKey(const etk::String& _key) const { return ""; } +bool enet::HttpHeader::existKey(const etk::String& _key) const { + auto it = m_map.find(_key); + if (it != m_map.end()) { + return true; + } + return false; +} + etk::String enet::HttpHeader::generateKeys() const { etk::String out; for (auto &it : m_map) { @@ -660,6 +676,45 @@ etk::String enet::HttpHeader::generateKeys() const { return out; } +void enet::HttpHeader::setQuery(const etk::Map& _value) { + m_query = _value; +} +void enet::HttpHeader::setQueryKey(const etk::String& _key, const etk::String& _value) { + auto it = m_query.find(_key); + if (it == m_query.end()) { + m_query.add(_key, _value); + } else { + it->second = _value; + } +} + +void enet::HttpHeader::rmQueryKey(const etk::String& _key) { + auto it = m_query.find(_key); + if (it != m_query.end()) { + m_query.erase(it); + } +} + +etk::String enet::HttpHeader::getQueryKey(const etk::String& _key) const { + auto it = m_query.find(_key); + if (it != m_query.end()) { + return it->second; + } + return ""; +} + +bool enet::HttpHeader::existQueryKey(const etk::String& _key) const { + auto it = m_query.find(_key); + if (it != m_query.end()) { + return true; + } + return false; +} + +etk::String enet::HttpHeader::generateQueryKeys() const { + return enet::pourcentUriEncode(m_query); +} + enet::HttpHeader::HttpHeader(): m_protocol(enet::HTTPProtocol::http_1_0) { @@ -687,6 +742,13 @@ void enet::HttpAnswer::display() const { ENET_PRINT(" '" + it.first + "' = '" + it.second + "'"); } } + ENET_PRINT(" query:"); + for (auto &it : m_query) { + if ( it.first != "" + && it.second != "") { + ENET_PRINT(" '" + it.first + "' = '" + it.second + "'"); + } + } } etk::String enet::HttpAnswer::generate() const { @@ -739,6 +801,13 @@ void enet::HttpRequest::display() const { ENET_PRINT(" '" + it.first + "' = '" + it.second + "'"); } } + ENET_PRINT(" query:"); + for (auto &it : m_query) { + if ( it.first != "" + && it.second != "") { + ENET_PRINT(" '" + it.first + "' = '" + it.second + "'"); + } + } } etk::String enet::HttpRequest::generate() const { @@ -746,6 +815,10 @@ etk::String enet::HttpRequest::generate() const { out += etk::toString(m_req); out += " "; out += m_uri; + etk::String querryData = generateQueryKeys(); + if (querryData.empty() != 0) { + out += "?" + querryData; + } out += " "; out += etk::toString(m_protocol); out += "\r\n"; diff --git a/enet/Http.hpp b/enet/Http.hpp index 073cd4e..42c2b4a 100644 --- a/enet/Http.hpp +++ b/enet/Http.hpp @@ -116,13 +116,23 @@ namespace enet { protected: // key, val etk::Map m_map; + etk::Map m_query; enum HTTPProtocol m_protocol; public: void setKey(const etk::String& _key, const etk::String& _value); void rmKey(const etk::String& _key); etk::String getKey(const etk::String& _key) const; + bool existKey(const etk::String& _key) const; protected: etk::String generateKeys() const; + public: + void setQuery(const etk::Map& _value); + void setQueryKey(const etk::String& _key, const etk::String& _value); + void rmQueryKey(const etk::String& _key); + etk::String getQueryKey(const etk::String& _key) const; + bool existQueryKey(const etk::String& _key) const; + protected: + etk::String generateQueryKeys() const; public: enum HTTPProtocol getProtocol() const { return m_protocol;