Skip to content

Commit 15d68e5

Browse files
committed
Managed HTTP transport: support user-agent
1 parent 4381f70 commit 15d68e5

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

LibGit2Sharp/Core/ManagedHttpSmartSubtransport.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,24 @@ private bool CertificateValidationProxy(object sender, X509Certificate cert, X50
110110
return true;
111111
}
112112

113+
private string getUserAgent()
114+
{
115+
string userAgent = GlobalSettings.GetUserAgent();
116+
117+
if (string.IsNullOrEmpty(userAgent))
118+
{
119+
userAgent = "LibGit2Sharp " + GlobalSettings.Version.InformationalVersion;
120+
}
121+
122+
return userAgent;
123+
}
124+
113125
private HttpWebRequest CreateWebRequest(Uri endpointUrl, bool isPost, string contentType)
114126
{
115127
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
116128

117129
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(endpointUrl);
118-
webRequest.UserAgent = "git/1.0 (libgit2 custom transport)";
130+
webRequest.UserAgent = String.Format("git/2.0 ({0})", getUserAgent());
119131
webRequest.ServicePoint.Expect100Continue = false;
120132
webRequest.AllowAutoRedirect = false;
121133
webRequest.ServerCertificateValidationCallback += CertificateValidationProxy;

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,15 @@ internal static extern int git_libgit2_opts(int option, uint level,
809809
// git_libgit2_opts(GIT_OPT_ENABLE_*, int enabled)
810810
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
811811
internal static extern int git_libgit2_opts(int option, int enabled);
812+
813+
// git_libgit2_opts(GIT_OPT_SET_USER_AGENT, const char *path)
814+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
815+
internal static extern int git_libgit2_opts(int option,
816+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string path);
817+
818+
// git_libgit2_opts(GIT_OPT_GET_USER_AGENT, git_buf *buf)
819+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
820+
internal static extern int git_libgit2_opts(int option, GitBuf buf);
812821
#endregion
813822

814823
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]

LibGit2Sharp/Core/Proxy.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3456,6 +3456,37 @@ public static void git_libgit2_opts_set_enable_strictobjectcreation(bool enabled
34563456
Ensure.ZeroResult(res);
34573457
}
34583458

3459+
/// <summary>
3460+
/// Sets the user-agent string to be used by the HTTP(S) transport.
3461+
/// Note that "git/2.0" will be prepended for compatibility.
3462+
/// </summary>
3463+
/// <param name="userAgent">The user-agent string to use</param>
3464+
public static void git_libgit2_opts_set_user_agent(string userAgent)
3465+
{
3466+
var res = NativeMethods.git_libgit2_opts((int)LibGit2Option.SetUserAgent, userAgent);
3467+
Ensure.ZeroResult(res);
3468+
}
3469+
3470+
/// <summary>
3471+
/// Gets the user-agent string used by libgit2.
3472+
/// <returns>
3473+
/// The user-agent string.
3474+
/// </returns>
3475+
public static string git_libgit2_opts_get_user_agent()
3476+
{
3477+
string userAgent;
3478+
3479+
using (var buf = new GitBuf())
3480+
{
3481+
var res = NativeMethods.git_libgit2_opts((int)LibGit2Option.GetUserAgent, buf);
3482+
Ensure.ZeroResult(res);
3483+
3484+
userAgent = LaxUtf8Marshaler.FromNative(buf.ptr) ?? string.Empty;
3485+
}
3486+
3487+
return userAgent;
3488+
}
3489+
34593490
#endregion
34603491

34613492
#region git_worktree_

LibGit2Sharp/GlobalSettings.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,5 +386,25 @@ public static void SetEnableStrictObjectCreation(bool enabled)
386386
{
387387
Proxy.git_libgit2_opts_set_enable_strictobjectcreation(enabled);
388388
}
389+
390+
/// <summary>
391+
/// Sets the user-agent string to be used by the HTTP(S) transport.
392+
/// Note that "git/2.0" will be prepended for compatibility.
393+
/// </summary>
394+
/// <param name="userAgent">The user-agent string to use</param>
395+
public static void SetUserAgent(string userAgent)
396+
{
397+
Proxy.git_libgit2_opts_set_user_agent(userAgent);
398+
}
399+
400+
/// <summary>
401+
/// Gets the user-agent string used by libgit2.
402+
/// <returns>
403+
/// The user-agent string.
404+
/// </returns>
405+
public static string GetUserAgent()
406+
{
407+
return Proxy.git_libgit2_opts_get_user_agent();
408+
}
389409
}
390410
}

0 commit comments

Comments
 (0)