Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit db39e8e

Browse files
committed
do not create cert path when not needed
1 parent 6f57a31 commit db39e8e

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/Titanium.Web.Proxy/Network/DefaultCertificateDiskCache.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public void SaveRootCertificate(string pathOrName, string password, X509Certific
3030
/// <inheritdoc />
3131
public X509Certificate2? LoadCertificate(string subjectName, X509KeyStorageFlags storageFlags)
3232
{
33-
string path = Path.Combine(getCertificatePath(), subjectName + defaultCertificateFileExtension);
34-
return loadCertificate(path, string.Empty, storageFlags);
33+
string filePath = Path.Combine(getCertificatePath(false), subjectName + defaultCertificateFileExtension);
34+
return loadCertificate(filePath, string.Empty, storageFlags);
3535
}
3636

3737
/// <inheritdoc />
3838
public void SaveCertificate(string subjectName, X509Certificate2 certificate)
3939
{
40-
string filePath = Path.Combine(getCertificatePath(), subjectName + defaultCertificateFileExtension);
40+
string filePath = Path.Combine(getCertificatePath(true), subjectName + defaultCertificateFileExtension);
4141
byte[] exported = certificate.Export(X509ContentType.Pkcs12);
4242
File.WriteAllBytes(filePath, exported);
4343
}
@@ -46,9 +46,13 @@ public void Clear()
4646
{
4747
try
4848
{
49-
Directory.Delete(getCertificatePath(), true);
49+
string path = getCertificatePath(false);
50+
if (Directory.Exists(path))
51+
{
52+
Directory.Delete(path, true);
53+
}
5054
}
51-
catch (DirectoryNotFoundException)
55+
catch (Exception)
5256
{
5357
// do nothing
5458
}
@@ -89,14 +93,14 @@ private string getRootCertificatePath(string pathOrName)
8993
string.IsNullOrEmpty(pathOrName) ? defaultRootCertificateFileName : pathOrName);
9094
}
9195

92-
private string getCertificatePath()
96+
private string getCertificatePath(bool create)
9397
{
9498
if (certificatePath == null)
9599
{
96100
string path = getRootCertificateDirectory();
97101

98102
string certPath = Path.Combine(path, defaultCertificateDirectoryName);
99-
if (!Directory.Exists(certPath))
103+
if (create && !Directory.Exists(certPath))
100104
{
101105
Directory.CreateDirectory(certPath);
102106
}

0 commit comments

Comments
 (0)