Skip to content

Commit 1c7cf94

Browse files
authored
common, server : use the same User-Agent by default (ggml-org#18957)
This commit also ensures that if a custom User-Agent is used, it will be the only one sent. Signed-off-by: Adrien Gallouët <angt@huggingface.co>
1 parent 2c1f199 commit 1c7cf94

4 files changed

Lines changed: 21 additions & 17 deletions

File tree

common/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ extern const char * LLAMA_COMMIT;
5757
extern const char * LLAMA_COMPILER;
5858
extern const char * LLAMA_BUILD_TARGET;
5959

60+
const static std::string build_info("b" + std::to_string(LLAMA_BUILD_NUMBER) + "-" + LLAMA_COMMIT);
61+
6062
struct common_control_vector_load_info;
6163

6264
//

common/download.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -314,23 +314,26 @@ static bool common_pull_file(httplib::Client & cli,
314314

315315
// download one single file from remote URL to local path
316316
// returns status code or -1 on error
317-
static int common_download_file_single_online(const std::string & url,
318-
const std::string & path,
319-
const std::string & bearer_token,
320-
const common_header_list & custom_headers) {
317+
static int common_download_file_single_online(const std::string & url,
318+
const std::string & path,
319+
const std::string & bearer_token,
320+
const common_header_list & custom_headers) {
321321
static const int max_attempts = 3;
322322
static const int retry_delay_seconds = 2;
323323

324324
auto [cli, parts] = common_http_client(url);
325325

326-
httplib::Headers default_headers = {{"User-Agent", "llama-cpp"}};
327-
if (!bearer_token.empty()) {
328-
default_headers.insert({"Authorization", "Bearer " + bearer_token});
329-
}
326+
httplib::Headers headers;
330327
for (const auto & h : custom_headers) {
331-
default_headers.emplace(h.first, h.second);
328+
headers.emplace(h.first, h.second);
332329
}
333-
cli.set_default_headers(default_headers);
330+
if (headers.find("User-Agent") == headers.end()) {
331+
headers.emplace("User-Agent", "llama-cpp/" + build_info);
332+
}
333+
if (!bearer_token.empty()) {
334+
headers.emplace("Authorization", "Bearer " + bearer_token);
335+
}
336+
cli.set_default_headers(headers);
334337

335338
const bool file_exists = std::filesystem::exists(path);
336339

@@ -437,10 +440,12 @@ std::pair<long, std::vector<char>> common_remote_get_content(const std::string
437440
const common_remote_params & params) {
438441
auto [cli, parts] = common_http_client(url);
439442

440-
httplib::Headers headers = {{"User-Agent", "llama-cpp"}};
441-
442-
for (const auto & header : params.headers) {
443-
headers.emplace(header.first, header.second);
443+
httplib::Headers headers;
444+
for (const auto & h : params.headers) {
445+
headers.emplace(h.first, h.second);
446+
}
447+
if (headers.find("User-Agent") == headers.end()) {
448+
headers.emplace("User-Agent", "llama-cpp/" + build_info);
444449
}
445450

446451
if (params.timeout > 0) {

tools/server/server-common.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,6 @@ static void handle_media(
779779
// download remote image
780780
// TODO @ngxson : maybe make these params configurable
781781
common_remote_params params;
782-
params.headers.push_back({"User-Agent", "llama.cpp/" + build_info});
783782
params.max_size = 1024 * 1024 * 10; // 10MB
784783
params.timeout = 10; // seconds
785784
SRV_INF("downloading image from '%s'\n", url.c_str());

tools/server/server-common.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#include <vector>
1414
#include <cinttypes>
1515

16-
const static std::string build_info("b" + std::to_string(LLAMA_BUILD_NUMBER) + "-" + LLAMA_COMMIT);
17-
1816
using json = nlohmann::ordered_json;
1917

2018
#define SLT_INF(slot, fmt, ...) LOG_INF("slot %12.*s: id %2d | task %d | " fmt, 12, __func__, (slot).id, ((slot).task ? (slot).task->id : -1), __VA_ARGS__)

0 commit comments

Comments
 (0)