forked from love2d/lua-https
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndroidClient.cpp
More file actions
218 lines (167 loc) · 6.06 KB
/
AndroidClient.cpp
File metadata and controls
218 lines (167 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "AndroidClient.h"
#ifdef HTTPS_BACKEND_ANDROID
#include <sstream>
#include <type_traits>
#include <dlfcn.h>
static std::string replace(const std::string &str, const std::string &from, const std::string &to)
{
std::stringstream ss;
size_t oldpos = 0;
while (true)
{
size_t pos = str.find(from, oldpos);
if (pos == std::string::npos)
{
ss << str.substr(oldpos);
break;
}
ss << str.substr(oldpos, pos - oldpos) << to;
oldpos = pos + from.length();
}
return ss.str();
}
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);
env->ReleaseStringUTFChars(str, c);
return result;
}
AndroidClient::AndroidClient()
: HTTPSClient()
, SDL_AndroidGetJNIEnv(nullptr)
{
// Look for SDL_AndroidGetJNIEnv
SDL_AndroidGetJNIEnv = (decltype(SDL_AndroidGetJNIEnv)) dlsym(RTLD_DEFAULT, "SDL_AndroidGetJNIEnv");
// Look for SDL_AndroidGetActivity
SDL_AndroidGetActivity = (decltype(SDL_AndroidGetActivity)) dlsym(RTLD_DEFAULT, "SDL_AndroidGetActivity");
}
bool AndroidClient::valid() const
{
if (SDL_AndroidGetJNIEnv && SDL_AndroidGetActivity)
{
JNIEnv *env = SDL_AndroidGetJNIEnv();
if (env)
{
jclass httpsClass = getHTTPSClass();
if (env->ExceptionCheck())
{
env->ExceptionClear();
return false;
}
env->DeleteLocalRef(httpsClass);
return true;
}
}
return false;
}
HTTPSClient::Reply AndroidClient::request(const HTTPSClient::Request &req)
{
JNIEnv *env = SDL_AndroidGetJNIEnv();
jclass httpsClass = getHTTPSClass();
if (httpsClass == nullptr)
{
env->ExceptionClear();
throw std::runtime_error("Could not find class 'org.love2d.luahttps.LuaHTTPS'");
}
jmethodID constructor = env->GetMethodID(httpsClass, "<init>", "()V");
jmethodID setURL = env->GetMethodID(httpsClass, "setUrl", "(Ljava/lang/String;)V");
jmethodID setMethod = env->GetMethodID(httpsClass, "setMethod", "(Ljava/lang/String;)V");
jmethodID request = env->GetMethodID(httpsClass, "request", "()Z");
jmethodID getInterleavedHeaders = env->GetMethodID(httpsClass, "getInterleavedHeaders", "()[Ljava/lang/String;");
jmethodID getResponse = env->GetMethodID(httpsClass, "getResponse", "()[B");
jmethodID getResponseCode = env->GetMethodID(httpsClass, "getResponseCode", "()I");
jobject httpsObject = env->NewObject(httpsClass, constructor);
// Set URL
jstring url = env->NewStringUTF(req.url.c_str());
env->CallVoidMethod(httpsObject, setURL, url);
env->DeleteLocalRef(url);
// Set method
jstring method = env->NewStringUTF(req.method.c_str());
env->CallVoidMethod(httpsObject, setMethod, method);
env->DeleteLocalRef(method);
// Set post data
if (req.postdata.size() > 0)
{
jmethodID setPostData = env->GetMethodID(httpsClass, "setPostData", "([B)V");
jbyteArray byteArray = env->NewByteArray((jsize) req.postdata.length());
jbyte *byteArrayData = env->GetByteArrayElements(byteArray, nullptr);
memcpy(byteArrayData, req.postdata.data(), req.postdata.length());
env->ReleaseByteArrayElements(byteArray, byteArrayData, 0);
env->CallVoidMethod(httpsObject, setPostData, byteArray);
env->DeleteLocalRef(byteArray);
}
// Set headers
if (!req.headers.empty())
{
jmethodID addHeader = env->GetMethodID(httpsClass, "addHeader", "(Ljava/lang/String;Ljava/lang/String;)V");
for (auto &header : req.headers)
{
jstring headerKey = newStringUTF(env, header.first);
jstring headerValue = newStringUTF(env, header.second);
env->CallVoidMethod(httpsObject, addHeader, headerKey, headerValue);
env->DeleteLocalRef(headerKey);
env->DeleteLocalRef(headerValue);
}
}
// Do request
HTTPSClient::Reply response;
jboolean status = env->CallBooleanMethod(httpsObject, request);
// Get response
response.responseCode = env->CallIntMethod(httpsObject, getResponseCode);
if (status)
{
// Get headers
jobjectArray interleavedHeaders = (jobjectArray) env->CallObjectMethod(httpsObject, getInterleavedHeaders);
int len = env->GetArrayLength(interleavedHeaders);
for (int i = 0; i < len; i += 2)
{
jstring key = (jstring) env->GetObjectArrayElement(interleavedHeaders, i);
jstring value = (jstring) env->GetObjectArrayElement(interleavedHeaders, i + 1);
response.headers[getStringUTF(env, key)] = getStringUTF(env, value);
env->DeleteLocalRef(key);
env->DeleteLocalRef(value);
}
env->DeleteLocalRef(interleavedHeaders);
// Get response data
jbyteArray responseData = (jbyteArray) env->CallObjectMethod(httpsObject, getResponse);
if (responseData)
{
int len = env->GetArrayLength(responseData);
jbyte *responseByte = env->GetByteArrayElements(responseData, nullptr);
response.body = std::string((char *) responseByte, len);
env->DeleteLocalRef(responseData);
}
}
return response;
}
jclass AndroidClient::getHTTPSClass() const
{
JNIEnv *env = SDL_AndroidGetJNIEnv();
jclass classLoaderClass = env->FindClass("java/lang/ClassLoader");
jmethodID loadClass = env->GetMethodID(classLoaderClass, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
jobject activity = SDL_AndroidGetActivity();
if (activity == nullptr)
return nullptr;
jclass gameActivity = env->GetObjectClass(activity);
jmethodID getLoader = env->GetMethodID(gameActivity, "getClassLoader", "()Ljava/lang/ClassLoader;");
jobject classLoader = env->CallObjectMethod(activity, getLoader);
jstring httpsClassName = env->NewStringUTF("org.love2d.luahttps.LuaHTTPS");
jclass httpsClass = (jclass) env->CallObjectMethod(classLoader, loadClass, httpsClassName);
env->DeleteLocalRef(gameActivity);
env->DeleteLocalRef(httpsClassName);
env->DeleteLocalRef(activity);
env->DeleteLocalRef(classLoaderClass);
return httpsClass;
}
#endif // HTTPS_BACKEND_ANDROID