HttpHelper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 Stream CopyStream(Stream inStream)
  67. {
  68. Stream outStream = new MemoryStream();
  69. int temp;
  70. try
  71. {
  72. while(true)
  73. {
  74. temp = inStream.ReadByte();
  75. if(temp==-1)
  76. break;
  77. outStream.WriteByte((byte)temp);
  78. }
  79. outStream.Flush();
  80. outStream.Seek(0,SeekOrigin.Begin);
  81. }
  82. catch(Exception e)
  83. {
  84. Console.WriteLine(e);
  85. }
  86. return outStream;
  87. }
  88. public static bool CopyStream(Stream inStream, Stream outStream)
  89. {
  90. int temp;
  91. try
  92. {
  93. while(true)
  94. {
  95. temp = inStream.ReadByte();
  96. if(temp==-1)
  97. break;
  98. outStream.WriteByte((byte)temp);
  99. }
  100. outStream.Flush();
  101. if(outStream.CanSeek)
  102. outStream.Seek(0,SeekOrigin.Begin);
  103. }
  104. catch(Exception e)
  105. {
  106. Console.WriteLine(e);
  107. return false;
  108. }
  109. return true;
  110. }
  111. public static String GetHostName()
  112. {
  113. string hostName = Dns.GetHostName();
  114. if (hostName == null)
  115. {
  116. throw new ArgumentNullException("hostName");
  117. }
  118. return hostName;
  119. } // GetHostName
  120. public static String GetMachineName()
  121. {
  122. String machineName = GetHostName();
  123. if (machineName != null)
  124. machineName= Dns.GetHostByName(machineName).HostName;
  125. if (machineName== null)
  126. {
  127. throw new ArgumentNullException("machine");
  128. }
  129. return machineName;
  130. }
  131. }
  132. }