Skip to content

Commit 4c09328

Browse files
committed
support max size and timeout
1 parent e6c4319 commit 4c09328

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

common/arg.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,7 @@ static bool common_download_model(
527527
return true;
528528
}
529529

530-
// get remote file content, returns <http_code, content>
531-
std::pair<long, std::vector<char>> common_remote_get_content(const std::string & url, const std::vector<std::string> & headers) {
530+
std::pair<long, std::vector<char>> common_remote_get_content(const std::string & url, const common_remote_params & params) {
532531
curl_ptr curl(curl_easy_init(), &curl_easy_cleanup);
533532
curl_slist_ptr http_headers;
534533
std::vector<char> res_buffer;
@@ -546,23 +545,29 @@ std::pair<long, std::vector<char>> common_remote_get_content(const std::string &
546545
#if defined(_WIN32)
547546
curl_easy_setopt(curl.get(), CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
548547
#endif
549-
// Important: the User-Agent must be "llama-cpp" to get the "ggufFile" field in the response
548+
if (params.timeout > 0) {
549+
curl_easy_setopt(curl.get(), CURLOPT_TIMEOUT, params.timeout);
550+
}
551+
if (params.max_size > 0) {
552+
curl_easy_setopt(curl.get(), CURLOPT_MAXFILESIZE, params.max_size);
553+
}
550554
http_headers.ptr = curl_slist_append(http_headers.ptr, "User-Agent: llama-cpp");
551-
for (const auto & header : headers) {
555+
for (const auto & header : params.headers) {
552556
http_headers.ptr = curl_slist_append(http_headers.ptr, header.c_str());
553557
}
554558
curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, http_headers.ptr);
555559

556560
CURLcode res = curl_easy_perform(curl.get());
557561

558562
if (res != CURLE_OK) {
559-
throw std::runtime_error("error: cannot make GET request to HF API");
563+
std::string error_msg = curl_easy_strerror(res);
564+
throw std::runtime_error("error: cannot make GET request : " + error_msg);
560565
}
561566

562567
long res_code;
563568
curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &res_code);
564569

565-
return { res_code, res_buffer };
570+
return { res_code, std::move(res_buffer) };
566571
}
567572

568573
/**
@@ -592,9 +597,13 @@ static struct common_hf_file_res common_get_hf_file(const std::string & hf_repo_
592597
if (!bearer_token.empty()) {
593598
headers.push_back("Authorization: Bearer " + bearer_token);
594599
}
600+
// Important: the User-Agent must be "llama-cpp" to get the "ggufFile" field in the response
601+
// User-Agent header is already set in common_remote_get_content, no need to set it here
595602

596603
// make the request
597-
auto res = common_remote_get_content(url, headers);
604+
common_remote_params params;
605+
params.headers = headers;
606+
auto res = common_remote_get_content(url, params);
598607
long res_code = res.first;
599608
std::string res_str(res.second.data(), res.second.size());
600609
std::string ggufFile;
@@ -655,7 +664,7 @@ static struct common_hf_file_res common_get_hf_file(const std::string &, const s
655664
return {};
656665
}
657666

658-
std::pair<long, std::vector<char>> common_remote_get_content(const std::string & url, const std::vector<std::string> & headers) {
667+
std::pair<long, std::vector<char>> common_remote_get_content(const std::string & url, const common_remote_params & params) {
659668
throw std::runtime_error("error: built without CURL, cannot download model from the internet");
660669
}
661670

common/arg.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,10 @@ bool common_params_parse(int argc, char ** argv, common_params & params, llama_e
7979
// function to be used by test-arg-parser
8080
common_params_context common_params_parser_init(common_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr);
8181

82-
// get remote file content, returns <http_code, content>
83-
std::pair<long, std::vector<char>> common_remote_get_content(const std::string & url, const std::vector<std::string> & headers);
82+
struct common_remote_params {
83+
std::vector<std::string> headers;
84+
long timeout = 0; // CURLOPT_TIMEOUT, in seconds ; 0 means no timeout
85+
long max_size = 0; // max size of the response ; unlimited if 0 ; max is 2GB
86+
};
87+
// get remote file content, returns <http_code, raw_response_body>
88+
std::pair<long, std::vector<char>> common_remote_get_content(const std::string & url, const common_remote_params & params);

0 commit comments

Comments
 (0)