Add parameter encoding helpers to NETWORK

This adds two more methods:
- `NETWORK:UrlEncode()`: Encode a single string value.
- `NETWORK:EncodeQueryParameters()`: Encode a table as query string.
This commit is contained in:
Martin Natano
2021-09-23 22:54:47 +02:00
parent dc49af3c59
commit bc4a275510
4 changed files with 73 additions and 2 deletions
+2
View File
@@ -1110,6 +1110,8 @@
<Class name='NetworkManager'>
<Function name='IsUrlAllowed'/>
<Function name='HttpRequest'/>
<Function name='UrlEncode'/>
<Function name='EncodeQueryParameters'/>
</Class>
<Class name='NCSplineHandler'>
<Function name='get_spline'/>
+6
View File
@@ -3603,6 +3603,12 @@ NETWORK:HttpRequest{
}
</code></pre>
</Function>
<Function name='UrlEncode' return='string' arguments='string value'>
Returns the URL encoded representation of <code>value</code>.
</Function>
<Function name='EncodeQueryParameters' return='string' arguments='table query'>
Returns the <code>query</code> encoded as a query string. Keys and values are automatically URL encoded.
</Function>
</Class>
<Class name='NoteSkinManager'>
<Description>
+61
View File
@@ -10,6 +10,7 @@
#include <ixwebsocket/IXUrlParser.h>
#include <algorithm>
#include <unordered_map>
#include <vector>
@@ -115,6 +116,16 @@ void NetworkManager::HttpRequest(const HttpRequestArgs& args)
this->httpClient.performRequest(req, args.onResponse);
}
std::string NetworkManager::UrlEncode(const std::string& value)
{
return this->httpClient.urlEncode(value);
}
std::string NetworkManager::EncodeQueryParameters(const std::unordered_map<std::string, std::string>& query)
{
return this->httpClient.serializeHttpParameters(query);
}
// lua start
#include "LuaBinding.h"
@@ -306,10 +317,60 @@ public:
return 1;
}
static int UrlEncode(T* p, lua_State *L)
{
std::string url = SArg(1);
std::string encoded = p->UrlEncode(url);
lua_pushstring(L, encoded.c_str());
return 1;
}
static int EncodeQueryParameters(T* p, lua_State *L)
{
std::unordered_map<std::string, std::string> query;
if (lua_istable(L, 1))
{
lua_pushnil(L);
while(lua_next(L, -2) != 0)
{
if (!lua_isstring(L, -2))
{
luaL_error(L, "parameter keys must be strings");
}
if (!lua_isstring(L, -1))
{
luaL_error(L, "parameter values must be strings");
}
std::string key = lua_tostring(L, -2);
std::string value = lua_tostring(L, -1);
query[key] = value;
lua_pop(L, 1);
}
}
else
{
luaL_error(L, "query must be a table");
}
std::string encoded = p->EncodeQueryParameters(query);
lua_pushstring(L, encoded.c_str());
return 1;
}
LunaNetworkManager()
{
ADD_METHOD(IsUrlAllowed);
ADD_METHOD(HttpRequest);
ADD_METHOD(UrlEncode);
ADD_METHOD(EncodeQueryParameters);
}
private:
+4 -2
View File
@@ -5,8 +5,8 @@
#include "StdString.h"
#include <functional>
#include <map>
#include <string>
#include <unordered_map>
#include <ixwebsocket/IXHttp.h>
#include <ixwebsocket/IXHttpClient.h>
@@ -50,7 +50,7 @@ struct HttpRequestArgs
std::string method = ix::HttpClient::kGet;
std::string body;
std::string multipartBoundary;
std::map<string, string> headers;
std::unordered_map<std::string, std::string> headers;
int connectTimeout = -1;
int transferTimeout = -1;
std::function<void(const ix::HttpResponsePtr& response)> onResponse = [](const ix::HttpResponsePtr& response) {};
@@ -64,6 +64,8 @@ public:
bool IsUrlAllowed(const std::string& url);
void HttpRequest(const HttpRequestArgs& args);
std::string UrlEncode(const std::string& value);
std::string EncodeQueryParameters(const std::unordered_map<std::string, std::string>& query);
// Lua
void PushSelf(lua_State *L);