summaryrefslogtreecommitdiff
path: root/platform/default/asset_request_libuv.cpp
blob: 006e5736313996cb3bf9868b581c8e745a879b33 (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <mbgl/storage/default/asset_request.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/util.hpp>
#include <mbgl/util/uv.hpp>

#include <uv.h>
#include <boost/algorithm/string.hpp>

#include <cassert>


namespace algo = boost::algorithm;

namespace mbgl {

class AssetRequestImpl {
    MBGL_STORE_THREAD(tid)

public:
    AssetRequestImpl(AssetRequest *request, uv_loop_t *loop);
    ~AssetRequestImpl();

    static void fileOpened(uv_fs_t *req);
    static void fileStated(uv_fs_t *req);
    static void fileRead(uv_fs_t *req);
    static void fileClosed(uv_fs_t *req);
    static void notifyError(uv_fs_t *req);
    static void cleanup(uv_fs_t *req);


    AssetRequest *request = nullptr;
    bool canceled = false;
    uv_fs_t req;
    uv_file fd = -1;
    uv_buf_t buffer;
    std::unique_ptr<Response> response;
};

AssetRequestImpl::~AssetRequestImpl() {
    MBGL_VERIFY_THREAD(tid);

    if (request) {
        request->ptr = nullptr;
    }
}

AssetRequestImpl::AssetRequestImpl(AssetRequest *request_, uv_loop_t *loop) : request(request_) {
    req.data = this;
    uv_fs_open(loop, &req, (request->resource.url.substr(8)).c_str(), O_RDONLY, S_IRUSR, fileOpened);
}

void AssetRequestImpl::fileOpened(uv_fs_t *req) {
    assert(req->data);
    auto self = reinterpret_cast<AssetRequestImpl *>(req->data);
    MBGL_VERIFY_THREAD(self->tid);

    if (req->result < 0) {
        // Opening failed or was canceled. There isn't much left we can do.
        notifyError(req);
        cleanup(req);
    } else {
        const uv_file fd = uv_file(req->result);

        // We're going to reuse this handle, so we need to cleanup first.
        uv_fs_req_cleanup(req);

        if (self->canceled) {
            // The request was canceled.
            uv_fs_close(req->loop, req, fd, fileClosed);
        } else {
            self->fd = fd;
            uv_fs_fstat(req->loop, req, fd, fileStated);
        }
    }
}

void AssetRequestImpl::fileStated(uv_fs_t *req) {
    assert(req->data);
    auto self = reinterpret_cast<AssetRequestImpl *>(req->data);
    MBGL_VERIFY_THREAD(self->tid);

    if (req->result != 0 || self->canceled) {
        // Stating failed or was canceled. We already have an open file handle
        // though, which we'll have to close.
        notifyError(req);

        uv_fs_req_cleanup(req);
        uv_fs_close(req->loop, req, self->fd, fileClosed);
    } else {
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
        auto stat = static_cast<const uv_statbuf_t *>(req->ptr);
#else
        auto stat = static_cast<const uv_stat_t *>(req->ptr);
#endif
        if (stat->st_size > std::numeric_limits<int>::max()) {
            // File is too large for us to open this way because uv_buf's only support unsigned
            // ints as maximum size.
            auto response = util::make_unique<Response>();
            response->status = Response::Error;
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
            response->message = uv_strerror(uv_err_t {UV_EFBIG, 0});
#else
            response->message = uv_strerror(UV_EFBIG);
#endif
            assert(self->request);
            self->request->notify(std::move(response), FileCache::Hint::No);
            delete self->request;

            uv_fs_req_cleanup(req);
            uv_fs_close(req->loop, req, self->fd, fileClosed);
        } else {
            self->response = util::make_unique<Response>();
#ifdef __APPLE__
            self->response->modified = stat->st_mtimespec.tv_sec;
#else
            self->response->modified = stat->st_mtime;
#endif
            self->response->etag = std::to_string(stat->st_ino);
            const auto size = (unsigned int)(stat->st_size);
            self->response->data.resize(size);
            self->buffer = uv_buf_init(const_cast<char *>(self->response->data.data()), size);
            uv_fs_req_cleanup(req);
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
            uv_fs_read(req->loop, req, self->fd, self->buffer.base, self->buffer.len, -1, fileRead);
#else
            uv_fs_read(req->loop, req, self->fd, &self->buffer, 1, 0, fileRead);
#endif
        }
    }
}

void AssetRequestImpl::fileRead(uv_fs_t *req) {
    assert(req->data);
    auto self = reinterpret_cast<AssetRequestImpl *>(req->data);
    MBGL_VERIFY_THREAD(self->tid);

    if (req->result < 0 || self->canceled) {
        // Stating failed or was canceled. We already have an open file handle
        // though, which we'll have to close.
        notifyError(req);
    } else {
        // File was successfully read.
        self->response->status = Response::Successful;
        assert(self->request);
        self->request->notify(std::move(self->response), FileCache::Hint::No);
        delete self->request;
    }

    uv_fs_req_cleanup(req);
    uv_fs_close(req->loop, req, self->fd, fileClosed);
}

void AssetRequestImpl::fileClosed(uv_fs_t *req) {
    assert(req->data);
    auto self = reinterpret_cast<AssetRequestImpl *>(req->data);
    MBGL_VERIFY_THREAD(self->tid);
    (void(self)); // Silence unused variable error in Release mode

    if (req->result < 0) {
        // Closing the file failed. But there isn't anything we can do.
    }

    cleanup(req);
}

void AssetRequestImpl::notifyError(uv_fs_t *req) {
    assert(req->data);
    auto self = reinterpret_cast<AssetRequestImpl *>(req->data);
    MBGL_VERIFY_THREAD(self->tid);

    if (req->result < 0 && !self->canceled && req->result != UV_ECANCELED) {
        auto response = util::make_unique<Response>();
        response->status = Response::Error;
        response->message = uv::getFileRequestError(req);
        assert(self->request);
        self->request->notify(std::move(response), FileCache::Hint::No);
        delete self->request;
    }
}

void AssetRequestImpl::cleanup(uv_fs_t *req) {
    assert(req->data);
    auto self = reinterpret_cast<AssetRequestImpl *>(req->data);
    MBGL_VERIFY_THREAD(self->tid);
    uv_fs_req_cleanup(req);
    delete self;
}

// -------------------------------------------------------------------------------------------------

AssetRequest::AssetRequest(DefaultFileSource *source_, const Resource &resource_)
    : SharedRequestBase(source_, resource_) {
    assert(algo::starts_with(resource.url, "asset://"));
}

AssetRequest::~AssetRequest() {
    MBGL_VERIFY_THREAD(tid);

    if (ptr) {
        reinterpret_cast<AssetRequestImpl *>(ptr)->request = nullptr;
    }
}

void AssetRequest::start(uv_loop_t *loop, std::unique_ptr<Response> response) {
    MBGL_VERIFY_THREAD(tid);

    // We're ignoring the existing response if any.
    (void(response));

    assert(!ptr);
    ptr = new AssetRequestImpl(this, loop);
    // Note: the AssetRequestImpl deletes itself.
}

void AssetRequest::cancel() {
    MBGL_VERIFY_THREAD(tid);

    if (ptr) {
        reinterpret_cast<AssetRequestImpl *>(ptr)->canceled = true;

        // uv_cancel fails frequently when the request has already been started.
        // In that case, we have to let it complete and check the canceled bool
        // instead. The cancelation callback will delete the AssetRequest object.
        uv_cancel((uv_req_t *)&reinterpret_cast<AssetRequestImpl *>(ptr)->req);
    } else {
        // This request is canceled before we called start. We're safe to delete
        // ourselves now.
        delete this;
    }
}

}