Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] HttpFileSource - rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
ivovandongen committed Sep 7, 2016
1 parent ff23651 commit 706e93b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class HTTPRequest implements Callback {

private native void nativeOnFailure(int type, String message);

private native void nativeOnResponse(int code, String etag, String modified, String cacheControl, String expires, byte[] body);
private native void nativeOnResponse(int code, String etag, String modified, String cacheControl, String expires, String retryAfter, String xRateLimitReset, byte[] body);

private HTTPRequest(long nativePtr, String resourceUrl, String userAgent, String etag, String modified) {
mNativePtr = nativePtr;
Expand Down Expand Up @@ -120,7 +120,14 @@ public void onResponse(Call call, Response response) throws IOException {

mLock.lock();
if (mNativePtr != 0) {
nativeOnResponse(response.code(), response.header("ETag"), response.header("Last-Modified"), response.header("Cache-Control"), response.header("Expires"), body);
nativeOnResponse(response.code(),
response.header("ETag"),
response.header("Last-Modified"),
response.header("Cache-Control"),
response.header("Expires"),
response.header("Retry-After"),
response.header("x-rate-limit-reset"),
body);
}
mLock.unlock();
}
Expand Down
18 changes: 16 additions & 2 deletions platform/android/src/http_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class HTTPRequest : public AsyncRequest {
void onResponse(jni::JNIEnv&, int code,
jni::String etag, jni::String modified,
jni::String cacheControl, jni::String expires,
jni::String retryAfter, jni::String xRateLimitReset,
jni::Array<jni::jbyte> body);

static jni::Class<HTTPRequest> javaClass;
Expand Down Expand Up @@ -101,8 +102,11 @@ HTTPRequest::~HTTPRequest() {
}

void HTTPRequest::onResponse(jni::JNIEnv& env, int code,
jni::String etag, jni::String modified, jni::String cacheControl,
jni::String expires, jni::Array<jni::jbyte> body) {
jni::String etag, jni::String modified,
jni::String cacheControl, jni::String expires,
jni::String jRetryAfter, jni::String jXRateLimitReset,
jni::Array<jni::jbyte> body) {

using Error = Response::Error;

if (etag) {
Expand Down Expand Up @@ -135,6 +139,16 @@ void HTTPRequest::onResponse(jni::JNIEnv& env, int code,
response.notModified = true;
} else if (code == 404) {
response.error = std::make_unique<Error>(Error::Reason::NotFound, "HTTP status code 404");
} else if (code == 429) {
optional<std::string> retryAfter;
optional<std::string> xRateLimitReset;
if (jRetryAfter) {
retryAfter = jni::Make<std::string>(env, jRetryAfter);
}
if (jXRateLimitReset) {
xRateLimitReset = jni::Make<std::string>(env, jXRateLimitReset);
}
response.error = std::make_unique<Error>(Error::Reason::RateLimit, "HTTP status code 429", http::parseRetryHeaders(retryAfter, xRateLimitReset));
} else if (code >= 500 && code < 600) {
response.error = std::make_unique<Error>(Error::Reason::Server, std::string{ "HTTP status code " } + std::to_string(code));
} else {
Expand Down
3 changes: 3 additions & 0 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,9 @@ void setOfflineRegionObserver(JNIEnv *env, jni::jobject* offlineRegion_, jni::jo
case mbgl::Response::Error::Reason::Connection:
errorReason = "REASON_CONNECTION";
break;
case mbgl::Response::Error::Reason::RateLimit:
errorReason = "REASON_RATE_LIMIT";
break;
case mbgl::Response::Error::Reason::Other:
errorReason = "REASON_OTHER";
break;
Expand Down

0 comments on commit 706e93b

Please sign in to comment.