Merge branch 'master' into libuv-0.10-headless-display
Conflicts: common/curl_request.cpp common/glfw_view.cpp common/glfw_view.hpp include/mbgl/platform/request.hpp ios/mapbox-gl-cocoa setup-libraries.sh src/map/map.cpp src/platform/request.cpp test/fixtures/fixture_request.cpp
This commit is contained in:
134
test/fixtures/fixture_request.cpp
vendored
134
test/fixtures/fixture_request.cpp
vendored
@@ -1,10 +1,16 @@
|
||||
#include <mbgl/platform/platform.hpp>
|
||||
#include <mbgl/platform/request.hpp>
|
||||
#include <mbgl/util/uv_detail.hpp>
|
||||
#include <mbgl/storage/http_request_baton.hpp>
|
||||
#include <mbgl/storage/file_request_baton.hpp>
|
||||
#include <mbgl/storage/response.hpp>
|
||||
#include <mbgl/util/url.hpp>
|
||||
#include <mbgl/util/std.hpp>
|
||||
#include <mbgl/platform/log.hpp>
|
||||
#include <iostream>
|
||||
|
||||
#include <uv.h>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
|
||||
const std::string base_directory = []{
|
||||
std::string fn = __FILE__;
|
||||
fn.erase(fn.find_last_of("/"));
|
||||
@@ -13,110 +19,48 @@ const std::string base_directory = []{
|
||||
return fn + "/node_modules/mapbox-gl-test-suite/";
|
||||
}();
|
||||
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
std::shared_ptr<platform::Request>
|
||||
platform::request_http(const std::string &url,
|
||||
std::function<void(Response *)> callback,
|
||||
std::shared_ptr<uv::loop> loop) {
|
||||
uv_loop_t *l = nullptr;
|
||||
if (loop) {
|
||||
l = **loop;
|
||||
} else {
|
||||
l = uv_default_loop();
|
||||
}
|
||||
void HTTPRequestBaton::start(const util::ptr<HTTPRequestBaton> &baton) {
|
||||
assert(uv_thread_self() == baton->thread_id);
|
||||
|
||||
std::string clean_url = util::percentDecode(url);
|
||||
std::string clean_url = util::percentDecode(baton->path);
|
||||
if (clean_url.find("local://") == 0) {
|
||||
clean_url = base_directory + clean_url.substr(8);
|
||||
}
|
||||
|
||||
std::shared_ptr<Request> req = std::make_shared<Request>(url, callback, loop);
|
||||
baton->response = std::make_unique<Response>();
|
||||
FILE *file = fopen(clean_url.c_str(),"rb");
|
||||
if (file != NULL) {
|
||||
fseek(file, 0, SEEK_END);
|
||||
const size_t size = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
baton->response->data.resize(size);
|
||||
fread(&baton->response->data[0], size, 1, file);
|
||||
fclose(file);
|
||||
|
||||
int err;
|
||||
|
||||
std::string body;
|
||||
FILE *file = std::fopen(clean_url.c_str(),"rb");
|
||||
|
||||
if (file != NULL)
|
||||
{
|
||||
std::fseek(file, 0, SEEK_END);
|
||||
std::size_t file_size = std::ftell(file);
|
||||
std::fseek(file, 0, SEEK_SET);
|
||||
body.resize(file_size);
|
||||
std::fread(&body[0], file_size, 1, file);
|
||||
std::fclose(file);
|
||||
baton->response->code = 200;
|
||||
baton->type = HTTPResponseType::Successful;
|
||||
} else {
|
||||
baton->type = HTTPResponseType::PermanentError;
|
||||
baton->response->code = 404;
|
||||
}
|
||||
|
||||
/*
|
||||
uv_fs_t open_req;
|
||||
err = uv_fs_open(l, &open_req, clean_url.c_str(), O_RDONLY, S_IRUSR, nullptr);
|
||||
uv_fs_req_cleanup(&open_req);
|
||||
if (err < 0) {
|
||||
req->res->code = err;
|
||||
req->res->error_message = uv_strerror(uv_last_error(l));
|
||||
Log::Warning(Event::HttpRequest, err, url + ": " + uv_strerror(uv_last_error(l)));
|
||||
req->complete();
|
||||
return req;
|
||||
}
|
||||
uv_file fd = err;
|
||||
|
||||
uv_fs_t stat_req;
|
||||
uv_fs_fstat(l, &stat_req, fd, nullptr);
|
||||
uv_fs_req_cleanup(&stat_req);
|
||||
err = uv_fs_fstat(l, &stat_req, fd, nullptr);
|
||||
if (err < 0) {
|
||||
req->res->code = err;
|
||||
req->res->error_message = uv_strerror(uv_last_error(l));
|
||||
Log::Warning(Event::HttpRequest, err, url + ": " + uv_strerror(uv_last_error(l)));
|
||||
req->complete();
|
||||
return req;
|
||||
}
|
||||
|
||||
const uint64_t size = static_cast<const uv_statbuf_t*>(stat_req.ptr)->st_size;
|
||||
|
||||
|
||||
std::string body;
|
||||
body.resize(size);
|
||||
uv_buf_t uvbuf = uv_buf_init(const_cast<char *>(body.data()), body.size());
|
||||
|
||||
uv_fs_t read_req;
|
||||
err = uv_fs_read(l, &read_req, fd, &uvbuf, 1, 0, nullptr);
|
||||
uv_fs_req_cleanup(&read_req);
|
||||
if (err < 0) {
|
||||
req->res->code = err;
|
||||
req->res->error_message = uv_strerror(uv_last_error(l));
|
||||
Log::Warning(Event::HttpRequest, err, url + ": " + uv_strerror(uv_last_error(l)));
|
||||
req->complete();
|
||||
return req;
|
||||
}
|
||||
|
||||
|
||||
uv_fs_t close_req;
|
||||
err = uv_fs_close(l, &close_req, fd, nullptr);
|
||||
uv_fs_req_cleanup(&close_req);
|
||||
if (err < 0) {
|
||||
req->res->code = err;
|
||||
req->res->error_message = uv_strerror(uv_last_error(l));
|
||||
Log::Warning(Event::HttpRequest, err, url + ": " + uv_strerror(uv_last_error(l)));
|
||||
req->complete();
|
||||
return req;
|
||||
}
|
||||
*/
|
||||
|
||||
req->res->body.swap(body);
|
||||
req->res->code = 200;
|
||||
Log::Info(Event::HttpRequest, 200, url);
|
||||
req->complete();
|
||||
|
||||
return req;
|
||||
uv_async_send(baton->async);
|
||||
}
|
||||
|
||||
void HTTPRequestBaton::stop(const util::ptr<HTTPRequestBaton> &baton) {
|
||||
fprintf(stderr, "HTTP request cannot be canceled because it is answered immediately");
|
||||
abort();
|
||||
}
|
||||
|
||||
namespace platform {
|
||||
|
||||
std::string defaultCacheDatabase() {
|
||||
// Disables the cache.
|
||||
return "";
|
||||
}
|
||||
|
||||
void platform::cancel_request_http(const std::shared_ptr<Request> &req) {
|
||||
if (req) {
|
||||
req->cancelled = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user