WebRequest.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #region Namespaces.
  14. using System;
  15. using System.Data.Services.Client;
  16. using System.Diagnostics;
  17. using System.IO;
  18. #endregion Namespaces.
  19. internal abstract class WebRequest
  20. {
  21. public abstract string ContentType
  22. {
  23. get;
  24. set;
  25. }
  26. public abstract System.Data.Services.Http.WebHeaderCollection Headers
  27. {
  28. get;
  29. }
  30. public abstract string Method
  31. {
  32. get;
  33. set;
  34. }
  35. public abstract Uri RequestUri
  36. {
  37. get;
  38. }
  39. public static System.Data.Services.Http.WebRequest Create(Uri requestUri, HttpStack httpStack)
  40. {
  41. Debug.Assert(requestUri != null, "requestUri != null");
  42. if ((requestUri.Scheme != Uri.UriSchemeHttp) && (requestUri.Scheme != Uri.UriSchemeHttps))
  43. {
  44. throw new NotSupportedException();
  45. }
  46. if (httpStack == HttpStack.Auto)
  47. {
  48. if (UriRequiresClientHttpWebRequest(requestUri))
  49. {
  50. httpStack = HttpStack.ClientHttp;
  51. }
  52. else
  53. {
  54. httpStack = HttpStack.XmlHttp;
  55. }
  56. }
  57. if (httpStack == HttpStack.ClientHttp)
  58. {
  59. return new ClientHttpWebRequest(requestUri);
  60. }
  61. else
  62. {
  63. Debug.Assert(httpStack == HttpStack.XmlHttp, "Only ClientHttp and XmlHttp are supported for now.");
  64. return new XHRHttpWebRequest(requestUri);
  65. }
  66. }
  67. public abstract void Abort();
  68. public abstract IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state);
  69. public abstract IAsyncResult BeginGetResponse(AsyncCallback callback, object state);
  70. public abstract Stream EndGetRequestStream(IAsyncResult asyncResult);
  71. public abstract System.Data.Services.Http.WebResponse EndGetResponse(IAsyncResult asyncResult);
  72. private static bool UriRequiresClientHttpWebRequest(Uri uri)
  73. {
  74. if (!XHRHttpWebRequest.IsAvailable())
  75. {
  76. return true;
  77. }
  78. Uri sameDomainUri = System.Windows.Browser.HtmlPage.Document.DocumentUri;
  79. if (sameDomainUri.Scheme != uri.Scheme || sameDomainUri.Port != uri.Port ||
  80. !string.Equals(sameDomainUri.DnsSafeHost, uri.DnsSafeHost, StringComparison.OrdinalIgnoreCase))
  81. {
  82. return true;
  83. }
  84. return false;
  85. }
  86. }
  87. }