Repository: ggerganov/llama.cpp
File: common/arg.cpp (C++)

Code before:
            std::ifstream metadata_in(metadata_path);
            if (metadata_in.good()) {
                try {
                    metadata_in >> metadata;
                    LOG_DBG("%s: previous metadata file found %s: %s\n", __func__, metadata_path.c_str(),
                            metadata.dump().c_str());
                    if (metadata.contains("etag") && metadata.at("etag").is_string()) {
                        etag = metadata.at("etag");
                    }
                    if (metadata.contains("lastModified") && metadata.at("lastModified").is_string()) {
                        last_modified = metadata.at("lastModified");
                    }
                } catch (const nlohmann::json::exception & e) {
                    LOG_ERR("%s: error reading metadata file %s: %s\n", __func__, metadata_path.c_str(), e.what());
                }
            }
            // if we cannot open the metadata file, we assume that the downloaded file is not valid (etag and last-modified are left empty, so we will download it again)
        } else {
            if (offline) {
                LOG_ERR("%s: required file is not available in cache (offline mode): %s\n", __func__, path.c_str());
                return false;
            }
            LOG_INF("%s: no previous model file found %s\n", __func__, path.c_str());
        }

        bool                               head_request_ok = false;
        bool should_download = !file_exists;  // by default, we should download if the file does not exist

        // Initialize libcurl
        curl_ptr       curl(curl_easy_init(), &curl_easy_cleanup);
        common_load_model_from_url_headers headers;
        curl_easy_setopt(curl.get(), CURLOPT_HEADERDATA, &headers);
        curl_slist_ptr http_headers;
        const bool     was_perform_successful = common_download_head(curl.get(), http_headers, url, bearer_token);
        if (!was_perform_successful) {
            head_request_ok = false;
        }

        long http_code = 0;
        curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &http_code);
        if (http_code == 200) {
            head_request_ok = true;
        } else {
            LOG_WRN("%s: HEAD invalid http status code received: %ld\n", __func__, http_code);
            head_request_ok = false;
        }

        // if head_request_ok is false, we don't have the etag or last-modified headers
        // we leave should_download as-is, which is true if the file does not exist
        bool should_download_from_scratch = false;
        if (head_request_ok) {

Review comment (suggestion):
```suggestion
        bool head_request_ok = false;
```

Code after:
    std::string metadata_path = path + ".json";
    static const int max_attempts        = 3;
    static const int retry_delay_seconds = 2;
    for (int i = 0; i < max_attempts; ++i) {
        nlohmann::json metadata;  // TODO @ngxson : get rid of this json, use regex instead
        std::string    etag;
        std::string    last_modified;

        // Check if the file already exists locally
        const auto file_exists = std::filesystem::exists(path);
        if (file_exists) {
            if (offline) {
                LOG_INF("%s: using cached file (offline mode): %s\n", __func__, path.c_str());
                return true;  // skip verification/downloading
            }
            // Try and read the JSON metadata file (note: stream autoclosed upon exiting this block).
            std::ifstream metadata_in(metadata_path);
            if (metadata_in.good()) {
                try {
                    metadata_in >> metadata;
                    LOG_DBG("%s: previous metadata file found %s: %s\n", __func__, metadata_path.c_str(),
                            metadata.dump().c_str());
                    if (metadata.contains("etag") && metadata.at("etag").is_string()) {
                        etag = metadata.at("etag");
                    }
                    if (metadata.contains("lastModified") && metadata.at("lastModified").is_string()) {
                        last_modified = metadata.at("lastModified");
                    }
                } catch (const nlohmann::json::exception & e) {
                    LOG_ERR("%s: error reading metadata file %s: %s\n", __func__, metadata_path.c_str(), e.what());
                }
            }
            // if we cannot open the metadata file, we assume that the downloaded file is not valid (etag and last-modified are left empty, so we will download it again)
        } else {
            if (offline) {
                LOG_ERR("%s: required file is not available in cache (offline mode): %s\n", __func__, path.c_str());
                return false;
            }
            LOG_INF("%s: no previous model file found %s\n", __func__, path.c_str());
        }

        bool head_request_ok = false;
        bool should_download = !file_exists;  // by default, we should download if the file does not exist

        // Initialize libcurl
        curl_ptr curl(curl_easy_init(), &curl_easy_cleanup);
        common_load_model_from_url_headers headers;
        curl_easy_setopt(curl.get(), CURLOPT_HEADERDATA, &headers);
        curl_slist_ptr http_headers;
        const bool     was_perform_successful = common_download_head(curl.get(), http_headers, url, bearer_token);
        if (!was_perform_successful) {