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 committed Oct 17, 2016
1 parent e70964f commit 48e6ae5
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 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,8 +309,9 @@ 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();
}
context.Log.Verbose("File upload complete");
Expand All @@ -335,5 +336,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 48e6ae5

Please sign in to comment.