HttpHelper.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.IO;
  36. using System.Net;
  37. using System.Text.RegularExpressions;
  38. namespace System.Runtime.Remoting.Channels.Http
  39. {
  40. internal class HttpHelper
  41. {
  42. public static string Parse(string URL , out string ObjectURI)
  43. {
  44. int Pos;
  45. ObjectURI = null;
  46. string ChannelURI = null;
  47. if(StartsWithHttp(URL))
  48. {
  49. Pos = URL.IndexOf("/",7);
  50. if(Pos >= 0)
  51. {
  52. ObjectURI = URL.Substring(Pos);
  53. ChannelURI = URL.Substring(0, Pos);
  54. }
  55. else ChannelURI = URL;
  56. }
  57. return ChannelURI;
  58. }
  59. public static bool StartsWithHttp(string URL)
  60. {
  61. if(URL.StartsWith("http://"))
  62. return true;
  63. else
  64. return false;
  65. }
  66. public static void CopyStream (Stream inStream, Stream outStream)
  67. {
  68. byte[] buffer = new byte [1024];
  69. int nr;
  70. while ((nr = inStream.Read (buffer, 0, buffer.Length)) > 0)
  71. outStream.Write (buffer, 0, nr);
  72. outStream.Flush ();
  73. if (outStream.CanSeek)
  74. outStream.Seek (0,SeekOrigin.Begin);
  75. }
  76. public static String GetHostName()
  77. {
  78. string hostName = Dns.GetHostName();
  79. if (hostName == null)
  80. {
  81. throw new ArgumentNullException("hostName");
  82. }
  83. return hostName;
  84. } // GetHostName
  85. public static String GetMachineName()
  86. {
  87. String machineName = GetHostName();
  88. if (machineName != null)
  89. machineName= Dns.GetHostByName(machineName).HostName;
  90. if (machineName== null)
  91. {
  92. throw new ArgumentNullException("machine");
  93. }
  94. return machineName;
  95. }
  96. }
  97. }