PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/WCFWebApi/src/System.Net.Http/System/Net/Http/HttpRequestMessage.cs

#
C# | 230 lines | 185 code | 34 blank | 11 comment | 35 complexity | 98581a83e5dcbd2f7477f2e70b901df8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Net.Http.Headers;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Collections.Generic;
  6. namespace System.Net.Http
  7. {
  8. public class HttpRequestMessage : IDisposable
  9. {
  10. private const int messageAlreadySent = 1; // signals that this message was already sent.
  11. private const int messageNotYetSent = 0;
  12. // If this field is 0 (default), then the message wasn't sent by an HttpClient instance yet. If the field
  13. // value is 'messageSent', then the message was already sent and should not be sent again.
  14. private int sendStatus;
  15. private HttpMethod method;
  16. private Uri requestUri;
  17. private HttpRequestHeaders headers;
  18. private Version version;
  19. private HttpContent content;
  20. private bool disposed;
  21. private IDictionary<String, Object> properties;
  22. public Version Version
  23. {
  24. get { return version; }
  25. set
  26. {
  27. if (value == null)
  28. {
  29. throw new ArgumentNullException("value");
  30. }
  31. CheckDisposed();
  32. version = value;
  33. }
  34. }
  35. public HttpContent Content
  36. {
  37. get { return content; }
  38. set
  39. {
  40. CheckDisposed();
  41. if (Logging.On)
  42. {
  43. if (value == null)
  44. {
  45. Logging.PrintInfo(Logging.Http, this, SR.net_http_log_content_null);
  46. }
  47. else
  48. {
  49. Logging.Associate(Logging.Http, this, value);
  50. }
  51. }
  52. // It's OK to set a 'null' content, even if the method is POST/PUT. We don't want to artificially
  53. // prevent scenarios by being to strict.
  54. content = value;
  55. }
  56. }
  57. public HttpMethod Method
  58. {
  59. get { return method; }
  60. set
  61. {
  62. if (value == null)
  63. {
  64. throw new ArgumentNullException("value");
  65. }
  66. CheckDisposed();
  67. method = value;
  68. }
  69. }
  70. public Uri RequestUri
  71. {
  72. get { return requestUri; }
  73. set
  74. {
  75. if ((value != null) && (value.IsAbsoluteUri) && (!HttpUtilities.IsHttpUri(value)))
  76. {
  77. throw new ArgumentException(SR.net_http_client_http_baseaddress_required, "value");
  78. }
  79. CheckDisposed();
  80. // It's OK to set 'null'. HttpClient will add the 'BaseAddress'. If there is no 'BaseAddress'
  81. // sending this message will throw.
  82. requestUri = value;
  83. }
  84. }
  85. public HttpRequestHeaders Headers
  86. {
  87. get
  88. {
  89. if (headers == null)
  90. {
  91. headers = new HttpRequestHeaders();
  92. }
  93. return headers;
  94. }
  95. }
  96. public IDictionary<String, Object> Properties
  97. {
  98. get
  99. {
  100. if (properties == null)
  101. {
  102. properties = new Dictionary<String, Object>();
  103. }
  104. return properties;
  105. }
  106. }
  107. public HttpRequestMessage()
  108. : this(HttpMethod.Get, (Uri)null)
  109. {
  110. }
  111. public HttpRequestMessage(HttpMethod method, Uri requestUri)
  112. {
  113. if (Logging.On) Logging.Enter(Logging.Http, this, ".ctor", "Method: " + method + ", Uri: '" + requestUri + "'");
  114. InitializeValues(method, requestUri);
  115. if (Logging.On) Logging.Exit(Logging.Http, this, ".ctor", null);
  116. }
  117. [SuppressMessage("Microsoft.Design", "CA1057:StringUriOverloadsCallSystemUriOverloads",
  118. Justification = "It is OK to provide 'null' values. A Uri instance is created from 'requestUri' if it is != null.")]
  119. public HttpRequestMessage(HttpMethod method, string requestUri)
  120. {
  121. if (Logging.On) Logging.Enter(Logging.Http, this, ".ctor", "Method: " + method + ", Uri: '" + requestUri + "'");
  122. // It's OK to have a 'null' request Uri. If HttpClient is used, the 'BaseAddress' will be added.
  123. // If there is no 'BaseAddress', sending this request message will throw.
  124. // Note that we also allow the string to be empty: null and empty should be considered equivalent.
  125. if (string.IsNullOrEmpty(requestUri))
  126. {
  127. InitializeValues(method, null);
  128. }
  129. else
  130. {
  131. InitializeValues(method, new Uri(requestUri, UriKind.RelativeOrAbsolute));
  132. }
  133. if (Logging.On) Logging.Exit(Logging.Http, this, ".ctor", null);
  134. }
  135. public override string ToString()
  136. {
  137. StringBuilder sb = new StringBuilder();
  138. sb.Append("Method: ");
  139. sb.Append(method);
  140. sb.Append(", RequestUri: '");
  141. sb.Append(requestUri == null ? "<null>" : requestUri.ToString());
  142. sb.Append("', Version: ");
  143. sb.Append(version);
  144. sb.Append(", Content: ");
  145. sb.Append(content == null ? "<null>" : content.GetType().FullName);
  146. sb.Append(", Headers:\r\n");
  147. sb.Append(HeaderUtilities.DumpHeaders(headers, content == null ? null : content.Headers));
  148. return sb.ToString();
  149. }
  150. private void InitializeValues(HttpMethod method, Uri requestUri)
  151. {
  152. if (method == null)
  153. {
  154. throw new ArgumentNullException("method");
  155. }
  156. if ((requestUri != null) && (requestUri.IsAbsoluteUri) && (!HttpUtilities.IsHttpUri(requestUri)))
  157. {
  158. throw new ArgumentException(SR.net_http_client_http_baseaddress_required, "requestUri");
  159. }
  160. this.method = method;
  161. this.requestUri = requestUri;
  162. this.version = HttpUtilities.DefaultVersion;
  163. }
  164. internal bool MarkAsSent()
  165. {
  166. return Interlocked.Exchange(ref sendStatus, messageAlreadySent) == messageNotYetSent;
  167. }
  168. #region IDisposable Members
  169. protected virtual void Dispose(bool disposing)
  170. {
  171. // The reason for this type to implement IDisposable is that it contains instances of types that implement
  172. // IDisposable (content).
  173. if (disposing && !disposed)
  174. {
  175. disposed = true;
  176. if (content != null)
  177. {
  178. content.Dispose();
  179. }
  180. }
  181. }
  182. public void Dispose()
  183. {
  184. Dispose(true);
  185. GC.SuppressFinalize(this);
  186. }
  187. #endregion
  188. private void CheckDisposed()
  189. {
  190. if (disposed)
  191. {
  192. throw new ObjectDisposedException(this.GetType().FullName);
  193. }
  194. }
  195. }
  196. }