Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user agent for DownloadFile #1289

Closed
daveaglick opened this issue Oct 17, 2016 · 1 comment
Closed

Add user agent for DownloadFile #1289

daveaglick opened this issue Oct 17, 2016 · 1 comment
Milestone

Comments

@daveaglick
Copy link
Member

What You Are Seeing?

Exception from sites that require a user agent header when calling DownloadFile() (the GitHub API is one such server that doesn't like agentless requests).

What is Expected?

A successful file download.

What version of Cake are you using?

0.16.2

Are you running on a 32 or 64 bit system?

64

What environment are you running on? Windows? Linux? Mac?

Windows

Are you running on a CI Server? If so, which one?

No

How Did You Get This To Happen? (Steps to Reproduce)

Use the following code in a task (note that you must also have Octokit as an addin for this sample):

GitHubClient github = new GitHubClient(new ProductHeaderValue("Cake"));
Release release = github.Repository.Release.GetAll("cake-build", "cake").Result.First();
FilePath releaseZip = DownloadFile(release.ZipballUrl);
Unzip(releaseZip, releaseDir);

Output Log

Click to expand

Downloading file: https://api.github.com/repos/cake-build/cake/zipball/v0.16.2
An error occured when executing task 'GetSource'.
Error: System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The server committed a protocol violation. Section=ResponseStatusLine
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Cake.Common.Net.HttpClientExtensions.<DownloadFileAsync>d__1.MoveNext()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at Cake.Common.Net.HttpAliases.DownloadFile(ICakeContext context, Uri address, FilePath outputPath, DownloadFileSettings settings)
   at Cake.Common.Net.HttpAliases.DownloadFile(ICakeContext context, String address, FilePath outputPath)
   at Submission#0.<.ctor>b__2()
   at Cake.Core.CakeTaskBuilderExtensions.<>c__DisplayClass4_0.<Does>b__0(ICakeContext context) in E:\Code\cake\src\Cake.Core\CakeTaskBuilderExtensions.cs:line 104
   at Cake.Core.ActionTask.Execute(ICakeContext context) in E:\Code\cake\src\Cake.Core\ActionTask.cs:line 52
   at Cake.Core.DefaultExecutionStrategy.Execute(CakeTask task, ICakeContext context) in E:\Code\cake\src\Cake.Core\DefaultExecutionStrategy.cs:line 83
   at Cake.Core.CakeEngine.ExecuteTask(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report) in E:\Code\cake\src\Cake.Core\CakeEngine.cs:line 245
   at Cake.Core.CakeEngine.RunTarget(ICakeContext context, IExecutionStrategy strategy, String target) in E:\Code\cake\src\Cake.Core\CakeEngine.cs:line 152
   at Cake.Scripting.BuildScriptHost.RunTarget(String target)
   at Submission#0..ctor(Session session, Object& submissionResult)
   at Submission#0.<Factory>(Session session)
   at Roslyn.Scripting.CommonScriptEngine.Execute[T](String code, String path, DiagnosticBag diagnostics, Session session, Boolean isInteractive)
   at Roslyn.Scripting.Session.Execute(String code)
   at Cake.Scripting.Roslyn.Stable.DefaultRoslynScriptSession.Execute(Script script)
   at Cake.Core.Scripting.ScriptRunner.Run(IScriptHost host, FilePath scriptPath, IDictionary`2 arguments) in E:\Code\cake\src\Cake.Core\Scripting\ScriptRunner.cs:line 201
   at Cake.Commands.BuildCommand.Execute(CakeOptions options)
   at Cake.CakeApplication.Run(CakeOptions options)
   at Cake.Program.Main()
---> (Inner Exception #0) System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The server committed a protocol violation. Section=ResponseStatusLine
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Cake.Common.Net.HttpClientExtensions.<DownloadFileAsync>d__1.MoveNext()<---
### More Information

I suggest we set a user agent of "Cake" with the current version for every request. That should resolve the problem with endpoints that expect one, and it seems like the polite thing to do anyway. I'll submit a PR momentarily.

daveaglick added a commit to daveaglick/cake that referenced this issue Oct 17, 2016
daveaglick added a commit to daveaglick/cake that referenced this issue Oct 17, 2016
devlead pushed a commit to daveaglick/cake that referenced this issue Oct 17, 2016
@devlead devlead added this to the v0.17.0 milestone Oct 17, 2016
@devlead
Copy link
Member

devlead commented Oct 17, 2016

Fixed by #1290

@devlead devlead closed this as completed Oct 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants