using System;
using System.Collections.Generic;
using System.Text;
using COSXML.Common;
using COSXML.Network;
using COSXML.Log;
using COSXML.Auth;
using COSXML.Utils;
namespace COSXML.Model
{
public abstract class CosRequest
{
private static string TAG = typeof(CosRequest).FullName;
protected Request realRequest;
///
/// isHttps = true, https 请求; isHttps = false, http 请求; default isHttps = false.
///
protected bool? isHttps = null;
///
/// cos api 请求对应的 http method.
///
protected string method = CosRequestMethod.GET;
///
/// http 请求url中 path 部分.
///
protected string path;
///
/// http 请求url中 query 部分.
///
protected Dictionary queryParameters = new Dictionary();
///
/// http 请求 header 部分.
///
protected Dictionary headers = new Dictionary();
///
/// cos 服务的 appid.
///
protected string appid;
///
/// cos 服务签名的签名源部分.
///
protected CosXmlSignSourceProvider cosXmlSignSourceProvider = new CosXmlSignSourceProvider();
///
/// needMD5 = true, 请求中带上 Content-Md5; needMd5 = false, 请求中不带 Content-Md5; defalut needMd5 = false.
///
protected bool needMD5 = true;
///
/// 请求预签名URL
///
protected string requestUrlWithSign = null;
public CosXmlConfig serviceConfig { get; set; }
///
/// http or https for cos request.
///
public bool? IsHttps
{
get
{
return isHttps;
}
set { isHttps = value; }
}
///
/// http method
///
public string Method
{
get
{
return method;
}
set { this.method = value; }
}
///
/// path of http url.
///
public string RequestPath
{
get
{
return path;
}
private set { }
}
///
/// query of http url.
///
///
public virtual Dictionary GetRequestParamters()
{
return queryParameters;
}
///
/// http request header
///
///
public virtual Dictionary GetRequestHeaders()
{
return headers;
}
///
/// add query parameter for cos request, and cover the current value if it exists with the key.
///
///
///
public void SetQueryParameter(string key, string value)
{
SetQueryParameter(key, value, true);
}
///
/// url 部分都统一 url encode
///
///
///
///
public void SetQueryParameter(string key, string value, bool isNeedUrlEncode)
{
try
{
if (value == null)
{
value = "";
}
if (isNeedUrlEncode)
{
value = URLEncodeUtils.Encode(value);
}
queryParameters.Add(key, value);
}
catch (ArgumentException)
{
// cover the current value
// cover the current value
queryParameters[key] = value;
}
}
///
/// add header for cos request, and cover the current value, if it exists with the key.
///
/// header: key
/// header: value
public void SetRequestHeader(string key, string value)
{
SetRequestHeader(key, value, false);
}
///
/// add headers for cos request, and cover the current value, if it exists with the key.
///
///
public void SetRequestHeaders(Dictionary headers)
{
foreach (KeyValuePair entry in headers)
{
SetRequestHeader(entry.Key, entry.Value);
}
}
///
/// header 默认不 encode
///
/// 不能为null 即不包含空格,即 位于(\u0020, \u007F),超过这个范围,urlencode
/// 可以为null,为空,且位于(\u001f,\u007F) 和 '\t',超过这个范围,urlencode
///
public void SetRequestHeader(string key, string value, bool isNeedUrlEncode)
{
try
{
if (value == null)
{
value = "";
}
if (isNeedUrlEncode)
{
value = URLEncodeUtils.Encode(value);
}
headers.Add(key, value);
}
catch (ArgumentException)
{
// cover the current value
// cover the current value
headers[key] = value;
}
}
///
/// set appid for cos.
///
public string APPID
{
get
{
return this.appid;
}
set { this.appid = value; }
}
///
/// 是否带上content-md5
///
public bool IsNeedMD5
{
get
{
return needMD5;
}
set { needMD5 = value; }
}
///
/// return the host for cos request
///
/// host(string)
public abstract string GetHost();
///
/// return the body for cos request. such as upload file.
///
///
public abstract RequestBody GetRequestBody();
///
/// 返回 xml 格式的 requestBody
///
///
///
protected Network.RequestBody GetXmlRequestBody(object d)
{
string content = Transfer.XmlBuilder.Serialize(d);
byte[] data = Encoding.UTF8.GetBytes(content);
ByteRequestBody body = new ByteRequestBody(data);
return body;
}
///
/// check parameter for cos.
///
///
public abstract void CheckParameters();
///
/// 设置签名的有效期: [signStartTimeSecond, signStartTimeSecond + durationSecond]
///
///
///
public virtual void SetSign(long signStartTimeSecond, long durationSecond)
{
cosXmlSignSourceProvider.SetSignTime(signStartTimeSecond, durationSecond);
}
///
/// 计算签名时,带上头部header 和查询参数 query验证.
/// 设置签名的有效期: [signStartTimeSecond, signStartTimeSecond + durationSecond]
///
///
///
///
///
public virtual void SetSign(long signStartTimeSecond, long durationSecond, List headerKeys, List queryParameterKeys)
{
cosXmlSignSourceProvider.SetSignTime(signStartTimeSecond, durationSecond);
cosXmlSignSourceProvider.AddHeaderKeys(headerKeys);
cosXmlSignSourceProvider.AddParameterKeys(queryParameterKeys);
}
///
/// 直接设置签名串.
///
///
public virtual void SetSign(string sign)
{
SetRequestHeader(CosRequestHeaderKey.AUTHORIZAIION, sign);
}
///
/// 返回签名数据源
///
///
public virtual CosXmlSignSourceProvider GetSignSourceProvider()
{
// 默认签署的头部跟参数
cosXmlSignSourceProvider.AddHeaderKeys(new List()
{
"cache-control",
"content-disposition",
"content-encoding",
"content-length",
"content-md5",
"content-type",
"expect",
"expires",
"host",
"if-match",
"if-modified-since",
"if-none-match",
"if-unmodified-since",
"origin",
"range",
"response-cache-control",
"response-content-disposition",
"response-content-encoding",
"response-content-language",
"response-content-type",
"response-expires",
"transfer-encoding",
"versionid"
});
foreach (KeyValuePair pair in headers)
{
if (pair.Key.StartsWith("x-cos-"))
{
cosXmlSignSourceProvider.AddHeaderKey(pair.Key.ToLower());
}
}
foreach (KeyValuePair pair in queryParameters)
{
cosXmlSignSourceProvider.AddParameterKey(pair.Key.ToLower());
}
return cosXmlSignSourceProvider;
}
///
/// 设置预签名URL
///
public string RequestURLWithSign
{
get
{
return requestUrlWithSign;
}
set { requestUrlWithSign = value; }
}
public void BindRequest(Request request)
{
this.realRequest = request;
}
public void Cancel()
{
if (realRequest != null)
{
realRequest.Cancel();
}
}
}
}