Skip to content

Commit a97a448

Browse files
liaoyustudentJacksonTian
authored andcommitted
add validate
1 parent 265263b commit a97a448

File tree

5 files changed

+111
-15
lines changed

5 files changed

+111
-15
lines changed

aliyun-net-sdk-core.Tests/Units/AcsRequest.cs

+28
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* under the License.
1818
*/
1919

20+
using System;
2021
using System.Collections.Generic;
2122

2223
using Aliyun.Acs.Core.Auth;
@@ -88,6 +89,9 @@ public void GetEndpoint()
8889
mockAcsRequest.ProductNetwork = "vpc";
8990
Assert.Equal("test-vpc.aliyuncs.com", mockAcsRequest.GetProductEndpoint());
9091

92+
mockAcsRequest.ProductSuffix = "suffix";
93+
Assert.Equal("testsuffix-vpc.aliyuncs.com", mockAcsRequest.GetProductEndpoint());
94+
9195
var productEndpointMap = new Dictionary<string, string>();
9296
mockAcsRequest.ProductEndpointType = "test-type";
9397
mockAcsRequest.ProductEndpointMap = productEndpointMap;
@@ -177,6 +181,30 @@ public void UserAgentConfigTest()
177181

178182
Assert.Equal(resultStr, userAgent);
179183
}
184+
185+
[Fact]
186+
public void validateParam()
187+
{
188+
var acsRequest = new MockAcsRequest();
189+
Assert.Throws<ArgumentException>(() =>
190+
{
191+
acsRequest.RegionId = "a.b";
192+
});
193+
194+
acsRequest.RegionId = "";
195+
Assert.Throws<ArgumentException>(() =>
196+
{
197+
acsRequest.ProductNetwork = "a.b";
198+
});
199+
200+
acsRequest.ProductNetwork = "private";
201+
Assert.Equal("private", acsRequest.ProductNetwork);
202+
203+
Assert.Throws<ArgumentException>(() =>
204+
{
205+
acsRequest.ProductSuffix = "a.b";
206+
});
207+
}
180208
}
181209

182210
public sealed class MockAcsRequest : AcsRequest<CommonRequest>

aliyun-net-sdk-core/AcsRequest.cs

+72-9
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
* under the License.
1818
*/
1919

20+
using System;
2021
using System.Collections.Generic;
2122
using System.Text;
23+
using System.Text.RegularExpressions;
2224

2325
using Aliyun.Acs.Core.Auth;
2426
using Aliyun.Acs.Core.Http;
@@ -36,12 +38,15 @@ public abstract class AcsRequest<T> : HttpRequest
3638

3739
private Dictionary<string, string> domainParameters = new Dictionary<string, string>();
3840

39-
public string ProductNetwork = "public";
41+
private string productNetwork = "public";
42+
43+
private string productSuffix;
4044

4145
private ProtocolType protocol = ProtocolType.HTTP;
4246

4347
private Dictionary<string, string> queryParameters = new Dictionary<string, string>();
4448

49+
private string regionId;
4550

4651
public string StringToSign;
4752
private UserAgent userAgentConfig;
@@ -56,13 +61,62 @@ public AcsRequest(string product) : base(null)
5661
public virtual string Product { get; set; }
5762
public virtual string Version { get; set; }
5863
public virtual string ActionName { get; set; }
59-
public virtual string RegionId { get; set; }
64+
public virtual string RegionId
65+
{
66+
get
67+
{
68+
return regionId;
69+
}
70+
set
71+
{
72+
var match = Regex.Match(value, "^[a-zA-Z0-9_-]*$");
73+
if (!match.Success)
74+
{
75+
throw new ArgumentException("regionId is invalid", "regionId");
76+
}
77+
regionId = value;
78+
}
79+
}
6080
public virtual string SecurityToken { get; set; }
6181
public ISignatureComposer Composer { get; set; }
6282
public string LocationProduct { get; set; }
6383
public string LocationEndpointType { get; set; }
6484
public ProductDomain ProductDomain { get; set; }
6585

