summaryrefslogtreecommitdiff
path: root/platform/darwin/http_request_nsurl.mm
blob: 638b5062d4a0c96acf7264e57e1d9acfd3831bae (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <mbgl/storage/default/http_request.hpp>
#include <mbgl/storage/default/http_context.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/uv.hpp>

#include <mbgl/util/time.hpp>
#include <mbgl/util/parsedate.h>

#import <Foundation/Foundation.h>

#include <map>
#include <cassert>

namespace mbgl {

enum class ResponseStatus : uint8_t {
    // This error probably won't be resolved by retrying anytime soon. We are giving up.
    PermanentError,

    // This error might be resolved by waiting some time (e.g. server issues).
    // We are going to do an exponential back-off and will try again in a few seconds.
    TemporaryError,

    // This error was caused by a temporary error and it is likely that it will be resolved
    // immediately. We are going to try again right away. This is like the TemporaryError, except
    // that we will not perform exponential back-off.
    SingularError,

    // This error might be resolved once the network reachability status changes.
    // We are going to watch the network status for changes and will retry as soon as the
    // operating system notifies us of a network status change.
    ConnectionError,

    // The request was canceled mid-way.
    Canceled,

    // The request returned data successfully. We retrieved and decoded the data successfully.
    Successful,

    // The request confirmed that the data wasn't changed. We already have the data.
    NotModified,
};

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

class HTTPNSURLContext;

class HTTPRequestImpl {
public:
    HTTPRequestImpl(HTTPRequest *request, uv_loop_t *loop, std::unique_ptr<Response> response);
    ~HTTPRequestImpl();

    void cancel();
    void cancelTimer();

    void start();
    void handleResult(NSData *data, NSURLResponse *res, NSError *error);
    void handleResponse();

    void retry(uint64_t timeout);
    void retryImmediately();
    static void restart(uv_timer_t *timer, int);

private:
    HTTPNSURLContext *context = nullptr;
    HTTPRequest *request = nullptr;
    NSURLSessionDataTask *task = nullptr;
    std::unique_ptr<Response> response;
    std::unique_ptr<Response> existingResponse;
    ResponseStatus status = ResponseStatus::PermanentError;
    uv_async_t *async = nullptr;
    int attempts = 0;
    uv_timer_t *timer = nullptr;
    enum : bool { PreemptImmediately, ExponentialBackoff } strategy = PreemptImmediately;

    static const int maxAttempts = 4;
};

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

class HTTPNSURLContext : public HTTPContext<HTTPNSURLContext> {
public:
    HTTPNSURLContext(uv_loop_t *loop);
    ~HTTPNSURLContext();

    NSURLSession *session = nil;
    NSString *userAgent = nil;
};

template<> pthread_key_t ThreadContext<HTTPNSURLContext>::key{};
template<> pthread_once_t ThreadContext<HTTPNSURLContext>::once = PTHREAD_ONCE_INIT;

HTTPNSURLContext::HTTPNSURLContext(uv_loop_t *loop_) : HTTPContext(loop_) {
    @autoreleasepool {
        NSURLSessionConfiguration *sessionConfig =
                [NSURLSessionConfiguration defaultSessionConfiguration];
        sessionConfig.timeoutIntervalForResource = 30;
        sessionConfig.HTTPMaximumConnectionsPerHost = 8;
        sessionConfig.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
        sessionConfig.URLCache = nil;

        session = [NSURLSession sessionWithConfiguration:sessionConfig];
        [session retain];

        // Write user agent string
        userAgent = @"MapboxGL";
    }
}

HTTPNSURLContext::~HTTPNSURLContext() {
    [session release];
    session = nullptr;

    [userAgent release];
    userAgent = nullptr;
}

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

HTTPRequestImpl::HTTPRequestImpl(HTTPRequest *request_, uv_loop_t *loop,
                                 std::unique_ptr<Response> existingResponse_)
    : context(HTTPNSURLContext::Get(loop)),
      request(request_),
      existingResponse(std::move(existingResponse_)),
      async(new uv_async_t) {
    assert(request);
    context->addRequest(request);

    async->data = this;
    uv_async_init(loop, async, [](uv_async_t *as, int) {
        auto impl = reinterpret_cast<HTTPRequestImpl *>(as->data);
        impl->handleResponse();
    });

    start();
}

void HTTPRequestImpl::start() {
    assert(!task);

    attempts++;

    @autoreleasepool {
        NSMutableURLRequest *req = [[NSMutableURLRequest alloc]
            initWithURL:[NSURL URLWithString:@(request->resource.url.c_str())]];
        if (existingResponse) {
            if (!existingResponse->etag.empty()) {
                [req addValue:@(existingResponse->etag.c_str()) forHTTPHeaderField:@"If-None-Match"];
            } else if (existingResponse->modified) {
                const std::string time = util::rfc1123(existingResponse->modified);
                [req addValue:@(time.c_str()) forHTTPHeaderField:@"If-Modified-Since"];
            }
        }

        [req addValue:context->userAgent forHTTPHeaderField:@"User-Agent"];

        task = [context->session dataTaskWithRequest:req
                          completionHandler:^(NSData *data, NSURLResponse *res,
                                              NSError *error) { handleResult(data, res, error); }];
        [req release];
        [task retain];
        [task resume];
    }
}

void HTTPRequestImpl::handleResponse() {
    if (task) {
        [task release];
        task = nullptr;
    }

    if (request) {
        if (status == ResponseStatus::TemporaryError && attempts < maxAttempts) {
            strategy = ExponentialBackoff;
            return retry((1 << (attempts - 1)) * 1000);
        } else if (status == ResponseStatus::ConnectionError && attempts < maxAttempts) {
            // By default, we will retry every 30 seconds (network change notification will
            // preempt the timeout).
            strategy = PreemptImmediately;
            return retry(30000);
        }

        // Actually return the response.
        if (status == ResponseStatus::NotModified) {
            request->notify(std::move(response), FileCache::Hint::Refresh);
        } else {
            request->notify(std::move(response), FileCache::Hint::Full);
        }

        context->removeRequest(request);
        request->ptr = nullptr;
        delete request;
        request = nullptr;
    }

    delete this;
}

void HTTPRequestImpl::cancel() {
    context->removeRequest(request);
    request = nullptr;

    // Stop the backoff timer to avoid re-triggering this request.
    cancelTimer();

    if (task) {
        [task cancel];
        [task release];
        task = nullptr;
    } else {
        delete this;
    }
}

void HTTPRequestImpl::cancelTimer() {
    if (timer) {
        uv_timer_stop(timer);
        uv::close(timer);
        timer = nullptr;
    }
}

HTTPRequestImpl::~HTTPRequestImpl() {
    assert(!task);
    assert(async);

    // Stop the backoff timer to avoid re-triggering this request.
    cancelTimer();

    uv::close(async);

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

int64_t parseCacheControl(const char *value) {
    if (value) {
        unsigned long long seconds = 0;
        // TODO: cache-control may contain other information as well:
        // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
        if (std::sscanf(value, "max-age=%llu", &seconds) == 1) {
            return std::chrono::duration_cast<std::chrono::seconds>(
                       std::chrono::system_clock::now().time_since_epoch()).count() +
                   seconds;
        }
    }

    return 0;
}

void HTTPRequestImpl::handleResult(NSData *data, NSURLResponse *res, NSError *error) {
    if (error) {
        if ([error code] == NSURLErrorCancelled) {
            status = ResponseStatus::Canceled;
        } else {
            // TODO: Use different codes for host not found, timeout, invalid URL etc.
            // These can be categorized in temporary and permanent errors.
            response = util::make_unique<Response>();
            response->status = Response::Error;
            response->message = [[error localizedDescription] UTF8String];

            switch ([error code]) {
                case NSURLErrorBadServerResponse: // 5xx errors
                    status = ResponseStatus::TemporaryError;
                    break;

                case NSURLErrorTimedOut:
                case NSURLErrorUserCancelledAuthentication:
                    status = ResponseStatus::SingularError; // retry immediately
                    break;

                case NSURLErrorNetworkConnectionLost:
                case NSURLErrorCannotFindHost:
                case NSURLErrorCannotConnectToHost:
                case NSURLErrorDNSLookupFailed:
                case NSURLErrorNotConnectedToInternet:
                case NSURLErrorInternationalRoamingOff:
                case NSURLErrorCallIsActive:
                case NSURLErrorDataNotAllowed:
                    status = ResponseStatus::ConnectionError;
                    break;

                default:
                    status = ResponseStatus::PermanentError;
            }
        }
    } else if ([res isKindOfClass:[NSHTTPURLResponse class]]) {
        const long responseCode = [(NSHTTPURLResponse *)res statusCode];

        response = util::make_unique<Response>();
        response->data = {(const char *)[data bytes], [data length]};

        NSDictionary *headers = [(NSHTTPURLResponse *)res allHeaderFields];
        NSString *cache_control = [headers objectForKey:@"Cache-Control"];
        if (cache_control) {
            response->expires = parseCacheControl([cache_control UTF8String]);
        }

        NSString *expires = [headers objectForKey:@"Expires"];
        if (expires) {
            response->expires = parse_date([expires UTF8String]);
        }

        NSString *last_modified = [headers objectForKey:@"Last-Modified"];
        if (last_modified) {
            response->modified = parse_date([last_modified UTF8String]);
        }

        NSString *etag = [headers objectForKey:@"ETag"];
        if (etag) {
            response->etag = [etag UTF8String];
        }

        if (responseCode == 304) {
            if (existingResponse) {
                // We're going to reuse the old response object, but need to copy over the new
                // expires value (if possible).
                std::swap(response, existingResponse);
                if (existingResponse->expires) {
                    response->expires = existingResponse->expires;
                }
                status = ResponseStatus::NotModified;
            } else {
                // This is an unsolicited 304 response and should only happen on malfunctioning
                // HTTP servers. It likely doesn't include any data, but we don't have much options.
                response->status = Response::Successful;
                status = ResponseStatus::Successful;
            }
        } else if (responseCode == 200) {
            response->status = Response::Successful;
            status = ResponseStatus::Successful;
        } else if (responseCode >= 500 && responseCode < 600) {
            // Server errors may be temporary, so back off exponentially.
            response->status = Response::Error;
            response->message = "HTTP status code " + std::to_string(responseCode);
            status = ResponseStatus::TemporaryError;
        } else {
            // We don't know how to handle any other errors, so declare them as permanently failing.
            response->status = Response::Error;
            response->message = "HTTP status code " + std::to_string(responseCode);
            status = ResponseStatus::PermanentError;
        }
    } else {
        // This should never happen.
        status = ResponseStatus::PermanentError;
        response = util::make_unique<Response>();
        response->status = Response::Error;
        response->message = "response class is not NSHTTPURLResponse";
    }

    uv_async_send(async);
}

void HTTPRequestImpl::retry(uint64_t timeout) {
    response.reset();

    assert(!timer);
    timer = new uv_timer_t;
    timer->data = this;
    uv_timer_init(async->loop, timer);
    uv_timer_start(timer, restart, timeout, 0);
}

void HTTPRequestImpl::retryImmediately() {
    // All batons get notified when the network status changed, but some of them
    // might not actually wait for the network to become available again.
    if (timer && strategy == PreemptImmediately) {
        // Triggers the timer upon the next event loop iteration.
        uv_timer_stop(timer);
        uv_timer_start(timer, restart, 0, 0);
    }
}

void HTTPRequestImpl::restart(uv_timer_t *timer, int) {
    // Restart the request.
    auto impl = reinterpret_cast<HTTPRequestImpl *>(timer->data);

    // Get rid of the timer.
    impl->timer = nullptr;
    uv::close(timer);

    impl->start();
}

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

HTTPRequest::HTTPRequest(DefaultFileSource *source, const Resource &resource)
    : SharedRequestBase(source, resource) {
}

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

    if (ptr) {
        reinterpret_cast<HTTPRequestImpl *>(ptr)->cancel();
    }
}

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

    assert(!ptr);
    ptr = new HTTPRequestImpl(this, loop, std::move(response));
}

void HTTPRequest::retryImmediately() {
    MBGL_VERIFY_THREAD(tid);

    if (ptr) {
        reinterpret_cast<HTTPRequestImpl *>(ptr)->retryImmediately();
    }
}

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

    delete this;
}

}