Skip to content

Commit

Permalink
Some cleanup (#769)
Browse files Browse the repository at this point in the history
* Move builder to extension methods

* Remove dead code

* Fixes the GitHub Actions warning

* Removes unnecessary target net48 from tests

---------

Co-authored-by: Ersan <ersan.bozduman@gmail.com>
  • Loading branch information
martijn00 and ebozduman committed Apr 20, 2023
1 parent a53bbc3 commit b505805
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 219 deletions.
2 changes: 1 addition & 1 deletion Minio.Functional.Tests/Minio.Functional.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net48</TargetFrameworks>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
<IsPackable>False</IsPackable>
</PropertyGroup>
Expand Down
10 changes: 5 additions & 5 deletions Minio.Tests/EndpointTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void TestIfIPIsValid()
{ "260.192.1.1", false }
};

foreach (var testCase in testIPDict) Assert.AreEqual(s3utils.IsValidIP(testCase.Key), testCase.Value);
foreach (var testCase in testIPDict) Assert.AreEqual(S3utils.IsValidIP(testCase.Key), testCase.Value);
}

[TestMethod]
Expand Down Expand Up @@ -177,8 +177,8 @@ public void TestIsAmazonEndpoint()

foreach (var testCase in testAmazonDict)
{
var value = s3utils.IsAmazonEndPoint(testCase.Key);
Assert.AreEqual(s3utils.IsAmazonEndPoint(testCase.Key), testCase.Value);
var value = S3utils.IsAmazonEndPoint(testCase.Key);
Assert.AreEqual(S3utils.IsAmazonEndPoint(testCase.Key), testCase.Value);
}
}

Expand All @@ -199,8 +199,8 @@ public void TestIsAmazonChinaEndpoint()

foreach (var testCase in testAmazonDict)
{
var value = s3utils.IsAmazonChinaEndPoint(testCase.Key);
Assert.AreEqual(s3utils.IsAmazonChinaEndPoint(testCase.Key), testCase.Value);
var value = S3utils.IsAmazonChinaEndPoint(testCase.Key);
Assert.AreEqual(S3utils.IsAmazonChinaEndPoint(testCase.Key), testCase.Value);
}
}
}
24 changes: 12 additions & 12 deletions Minio.Tests/UtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,23 @@ public void TestCaseInsensitiveContains()
[TestMethod]
public void TestIsAmazonEndpoint()
{
Assert.IsTrue(s3utils.IsAmazonEndPoint("s3.amazonaws.com"));
Assert.IsTrue(s3utils.IsAmazonEndPoint("s3.cn-north-1.amazonaws.com.cn"));
Assert.IsFalse(s3utils.IsAmazonEndPoint("s3.us-west-1amazonaws.com"));
Assert.IsFalse(s3utils.IsAmazonEndPoint("play.min.io"));
Assert.IsFalse(s3utils.IsAmazonEndPoint("192.168.12.1"));
Assert.IsFalse(s3utils.IsAmazonEndPoint("storage.googleapis.com"));
Assert.IsTrue(S3utils.IsAmazonEndPoint("s3.amazonaws.com"));
Assert.IsTrue(S3utils.IsAmazonEndPoint("s3.cn-north-1.amazonaws.com.cn"));
Assert.IsFalse(S3utils.IsAmazonEndPoint("s3.us-west-1amazonaws.com"));
Assert.IsFalse(S3utils.IsAmazonEndPoint("play.min.io"));
Assert.IsFalse(S3utils.IsAmazonEndPoint("192.168.12.1"));
Assert.IsFalse(S3utils.IsAmazonEndPoint("storage.googleapis.com"));
}

[TestMethod]
public void TestIsAmazonChinaEndpoint()
{
Assert.IsFalse(s3utils.IsAmazonChinaEndPoint("s3.amazonaws.com"));
Assert.IsTrue(s3utils.IsAmazonChinaEndPoint("s3.cn-north-1.amazonaws.com.cn"));
Assert.IsFalse(s3utils.IsAmazonChinaEndPoint("s3.us-west-1amazonaws.com"));
Assert.IsFalse(s3utils.IsAmazonChinaEndPoint("play.min.io"));
Assert.IsFalse(s3utils.IsAmazonChinaEndPoint("192.168.12.1"));
Assert.IsFalse(s3utils.IsAmazonChinaEndPoint("storage.googleapis.com"));
Assert.IsFalse(S3utils.IsAmazonChinaEndPoint("s3.amazonaws.com"));
Assert.IsTrue(S3utils.IsAmazonChinaEndPoint("s3.cn-north-1.amazonaws.com.cn"));
Assert.IsFalse(S3utils.IsAmazonChinaEndPoint("s3.us-west-1amazonaws.com"));
Assert.IsFalse(S3utils.IsAmazonChinaEndPoint("play.min.io"));
Assert.IsFalse(S3utils.IsAmazonChinaEndPoint("192.168.12.1"));
Assert.IsFalse(S3utils.IsAmazonChinaEndPoint("storage.googleapis.com"));
}

