[DEV] first post methode availlable

This commit is contained in:
Edouard DUPIN 2014-05-06 21:11:04 +02:00
parent c936af3c64
commit 488658aecf
4 changed files with 104 additions and 14 deletions

View File

@ -21,7 +21,8 @@ static std::map<int32_t, std::string> 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<std::string> 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<std::string, s
}
body += escapeChar(it.first) + "=" + escapeChar(it.second);
}
return post(_address, "application/x-www-form-urlencoded", body);
}
bool enet::Http::post(const std::string& _address, const std::string& _contentType, const std::string& _data) {
m_receiveData.clear();
m_receiveHeader.clear();
if (connect() == false) {
return false;
}
std::string req = "POST http://" + m_connection.getHostName();
if (_address != "") {
req += "/";
req += _address;
}
req += " HTTP/1.0\n";
setSendHeaderProperties("Lenght", std::to_string(body.size()));
setSendHeaderProperties("Content-Type", _contentType);
setSendHeaderProperties("Content-Length", std::to_string(_data.size()));
// add header properties :
for (auto &it : m_sendHeader) {
req += escapeChar(it.first) + ": " + escapeChar(it.second) + "\n";
}
// end of header
req += "\n";
req += body;
req += _data;
int32_t len = m_connection.write(req, false);
ENET_VERBOSE("read write=" << len << " data: " << req);
@ -268,6 +297,7 @@ bool enet::Http::post(const std::string& _address, const std::map<std::string, s
return receiveData();
}
std::string enet::Http::dataString(void) {
std::string data;
for (auto element : m_receiveData) {

View File

@ -21,6 +21,15 @@ namespace enet {
virtual ~Http(void);
private:
enet::Tcp m_connection;
private:
bool m_keepAlive;
public:
void setKeepAlive(bool _keepAlive) {
m_keepAlive = true;
}
bool getKeepAlive(void) {
return m_keepAlive;
}
private:
// key, val
std::map<std::string, std::string> 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<std::string, std::string>& _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);
};
};

27
test/index.php Executable file
View File

@ -0,0 +1,27 @@
<?php
// force display of error
error_reporting(E_ALL);
ini_set('display_errors', '1');
date_default_timezone_set('Europe/Paris');
echo "plop<br/>\n";
//echo $_POST;
if ( isset($_POST) == true
&& empty($_POST) == false) {
foreach ($_POST as $key => $value) {
echo "key='".$key."' val='".$value."'<br>\n";
}
}
if ( isset($HTTP_RAW_POST_DATA) == true
&& empty($HTTP_RAW_POST_DATA) == false) {
echo "raw data :'".$HTTP_RAW_POST_DATA."'<br>\n";
}
if ( isset($_SERVER)
&& isset($_SERVER["CONTENT_TYPE"]) ) {
echo "content Type : '".$_SERVER["CONTENT_TYPE"]."'<br>\n";
}
?>

View File

@ -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<std::string, std::string> values;
values.insert(std::make_pair<std::string, std::string>("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", "<plop><string>value1</string></plop>") == false) {
APPL_ERROR("can not POST XML data...");
return -1;
}
APPL_INFO("data : " << connection.dataString());
} else if (argc > 1) {
// client mode ...
enet::Tcp connection;