Skip to content

Commit

Permalink
Update lua-https to love2d/lua-https@0b2346f.
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuAuahDark committed Sep 28, 2023
1 parent f43c6fe commit 2fd2930
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/libraries/luahttps/src/android/AndroidClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

#include <dlfcn.h>

// We want std::string that contains null byte, hence length of 1.
// NOLINTNEXTLINE
static std::string null("", 1);

static std::string replace(const std::string &str, const std::string &from, const std::string &to)
{
std::stringstream ss;
Expand All @@ -31,19 +35,13 @@ static std::string replace(const std::string &str, const std::string &from, cons

static jstring newStringUTF(JNIEnv *env, const std::string &str)
{
// We want std::string that contains null byte, hence length of 1.
static std::string null("", 1);

std::string newStr = replace(str, null, "\xC0\x80");
jstring jstr = env->NewStringUTF(newStr.c_str());
return jstr;
}

static std::string getStringUTF(JNIEnv *env, jstring str)
{
// We want std::string that contains null byte, hence length of 1.
static std::string null("", 1);

const char *c = env->GetStringUTFChars(str, nullptr);
std::string result = replace(c, "\xC0\x80", null);

Expand All @@ -53,7 +51,6 @@ static std::string getStringUTF(JNIEnv *env, jstring str)

AndroidClient::AndroidClient()
: HTTPSClient()
, SDL_AndroidGetJNIEnv(nullptr)
{
// Look for SDL_AndroidGetJNIEnv
SDL_AndroidGetJNIEnv = (decltype(SDL_AndroidGetJNIEnv)) dlsym(RTLD_DEFAULT, "SDL_AndroidGetJNIEnv");
Expand Down Expand Up @@ -116,12 +113,14 @@ HTTPSClient::Reply AndroidClient::request(const HTTPSClient::Request &req)
env->DeleteLocalRef(method);

// Set post data
if (req.postdata.size() > 0)
if (!req.postdata.empty())
{
jmethodID setPostData = env->GetMethodID(httpsClass, "setPostData", "([B)V");
jbyteArray byteArray = env->NewByteArray((jsize) req.postdata.length());
jbyte *byteArrayData = env->GetByteArrayElements(byteArray, nullptr);

// The usage of memcpy is intentional.
// NOLINTNEXTLINE
memcpy(byteArrayData, req.postdata.data(), req.postdata.length());
env->ReleaseByteArrayElements(byteArray, byteArrayData, 0);

Expand Down Expand Up @@ -156,9 +155,9 @@ HTTPSClient::Reply AndroidClient::request(const HTTPSClient::Request &req)
{
// Get headers
jobjectArray interleavedHeaders = (jobjectArray) env->CallObjectMethod(httpsObject, getInterleavedHeaders);
int len = env->GetArrayLength(interleavedHeaders);
int headerLen = env->GetArrayLength(interleavedHeaders);

for (int i = 0; i < len; i += 2)
for (int i = 0; i < headerLen; i += 2)
{
jstring key = (jstring) env->GetObjectArrayElement(interleavedHeaders, i);
jstring value = (jstring) env->GetObjectArrayElement(interleavedHeaders, i + 1);
Expand All @@ -176,15 +175,17 @@ HTTPSClient::Reply AndroidClient::request(const HTTPSClient::Request &req)

if (responseData)
{
int len = env->GetArrayLength(responseData);
int responseLen = env->GetArrayLength(responseData);
jbyte *responseByte = env->GetByteArrayElements(responseData, nullptr);

response.body = std::string((char *) responseByte, len);
response.body = std::string((char *) responseByte, responseLen);

env->DeleteLocalRef(responseData);
}
}

env->DeleteLocalRef(httpsObject);

return response;
}

Expand Down
11 changes: 11 additions & 0 deletions src/libraries/luahttps/src/common/config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#pragma once

// MSVC warnings
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif

#if defined(HTTPS_HAVE_CONFIG_GENERATED_H)
#include "common/config-generated.h"
#elif defined(WIN32) || defined(_WIN32)
Expand All @@ -10,6 +15,12 @@
// WinINet is only supported on desktop.
#define HTTPS_BACKEND_WININET
#endif
// Visual Studio 2017 supports __has_include
#if defined __has_include
#if __has_include(<curl/curl.h>)
#define HTTPS_BACKEND_CURL
#endif
#endif
#elif defined(__ANDROID__)
#define HTTPS_BACKEND_ANDROID
#elif defined(__APPLE__)
Expand Down

0 comments on commit 2fd2930

Please sign in to comment.