using System; using System.Collections.Generic; using System.Text; using COSXML.Common; using COSXML.Network; using COSXML.Log; using COSXML.Auth; using COSXML.Utils; /** * Copyright (c) 2018 Tencent Cloud. All rights reserved. * 11/2/2018 1:05:09 PM * bradyxiao */ namespace COSXML.Model { /** * cos request base class, all cos operator must be extended this. * * request method | request path | query parameters * request headers * request body * * special: * cos sign; */ 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; } private set { } } /// /// 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 (ArgumentNullException) { QLog.D(TAG, "SetQueryParameter: key ==null"); } catch (ArgumentException) { queryParameters[key] = value; // cover the current 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); } /// /// 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 (ArgumentNullException) { QLog.D(TAG, "SetRequestHeader: key ==null"); } catch (ArgumentException) { headers[key] = value; // cover the current value } } /// /// set appid for cos. /// /// cos appid 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 GetCOSHost(); /// /// 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(); /// /// check parameter for cos. /// /// public abstract void CheckParameters(); /// /// 设置签名的有效期: [signStartTimeSecond, signStartTimeSecond + durationSecond] /// /// /// public virtual void SetSign(long signStartTimeSecond, long durationSecond) { if (cosXmlSignSourceProvider == null) cosXmlSignSourceProvider = new CosXmlSignSourceProvider(); cosXmlSignSourceProvider.SetSignTime(signStartTimeSecond, durationSecond); } /// /// 计算签名时,带上头部header 和查询参数 query验证. /// 设置签名的有效期: [signStartTimeSecond, signStartTimeSecond + durationSecond] /// /// /// /// /// public virtual void SetSign(long signStartTimeSecond, long durationSecond, List headerKeys, List queryParameterKeys) { if (cosXmlSignSourceProvider == null) cosXmlSignSourceProvider = new CosXmlSignSourceProvider(); cosXmlSignSourceProvider.SetSignTime(signStartTimeSecond, durationSecond); cosXmlSignSourceProvider.AddHeaderKeys(headerKeys); cosXmlSignSourceProvider.AddParameterKeys(queryParameterKeys); } /// /// 直接设置签名串. /// /// public virtual void SetSign(string sign) { SetRequestHeader(CosRequestHeaderKey.AUTHORIZAIION, sign); } /// /// 返回签名数据源 /// /// public virtual CosXmlSignSourceProvider GetSignSourceProvider() { 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(); } } } }