From 488658aecf730231e265d7d6b6887d00b74f9dec Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 6 May 2014 21:11:04 +0200 Subject: [PATCH] [DEV] first post methode availlable --- enet/Http.cpp | 48 +++++++++++++++++++++++++++++++++++++++--------- enet/Http.h | 13 +++++++++++++ test/index.php | 27 +++++++++++++++++++++++++++ test/main.cpp | 30 +++++++++++++++++++++++++----- 4 files changed, 104 insertions(+), 14 deletions(-) create mode 100755 test/index.php diff --git a/enet/Http.cpp b/enet/Http.cpp index a44b11d..b1a2298 100644 --- a/enet/Http.cpp +++ b/enet/Http.cpp @@ -21,7 +21,8 @@ static std::map getErrorList(void) { return g_list; } -enet::Http::Http(void) { +enet::Http::Http(void) : + m_keepAlive(false) { m_connection.setPort(80); m_connection.setServer(false); } @@ -42,7 +43,12 @@ bool enet::Http::connect(void) { } void enet::Http::setSendHeaderProperties(const std::string& _key, const std::string& _val) { - m_sendHeader.insert(make_pair(_key, _val)); + auto it = m_sendHeader.find(_key); + if (it == m_sendHeader.end()) { + m_sendHeader.insert(make_pair(_key, _val)); + } else { + it->second = _val; + } } std::string enet::Http::getSendHeaderProperties(const std::string& _key) { @@ -63,6 +69,10 @@ bool enet::Http::reset(void) { m_sendHeader.clear(); m_receiveHeader.clear(); setSendHeaderProperties("User-Agent", "e-net (ewol network interface)"); + if (m_keepAlive == true) { + setSendHeaderProperties("Connection", "Keep-Alive"); + } + return true; } bool enet::Http::setServer(const std::string& _hostName) { @@ -89,7 +99,7 @@ bool enet::Http::receiveData(void) { std::string header; // Get data char data[1025]; - len = 1; + int32_t len = 1; bool headerEnded = false; while ( m_connection.getConnectionStatus() == enet::Tcp::statusLink && len > 0) { @@ -135,21 +145,31 @@ bool enet::Http::receiveData(void) { // parse header : std::vector list = std::split(header, '\n'); headerEnded = false; + m_receiveHeader.clear(); for (auto element : list) { if (headerEnded == false) { header = element; headerEnded = true; } else { - //ENET_INFO("header : '" << element << "'"); size_t found = element.find(":"); if (found == std::string::npos) { // nothing continue; } - //ENET_INFO("header : key='" << std::string(element, 0, found) << "' value='" << std::string(element, found+2) << "'"); + ENET_VERBOSE("header : key='" << std::string(element, 0, found) << "' value='" << std::string(element, found+2) << "'"); m_receiveHeader.insert(make_pair(unEscapeChar(std::string(element, 0, found)), unEscapeChar(std::string(element, found+2)))); } } + for (auto &it : m_receiveHeader) { + if (it.first == "Connection") { + if (it.second == "close") { + ENET_DEBUG("connection closed by remote :"); + m_connection.unlink(); + } else { + ENET_TODO("manage connection type : '" << it.second); + } + } + } /* ENET_INFO("header : '" << header << "'"); for (auto &it : m_receiveHeader) { @@ -204,7 +224,7 @@ bool enet::Http::get(const std::string& _address) { req += _address; } req += " HTTP/1.0\n"; - setSendHeaderProperties("Lenght", "0"); + setSendHeaderProperties("Content-Length", "0"); // add header properties : for (auto &it : m_sendHeader) { req += escapeChar(it.first) + ": " + escapeChar(it.second) + "\n"; @@ -243,21 +263,30 @@ bool enet::Http::post(const std::string& _address, const std::map m_sendHeader; @@ -36,6 +45,7 @@ namespace enet { bool setPort(uint16_t _port); bool get(const std::string& _address); bool post(const std::string& _address, const std::map& _values); + bool post(const std::string& _address, const std::string& _contentType, const std::string& _data); int32_t dataSize(void) { return m_receiveData.size(); @@ -44,6 +54,9 @@ namespace enet { return m_receiveData; } std::string dataString(void); + std::string escapeChar(const std::string& _value); + std::string unEscapeChar(const std::string& _value); + bool receiveData(void); }; }; diff --git a/test/index.php b/test/index.php new file mode 100755 index 0000000..2459e0f --- /dev/null +++ b/test/index.php @@ -0,0 +1,27 @@ +\n"; +//echo $_POST; +if ( isset($_POST) == true + && empty($_POST) == false) { + foreach ($_POST as $key => $value) { + echo "key='".$key."' val='".$value."'
\n"; + } +} +if ( isset($HTTP_RAW_POST_DATA) == true + && empty($HTTP_RAW_POST_DATA) == false) { + echo "raw data :'".$HTTP_RAW_POST_DATA."'
\n"; +} + +if ( isset($_SERVER) + && isset($_SERVER["CONTENT_TYPE"]) ) { + echo "content Type : '".$_SERVER["CONTENT_TYPE"]."'
\n"; +} + +?> \ No newline at end of file diff --git a/test/main.cpp b/test/main.cpp index a184eb5..8458501 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -15,19 +15,39 @@ #define __class__ "test" int main(int argc, const char *argv[]) { - etk::log::setLevel(etk::log::logLevelVerbose); + etk::log::setLevel(etk::log::logLevelDebug); APPL_VERBOSE("plop"); if (argc > 2) { // client mode ... enet::Http connection; - connection.setServer("example.com"); - - APPL_INFO("Get data : "); + connection.setServer("127.0.0.1"); + connection.setKeepAlive(true); + APPL_INFO("----------------------------"); + APPL_INFO("GET data : "); if (connection.get("") == false) { - APPL_ERROR("can not Get data..."); + APPL_ERROR("can not GET data..."); return -1; } APPL_INFO("data : " << connection.dataString()); + + APPL_INFO("----------------------------"); + APPL_INFO("POST data : "); + std::map values; + values.insert(std::make_pair("plop", "valuePlop")); + if (connection.post("", values) == false) { + APPL_ERROR("can not POST data..."); + return -1; + } + APPL_INFO("data : " << connection.dataString()); + + APPL_INFO("----------------------------"); + APPL_INFO("POST xml : "); + if (connection.post("", /*"application/xml"*/ "text/xml; charset=utf-8", "value1") == false) { + APPL_ERROR("can not POST XML data..."); + return -1; + } + APPL_INFO("data : " << connection.dataString()); + } else if (argc > 1) { // client mode ... enet::Tcp connection;