[DEV] add pourcentage enoding for URI in HTTP
This commit is contained in:
parent
212731101e
commit
273982e7d3
100
enet/pourcentEncoding.cpp
Normal file
100
enet/pourcentEncoding.cpp
Normal file
@ -0,0 +1,100 @@
|
||||
/** @file
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2014, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#include <enet/pourcentEncoding.hpp>
|
||||
#include <enet/debug.hpp>
|
||||
#include <etk/types.hpp>
|
||||
#include <etk/String.hpp>
|
||||
|
||||
const etk::String hexData = "0123456789ABCDEF";
|
||||
|
||||
etk::String enet::pourcentEncode(const etk::String& _data) {
|
||||
etk::String out;
|
||||
for (auto &it : _data) {
|
||||
if ( (it >= 'a' && it <= 'z')
|
||||
|| (it >= 'A' && it <= 'Z')
|
||||
|| (it >= '0' && it <= '9')
|
||||
|| it == '-'
|
||||
|| it == '_'
|
||||
|| it == '.'
|
||||
|| it == '~') {
|
||||
out += it;
|
||||
} else {
|
||||
out += "%";
|
||||
out += hexData[(uint32_t(it)>>4)&0x0F];
|
||||
out += hexData[uint32_t(it)&0x0F];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static int32_t convertStringHexToInt(const char _value) {
|
||||
if (_value >= 'a' && _value <= 'z') {
|
||||
return int32_t(_value) - int32_t('a') + 10;
|
||||
}
|
||||
if (_value >= 'A' && _value <= 'Z') {
|
||||
return int32_t(_value) - int32_t('A') + 10;
|
||||
}
|
||||
if (_value >= '0' && _value <= '9') {
|
||||
return int32_t(_value) - int32_t('0');
|
||||
}
|
||||
ENET_ERROR("Not a hexadecimal Value: '" << _value << "'");
|
||||
return 0;
|
||||
}
|
||||
|
||||
etk::String enet::pourcentDecode(const etk::String& _data) {
|
||||
etk::String out;
|
||||
for (size_t iii=0; iii<_data.size(); ++iii) {
|
||||
auto it = _data[iii];
|
||||
if (it == '%') {
|
||||
if (iii+2 < _data.size()) {
|
||||
auto val1 = convertStringHexToInt(_data[iii+1])<<4;
|
||||
val1 += convertStringHexToInt(_data[iii+2]);
|
||||
out += char(val1);
|
||||
iii += 2;
|
||||
} else {
|
||||
ENET_ERROR("can not convert pourcent ==> input size error: '" << _data << "'");
|
||||
return out;
|
||||
}
|
||||
} else {
|
||||
out += it;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
etk::String enet::pourcentUriEncode(const etk::Map<etk::String, etk::String>& _data) {
|
||||
etk::String out;
|
||||
for (auto &it: _data) {
|
||||
if (out.empty() == false) {
|
||||
out += "&";
|
||||
}
|
||||
out += enet::pourcentEncode(it.first);
|
||||
if (it.second.empty() == false) {
|
||||
out += "=";
|
||||
out += enet::pourcentEncode(it.second);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
etk::Map<etk::String, etk::String> enet::pourcentUriDecode(const etk::String& _data) {
|
||||
etk::Map<etk::String, etk::String> out;
|
||||
auto listElements = etk::split(_data, '&');
|
||||
for (auto &it : listElements) {
|
||||
if (it.size() == 0) {
|
||||
continue;
|
||||
}
|
||||
auto offset = it.find('=');
|
||||
if (offset == etk::String::npos) {
|
||||
out.set(enet::pourcentDecode(it), "");
|
||||
continue;
|
||||
}
|
||||
out.set(enet::pourcentDecode(it.extract(0, offset)),
|
||||
enet::pourcentDecode(it.extract(offset+1, etk::String::npos)));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
43
enet/pourcentEncoding.hpp
Normal file
43
enet/pourcentEncoding.hpp
Normal file
@ -0,0 +1,43 @@
|
||||
/** @file
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2014, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <etk/String.hpp>
|
||||
#include <etk/Map.hpp>
|
||||
|
||||
|
||||
namespace enet {
|
||||
/**
|
||||
* @brief Encode a string with transform the unvalid char in %XX.
|
||||
* @param[in] _data Raw string.
|
||||
* @return Encoded string.
|
||||
*/
|
||||
etk::String pourcentEncode(const etk::String& _data);
|
||||
|
||||
/**
|
||||
* @brief Decode a string with %XX with normal chars.
|
||||
* @param[in] _data Encoded string.
|
||||
* @return Raw string.
|
||||
*/
|
||||
etk::String pourcentDecode(const etk::String& _data);
|
||||
|
||||
/**
|
||||
* @brief Encode a map of string with %XX and "&" between element and = between key and value.
|
||||
* @param[in] _data Imput map to encode.
|
||||
* @return Encoded string.
|
||||
*/
|
||||
etk::String pourcentUriEncode(const etk::Map<etk::String, etk::String>& _data);
|
||||
|
||||
/**
|
||||
* @brief Encode a map of string with %XX and "&" between element and = between key and value.
|
||||
* @param[in] _data Encoded string like AAA=333&RRR=333&RRR
|
||||
* @return Te decoded map of values.
|
||||
*/
|
||||
etk::Map<etk::String, etk::String> pourcentUriDecode(const etk::String& _data);
|
||||
|
||||
}
|
||||
|
45
lutin_enet-test.py
Normal file
45
lutin_enet-test.py
Normal file
@ -0,0 +1,45 @@
|
||||
#!/usr/bin/python
|
||||
import lutin.debug as debug
|
||||
import lutin.tools as tools
|
||||
|
||||
|
||||
def get_type():
|
||||
return "BINARY"
|
||||
|
||||
def get_sub_type():
|
||||
return "TEST"
|
||||
|
||||
def get_desc():
|
||||
return "e-net TEST basic backend"
|
||||
|
||||
def get_licence():
|
||||
return "MPL-2"
|
||||
|
||||
def get_compagny_type():
|
||||
return "com"
|
||||
|
||||
def get_compagny_name():
|
||||
return "atria-soft"
|
||||
|
||||
def get_maintainer():
|
||||
return "authors.txt"
|
||||
|
||||
def configure(target, my_module):
|
||||
my_module.add_path(".")
|
||||
my_module.add_depend([
|
||||
'enet',
|
||||
'etest',
|
||||
'test-debug'
|
||||
])
|
||||
my_module.add_src_file([
|
||||
'test/main-test.cpp',
|
||||
'test/main-unit-pourcentEncoding.cpp'
|
||||
])
|
||||
return True
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -42,6 +42,7 @@ def configure(target, my_module):
|
||||
'enet/Http.cpp',
|
||||
'enet/Ftp.cpp',
|
||||
'enet/WebSocket.cpp',
|
||||
'enet/pourcentEncoding.cpp',
|
||||
])
|
||||
my_module.add_header_file([
|
||||
'enet/enet.hpp',
|
||||
@ -53,6 +54,7 @@ def configure(target, my_module):
|
||||
'enet/Http.hpp',
|
||||
'enet/Ftp.hpp',
|
||||
'enet/WebSocket.hpp',
|
||||
'enet/pourcentEncoding.hpp',
|
||||
])
|
||||
if "Windows" in target.get_type():
|
||||
my_module.add_depend("ws2");
|
||||
|
18
test/main-test.cpp
Normal file
18
test/main-test.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
/** @file
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2018, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <etk/types.hpp>
|
||||
#include <test-debug/debug.hpp>
|
||||
|
||||
|
||||
#include <etest/etest.hpp>
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
// init test engine:
|
||||
etest::init(argc, argv);
|
||||
TEST_INFO("TEST ETK");
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
38
test/main-unit-pourcentEncoding.cpp
Normal file
38
test/main-unit-pourcentEncoding.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
/** @file
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2018, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <etk/types.hpp>
|
||||
#include <test-debug/debug.hpp>
|
||||
#include <etest/etest.hpp>
|
||||
#include <enet/pourcentEncoding.hpp>
|
||||
|
||||
TEST(pourcentSerializer, stringEncode) {
|
||||
EXPECT_EQ(enet::pourcentEncode("hello"), "hello");
|
||||
EXPECT_EQ(enet::pourcentEncode("hello@plop plouf-_~"), "hello%40plop%20plouf-_~");
|
||||
}
|
||||
|
||||
TEST(pourcentSerializer, stringDecode) {
|
||||
EXPECT_EQ(enet::pourcentDecode("hello"), "hello");
|
||||
EXPECT_EQ(enet::pourcentDecode("hello%40plop%20plouf-_~"), "hello@plop plouf-_~");
|
||||
}
|
||||
|
||||
TEST(pourcentSerializer, mapEncode) {
|
||||
etk::Map<etk::String, etk::String> data;
|
||||
data.set("hello", "value1");
|
||||
data.set("simpleKey", "");
|
||||
data.set("special Key", "coucou");
|
||||
data.set("email", "you@example.com");
|
||||
EXPECT_EQ(enet::pourcentUriEncode(data), "email=you%40example.com&hello=value1&simpleKey&special%20Key=coucou");
|
||||
}
|
||||
|
||||
TEST(pourcentSerializer, mapDecode) {
|
||||
auto data = enet::pourcentUriDecode("hello=value1&simpleKey&special%20Key=coucou&email=you%40example.com");
|
||||
EXPECT_EQ(data.size(), 4);
|
||||
EXPECT_EQ(data["hello"], "value1");
|
||||
EXPECT_EQ(data["simpleKey"], "");
|
||||
EXPECT_EQ(data["special Key"], "coucou");
|
||||
EXPECT_EQ(data["email"], "you@example.com");
|
||||
}
|
Loading…
Reference in New Issue
Block a user