Add verb parameter to http.get/post

Also make ENFORCE_HTTPS optional, but default to enabled, so unencrypted HTTP is disabled by default, and require it to be enabled for release binaries.
This commit is contained in:
Tamás Bálint Misius 2022-11-01 11:21:01 +01:00
parent 3011e45475
commit 059b3a8e38
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
6 changed files with 40 additions and 5 deletions

View File

@ -56,6 +56,11 @@ endif
is_static = static_variant != 'none'
is_debug = get_option('optimization') in [ '0', 'g' ]
enforce_https = get_option('enforce_https')
if not is_debug and not enforce_https
error('refusing to build a release binary with enforce_https=false')
endif
tpt_libs_static = 'none'
if static_variant == 'prebuilt'
@ -323,6 +328,7 @@ conf_data.set('SERVER', '"@0@"'.format(get_option('server')))
conf_data.set('STATICSERVER', '"@0@"'.format(get_option('static_server')))
conf_data.set('IDENT_PLATFORM', '"@0@"'.format(ident_platform))
conf_data.set('IDENT', '"@0@-@1@-@2@"'.format(host_arch, host_platform, host_libc).to_upper())
conf_data.set('ENFORCE_HTTPS', enforce_https)
conf_data.set('APPNAME', get_option('app_name'))
conf_data.set('APPCOMMENT', get_option('app_comment'))
conf_data.set('APPEXE', get_option('app_exe'))

View File

@ -201,3 +201,9 @@ option(
value: 'powdertoy',
description: 'App vendor prefix, used for desktop integration, do not change even if you work on a mod, only if you know what you are doing'
)
option(
'enforce_https',
type: 'boolean',
value: true,
description: 'Enforce encrypted HTTP traffic, may be disabled for debugging'
)

View File

@ -29,6 +29,7 @@
#mesondefine UPDATESERVER
#mesondefine IDENT_PLATFORM
#mesondefine IDENT
#mesondefine ENFORCE_HTTPS
#define APPNAME "@APPNAME@"
#define APPCOMMENT "@APPCOMMENT@"
#define APPEXE "@APPEXE@"
@ -86,7 +87,6 @@
#define SCHEME "https://"
#define STATICSCHEME "https://"
#define ENFORCE_HTTPS
#define LOCAL_SAVE_DIR "Saves"

View File

@ -77,6 +77,13 @@ namespace http
#endif
}
void Request::Verb(ByteString newVerb)
{
#ifndef NOHTTP
verb = newVerb;
#endif
}
void Request::AddHeader(ByteString header)
{
#ifndef NOHTTP
@ -217,6 +224,10 @@ namespace http
{
curl_easy_setopt(easy, CURLOPT_HTTPGET, 1L);
}
if (verb.size())
{
curl_easy_setopt(easy, CURLOPT_CUSTOMREQUEST, verb.c_str());
}
curl_easy_setopt(easy, CURLOPT_FOLLOWLOCATION, 1L);
#ifdef ENFORCE_HTTPS

View File

@ -44,6 +44,7 @@ namespace http
bool added_to_multi;
int status;
ByteString verb;
struct curl_slist *headers;
bool isPost = false;
@ -64,6 +65,7 @@ namespace http
Request(ByteString uri);
virtual ~Request();
void Verb(ByteString newVerb);
void AddHeader(ByteString header);
void AddPostData(std::map<ByteString, ByteString> data);
void AuthHeaders(ByteString ID, ByteString session);

View File

@ -4077,7 +4077,7 @@ private:
}
public:
static int Make(lua_State *l, const ByteString &uri, bool isPost, RequestType type, const std::map<ByteString, ByteString> &post_data, const std::vector<ByteString> &headers)
static int Make(lua_State *l, const ByteString &uri, bool isPost, const ByteString &verb, RequestType type, const std::map<ByteString, ByteString> &post_data, const std::vector<ByteString> &headers)
{
auto authUser = Client::Ref().GetAuthUser();
if (type == getAuthToken && !authUser.UserID)
@ -4094,6 +4094,10 @@ public:
new(rh) RequestHandle();
rh->type = type;
rh->request = new http::Request(uri);
if (verb.size())
{
rh->request->Verb(verb);
}
for (auto &header : headers)
{
rh->request->AddHeader(header);
@ -4240,8 +4244,13 @@ static int http_request(lua_State *l, bool isPost)
{
ByteString uri = tpt_lua_checkByteString(l, 1);
std::map<ByteString, ByteString> post_data;
auto headersIndex = 2;
auto verbIndex = 3;
if (isPost)
{
headersIndex += 1;
verbIndex += 1;
if (lua_istable(l, 2))
{
lua_pushnil(l);
@ -4255,7 +4264,6 @@ static int http_request(lua_State *l, bool isPost)
}
std::vector<ByteString> headers;
auto headersIndex = isPost ? 3 : 2;
if (lua_istable(l, headersIndex))
{
auto size = lua_objlen(l, headersIndex);
@ -4280,12 +4288,14 @@ static int http_request(lua_State *l, bool isPost)
}
}
}
return RequestHandle::Make(l, uri, isPost, RequestHandle::normal, post_data, headers);
auto verb = tpt_lua_optByteString(l, verbIndex, "");
return RequestHandle::Make(l, uri, isPost, verb, RequestHandle::normal, post_data, headers);
}
static int http_get_auth_token(lua_State *l)
{
return RequestHandle::Make(l, SCHEME SERVER "/ExternalAuth.api?Action=Get&Audience=" + format::URLEncode(tpt_lua_checkByteString(l, 1)), false, RequestHandle::getAuthToken, {}, {});
return RequestHandle::Make(l, SCHEME SERVER "/ExternalAuth.api?Action=Get&Audience=" + format::URLEncode(tpt_lua_checkByteString(l, 1)), false, {}, RequestHandle::getAuthToken, {}, {});
}
int LuaScriptInterface::http_get(lua_State * l)