Skip to content

Commit

Permalink
feat: custom user-agent with api client and ostree
Browse files Browse the repository at this point in the history
将玲珑版本号添加到user-agent便于服务端识别
  • Loading branch information
myml authored and dengbo11 committed Dec 4, 2024
1 parent 7e98b50 commit 7c0ae3f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 28 deletions.
1 change: 1 addition & 0 deletions external/http/include/apiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef struct apiClient_t {
int (*progress_func)(void *, curl_off_t, curl_off_t, curl_off_t, curl_off_t);
void *progress_data;
long response_code;
const char *userAgent;
list_t *apiKeys_Token;
} apiClient_t;

Expand Down
7 changes: 6 additions & 1 deletion external/http/src/apiClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
}else{
apiClient->sslConfig = NULL;
}

apiClient->userAgent = NULL;
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->data_callback_func = NULL;
Expand Down Expand Up @@ -269,6 +269,11 @@ void apiClient_invoke(apiClient_t *apiClient,
requestType);
}

if(apiClient->userAgent != NULL) {
curl_easy_setopt(handle, CURLOPT_USERAGENT,
apiClient->userAgent);
}

if(formParameters != NULL) {
if(contentType &&
findStrInStrList(contentType, "application/x-www-form-urlencoded") != NULL)
Expand Down
15 changes: 8 additions & 7 deletions libs/linglong/src/linglong/repo/client_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,29 @@
namespace linglong::repo {

ClientFactory::ClientFactory(const QString &server)
: m_server(server)
: m_server(server.toStdString())
{
}

ClientFactory::ClientFactory(const std::string &server)
: m_server(QString::fromStdString(server))
: m_server(server)
{
}

std::shared_ptr<apiClient_t> ClientFactory::createClientV2()
{
auto client = apiClient_create_with_base_path(m_server.toStdString().c_str(), nullptr, nullptr);
auto client = apiClient_create_with_base_path(m_server.c_str(), nullptr, nullptr);
client->userAgent = m_user_agent.c_str();
return std::shared_ptr<apiClient_t>(client, apiClient_free);
}

void ClientFactory::setServer(QString server)
void ClientFactory::setServer(const QString &server)
{
m_server = server;
m_server = server.toStdString();
}

void ClientFactory::setServer(std::string server)
void ClientFactory::setServer(const std::string &server)
{
m_server = QString::fromStdString(server);
m_server = server;
}
} // namespace linglong::repo
9 changes: 6 additions & 3 deletions libs/linglong/src/linglong/repo/client_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ extern "C" {
#include "api/ClientAPI.h"
}

#include "linglong/utils/configure.h"

#include <QObject>
#include <QPoint>

Expand All @@ -25,10 +27,11 @@ class ClientFactory : public QObject
ClientFactory(const std::string &server);

std::shared_ptr<apiClient_t> createClientV2();
void setServer(QString server);
void setServer(std::string server);
void setServer(const QString &server);
void setServer(const std::string &server);

private:
QString m_server;
std::string m_server;
std::string m_user_agent = "linglong/" LINGLONG_VERSION;
};
} // namespace linglong::repo
55 changes: 39 additions & 16 deletions libs/linglong/src/linglong/repo/ostree_repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,7 @@ utils::error::Result<void> OSTreeRepo::pushToRemote(const std::string &remoteRep
auto env = QProcessEnvironment::systemEnvironment();
auto client = this->m_clientFactory.createClientV2();
// apiClient会调用free释放basePath,为避免重复释放这里复制一次url
client->basePath = (char *)malloc(url.length() + 1);
strcpy(client->basePath, url.c_str());
client->basePath = strdup(client->basePath);
// 登录认证
auto envUsername = env.value("LINGLONG_USERNAME").toUtf8();
auto envPassword = env.value("LINGLONG_PASSWORD").toUtf8();
Expand Down Expand Up @@ -1124,14 +1123,26 @@ void OSTreeRepo::pull(service::PackageTask &taskContext,

g_autoptr(GError) gErr = nullptr;

std::string userAgent = "linglong/" LINGLONG_VERSION;
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&builder,
"{s@v}",
"refs",
g_variant_new_variant(g_variant_new_strv(refs.data(), -1)));
g_variant_builder_add(&builder,
"{s@v}",
"append-user-agent",
g_variant_new_variant(g_variant_new_string(userAgent.c_str())));

g_autoptr(GVariant) pull_options = g_variant_ref_sink(g_variant_builder_end(&builder));
// 这里不能使用g_main_context_push_thread_default,因为会阻塞Qt的事件循环
auto status = ostree_repo_pull(this->ostreeRepo.get(),
this->cfg.defaultRepo.c_str(),
const_cast<char **>(refs.data()), // NOLINT
OSTREE_REPO_PULL_FLAGS_NONE,
progress,
cancellable,
&gErr);
auto status = ostree_repo_pull_with_options(this->ostreeRepo.get(),
this->cfg.defaultRepo.c_str(),
pull_options,
progress,
cancellable,
&gErr);
ostree_async_progress_finish(progress);
auto shouldFallback = false;
if (status == FALSE) {
Expand All @@ -1154,13 +1165,25 @@ void OSTreeRepo::pull(service::PackageTask &taskContext,
refs[0] = refString.c_str();
g_clear_error(&gErr);

status = ostree_repo_pull(this->ostreeRepo.get(),
this->cfg.defaultRepo.c_str(),
const_cast<char **>(refs.data()), // NOLINT
OSTREE_REPO_PULL_FLAGS_NONE,
progress,
cancellable,
&gErr);
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&builder,
"{s@v}",
"refs",
g_variant_new_variant(g_variant_new_strv(refs.data(), -1)));
g_variant_builder_add(&builder,
"{s@v}",
"append-user-agent",
g_variant_new_variant(g_variant_new_string(userAgent.c_str())));

g_autoptr(GVariant) pull_options = g_variant_ref_sink(g_variant_builder_end(&builder));

status = ostree_repo_pull_with_options(this->ostreeRepo.get(),
this->cfg.defaultRepo.c_str(),
pull_options,
progress,
cancellable,
&gErr);
ostree_async_progress_finish(progress);
if (status == FALSE) {
taskContext.reportError(LINGLONG_ERRV("ostree_repo_pull", gErr));
Expand Down
7 changes: 6 additions & 1 deletion tools/openapi-c-libcurl-client/apiClient.c.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
}else{
apiClient->sslConfig = NULL;
}

apiClient->userAgent = NULL;
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->data_callback_func = NULL;
Expand Down Expand Up @@ -320,6 +320,11 @@ void apiClient_invoke(apiClient_t *apiClient,
requestType);
}

if(apiClient->userAgent != NULL) {
curl_easy_setopt(handle, CURLOPT_USERAGENT,
apiClient->userAgent);
}

if(formParameters != NULL) {
if(contentType &&
findStrInStrList(contentType, "application/x-www-form-urlencoded") != NULL)
Expand Down
1 change: 1 addition & 0 deletions tools/openapi-c-libcurl-client/apiClient.h.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef struct apiClient_t {
int (*progress_func)(void *, curl_off_t, curl_off_t, curl_off_t, curl_off_t);
void *progress_data;
long response_code;
char *userAgent;
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasicBasic}}
Expand Down

0 comments on commit 7c0ae3f

Please sign in to comment.