From fd847bca9e1e7bc93890bbe6a10c3b61ff350ec0 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 20 Jan 2016 17:35:07 -0800 Subject: [android] Fix semantics in absence of response headers Absence of response headers should result in null etag/modified/expires optionals, not present-but-0-or-"" optionals. --- platform/android/src/http_request_android.cpp | 81 +++++++++++---------------- 1 file changed, 33 insertions(+), 48 deletions(-) diff --git a/platform/android/src/http_request_android.cpp b/platform/android/src/http_request_android.cpp index 81fb6fd306..d30d1572ff 100644 --- a/platform/android/src/http_request_android.cpp +++ b/platform/android/src/http_request_android.cpp @@ -42,8 +42,8 @@ public: void cancel() final; - void onFailure(int type, std::string message); - void onResponse(int code, std::string message, std::string etag, std::string modified, std::string cacheControl, std::string expires, std::string body); + void onFailure(JNIEnv*, int type, jstring message); + void onResponse(JNIEnv*, int code, jstring message, jstring etag, jstring modified, jstring cacheControl, jstring expires, jbyteArray body); private: void finish(); @@ -188,20 +188,31 @@ void HTTPAndroidRequest::finish() { delete this; } -void HTTPAndroidRequest::onResponse(int code, std::string message, std::string etag, std::string modified, std::string cacheControl, std::string expires, std::string body) { +void HTTPAndroidRequest::onResponse(JNIEnv* env, int code, jstring /* message */, jstring etag, jstring modified, jstring cacheControl, jstring expires, jbyteArray body) { response = std::make_unique(); using Error = Response::Error; - // the message param is unused, this generates a warning at build time - // this was breaking builds for `make android -j4` - (void)message; - response->modified = SystemClock::from_time_t(parse_date(modified.c_str())); - response->etag = etag; - response->expires = parseCacheControl(cacheControl.c_str()); - if (!expires.empty()) { - response->expires = SystemClock::from_time_t(parse_date(expires.c_str())); + if (etag != nullptr) { + response->etag = mbgl::android::std_string_from_jstring(env, etag); + } + + if (modified != nullptr) { + response->modified = SystemClock::from_time_t(parse_date(mbgl::android::std_string_from_jstring(env, modified).c_str())); + } + + if (cacheControl != nullptr) { + response->expires = parseCacheControl(mbgl::android::std_string_from_jstring(env, cacheControl).c_str()); + } + + if (expires != nullptr) { + response->expires = SystemClock::from_time_t(parse_date(mbgl::android::std_string_from_jstring(env, expires).c_str())); + } + + if (body != nullptr) { + jbyte* bodyData = env->GetByteArrayElements(body, nullptr); + response->data = std::make_shared(reinterpret_cast(bodyData), env->GetArrayLength(body)); + env->ReleaseByteArrayElements(body, bodyData, JNI_ABORT); } - response->data = std::make_shared(body); if (code == 200) { // Nothing to do; this is what we want @@ -234,22 +245,24 @@ void HTTPAndroidRequest::onResponse(int code, std::string message, std::string e async.send(); } -void HTTPAndroidRequest::onFailure(int type, std::string message) { +void HTTPAndroidRequest::onFailure(JNIEnv* env, int type, jstring message) { + std::string messageStr = mbgl::android::std_string_from_jstring(env, message); + response = std::make_unique(); using Error = Response::Error; switch (type) { case connectionError: - response->error = std::make_unique(Error::Reason::Connection, message); + response->error = std::make_unique(Error::Reason::Connection, messageStr); break; case temporaryError: - response->error = std::make_unique(Error::Reason::Server, message); + response->error = std::make_unique(Error::Reason::Server, messageStr); break; case canceledError: response->error = std::make_unique(Error::Reason::Canceled, "Request was cancelled"); break; default: - response->error = std::make_unique(Error::Reason::Other, message); + response->error = std::make_unique(Error::Reason::Other, messageStr); } async.send(); @@ -259,44 +272,16 @@ std::unique_ptr HTTPContextBase::createContext() { return std::make_unique(); } -#pragma clang diagnostic ignored "-Wunused-parameter" - -void JNICALL nativeOnFailure(JNIEnv *env, jobject obj, jlong nativePtr, jint type, jstring message) { +void JNICALL nativeOnFailure(JNIEnv* env, jobject, jlong nativePtr, jint type, jstring message) { mbgl::Log::Debug(mbgl::Event::JNI, "nativeOnFailure"); assert(nativePtr != 0); - HTTPAndroidRequest *request = reinterpret_cast(nativePtr); - std::string messageStr = mbgl::android::std_string_from_jstring(env, message); - return request->onFailure(type, messageStr); + return reinterpret_cast(nativePtr)->onFailure(env, type, message); } -void JNICALL nativeOnResponse(JNIEnv *env, jobject obj, jlong nativePtr, jint code, jstring message, jstring etag, jstring modified, jstring cacheControl, jstring expires, jbyteArray body) { +void JNICALL nativeOnResponse(JNIEnv* env, jobject, jlong nativePtr, jint code, jstring message, jstring etag, jstring modified, jstring cacheControl, jstring expires, jbyteArray body) { mbgl::Log::Debug(mbgl::Event::JNI, "nativeOnResponse"); assert(nativePtr != 0); - HTTPAndroidRequest *request = reinterpret_cast(nativePtr); - std::string messageStr = mbgl::android::std_string_from_jstring(env, message); - std::string etagStr; - if (etag != nullptr) { - etagStr = mbgl::android::std_string_from_jstring(env, etag); - } - std::string modifiedStr; - if (modified != nullptr) { - modifiedStr = mbgl::android::std_string_from_jstring(env, modified); - } - std::string cacheControlStr; - if (cacheControl != nullptr) { - cacheControlStr = mbgl::android::std_string_from_jstring(env, cacheControl); - } - std::string expiresStr; - if (expires != nullptr) { - expiresStr = mbgl::android::std_string_from_jstring(env, expires); - } - std::string bodyStr; - if (body != nullptr) { - jbyte* bodyData = env->GetByteArrayElements(body, nullptr); - bodyStr = std::string(reinterpret_cast(bodyData), env->GetArrayLength(body)); - env->ReleaseByteArrayElements(body, bodyData, JNI_ABORT); - } - return request->onResponse(code, messageStr, etagStr, modifiedStr, cacheControlStr, expiresStr, bodyStr); + return reinterpret_cast(nativePtr)->onResponse(env, code, message, etag, modified, cacheControl, expires, body); } } -- cgit v1.2.1