XHRHttpWebResponse.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.Diagnostics;
  15. using System.Globalization;
  16. using System.IO;
  17. internal sealed class XHRHttpWebResponse : System.Data.Services.Http.HttpWebResponse
  18. {
  19. #region Fields.
  20. private System.Data.Services.Http.XHRWebHeaderCollection headers;
  21. private System.Data.Services.Http.XHRHttpWebRequest request;
  22. private int statusCode;
  23. #endregion Fields.
  24. internal XHRHttpWebResponse(System.Data.Services.Http.XHRHttpWebRequest request, int statusCode, string responseHeaders)
  25. {
  26. Debug.Assert(request != null, "request can't be null.");
  27. this.request = request;
  28. NormalizeResponseStatus(ref statusCode);
  29. this.statusCode = statusCode;
  30. this.headers = new System.Data.Services.Http.XHRWebHeaderCollection();
  31. this.ParseHeaders(responseHeaders);
  32. }
  33. #region Properties.
  34. public override long ContentLength
  35. {
  36. get
  37. {
  38. return Convert.ToInt64(this.Headers["Content-Length"], CultureInfo.InvariantCulture);
  39. }
  40. }
  41. public override string ContentType
  42. {
  43. get
  44. {
  45. return this.Headers["Content-Type"];
  46. }
  47. }
  48. public override System.Data.Services.Http.WebHeaderCollection Headers
  49. {
  50. get
  51. {
  52. return this.headers;
  53. }
  54. }
  55. public override System.Data.Services.Http.HttpWebRequest Request
  56. {
  57. get
  58. {
  59. return this.request;
  60. }
  61. }
  62. public override System.Data.Services.Http.HttpStatusCode StatusCode
  63. {
  64. get
  65. {
  66. return (System.Data.Services.Http.HttpStatusCode)this.statusCode;
  67. }
  68. }
  69. internal System.Data.Services.Http.XHRHttpWebRequest InternalRequest
  70. {
  71. set
  72. {
  73. this.request = value;
  74. }
  75. }
  76. #endregion Properties.
  77. public override void Close()
  78. {
  79. this.request.Close();
  80. }
  81. public override string GetResponseHeader(string headerName)
  82. {
  83. return this.headers[headerName];
  84. }
  85. public override Stream GetResponseStream()
  86. {
  87. return this.request.ReadResponse(this);
  88. }
  89. protected override void Dispose(bool disposing)
  90. {
  91. this.Close();
  92. }
  93. private static void NormalizeResponseStatus(ref int statusCodeParam)
  94. {
  95. string userAgent = System.Windows.Browser.HtmlPage.BrowserInformation.UserAgent;
  96. bool browserIsIE = userAgent != null && userAgent.ToUpper(CultureInfo.InvariantCulture).Contains("MSIE");
  97. if (browserIsIE)
  98. {
  99. if (statusCodeParam == 1223)
  100. {
  101. statusCodeParam = 204;
  102. }
  103. else if (statusCodeParam == 12150)
  104. {
  105. statusCodeParam = 399;
  106. }
  107. }
  108. if (statusCodeParam > (int)HttpStatusCodeRange.MaxValue || statusCodeParam < (int)HttpStatusCodeRange.MinValue)
  109. {
  110. throw WebException.CreateInternal("HttpWebResponse.NormalizeResponseStatus");
  111. }
  112. }
  113. private void ParseHeaders(string responseHeaders)
  114. {
  115. if (string.IsNullOrEmpty(responseHeaders))
  116. {
  117. return;
  118. }
  119. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseHeaders);
  120. WebParseError error = new WebParseError();
  121. int totalResponseHeadersLength = 0;
  122. int offset = 0;
  123. int maxHeaderLength = 64000;
  124. try
  125. {
  126. DataParseStatus result = this.headers.ParseHeaders(buffer, buffer.Length, ref offset, ref totalResponseHeadersLength, maxHeaderLength, ref error);
  127. if (result != DataParseStatus.Done)
  128. {
  129. throw WebException.CreateInternal("HttpWebResponse.ParseHeaders");
  130. }
  131. }
  132. catch (WebException)
  133. {
  134. throw;
  135. }
  136. catch (Exception e)
  137. {
  138. string message = System.Data.Services.Client.Strings.HttpWeb_Internal("HttpWebResponse.ParseHeaders.2");
  139. throw new WebException(message, e);
  140. }
  141. }
  142. }
  143. }