| 359 | } |
| 360 | |
| 361 | bool shouldCacheRequest( |
| 362 | const IAssetRequest& request, |
| 363 | const std::optional<ResponseCacheControl>& cacheControl) { |
| 364 | // no response then no cache |
| 365 | const IAssetResponse* pResponse = request.response(); |
| 366 | if (!pResponse) { |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | // only cache GET method |
| 371 | if (request.method() != "GET") { |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | // check if response status code is cacheable |
| 376 | const uint16_t statusCode = pResponse->statusCode(); |
| 377 | if (statusCode != 200 && // status OK |
| 378 | statusCode != 201 && // status Created |
| 379 | statusCode != 202 && // status Accepted |
| 380 | statusCode != 203 && // status Non-Authoritive Information |
| 381 | statusCode != 204 && // status No-Content |
| 382 | statusCode != 205 && // status Reset-Content |
| 383 | statusCode != 304) // status Not-Modifed |
| 384 | { |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | const HttpHeaders& headers = pResponse->headers(); |
| 389 | HttpHeaders::const_iterator expiresHeader = headers.find("Expires"); |
| 390 | bool expiresExists = expiresHeader != headers.end(); |
| 391 | |
| 392 | // |
| 393 | // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control |
| 394 | // |
| 395 | // The no-store response directive indicates that any caches of any kind |
| 396 | // (private or shared) should not store this response. |
| 397 | if (cacheControl && cacheControl->noStore()) |
| 398 | return false; |
| 399 | |
| 400 | // |
| 401 | // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expires |
| 402 | // |
| 403 | // If there is a Cache-Control header with the max-age or s-maxage directive |
| 404 | // in the response, the Expires header is ignored |
| 405 | bool preferCacheControl = false; |
| 406 | |
| 407 | // If Cache-Control expiration is in the future, definitely cache. But we |
| 408 | // might be able to cache even if it's not. |
| 409 | if (cacheControl) { |
| 410 | if (cacheControl->maxAgeExists()) { |
| 411 | preferCacheControl = true; |
| 412 | if (cacheControl->maxAgeValue() > 0) |
| 413 | return true; |
| 414 | } else if (cacheControl->sharedMaxAgeExists()) { |
| 415 | preferCacheControl = true; |
| 416 | if (cacheControl->sharedMaxAgeValue() > 0) |
| 417 | return true; |
| 418 | } |
no test coverage detected