From 2fd2930c24df263324cc0fa9a38feeff02c57e66 Mon Sep 17 00:00:00 2001 From: Miku AuahDark Date: Thu, 28 Sep 2023 16:53:59 +0800 Subject: [PATCH] Update lua-https to love2d/lua-https@0b2346f. --- .../luahttps/src/android/AndroidClient.cpp | 25 ++++++++++--------- src/libraries/luahttps/src/common/config.h | 11 ++++++++ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/libraries/luahttps/src/android/AndroidClient.cpp b/src/libraries/luahttps/src/android/AndroidClient.cpp index 24cbb1d0e..a5994e24b 100644 --- a/src/libraries/luahttps/src/android/AndroidClient.cpp +++ b/src/libraries/luahttps/src/android/AndroidClient.cpp @@ -7,6 +7,10 @@ #include +// 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; @@ -31,9 +35,6 @@ 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; @@ -41,9 +42,6 @@ static jstring newStringUTF(JNIEnv *env, const std::string &str) 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); @@ -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"); @@ -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); @@ -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); @@ -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; } diff --git a/src/libraries/luahttps/src/common/config.h b/src/libraries/luahttps/src/common/config.h index e55c3fe3f..4068e9b63 100644 --- a/src/libraries/luahttps/src/common/config.h +++ b/src/libraries/luahttps/src/common/config.h @@ -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) @@ -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() + #define HTTPS_BACKEND_CURL + #endif + #endif #elif defined(__ANDROID__) #define HTTPS_BACKEND_ANDROID #elif defined(__APPLE__)