Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1411cb9

Browse files
committed
winhttp: use a custom user-agent if the user has set it
We also keep the "git/1.0" prefix in order to maintain compatibility with hosters.
1 parent 94bac76 commit 1411cb9

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/transports/winhttp.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "smart.h"
1616
#include "remote.h"
1717
#include "repository.h"
18+
#include "global.h"
1819

1920
#include <wincrypt.h>
2021
#include <winhttp.h>
@@ -567,12 +568,28 @@ static int winhttp_close_connection(winhttp_subtransport *t)
567568
return ret;
568569
}
569570

571+
static int user_agent(git_buf *ua)
572+
{
573+
const char *custom = git_libgit2__user_agent();
574+
575+
git_buf_clear(ua);
576+
git_buf_PUTS(ua, "git/1.0 (");
577+
578+
if (custom)
579+
git_buf_puts(ua, custom);
580+
else
581+
git_buf_PUTS(ua, "libgit2 " LIBGIT2_VERSION);
582+
583+
return git_buf_putc(ua, ')');
584+
}
585+
570586
static int winhttp_connect(
571587
winhttp_subtransport *t)
572588
{
573-
wchar_t *ua = L"git/1.0 (libgit2 " WIDEN(LIBGIT2_VERSION) L")";
574589
wchar_t *wide_host;
575590
int32_t port;
591+
wchar_t *wide_ua;
592+
git_buf ua = GIT_BUF_INIT;
576593
int error = -1;
577594
int default_timeout = TIMEOUT_INFINITE;
578595
int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
@@ -590,9 +607,23 @@ static int winhttp_connect(
590607
return -1;
591608
}
592609

610+
if ((error = user_agent(&ua)) < 0) {
611+
git__free(wide_host);
612+
return error;
613+
}
614+
615+
if (git__utf8_to_16_alloc(&wide_ua, git_buf_cstr(&ua)) < 0) {
616+
giterr_set(GITERR_OS, "Unable to convert host to wide characters");
617+
git__free(wide_host);
618+
git_buf_free(&ua);
619+
return -1;
620+
}
621+
622+
git_buf_free(&ua);
623+
593624
/* Establish session */
594625
t->session = WinHttpOpen(
595-
ua,
626+
wide_ua,
596627
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
597628
WINHTTP_NO_PROXY_NAME,
598629
WINHTTP_NO_PROXY_BYPASS,
@@ -628,6 +659,7 @@ static int winhttp_connect(
628659
winhttp_close_connection(t);
629660

630661
git__free(wide_host);
662+
git__free(wide_ua);
631663

632664
return error;
633665
}

0 commit comments

Comments
 (0)