Skip to content

Commit

Permalink
fixed download problems for some URLs - bumped version to 0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
donid committed Jul 27, 2021
1 parent 519973c commit d50d48e
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/WingetRepoBrowser/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 7 additions & 26 deletions src/WingetRepoBrowser/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,34 +324,15 @@ private static List<NewDownload> FindNewDownloads(YamlFileHelper yamlFileHelper,
string idFileFolder = Path.GetDirectoryName(idFilePath);
string versionFolder = Path.Combine(idFileFolder, ConvertVersionToDirectoryName(manifestPackage.Version)); // illegal chars in version shouldn't be a problem, because yaml files are stored in folders with version as name
bool exists = versionsToIgnoreDownload.Any(v => v == manifestPackage.Version) || Directory.Exists(versionFolder);
//if (manifestPackage.Version == "latest" && exists)
if (false)//TODO: the following code crashes when downloaded yaml-files are not version 1.0.0
if (!exists)
{
string downloadedYamlFilePath = Path.Combine(versionFolder, "latest.yaml");
ManifestPackage_1_0_0 downloadedManifestPackage = yamlFileHelper.ReadYamlFile(downloadedYamlFilePath).Manifest;
//TODO test all installers when winget supports multiple installers
if (manifestPackage.Installers[0].Sha256 != downloadedManifestPackage.Installers[0].InstallerSha256)
NewDownload dl = new NewDownload()
{
FileInfo fi = new FileInfo(downloadedYamlFilePath);
string versionSuffix = fi.LastWriteTime.ToString("_yyyy-MM-dd");
downloadedManifestPackage.PackageVersion += versionSuffix;
yamlFileHelper.WriteYamlFile(downloadedYamlFilePath, downloadedManifestPackage);
Directory.Move(versionFolder, versionFolder + versionSuffix);
exists = Directory.Exists(versionFolder);
}
}
if (manifestPackage.Version != "latest")//TODO!!!!
{
if (!exists)
{
NewDownload dl = new NewDownload()
{
MultiFileYaml = manifestPackage.MultiFileYaml,
VersionFolder = versionFolder,
IdFilePath = idFilePath
};
result.Add(dl);
}
MultiFileYaml = manifestPackage.MultiFileYaml,
VersionFolder = versionFolder,
IdFilePath = idFilePath
};
result.Add(dl);
}
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/WingetRepoBrowser/ManifestPackageVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static string SafeJoin(string separator, IEnumerable<string> arr)

private string GetDebuggerDisplay()
{
return ToString();
return Id;
}


Expand Down
8 changes: 4 additions & 4 deletions src/WingetRepoBrowser/Properties/licenses.licx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DevExpress.XtraEditors.TextEdit, DevExpress.XtraEditors.v21.1, Version=21.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit, DevExpress.XtraEditors.v21.1, Version=21.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraGrid.GridControl, DevExpress.XtraGrid.v21.1, Version=21.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraLayout.LayoutControl, DevExpress.XtraLayout.v21.1, Version=21.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit, DevExpress.XtraEditors.v21.1, Version=21.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraGrid.GridControl, DevExpress.XtraGrid.v21.1, Version=21.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraLayout.LayoutControl, DevExpress.XtraLayout.v21.1, Version=21.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
DevExpress.XtraEditors.TextEdit, DevExpress.XtraEditors.v21.1, Version=21.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
16 changes: 8 additions & 8 deletions src/WingetRepoBrowser/WingetRepoBrowser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@
<ApplicationManifest>Properties\app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="DevExpress.Data.Desktop.v21.1, Version=21.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.Data.v21.1, Version=21.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.Printing.v21.1.Core, Version=21.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.Utils.v21.1, Version=21.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.XtraEditors.v21.1, Version=21.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.XtraGrid.v21.1, Version=21.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.XtraLayout.v21.1, Version=21.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.XtraPrinting.v21.1, Version=21.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.Data.Desktop.v21.1, Version=21.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.Data.v21.1, Version=21.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.Printing.v21.1.Core, Version=21.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.Utils.v21.1, Version=21.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.XtraEditors.v21.1, Version=21.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.XtraGrid.v21.1, Version=21.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.XtraLayout.v21.1, Version=21.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.XtraPrinting.v21.1, Version=21.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
Expand Down
4 changes: 3 additions & 1 deletion src/WingetRepoBrowserCore/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public static string[] GetVersionsToIgnoreDownload(string idFilePath)
return result;
}


