CORSConfiguration.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml.Serialization;
  4. using System.Text;
  5. namespace COSXML.Model.Tag
  6. {
  7. [XmlRoot]
  8. public sealed class CORSConfiguration
  9. {
  10. /// <summary>
  11. /// 跨域资源共享配置的信息,最多可以包含100条 CORSRule
  12. /// <see href="CORSRule"/>
  13. /// </summary>
  14. [XmlElement("CORSRule")]
  15. public List<CORSRule> corsRules;
  16. public string GetInfo()
  17. {
  18. StringBuilder stringBuilder = new StringBuilder("{CORSConfiguration:\n");
  19. if (corsRules != null)
  20. {
  21. foreach (CORSRule corsRule in corsRules)
  22. {
  23. if (corsRule != null)
  24. {
  25. stringBuilder.Append(corsRule.GetInfo()).Append("\n");
  26. }
  27. }
  28. }
  29. stringBuilder.Append("}");
  30. return stringBuilder.ToString();
  31. }
  32. public sealed class CORSRule
  33. {
  34. /// <summary>
  35. /// 配置规则的 ID,可选填
  36. /// </summary>
  37. [XmlElement("ID")]
  38. public string id;
  39. /// <summary>
  40. /// 允许的访问来源,支持通配符 *, 格式为:协议://域名[:端口]如:http://www.qq.com
  41. /// </summary>
  42. [XmlElement("AllowedOrigin")]
  43. public List<string> allowedOrigins;
  44. /// <summary>
  45. /// 允许的 HTTP 操作,枚举值:GET,PUT,HEAD,POST,DELETE
  46. /// </summary>
  47. [XmlElement("AllowedMethod")]
  48. public List<string> allowedMethods;
  49. /// <summary>
  50. /// 在发送 OPTIONS 请求时告知服务端,接下来的请求可以使用哪些自定义的 HTTP 请求头部,支持通配符 *
  51. /// </summary>
  52. [XmlElement("AllowedHeader")]
  53. public List<string> allowedHeaders;
  54. /// <summary>
  55. /// 设置浏览器可以接收到的来自服务器端的自定义头部信息
  56. /// </summary>
  57. [XmlElement("ExposeHeader")]
  58. public List<string> exposeHeaders;
  59. /// <summary>
  60. /// 设置 OPTIONS 请求得到结果的有效期
  61. /// </summary>
  62. [XmlElement("MaxAgeSeconds")]
  63. public int maxAgeSeconds;
  64. public string GetInfo()
  65. {
  66. StringBuilder stringBuilder = new StringBuilder("{CORSRule:\n");
  67. stringBuilder.Append("ID:").Append(id).Append("\n");
  68. if (allowedOrigins != null)
  69. {
  70. foreach (string origin in allowedOrigins)
  71. {
  72. if (origin != null)
  73. {
  74. stringBuilder.Append("AllowedOrigin:").Append(origin).Append("\n");
  75. }
  76. }
  77. }
  78. if (allowedMethods != null)
  79. {
  80. foreach (string method in allowedMethods)
  81. {
  82. if (method != null)
  83. {
  84. stringBuilder.Append("AllowedMethod:").Append(method).Append("\n");
  85. }
  86. }
  87. }
  88. if (allowedHeaders != null)
  89. {
  90. foreach (string header in allowedHeaders)
  91. {
  92. if (header != null)
  93. {
  94. stringBuilder.Append("AllowedHeader:").Append(header).Append("\n");
  95. }
  96. }
  97. }
  98. if (exposeHeaders != null)
  99. {
  100. foreach (string exposeHeader in exposeHeaders)
  101. {
  102. if (exposeHeader != null)
  103. {
  104. stringBuilder.Append("ExposeHeader:").Append(exposeHeader).Append("\n");
  105. }
  106. }
  107. }
  108. stringBuilder.Append("MaxAgeSeconds:").Append(maxAgeSeconds).Append("\n");
  109. stringBuilder.Append("}");
  110. return stringBuilder.ToString();
  111. }
  112. }
  113. }
  114. }