HttpHelper.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //==========================================================================
  2. // File: HttpHelper.cs
  3. //
  4. // Summary: Implements Helper internal class for transmission over HTTP.
  5. //
  6. // Classes: internal HttpHelper
  7. //
  8. // By :
  9. // Ahmad Tantawy [email protected]
  10. // Ahmad Kadry [email protected]
  11. // Hussein Mehanna [email protected]
  12. //
  13. //==========================================================================
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System;
  35. using System.Globalization;
  36. using System.IO;
  37. using System.Net;
  38. using System.Text.RegularExpressions;
  39. namespace System.Runtime.Remoting.Channels.Http
  40. {
  41. internal class HttpHelper
  42. {
  43. public static string Parse(string URL , out string ObjectURI)
  44. {
  45. int Pos;
  46. ObjectURI = null;
  47. string ChannelURI = null;
  48. Pos = IndexOfChannelUri (URL);
  49. if(Pos > 0)
  50. {
  51. Pos = URL.IndexOf("/", Pos);
  52. if(Pos >= 0)
  53. {
  54. ObjectURI = URL.Substring(Pos);
  55. ChannelURI = URL.Substring(0, Pos);
  56. }
  57. else ChannelURI = URL;
  58. }
  59. return ChannelURI;
  60. }
  61. static int IndexOfChannelUri(string URL)
  62. {
  63. CompareInfo ci = CultureInfo.InvariantCulture.CompareInfo;
  64. if (ci.IsPrefix (URL, "http://", CompareOptions.IgnoreCase))
  65. return "http://".Length;
  66. if (ci.IsPrefix (URL, "https://", CompareOptions.IgnoreCase))
  67. return "https://".Length;
  68. return -1;
  69. }
  70. public static bool StartsWithHttp (string url)
  71. {
  72. return IndexOfChannelUri (url) > 0;
  73. }
  74. public static void CopyStream (Stream inStream, Stream outStream)
  75. {
  76. byte[] buffer = new byte [1024];
  77. int nr;
  78. while ((nr = inStream.Read (buffer, 0, buffer.Length)) > 0)
  79. outStream.Write (buffer, 0, nr);
  80. outStream.Flush ();
  81. if (outStream.CanSeek)
  82. outStream.Seek (0,SeekOrigin.Begin);
  83. }
  84. public static String GetHostName()
  85. {
  86. string hostName = Dns.GetHostName();
  87. if (hostName == null)
  88. {
  89. throw new ArgumentNullException("hostName");
  90. }
  91. return hostName;
  92. } // GetHostName
  93. public static String GetMachineName()
  94. {
  95. String machineName = GetHostName();
  96. if (machineName != null)
  97. machineName= Dns.GetHostByName(machineName).HostName;
  98. if (machineName== null)
  99. {
  100. throw new ArgumentNullException("machine");
  101. }
  102. return machineName;
  103. }
  104. }
  105. }