PageRenderTime 81ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/wcf/WCFWebApi/Http/Src/Microsoft.Net.Http/HttpResponseMessage.cs

http://webapicontrib.codeplex.com
C# | 203 lines | 167 code | 30 blank | 6 comment | 27 complexity | fe3d7f465bddf770ab13c4f3996844a5 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0, Apache-2.0
  1. using System.Net.Http.Headers;
  2. using System.Text;
  3. namespace System.Net.Http
  4. {
  5. public class HttpResponseMessage : IDisposable
  6. {
  7. private const HttpStatusCode defaultStatusCode = HttpStatusCode.OK;
  8. private const string defaultReasonPhrase = "OK";
  9. private HttpStatusCode statusCode;
  10. private HttpResponseHeaders headers;
  11. private string reasonPhrase;
  12. private HttpRequestMessage requestMessage;
  13. private Version version;
  14. private HttpContent content;
  15. private bool disposed;
  16. public Version Version
  17. {
  18. get { return version; }
  19. set
  20. {
  21. if (value == null)
  22. {
  23. throw new ArgumentNullException("value");
  24. }
  25. CheckDisposed();
  26. version = value;
  27. }
  28. }
  29. public HttpContent Content
  30. {
  31. get { return content; }
  32. set
  33. {
  34. CheckDisposed();
  35. content = value;
  36. }
  37. }
  38. public HttpStatusCode StatusCode
  39. {
  40. get { return statusCode; }
  41. set
  42. {
  43. if (((int)statusCode < 0) || ((int)statusCode > 999))
  44. {
  45. throw new ArgumentOutOfRangeException("value");
  46. }
  47. CheckDisposed();
  48. statusCode = value;
  49. }
  50. }
  51. public string ReasonPhrase
  52. {
  53. get { return reasonPhrase; }
  54. set
  55. {
  56. if ((value != null) && ContainsNewLineCharacter(value))
  57. {
  58. throw new FormatException("The reason phrase must not contain new-line characters.");
  59. }
  60. CheckDisposed();
  61. reasonPhrase = value; // It's OK to have a 'null' reason phrase
  62. }
  63. }
  64. public HttpResponseHeaders Headers
  65. {
  66. get
  67. {
  68. if (headers == null)
  69. {
  70. headers = new HttpResponseHeaders();
  71. }
  72. return headers;
  73. }
  74. }
  75. public HttpRequestMessage RequestMessage
  76. {
  77. get { return requestMessage; }
  78. set
  79. {
  80. CheckDisposed();
  81. requestMessage = value;
  82. }
  83. }
  84. public bool IsSuccessStatusCode
  85. {
  86. get { return ((int)statusCode >= 200) && ((int)statusCode <= 299); }
  87. }
  88. public HttpResponseMessage()
  89. : this(defaultStatusCode, defaultReasonPhrase)
  90. {
  91. }
  92. public HttpResponseMessage(HttpStatusCode statusCode, string reasonPhrase)
  93. {
  94. if (((int)statusCode < 0) || ((int)statusCode > 999))
  95. {
  96. throw new ArgumentOutOfRangeException("statusCode");
  97. }
  98. this.statusCode = statusCode;
  99. this.reasonPhrase = reasonPhrase;
  100. this.version = HttpUtilities.DefaultVersion;
  101. }
  102. public HttpResponseMessage EnsureSuccessStatusCode()
  103. {
  104. if (!IsSuccessStatusCode)
  105. {
  106. // Disposing the content should help users: If users call EnsureSuccessStatusCode(), an exception is
  107. // thrown if the response status code is != 2xx. I.e. the behavior is similar to a failed request (e.g.
  108. // connection failure). Users don't expect to dispose the content in this case: If an exception is
  109. // thrown, the object is responsible fore cleaning up its state.
  110. if (content != null)
  111. {
  112. content.Dispose();
  113. }
  114. throw new HttpException(string.Format("Response status code does not indicate success: {0} ({1})", (int)statusCode,
  115. reasonPhrase));
  116. }
  117. return this;
  118. }
  119. public override string ToString()
  120. {
  121. StringBuilder sb = new StringBuilder();
  122. sb.Append("StatusCode: ");
  123. sb.Append((int)statusCode);
  124. sb.Append(", ReasonPhrase: '");
  125. sb.Append(reasonPhrase == null ? "<null>" : reasonPhrase);
  126. sb.Append("', Version: ");
  127. sb.Append(version);
  128. sb.Append(", Content: ");
  129. sb.Append(content == null ? "<null>" : content.GetType().FullName);
  130. sb.Append(", Headers:\r\n");
  131. sb.Append(HeaderUtilities.DumpHeaders(headers, content == null ? null : content.Headers));
  132. return sb.ToString();
  133. }
  134. private bool ContainsNewLineCharacter(string value)
  135. {
  136. foreach (char character in value)
  137. {
  138. if ((character == HttpRuleParser.CR) || (character == HttpRuleParser.LF))
  139. {
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. #region IDisposable Members
  146. protected virtual void Dispose(bool disposing)
  147. {
  148. // The reason for this type to implement IDisposable is that it contains instances of types that implement
  149. // IDisposable (content).
  150. if (disposing && !disposed)
  151. {
  152. disposed = true;
  153. if (content != null)
  154. {
  155. content.Dispose();
  156. }
  157. }
  158. }
  159. public void Dispose()
  160. {
  161. Dispose(true);
  162. GC.SuppressFinalize(this);
  163. }
  164. #endregion
  165. private void CheckDisposed()
  166. {
  167. if (disposed)
  168. {
  169. throw new ObjectDisposedException(this.GetType().FullName);
  170. }
  171. }
  172. }
  173. }