|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Net; |
| 3 | +using Devlooped.Http; |
| 4 | + |
| 5 | +namespace Devlooped; |
| 6 | + |
| 7 | +public abstract class DownloadProvider |
| 8 | +{ |
| 9 | + public static DownloadProvider Create(RemoteRef location) => (location.Host?.ToLowerInvariant() ?? "github.com") switch |
| 10 | + { |
| 11 | + "gitlab.com" => new GitLabDownloadProvider(), |
| 12 | + "dev.azure.com" => new AzureDevOpsDownloadProvider(), |
| 13 | + //"bitbucket.org" => new BitbucketDownloadProvider(), |
| 14 | + "gist.github.com" => new GitHubDownloadProvider(gist: true), |
| 15 | + _ => new GitHubDownloadProvider(), |
| 16 | + }; |
| 17 | + |
| 18 | + public abstract Task<HttpResponseMessage> GetAsync(RemoteRef location); |
| 19 | +} |
| 20 | + |
| 21 | +public class GitHubDownloadProvider(bool gist = false) : DownloadProvider |
| 22 | +{ |
| 23 | + static readonly HttpClient http = new(new GitHubAuthHandler( |
| 24 | + new RedirectingHttpHandler( |
| 25 | + new HttpClientHandler |
| 26 | + { |
| 27 | + AllowAutoRedirect = false, |
| 28 | + AutomaticDecompression = DecompressionMethods.Brotli | DecompressionMethods.GZip |
| 29 | + }))) |
| 30 | + { |
| 31 | + Timeout = Debugger.IsAttached ? Timeout.InfiniteTimeSpan : TimeSpan.FromSeconds(15) |
| 32 | + }; |
| 33 | + |
| 34 | + public override async Task<HttpResponseMessage> GetAsync(RemoteRef location) |
| 35 | + { |
| 36 | + if (location.ResolvedUri != null) |
| 37 | + { |
| 38 | + return await http.SendAsync( |
| 39 | + new HttpRequestMessage(HttpMethod.Get, location.ResolvedUri).WithTag(location.ETag), |
| 40 | + HttpCompletionOption.ResponseHeadersRead); |
| 41 | + } |
| 42 | + |
| 43 | + var subdomain = gist ? "gist." : ""; |
| 44 | + var request = new HttpRequestMessage(HttpMethod.Get, |
| 45 | + // Direct archive link works for branch, tag, sha |
| 46 | + new Uri($"https://{subdomain}github.com/{location.Owner}/{location.Repo}/archive/{location.Ref ?? "main"}.zip")) |
| 47 | + .WithTag(location.ETag); |
| 48 | + |
| 49 | + return await http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +public class GitLabDownloadProvider : DownloadProvider |
| 54 | +{ |
| 55 | + static readonly HttpClient http = new(new GitLabAuthHandler( |
| 56 | + new RedirectingHttpHandler( |
| 57 | + new HttpClientHandler |
| 58 | + { |
| 59 | + AllowAutoRedirect = false, |
| 60 | + AutomaticDecompression = DecompressionMethods.Brotli | DecompressionMethods.GZip, |
| 61 | + }))) |
| 62 | + { |
| 63 | + Timeout = Debugger.IsAttached ? Timeout.InfiniteTimeSpan : TimeSpan.FromSeconds(15) |
| 64 | + }; |
| 65 | + |
| 66 | + public override async Task<HttpResponseMessage> GetAsync(RemoteRef location) |
| 67 | + { |
| 68 | + if (location.ResolvedUri != null) |
| 69 | + { |
| 70 | + return await http.SendAsync( |
| 71 | + new HttpRequestMessage(HttpMethod.Get, location.ResolvedUri).WithTag(location.ETag), |
| 72 | + HttpCompletionOption.ResponseHeadersRead); |
| 73 | + } |
| 74 | + |
| 75 | + var url = $"https://gitlab.com/api/v4/projects/{Uri.EscapeDataString(location.Owner + "/" + location.Repo)}/repository/archive.zip?sha={location.Ref ?? "main"}"; |
| 76 | + var request = new HttpRequestMessage(HttpMethod.Get, new Uri(url)).WithTag(location.ETag); |
| 77 | + var response = await http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); |
| 78 | + |
| 79 | + return response; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +public class AzureDevOpsDownloadProvider : DownloadProvider |
| 84 | +{ |
| 85 | + static readonly HttpClient http = new(new AzureRepoAuthHandler( |
| 86 | + new RedirectingHttpHandler( |
| 87 | + new HttpClientHandler |
| 88 | + { |
| 89 | + AllowAutoRedirect = false, |
| 90 | + AutomaticDecompression = DecompressionMethods.Brotli | DecompressionMethods.GZip, |
| 91 | + }, "visualstudio.com"))) |
| 92 | + { |
| 93 | + Timeout = Debugger.IsAttached ? Timeout.InfiniteTimeSpan : TimeSpan.FromSeconds(15) |
| 94 | + }; |
| 95 | + |
| 96 | + static AzureDevOpsDownloadProvider() |
| 97 | + { |
| 98 | + http.DefaultRequestHeaders.TryAddWithoutValidation("X-TFS-FedAuthRedirect", "Suppress"); |
| 99 | + } |
| 100 | + |
| 101 | + public override async Task<HttpResponseMessage> GetAsync(RemoteRef location) |
| 102 | + { |
| 103 | + if (location.ResolvedUri != null) |
| 104 | + { |
| 105 | + return await http.SendAsync( |
| 106 | + new HttpRequestMessage(HttpMethod.Get, location.ResolvedUri).WithTag(location.ETag), |
| 107 | + HttpCompletionOption.ResponseHeadersRead); |
| 108 | + } |
| 109 | + |
| 110 | + // For Azure DevOps we support dev.azure.com/org/project/repo, defaulting project=repo if not specified |
| 111 | + var project = location.Project ?? location.Repo; |
| 112 | + |
| 113 | + // Branch/ref support |
| 114 | + var version = location.Ref ?? "main"; |
| 115 | + var url = $"https://dev.azure.com/{location.Owner}/{project}/_apis/git/repositories/{location.Repo}/items?download=true&version={Uri.EscapeDataString(version)}&$format=zip&api-version=7.1"; |
| 116 | + var request = new HttpRequestMessage(HttpMethod.Get, new Uri(url)).WithTag(location.ETag); |
| 117 | + var response = await http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); |
| 118 | + |
| 119 | + if (response.StatusCode == HttpStatusCode.NotFound) |
| 120 | + { |
| 121 | + // try tag & commit |
| 122 | + url = $"https://dev.azure.com/{location.Owner}/{project}/_apis/git/repositories/{location.Repo}/items?download=true&version={Uri.EscapeDataString(version)}&versionType=tag&$format=zip&api-version=7.1"; |
| 123 | + request = new HttpRequestMessage(HttpMethod.Get, new Uri(url)).WithTag(location.ETag); |
| 124 | + response = await http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); |
| 125 | + |
| 126 | + if (response.StatusCode == HttpStatusCode.NotFound) |
| 127 | + { |
| 128 | + url = $"https://dev.azure.com/{location.Owner}/{project}/_apis/git/repositories/{location.Repo}/items?download=true&version={Uri.EscapeDataString(version)}&versionType=commit&$format=zip&api-version=7.1"; |
| 129 | + request = new HttpRequestMessage(HttpMethod.Get, new Uri(url)).WithTag(location.ETag); |
| 130 | + response = await http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return response; |
| 135 | + } |
| 136 | +} |
0 commit comments