86+
public string ProductNetwork
87+
{
88+
get
89+
{
90+
return productNetwork;
91+
}
92+
set
93+
{
94+
var match = Regex.Match(value, "^[a-zA-Z0-9_-]+$");
95+
if (!match.Success)
96+
{
97+
throw new ArgumentException("productNetwork is invalid", "productNetwork");
98+
}
99+
productNetwork = value;
100+
}
101+
}
102+
103+
public string ProductSuffix
104+
{
105+
get
106+
{
107+
return productSuffix;
108+
}
109+
set
110+
{
111+
var match = Regex.Match(value, "^[a-zA-Z0-9_-]+$");
112+
if (!match.Success)
113+
{
114+
throw new ArgumentException("productSuffix is invalid", "productSuffix");
115+
}
116+
productSuffix = value;
117+
}
118+
}
119+
66120
public virtual FormatType AcceptFormat
67121
{
68122
get { return acceptFormat; }
@@ -110,13 +164,13 @@ public void SetProductDomain(string endpoint = "")
110164

111165
if (endpoint != "" && ProductDomain == null)
112166
{
113-
ProductDomain = new ProductDomain {ProductName = Product, DomainName = endpoint};
167+
ProductDomain = new ProductDomain { ProductName = Product, DomainName = endpoint };
114168
}
115169
}
116170

117171
public void SetEndpoint(string endpoint)
118172
{
119-
ProductDomain = new ProductDomain {ProductName = Product, DomainName = endpoint};
173+
ProductDomain = new ProductDomain { ProductName = Product, DomainName = endpoint };
120174
}
121175

122176
public string GetProductEndpoint()
@@ -137,24 +191,33 @@ public string GetProductEndpoint()
137191
var endpoint = "";
138192
if (ProductEndpointType == "central")
139193
{
140-
endpoint = "<product_id><network>.aliyuncs.com";
194+
endpoint = "<product_id><suffix><network>.aliyuncs.com";
141195
}
142196
else if (ProductEndpointType == "regional")
143197
{
144-
endpoint = "<product_id><network>.<region_id>.aliyuncs.com";
198+
endpoint = "<product_id><suffix><network>.<region_id>.aliyuncs.com";
145199
endpoint = endpoint.Replace("<region_id>", RegionId);
146200
}
147201

202+
if (string.IsNullOrWhiteSpace(ProductSuffix))
203+
{
204+
endpoint = endpoint.Replace("<suffix>", string.Empty);
205+
}
206+
else
207+
{
208+
endpoint = endpoint.Replace("<suffix>", ProductSuffix);
209+
}
210+
148211
if (endpoint == "")
149212
{
150213
return "";
151214
}
152215

153216
endpoint = endpoint.Replace("<product_id>", Product.ToLower());
154217

155-
endpoint = ProductNetwork == "public"
156-
? endpoint.Replace("<network>", "")
157-
: endpoint.Replace("<network>", "-" + ProductNetwork);
218+
endpoint = ProductNetwork == "public" ?
219+
endpoint.Replace("<network>", "") :
220+
endpoint.Replace("<network>", "-" + ProductNetwork);
158221

159222
return endpoint;
160223
}

aliyun-net-sdk-core/DefaultAcsClient.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
using Aliyun.Acs.Core.Retry.Condition;
3737
using Aliyun.Acs.Core.Timeout.Util;
3838
using Aliyun.Acs.Core.Transform;
39-
using Aliyun.Acs.Core.Utils;
40-
39+
using Aliyun.Acs.Core.Utils;
40+
4141
namespace Aliyun.Acs.Core
4242
{
4343
public class DefaultAcsClient : IAcsClient

aliyun-net-sdk-core/Regions/Endpoints/EndpointResolve.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ public List<Endpoint> Resolve(string product, string regionId, string serviceCod
4545

4646
if (endpoint != null)
4747
{
48-
_endpoints.Add(endpoint);
49-
CacheTimeHelper.AddLastClearTimePerProduct(product, regionId, DateTime.UtcNow);
48+
foreach (var region in endpoint.RegionIds)
49+
{
50+
foreach (var productDomain in endpoint.ProductDomains.ToList())
51+
{
52+
AddEndpoint(endpoint.Name, region, product, productDomain.DomainName);
53+
CacheTimeHelper.AddLastClearTimePerProduct(product, region, DateTime.UtcNow);
54+
}
55+
}
5056
}
5157
else
5258
{

aliyun-sdk-feature-test/FeatureTest.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ internal class FeatureTest
4545
accessKeyId,
4646
accessKeySecret
4747
);
48-
4948
client = new DefaultAcsClient(profile);
50-
client.SetConnectTimeoutInMilliSeconds(2 * 60 * 1000);
49+
client.SetConnectTimeoutInMilliSeconds(2 * 60 * 1000);
5150
client.SetReadTimeoutInMilliSeconds(2 * 60 * 1000);
5251
}
5352

0 commit comments

Comments
 (0)