123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- /*
- * Copyright (C) Alibaba Cloud Computing
- * All rights reserved.
- *
- * 版权所有 (C)阿里云计算有限公司
- */
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using Aliyun.OSS.Util;
- namespace Aliyun.OSS.Common.Communication
- {
- internal class ServiceRequest : ServiceMessage, IDisposable
- {
- private bool _disposed;
-
- private readonly IDictionary<String, String> _parameters
- = new Dictionary<String, String>();
- /// <summary>
- /// Gets or sets the endpoint.
- /// </summary>
- public Uri Endpoint { get; set; }
-
- /// <summary>
- /// Gets or sets the resource path of the request URI.
- /// </summary>
- public String ResourcePath { get; set; }
- /// <summary>
- /// Gets or sets the HTTP method.
- /// </summary>
- public HttpMethod Method { get; set; }
-
- /// <summary>
- /// Gets the dictionary of the request parameters.
- /// </summary>
- public IDictionary<String, String> Parameters
- {
- get { return _parameters; }
- }
-
- /// <summary>
- /// Gets whether the request can be repeated.
- /// </summary>
- public bool IsRepeatable
- {
- get { return Content == null || Content.CanSeek; }
- }
-
- /// <summary>
- /// Build the request URI from the request message.
- /// </summary>
- /// <returns></returns>
- public string BuildRequestUri()
- {
- const string delimiter = "/";
- var uri = Endpoint.ToString();
- if (!uri.EndsWith(delimiter) &&
- (ResourcePath == null || !ResourcePath.StartsWith(delimiter)))
- {
- uri += delimiter;
- }
- if (ResourcePath != null)
- uri += ResourcePath;
-
- if (IsParameterInUri())
- {
- var paramString = HttpUtils.ConbineQueryString(_parameters);
- if (!string.IsNullOrEmpty(paramString))
- uri += "?" + paramString;
- }
-
- return uri;
- }
-
- public Stream BuildRequestContent()
- {
- if (!IsParameterInUri())
- {
- var paramString = HttpUtils.ConbineQueryString(_parameters);
- if (!string.IsNullOrEmpty(paramString))
- {
- var buffer = Encoding.GetEncoding("utf-8").GetBytes(paramString);
- Stream content = new MemoryStream();
- content.Write(buffer, 0, buffer.Length);
- content.Flush();
- // Move the marker to the beginning for further read.
- content.Seek(0, SeekOrigin.Begin);
- return content;
- }
- }
- return Content;
- }
-
- private bool IsParameterInUri()
- {
- var requestHasPayload = Content != null;
- var requestIsPost = Method == HttpMethod.Post;
- var putParamsInUri = !requestIsPost || requestHasPayload;
- return putParamsInUri;
- }
-
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
-
- protected virtual void Dispose(bool disposing)
- {
- if (_disposed)
- return;
-
- if (disposing)
- {
- if (Content != null)
- {
- Content.Close();
- Content = null;
- }
- _disposed = true;
- }
- }
- }
- }
|