//TODO: Retry if this occurs:
// happens regularly on a network share (due to malware scanner?)
// Error: Calculating Sha256-Hash: I/O Exception: The process cannot access the file '...\Firefox\89.0.1\x86_en-US\Firefox Setup 89.0.1.msi' because it is being used by another process.
public static CalculateFileHashResult CalculateSha256HashFromFile(string downloadFilePath)
{
using (SHA256 mySHA256 = SHA256.Create())
Expand Down
52 changes: 29 additions & 23 deletions src/WingetRepoBrowserCore/InstallerDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,36 @@ public class InstallerDownloader : IDisposable
{
// newer, but very specific: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0";
// or "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"
const string cUserAgent = "Mozilla/4.0 (Windows NT 10.0; Win64; x64)";
//const string cUserAgent = "Mozilla/4.0 (Windows NT 10.0; Win64; x64)";
public const string cUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0";

WebClient _webClient;
public InstallerDownloader()
{
_webClient = new WebClient();
_webClient = new MyWebClient();
//without this: "https://fossies.org/windows/misc/audacity-win-2.4.1.exe" throws WebException 403 forbidden
_webClient.Headers.Add("user-agent", cUserAgent);

}

public string GetFileNameFromUrl(string url, out Uri responseUri)
{
responseUri = null;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "HEAD";
//without this: "https://fossies.org/windows/misc/audacity-win-2.4.1.exe" -> WebException 403 forbidden
//req.Headers.Add("user-agent" -> System.ArgumentException: 'The 'user-agent' header must be modified using the appropriate property or method. Parameter name: name'
req.Method = "HEAD"; //without this: "https://fossies.org/windows/misc/audacity-win-2.4.1.exe" -> WebException 403 forbidden
req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; // avoid 404 for "https://dl.pstmn.io/download/version/8.9.0/win32"

// https://github.com/HandBrake/HandBrake/releases/download/1.3.2/HandBrake-1.3.2-x86_64-Win_GUI.exe
// this and other github release URIs throws 403 even with useragent, but webclient.downloadfileworks
// this and other github release URIs throws 403 even with useragent, but webclient.downloadfile works

if (req.Host.ToLower() != "sourceforge.net")//HACK!! sourceforge does not remove 'download' at the end of ResponseUri when we provide UserAgent
if (!string.Equals(req.Host, "sourceforge.net", StringComparison.OrdinalIgnoreCase))//HACK!! sourceforge does not remove 'download' at the end of ResponseUri when we provide UserAgent
{
req.UserAgent = cUserAgent;
}

WebResponse resp = null;
WebResponse response;
try
{
resp = req.GetResponse();
response = req.GetResponse();
}
catch (WebException /*ex*/)
{
Expand All @@ -51,30 +50,25 @@ public string GetFileNameFromUrl(string url, out Uri responseUri)
}
return fileNameFromUrl;
}
using (resp)

using (response)
{
responseUri = resp.ResponseUri;
responseUri = response.ResponseUri;
// Try to extract the filename from the Content-Disposition header
string contDisp = resp.Headers["Content-Disposition"];
string contDisp = response.Headers["Content-Disposition"];
string filenameFromCd = GetFilenameFromContentDisposition(contDisp);
if (filenameFromCd != null)
{
return filenameFromCd;
}

string location = resp.Headers["Location"];
string location = response.Headers["Location"];
if (location != null)
{
return Path.GetFileName(location);
}

//string fileNameFromUrl = Path.GetFileName(url);
//if (fileNameFromUrl.Contains('?') || fileNameFromUrl.Contains('=') )
//{
// return Path.GetFileName(resp.ResponseUri.ToString());
//}

string potentialFilename = Path.GetFileName(resp.ResponseUri.ToString());
string potentialFilename = Path.GetFileName(response.ResponseUri.ToString());
int foundIndex = potentialFilename.IndexOfAny(new[] { '?' });
if (foundIndex == -1)
{
Expand Down Expand Up @@ -117,8 +111,8 @@ static string GetFilenameFromContentDisposition(string contDisp)
static string GetFilenameFromContentDispositionWorkaround(string contDisp)
{
string[] parts = contDisp.Split(';');
string filenamePart=parts.Select(i => i.Trim()).FirstOrDefault(i=>i.StartsWith("filename=",StringComparison.InvariantCultureIgnoreCase));
if (filenamePart==null)
string filenamePart = parts.Select(i => i.Trim()).FirstOrDefault(i => i.StartsWith("filename=", StringComparison.InvariantCultureIgnoreCase));
if (filenamePart == null)
{
return null;
}
Expand Down Expand Up @@ -153,4 +147,16 @@ public void Dispose()
((IDisposable)_webClient).Dispose();
}
}

class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
request.UserAgent = InstallerDownloader.cUserAgent; // avoid 403 for some URLs - see InstallerDownloader
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; // avoid 404 for "https://dl.pstmn.io/download/version/8.9.0/win32"
return request;
}
}

}
2 changes: 1 addition & 1 deletion src/WingetRepoBrowserCore/WingetRepoBrowserCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="5.0.2" />
<PackageReference Include="YamlDotNet" Version="11.1.1" />
<PackageReference Include="YamlDotNet" Version="11.2.1" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions src/WingetRepoBrowserCoreTests/DownloadTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace WingetRepoBrowserCoreTests
{
[TestClass]

public class DownloadTests
{
[TestMethod]

public void TestWebRequest()
{
// https://stackoverflow.com/questions/68529103/why-does-webrequest-fail-with-404/68529810?noredirect=1#comment121112114_68529810

//string url = "https://dl.pstmn.io/download/version/8.9.0/win64"; // works
//string url = "https://dl.pstmn.io/download/version/8.9.0/win32"; //404 here, but works in FF or Chrome
string url = "https://download.filezilla-project.org/client/FileZilla_3.55.0_win32-setup.exe"; // 403
//string url = "https://sourceforge.net/projects/smplayer/files/SMPlayer/21.1.0/smplayer-21.1.0-win32.exe/download";// do not use UserAgent when trying to get filename
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "HEAD";
req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;// for postman!! avoid 404
req.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0";
WebResponse response = req.GetResponse();
}
}
}
5 changes: 4 additions & 1 deletion src/WingetRepoBrowserCoreTests/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using WingetRepoBrowserCore;
using System.Collections.Generic;
using System.IO;

using WingetRepoBrowserCore;

using WingetRepoBrowserTests;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DownloadTests.cs" />
<Compile Include="YamlTest.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand All @@ -61,13 +62,13 @@
<Version>16.10.0</Version>
</PackageReference>
<PackageReference Include="MSTest.TestAdapter">
<Version>2.2.4</Version>
<Version>2.2.5</Version>
</PackageReference>
<PackageReference Include="MSTest.TestFramework">
<Version>2.2.4</Version>
<Version>2.2.5</Version>
</PackageReference>
<PackageReference Include="YamlDotNet">
<Version>11.1.1</Version>
<Version>11.2.1</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
3 changes: 2 additions & 1 deletion src/WingetRepoBrowserCoreTests/YamlTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Collections.Generic;
using System.IO;

using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down

0 comments on commit d50d48e

Please sign in to comment.