Skip to content

Commit

Permalink
Code adjustements and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tdcosta100 committed Oct 2, 2024
1 parent 6a0e949 commit e778def
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
5 changes: 3 additions & 2 deletions platform/android/MapLibreAndroid/src/cpp/http_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ HTTPRequest::HTTPRequest(jni::JNIEnv& env, const Resource& resource_, FileSource
std::string modifiedStr;

if (resource.dataRange) {
dataRangeStr = std::string("bytes=") + std::to_string(resource.dataRange->first) + std::string("-") + std::to_string(resource.dataRange->second);
dataRangeStr = std::string("bytes=") + std::to_string(resource.dataRange->first) + std::string("-") +
std::to_string(resource.dataRange->second);
}

if (resource.priorEtag) {
etagStr = *resource.priorEtag;
} else if (resource.priorModified) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ public class NativeHttpRequest implements HttpResponder {
private long nativePtr;

@Keep
private NativeHttpRequest(long nativePtr, String resourceUrl, String etag, String modified, boolean offlineUsage) {
private NativeHttpRequest(long nativePtr, String resourceUrl, String dataRange, String etag, String modified,
boolean offlineUsage) {
this.nativePtr = nativePtr;

if (resourceUrl.startsWith("local://")) {
// used by render test to serve files from assets
executeLocalRequest(resourceUrl);
return;
}
httpRequest.executeRequest(this, nativePtr, resourceUrl, etag, modified, offlineUsage);
httpRequest.executeRequest(this, nativePtr, resourceUrl, dataRange, etag, modified, offlineUsage);
}

public void cancel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public class HttpRequestImpl implements HttpRequest {

@Override
public void executeRequest(HttpResponder httpRequest, long nativePtr, @NonNull String resourceUrl,
@NonNull String dataRange, @NonNull String etag, @NonNull String modified, boolean offlineUsage) {
@NonNull String dataRange, @NonNull String etag, @NonNull String modified,
boolean offlineUsage) {
OkHttpCallback callback = new OkHttpCallback(httpRequest);
try {
HttpUrl httpUrl = HttpUrl.parse(resourceUrl);
Expand All @@ -73,7 +74,7 @@ public void executeRequest(HttpResponder httpRequest, long nativePtr, @NonNull S
.url(resourceUrl)
.tag(resourceUrl.toLowerCase(MapLibreConstants.MAPLIBRE_LOCALE))
.addHeader("User-Agent", userAgentString);

if (dataRange.length() > 0) {
builder.addHeader("Range", dataRange);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import org.maplibre.android.module.http.HttpRequestImpl
*/
class ExampleHttpRequestImpl : HttpRequest {
override fun executeRequest(httpRequest: HttpResponder, nativePtr: Long, resourceUrl: String,
etag: String, modified: String, offlineUsage: Boolean)
dataRange: String, etag: String, modified: String, offlineUsage: Boolean)
{
// Load all json documents and any pbf ending with a 0.
if (resourceUrl.endsWith(".json") || resourceUrl.endsWith("0.pbf")) {
impl.executeRequest(httpRequest, nativePtr, resourceUrl, etag, modified, offlineUsage)
impl.executeRequest(httpRequest, nativePtr, resourceUrl, dataRange, etag, modified, offlineUsage)
} else {
// All other requests get an instant 404!
httpRequest.onResponse(
Expand Down
34 changes: 14 additions & 20 deletions platform/default/src/mbgl/storage/pmtiles_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
#endif

namespace {
const int MAX_DIRECTORY_CACHE_ENTRIES = 100;
constexpr int MAX_DIRECTORY_CACHE_ENTRIES = 100;

bool acceptsURL(const std::string& url) {
return 0 == url.rfind(mbgl::util::PMTILES_PROTOCOL, 0);
return url.starts_with(mbgl::util::PMTILES_PROTOCOL);
}

std::string extract_url(const std::string& url) {
Expand Down Expand Up @@ -74,7 +74,7 @@ class PMTilesFileSource::Impl {
void request_tile(AsyncRequest* req, const Resource& resource, ActorRef<FileSourceRequest> ref) {
auto url = extract_url(resource.url);

getHeaderAndRootDirectory(url, req, [=, this](std::unique_ptr<Response::Error> error) {
getHeader(url, req, [=, this](std::unique_ptr<Response::Error> error) {
if (error) {
Response response;
response.noContent = true;
Expand Down Expand Up @@ -206,20 +206,21 @@ class PMTilesFileSource::Impl {
return fileSource;
}

void getHeaderAndRootDirectory(const std::string& url, AsyncRequest* req, AsyncCallback callback) {
void getHeader(const std::string& url, AsyncRequest* req, AsyncCallback callback) {
if (header_cache.find(url) != header_cache.end()) {
callback(std::unique_ptr<Response::Error>());
}

Resource resource(Resource::Kind::Source, url);
resource.loadingMethod = Resource::LoadingMethod::Network;
resource.dataRange = std::make_pair<uint64_t, uint64_t>(0, 16383);

// https://github.com/protomaps/PMTiles/blob/main/spec/v3/spec.md#3-header
resource.dataRange = std::make_pair<uint64_t, uint64_t>(0, 127);

tasks[req] = getFileSource()->request(resource, [=, this](const Response& response) {
if (response.error) {
callback(std::make_unique<Response::Error>(
response.error->reason,
std::string("Error fetching PMTiles header and root directory: ") + response.error->message));
response.error->reason, std::string("Error fetching PMTiles header: ") + response.error->message));

return;
}
Expand All @@ -234,19 +235,10 @@ class PMTilesFileSource::Impl {

header_cache.emplace(url, header);

std::string directoryData = response.data->substr(header.root_dir_offset, header.root_dir_bytes);

if (header.tile_compression == pmtiles::COMPRESSION_GZIP) {
directoryData = util::decompress(directoryData);
}

storeDirectory(url, header.root_dir_offset, header.root_dir_bytes, directoryData);

callback(std::unique_ptr<Response::Error>());
} catch (const std::exception& e) {
callback(std::make_unique<Response::Error>(
Response::Error::Reason::Other,
std::string("Error parsing PMTiles header and root directory: ") + e.what()));
callback(std::make_unique<Response::Error>(Response::Error::Reason::Other,
std::string("Error parsing PMTiles header: ") + e.what()));
}
});
}
Expand All @@ -256,7 +248,7 @@ class PMTilesFileSource::Impl {
callback(std::unique_ptr<Response::Error>());
}

getHeaderAndRootDirectory(url, req, [=, this](std::unique_ptr<Response::Error> error) {
getHeader(url, req, [=, this](std::unique_ptr<Response::Error> error) {
if (error) {
callback(std::move(error));
return;
Expand Down Expand Up @@ -406,7 +398,7 @@ class PMTilesFileSource::Impl {
return;
}

getHeaderAndRootDirectory(url, req, [=, this](std::unique_ptr<Response::Error> error) {
getHeader(url, req, [=, this](std::unique_ptr<Response::Error> error) {
if (error) {
callback(std::move(error));
return;
Expand Down Expand Up @@ -458,6 +450,8 @@ class PMTilesFileSource::Impl {
std::make_unique<Response::Error>(
Response::Error::Reason::Other,
std::string("Error fetching PMTiles tile address: Maximum directory depth exceeded")));

return;
}

getDirectory(url, req, directoryOffset, directoryLength, [=, this](std::unique_ptr<Response::Error> error) {
Expand Down
3 changes: 2 additions & 1 deletion platform/qt/src/mbgl/http_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ QNetworkRequest HTTPRequest::networkRequest() const {
#endif

if (m_resource.dataRange) {
std::string range = std::string("bytes=") + std::to_string(resource.dataRange->first) + std::string("-") + std::to_string(resource.dataRange->second);
std::string range = std::string("bytes=") + std::to_string(m_resource.dataRange->first) + std::string("-") +
std::to_string(m_resource.dataRange->second);
req.setRawHeader("Range", QByteArray(range.data(), static_cast<int>(range.size())));
}

Expand Down

0 comments on commit e778def

Please sign in to comment.