ClientWebHeaderCollection.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //Copyright 2010 Microsoft Corporation
  2. //
  3. //Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  4. //You may obtain a copy of the License at
  5. //
  6. //http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. //Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
  9. //"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. //See the License for the specific language governing permissions and limitations under the License.
  11. namespace System.Data.Services.Http
  12. {
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Diagnostics;
  16. using System.Data.Services.Client;
  17. internal sealed class ClientWebHeaderCollection : WebHeaderCollection
  18. {
  19. private System.Net.WebHeaderCollection innerCollection;
  20. private System.Net.HttpWebRequest request;
  21. internal ClientWebHeaderCollection(System.Net.WebHeaderCollection collection)
  22. {
  23. Debug.Assert(collection != null, "collection can't be null.");
  24. this.innerCollection = collection;
  25. }
  26. internal ClientWebHeaderCollection(System.Net.WebHeaderCollection collection, System.Net.HttpWebRequest request)
  27. {
  28. Debug.Assert(collection != null, "collection can't be null.");
  29. this.innerCollection = collection;
  30. this.request = request;
  31. }
  32. #region Properties.
  33. public override int Count
  34. {
  35. get
  36. {
  37. return this.innerCollection.Count;
  38. }
  39. }
  40. public override ICollection<string> AllKeys
  41. {
  42. get
  43. {
  44. return this.innerCollection.AllKeys;
  45. }
  46. }
  47. public override string this[string name]
  48. {
  49. get
  50. {
  51. return this.innerCollection[name];
  52. }
  53. set
  54. {
  55. if (name == XmlConstants.HttpContentLength)
  56. {
  57. return;
  58. }
  59. else if (name == XmlConstants.HttpAcceptCharset)
  60. {
  61. Debug.Assert(value == XmlConstants.Utf8Encoding, "Asking for AcceptCharset different thatn UTF-8.");
  62. return;
  63. }
  64. else if (name == XmlConstants.HttpCookie)
  65. {
  66. if (this.request != null)
  67. {
  68. System.Net.CookieContainer cookieContainer = new System.Net.CookieContainer();
  69. cookieContainer.SetCookies(this.request.RequestUri, value);
  70. this.request.CookieContainer = cookieContainer;
  71. }
  72. else
  73. {
  74. this.innerCollection[name] = value;
  75. }
  76. }
  77. else
  78. {
  79. this.innerCollection[name] = value;
  80. }
  81. }
  82. }
  83. public override string this[System.Data.Services.Http.HttpRequestHeader header]
  84. {
  85. get
  86. {
  87. return this[HttpHeaderToName.RequestHeaderNames[header]];
  88. }
  89. set
  90. {
  91. this[HttpHeaderToName.RequestHeaderNames[header]] = value;
  92. }
  93. }
  94. #endregion Properties.
  95. }
  96. }