ServiceRequest.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Text;
  11. using Aliyun.OSS.Util;
  12. namespace Aliyun.OSS.Common.Communication
  13. {
  14. internal class ServiceRequest : ServiceMessage, IDisposable
  15. {
  16. private bool _disposed;
  17. private readonly IDictionary<String, String> _parameters
  18. = new Dictionary<String, String>();
  19. /// <summary>
  20. /// Gets or sets the endpoint.
  21. /// </summary>
  22. public Uri Endpoint { get; set; }
  23. /// <summary>
  24. /// Gets or sets the resource path of the request URI.
  25. /// </summary>
  26. public String ResourcePath { get; set; }
  27. /// <summary>
  28. /// Gets or sets the HTTP method.
  29. /// </summary>
  30. public HttpMethod Method { get; set; }
  31. /// <summary>
  32. /// Gets the dictionary of the request parameters.
  33. /// </summary>
  34. public IDictionary<String, String> Parameters
  35. {
  36. get { return _parameters; }
  37. }
  38. /// <summary>
  39. /// Gets whether the request can be repeated.
  40. /// </summary>
  41. public bool IsRepeatable
  42. {
  43. get { return Content == null || Content.CanSeek; }
  44. }
  45. /// <summary>
  46. /// Build the request URI from the request message.
  47. /// </summary>
  48. /// <returns></returns>
  49. public string BuildRequestUri()
  50. {
  51. const string delimiter = "/";
  52. var uri = Endpoint.ToString();
  53. if (!uri.EndsWith(delimiter) &&
  54. (ResourcePath == null || !ResourcePath.StartsWith(delimiter)))
  55. {
  56. uri += delimiter;
  57. }
  58. if (ResourcePath != null)
  59. uri += ResourcePath;
  60. if (IsParameterInUri())
  61. {
  62. var paramString = HttpUtils.ConbineQueryString(_parameters);
  63. if (!string.IsNullOrEmpty(paramString))
  64. uri += "?" + paramString;
  65. }
  66. return uri;
  67. }
  68. public Stream BuildRequestContent()
  69. {
  70. if (!IsParameterInUri())
  71. {
  72. var paramString = HttpUtils.ConbineQueryString(_parameters);
  73. if (!string.IsNullOrEmpty(paramString))
  74. {
  75. var buffer = Encoding.GetEncoding("utf-8").GetBytes(paramString);
  76. Stream content = new MemoryStream();
  77. content.Write(buffer, 0, buffer.Length);
  78. content.Flush();
  79. // Move the marker to the beginning for further read.
  80. content.Seek(0, SeekOrigin.Begin);
  81. return content;
  82. }
  83. }
  84. return Content;
  85. }
  86. private bool IsParameterInUri()
  87. {
  88. var requestHasPayload = Content != null;
  89. var requestIsPost = Method == HttpMethod.Post;
  90. var putParamsInUri = !requestIsPost || requestHasPayload;
  91. return putParamsInUri;
  92. }
  93. public void Dispose()
  94. {
  95. Dispose(true);
  96. GC.SuppressFinalize(this);
  97. }
  98. protected virtual void Dispose(bool disposing)
  99. {
  100. if (_disposed)
  101. return;
  102. if (disposing)
  103. {
  104. if (Content != null)
  105. {
  106. Content.Close();
  107. Content = null;
  108. }
  109. _disposed = true;
  110. }
  111. }
  112. }
  113. }