Skip to content

Commit

Permalink
Merge pull request #1052 from KSP-CKAN/resolve_certs
Browse files Browse the repository at this point in the history
More robust cURL CA bundle file resolution
  • Loading branch information
pjf committed Jun 21, 2015
2 parents 48fc2a4 + 2d8ef35 commit d655652
Showing 1 changed file with 41 additions and 15 deletions.
56 changes: 41 additions & 15 deletions Netkan/Web.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using CurlSharp;
using log4net;
Expand All @@ -22,22 +24,12 @@ public Web()
CurlSharp.Curl.GlobalInit(CurlInitFlag.All);
init_complete = true;
}
var curl_ca_bundle_crt = "./curl-ca-bundle.crt";
if (File.Exists(curl_ca_bundle_crt))
{
easy = new CurlEasy
{
UserAgent = Net.UserAgentString,
CaInfo = curl_ca_bundle_crt
};
}
else

easy = new CurlEasy
{
easy = new CurlEasy
{
UserAgent = Net.UserAgentString,
};
}
UserAgent = Net.UserAgentString,
CaInfo = ResolveCurlCaBundle()
};
}

/// <summary>
Expand Down Expand Up @@ -82,6 +74,40 @@ public void Dispose()
CurlSharp.Curl.GlobalCleanup();
easy.Dispose();
}

/// <summary>
/// Resolves the location of the cURL CA bundle file to use.
/// </summary>
/// <returns>The absolute file path to the bundle file or null if none is found.</returns>
private static string ResolveCurlCaBundle()
{
const string caBundleFileName = "curl-ca-bundle.crt";
const string ckanSubDirectoryName = "CKAN";

string bundle = new[]
{
// Working Directory
Environment.CurrentDirectory,

// Executable Directory
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),

// %LOCALAPPDATA%/CKAN
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ckanSubDirectoryName),

// %APPDATA%/CKAN
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ckanSubDirectoryName),

// %PROGRAMDATA%/CKAN
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), ckanSubDirectoryName),
}
.Select(i => Path.Combine(i, caBundleFileName))
.FirstOrDefault(File.Exists);

log.InfoFormat("Using curl-ca bundle: {0}",bundle ?? "(none)");

return bundle;
}
}
}

0 comments on commit d655652

Please sign in to comment.