Skip to content

Commit 3101db8

Browse files
committed
Managed HTTP transport: support user-agent
1 parent 5c5619d commit 3101db8

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
@@ -667,6 +667,15 @@ internal static extern int git_libgit2_opts(int option, uint level,
667667
// git_libgit2_opts(GIT_OPT_ENABLE_*, int enabled)
668668
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
669669
internal static extern int git_libgit2_opts(int option, int enabled);
670+
671+
// git_libgit2_opts(GIT_OPT_SET_USER_AGENT, const char *path)
672+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
673+
internal static extern int git_libgit2_opts(int option,
674+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string path);
675+
676+
// git_libgit2_opts(GIT_OPT_GET_USER_AGENT, git_buf *buf)
677+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
678+
internal static extern int git_libgit2_opts(int option, GitBuf buf);
670679
#endregion
671680

672681
[DllImport(libgit2)]

LibGit2Sharp/Core/Proxy.cs

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

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

34533484
private static ICollection<TResult> git_foreach<T, TResult>(

LibGit2Sharp/GlobalSettings.cs

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

0 commit comments

Comments
 (0)