Skip to content

Commit

Permalink
Sets the user agent for HTTP aliases, #1289
Browse files Browse the repository at this point in the history
  • Loading branch information
daveaglick authored and devlead committed Oct 17, 2016
1 parent 1c3b754 commit 2f0bf88
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/Cake.Common/Net/HttpAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public static void DownloadFile(this ICakeContext context, Uri address, FilePath
// We track the last posted value since the event seems to fire many times for the same value.
var percentComplete = 0;

using (var http = new HttpClient())
using (var http = GetHttpClient(context))
{
if (!string.IsNullOrWhiteSpace(settings.Username) && !string.IsNullOrWhiteSpace(settings.Password))
{
Expand Down Expand Up @@ -252,7 +252,7 @@ public static void UploadFile(this ICakeContext context, Uri address, FilePath f
}

context.Log.Verbose("Uploading file: {0}", address);
using (var client = new HttpClient())
using (var client = GetHttpClient(context))
{
client.UploadFileAsync(address, filePath.FullPath).Wait();
}
Expand Down Expand Up @@ -309,7 +309,7 @@ public static void UploadFile(this ICakeContext context, Uri address, byte[] dat
}

context.Log.Verbose("Uploading file: {0}", address);
using (var client = new HttpClient())
using (var client = GetHttpClient(context))
{
client.UploadFileAsync(address, data, fileName).Wait();
}
Expand All @@ -335,5 +335,18 @@ public static void UploadFile(this ICakeContext context, string address, byte[]
{
UploadFile(context, new Uri(address), data, fileName);
}

/// <summary>
/// Gets an <see cref="HttpClient"/> pre-populated with the correct default headers such as User-Agent.
/// The returned client should be disposed of by the caller.
/// </summary>
/// <param name="context">The current Cake context.</param>
/// <returns>A <see cref="HttpClient"/> instance.</returns>
private static HttpClient GetHttpClient(ICakeContext context)
{
var client = new HttpClient();
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Cake", context.Environment.Runtime.CakeVersion.ToString()));
return client;
}
}
}

0 comments on commit 2f0bf88

Please sign in to comment.