[TestMethod]
Expand Down
2 changes: 1 addition & 1 deletion Minio/ApiEndpoints/BucketOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ await ExecuteTaskAsync(NoErrorHandlers, requestMessageBuilder, cancellationToken
public IObservable<MinioNotificationRaw> ListenBucketNotificationsAsync(ListenBucketNotificationsArgs args,
CancellationToken cancellationToken = default)
{
if (s3utils.IsAmazonEndPoint(BaseUrl))
if (S3utils.IsAmazonEndPoint(BaseUrl))
// Amazon AWS does not support bucket notifications
throw new ArgumentException(
"Listening for bucket notification is specific only to `minio` server endpoints");
Expand Down
24 changes: 0 additions & 24 deletions Minio/DataModel/Select/SelectObjectContentResponse.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

using System.Net;

namespace Minio;
namespace Minio.Helper;

public static class BuilderUtil
{
Expand Down
35 changes: 0 additions & 35 deletions Minio/Helper/Enum.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Minio/Helper/OperationsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace Minio;

public partial class MinioClient : IObjectOperations
public partial class MinioClient
{
/// <summary>
/// private helper method to remove list of objects from bucket
Expand Down
6 changes: 3 additions & 3 deletions Minio/Helper/RequestUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ internal static Uri GetEndpointURL(string endPoint, bool secure)
var parts = endPoint.Split(':');
var host = parts[0];
var port = parts[1];
if (!s3utils.IsValidIP(host) && !IsValidEndpoint(host))
if (!S3utils.IsValidIP(host) && !IsValidEndpoint(host))
throw new InvalidEndpointException("Endpoint: " + endPoint +
" does not follow ip address or domain name standards.");
}
else
{
if (!s3utils.IsValidIP(endPoint) && !IsValidEndpoint(endPoint))
if (!S3utils.IsValidIP(endPoint) && !IsValidEndpoint(endPoint))
throw new InvalidEndpointException("Endpoint: " + endPoint +
" does not follow ip address or domain name standards.");
}
Expand All @@ -51,7 +51,7 @@ internal static Uri GetEndpointURL(string endPoint, bool secure)
{
// For Amazon S3 endpoint, try to fetch location based endpoint.
var host = endPoint;
if (s3utils.IsAmazonEndPoint(endPoint))
if (S3utils.IsAmazonEndPoint(endPoint))
// Fetch new host based on the bucket location.
host = AWSS3Endpoints.Endpoint(region);

Expand Down
2 changes: 1 addition & 1 deletion Minio/Helper/s3utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace Minio.Helper;

internal static class s3utils
internal static class S3utils
{
internal static readonly Regex TrimWhitespaceRegex = new("\\s+");

Expand Down
121 changes: 1 addition & 120 deletions Minio/DataModel/MinioClientBuilder.cs → Minio/IMinioClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* MinIO .NET Library for Amazon S3 Compatible Cloud Storage,
* (C) 2020 MinIO, Inc.
*
Expand All @@ -15,8 +15,6 @@
* limitations under the License.
*/

using System.Net;
using Minio.Credentials;
using Minio.DataModel;
using Minio.DataModel.ILM;
using Minio.DataModel.ObjectLock;
Expand Down Expand Up @@ -122,121 +120,4 @@ public interface IMinioClient : IDisposable
Task<ObjectStat> StatObjectAsync(StatObjectArgs args, CancellationToken cancellationToken = default);
Task<HttpResponseMessage> WrapperGetAsync(string url);
Task WrapperPutAsync(string url, StreamContent strm);
}

public partial class MinioClient : IMinioClient
{
internal IWebProxy Proxy { get; private set; }

public MinioClient WithEndpoint(string endpoint)
{
BaseUrl = endpoint;
SetBaseURL(GetBaseUrl(endpoint));
return this;
}

public MinioClient WithEndpoint(string endpoint, int port)
{
if (port < 1 || port > 65535)
throw new ArgumentException(string.Format("Port {0} is not a number between 1 and 65535", port),
nameof(port));
return WithEndpoint(endpoint + ":" + port);
}

public MinioClient WithEndpoint(Uri url)
{
if (url == null) throw new ArgumentException("URL is null. Can't create endpoint.");
return WithEndpoint(url.AbsoluteUri);
}

public MinioClient WithRegion(string region)
{
if (string.IsNullOrEmpty(region))
throw new ArgumentException(string.Format("{0} the region value can't be null or empty.", region),
nameof(region));

Region = region;
return this;
}

public MinioClient WithCredentials(string accessKey, string secretKey)
{
AccessKey = accessKey;
SecretKey = secretKey;
return this;
}

public MinioClient WithSessionToken(string st)
{
SessionToken = st;
return this;
}

public MinioClient Build()
{
// Instantiate a region cache
regionCache = BucketRegionCache.Instance;
if (string.IsNullOrEmpty(BaseUrl)) throw new MinioException("Endpoint not initialized.");
if (Provider != null && Provider.GetType() != typeof(ChainedProvider) && SessionToken == null)
throw new MinioException("User Access Credentials Provider not initialized correctly.");
if (Provider == null && (string.IsNullOrEmpty(AccessKey) || string.IsNullOrEmpty(SecretKey)))
throw new MinioException("User Access Credentials not initialized.");

var host = BaseUrl;

var scheme = Secure ? Utils.UrlEncode("https") : Utils.UrlEncode("http");

if (!BaseUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase))
Endpoint = string.Format("{0}://{1}", scheme, host);
else
Endpoint = host;

HttpClient ??= Proxy is null ? new HttpClient() : new HttpClient(new HttpClientHandler { Proxy = Proxy });
HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", FullUserAgent);
return this;
}

private void SetBaseURL(Uri url)
{
if (url.IsDefaultPort)
BaseUrl = url.Host;
else
BaseUrl = url.Host + ":" + url.Port;
}

private Uri GetBaseUrl(string endpoint)
{
if (string.IsNullOrEmpty(endpoint))
throw new ArgumentException(
string.Format("{0} is the value of the endpoint. It can't be null or empty.", endpoint),
nameof(endpoint));

if (endpoint.EndsWith("/", StringComparison.OrdinalIgnoreCase))
endpoint = endpoint.Substring(0, endpoint.Length - 1);
if (!BuilderUtil.IsValidHostnameOrIPAddress(endpoint))
throw new InvalidEndpointException(string.Format("{0} is invalid hostname.", endpoint), "endpoint");
string conn_url;
if (endpoint.StartsWith("http", StringComparison.OrdinalIgnoreCase))
throw new InvalidEndpointException(
string.Format("{0} the value of the endpoint has the scheme (http/https) in it.", endpoint),
"endpoint");

var enable_https = Environment.GetEnvironmentVariable("ENABLE_HTTPS");
var scheme = enable_https?.Equals("1", StringComparison.OrdinalIgnoreCase) == true ? "https://" : "http://";
conn_url = scheme + endpoint;
var url = new Uri(conn_url);
var hostnameOfUri = url.Authority;
if (!string.IsNullOrEmpty(hostnameOfUri) && !BuilderUtil.IsValidHostnameOrIPAddress(hostnameOfUri))
throw new InvalidEndpointException(string.Format("{0}, {1} is invalid hostname.", endpoint, hostnameOfUri),
"endpoint");

return url;
}

public MinioClient WithRegion()
{
// Set region to its default value if empty or null
Region = "us-east-1";
return this;
}
}
25 changes: 12 additions & 13 deletions Minio/MinioClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,21 @@ public MinioClient()
}

// Save Credentials from user
internal string AccessKey { get; private set; }
internal string SecretKey { get; private set; }
internal string BaseUrl { get; private set; }
internal string AccessKey { get; set; }
internal string SecretKey { get; set; }
internal string BaseUrl { get; set; }

// Reconstructed endpoint with scheme and host.In the case of Amazon, this url
// is the virtual style path or location based endpoint
internal string Endpoint { get; private set; }
internal string SessionToken { get; private set; }
internal string Endpoint { get; set; }
internal string SessionToken { get; set; }

// Indicates if we are using HTTPS or not
internal bool Secure { get; private set; }
internal bool Secure { get; set; }

internal HttpClient HttpClient { get; private set; }
internal HttpClient HttpClient { get; set; }

internal IWebProxy Proxy { get; private set; }

private static string SystemUserAgent
{
Expand All @@ -120,7 +122,7 @@ private static string SystemUserAgent
/// <summary>
/// Returns the User-Agent header for the request
/// </summary>
private string FullUserAgent => $"{SystemUserAgent} {CustomUserAgent}";
internal string FullUserAgent => $"{SystemUserAgent} {CustomUserAgent}";

/// <summary>
/// Runs httpClient's GetAsync method
Expand Down Expand Up @@ -330,7 +332,7 @@ private void ArgsCheck(Args args)
var resource = string.Empty;
var usePathStyle = false;

if (bucketName != null && s3utils.IsAmazonEndPoint(BaseUrl))
if (bucketName != null && S3utils.IsAmazonEndPoint(BaseUrl))
{
if (method == HttpMethod.Put && objectName == null && resourcePath == null)
// use path style for make bucket to workaround "AuthorizationHeaderMalformed" error from s3.amazonaws.com
Expand Down Expand Up @@ -395,10 +397,7 @@ public MinioClient WithSSL(bool secure = true)
/// <summary>
/// Uses webproxy for all requests if this method is invoked on client object.
/// </summary>
/// <remarks>
/// This setting will be ignored when injecting an external <see cref="System.Net.Http.HttpClient" /> instance with
/// <see cref="MinioClient(HttpClient)" /> <see cref="WithHttpClient(HttpClient, bool)" />.
/// </remarks>
/// <param name="proxy">Information on the proxy server in the setup.</param>
/// <returns></returns>
public MinioClient WithProxy(IWebProxy proxy)
{
Expand Down
Loading

0 comments on commit b505805

Please sign in to comment.