OutgoingWebResponseContext.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // OutgoingWebResponseContext.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Globalization;
  30. using System.Net;
  31. using System.ServiceModel.Channels;
  32. namespace System.ServiceModel.Web
  33. {
  34. public class OutgoingWebResponseContext
  35. {
  36. WebHeaderCollection headers;
  37. long content_length;
  38. string content_type, etag, location, status_desc;
  39. DateTime last_modified;
  40. HttpStatusCode status_code;
  41. bool suppress_body;
  42. internal OutgoingWebResponseContext ()
  43. {
  44. }
  45. internal void Apply (HttpResponseMessageProperty hp)
  46. {
  47. if (headers != null)
  48. hp.Headers.Add (headers);
  49. if (content_length != 0)
  50. hp.Headers ["Content-Length"] = content_length.ToString (NumberFormatInfo.InvariantInfo);
  51. if (content_type != null)
  52. hp.Headers ["Content-Type"] = content_type;
  53. if (etag != null)
  54. hp.Headers ["ETag"] = etag;
  55. if (location != null)
  56. hp.Headers ["Location"] = location;
  57. if (last_modified != default (DateTime))
  58. hp.Headers ["Last-Modified"] = last_modified.ToString ("R");
  59. if (status_code != default (HttpStatusCode))
  60. hp.StatusCode = status_code;
  61. if (status_desc != null)
  62. hp.StatusDescription = status_desc;
  63. hp.SuppressEntityBody = suppress_body;
  64. }
  65. public long ContentLength {
  66. get { return content_length; }
  67. set { content_length = value; }
  68. }
  69. public string ContentType {
  70. get { return content_type; }
  71. set { content_type = value; }
  72. }
  73. public string ETag {
  74. get { return etag; }
  75. set { etag = value; }
  76. }
  77. public WebHeaderCollection Headers {
  78. get {
  79. if (headers == null)
  80. headers = new WebHeaderCollection ();
  81. return headers;
  82. }
  83. }
  84. public DateTime LastModified {
  85. get { return last_modified; }
  86. set { last_modified = value; }
  87. }
  88. public string Location {
  89. get { return location; }
  90. set { location = value; }
  91. }
  92. public HttpStatusCode StatusCode {
  93. get { return status_code; }
  94. set { status_code = value; }
  95. }
  96. public string StatusDescription {
  97. get { return status_desc; }
  98. set { status_desc = value; }
  99. }
  100. public bool SuppressEntityBody {
  101. get { return suppress_body; }
  102. set { suppress_body = value; }
  103. }
  104. public void SetStatusAsCreated (Uri locationUri)
  105. {
  106. StatusCode = HttpStatusCode.Created;
  107. Location = locationUri.AbsoluteUri;
  108. }
  109. public void SetStatusAsNotFound ()
  110. {
  111. StatusCode = HttpStatusCode.NotFound;
  112. }
  113. public void SetStatusAsNotFound (string description)
  114. {
  115. StatusCode = HttpStatusCode.NotFound;
  116. StatusDescription = description;
  117. }
  118. }
  119. }