7
7
8
8
#include < dlfcn.h>
9
9
10
+ // We want std::string that contains null byte, hence length of 1.
11
+ // NOLINTNEXTLINE
12
+ static std::string null (" " , 1 );
13
+
10
14
static std::string replace (const std::string &str, const std::string &from, const std::string &to)
11
15
{
12
16
std::stringstream ss;
@@ -31,19 +35,13 @@ static std::string replace(const std::string &str, const std::string &from, cons
31
35
32
36
static jstring newStringUTF (JNIEnv *env, const std::string &str)
33
37
{
34
- // We want std::string that contains null byte, hence length of 1.
35
- static std::string null (" " , 1 );
36
-
37
38
std::string newStr = replace (str, null, " \xC0\x80 " );
38
39
jstring jstr = env->NewStringUTF (newStr.c_str ());
39
40
return jstr;
40
41
}
41
42
42
43
static std::string getStringUTF (JNIEnv *env, jstring str)
43
44
{
44
- // We want std::string that contains null byte, hence length of 1.
45
- static std::string null (" " , 1 );
46
-
47
45
const char *c = env->GetStringUTFChars (str, nullptr );
48
46
std::string result = replace (c, " \xC0\x80 " , null);
49
47
@@ -53,7 +51,6 @@ static std::string getStringUTF(JNIEnv *env, jstring str)
53
51
54
52
AndroidClient::AndroidClient ()
55
53
: HTTPSClient()
56
- , SDL_AndroidGetJNIEnv(nullptr )
57
54
{
58
55
// Look for SDL_AndroidGetJNIEnv
59
56
SDL_AndroidGetJNIEnv = (decltype (SDL_AndroidGetJNIEnv)) dlsym (RTLD_DEFAULT, " SDL_AndroidGetJNIEnv" );
@@ -116,12 +113,14 @@ HTTPSClient::Reply AndroidClient::request(const HTTPSClient::Request &req)
116
113
env->DeleteLocalRef (method);
117
114
118
115
// Set post data
119
- if (req.postdata .size () > 0 )
116
+ if (! req.postdata .empty () )
120
117
{
121
118
jmethodID setPostData = env->GetMethodID (httpsClass, " setPostData" , " ([B)V" );
122
119
jbyteArray byteArray = env->NewByteArray ((jsize) req.postdata .length ());
123
120
jbyte *byteArrayData = env->GetByteArrayElements (byteArray, nullptr );
124
121
122
+ // The usage of memcpy is intentional.
123
+ // NOLINTNEXTLINE
125
124
memcpy (byteArrayData, req.postdata .data (), req.postdata .length ());
126
125
env->ReleaseByteArrayElements (byteArray, byteArrayData, 0 );
127
126
@@ -156,9 +155,9 @@ HTTPSClient::Reply AndroidClient::request(const HTTPSClient::Request &req)
156
155
{
157
156
// Get headers
158
157
jobjectArray interleavedHeaders = (jobjectArray) env->CallObjectMethod (httpsObject, getInterleavedHeaders);
159
- int len = env->GetArrayLength (interleavedHeaders);
158
+ int headerLen = env->GetArrayLength (interleavedHeaders);
160
159
161
- for (int i = 0 ; i < len ; i += 2 )
160
+ for (int i = 0 ; i < headerLen ; i += 2 )
162
161
{
163
162
jstring key = (jstring) env->GetObjectArrayElement (interleavedHeaders, i);
164
163
jstring value = (jstring) env->GetObjectArrayElement (interleavedHeaders, i + 1 );
@@ -176,15 +175,17 @@ HTTPSClient::Reply AndroidClient::request(const HTTPSClient::Request &req)
176
175
177
176
if (responseData)
178
177
{
179
- int len = env->GetArrayLength (responseData);
178
+ int responseLen = env->GetArrayLength (responseData);
180
179
jbyte *responseByte = env->GetByteArrayElements (responseData, nullptr );
181
180
182
- response.body = std::string ((char *) responseByte, len );
181
+ response.body = std::string ((char *) responseByte, responseLen );
183
182
184
183
env->DeleteLocalRef (responseData);
185
184
}
186
185
}
187
186
187
+ env->DeleteLocalRef (httpsObject);
188
+
188
189
return response;
189
190
}
190
191
0 commit comments