Copied remotely
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.Http / HttpHelper.cs
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        popsito82@hotmail.com
10 //              Ahmad    Kadry          kadrianoz@hotmail.com
11 //              Hussein  Mehanna        hussein_mehanna@hotmail.com
12 //
13 //==========================================================================
14
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36
37 using System;
38 using System.IO;
39 using System.Net;
40 using System.Text.RegularExpressions;
41
42 namespace System.Runtime.Remoting.Channels.Http
43 {
44         internal class HttpHelper
45         {
46                 public static string Parse(string URL , out string ObjectURI)
47                 {
48
49                         int Pos;
50                         ObjectURI = null;
51                         string ChannelURI = null;
52                         
53
54                         if(StartsWithHttp(URL))
55                         {
56                                 Pos = URL.IndexOf("/",7);
57                                 if(Pos >= 0) 
58                                 {
59                                         ObjectURI = URL.Substring(Pos);
60                                         ChannelURI = URL.Substring(0, Pos);
61                                 }
62                                 else ChannelURI = URL;
63                         }
64                         return ChannelURI;
65                         
66                 }
67
68                 public static bool StartsWithHttp(string URL)
69                 {
70                         if(URL.StartsWith("http://"))
71                                 return true;
72                         else
73                                 return false;
74                 }
75
76                 public static Stream CopyStream(Stream inStream)
77                 {
78                         
79                         Stream outStream = new MemoryStream();
80                         
81                         int  temp;
82                         
83                         try
84                         {
85                                 while(true)
86                                 {
87                                         temp = inStream.ReadByte();
88                                         if(temp==-1)
89                                                 break;
90                                         outStream.WriteByte((byte)temp);
91                                 }
92                                 outStream.Flush();
93                                 outStream.Seek(0,SeekOrigin.Begin);
94                         }
95                         catch(Exception e)
96                         {
97                                 Console.WriteLine(e);
98                         }
99                         
100                         return outStream;
101                 }
102
103                 public static bool CopyStream(Stream inStream, Stream outStream)
104                 {
105                         int  temp;
106
107                         try
108                         {
109                                 while(true)
110                                 {
111                                         temp = inStream.ReadByte();
112                                                                                 
113                                         if(temp==-1)
114                                                 break;
115                                         outStream.WriteByte((byte)temp);
116                                 }
117                                 
118                                 outStream.Flush();
119                                 
120                                 if(outStream.CanSeek)
121                                         outStream.Seek(0,SeekOrigin.Begin);
122                         }
123                         catch(Exception e)
124                         {
125                                 Console.WriteLine(e);
126                                 return false;
127                         }
128                         return true;
129                         
130                 }
131
132                 public static String GetHostName()
133                 {
134                                 string hostName = Dns.GetHostName();
135
136                                 if (hostName == null)
137                                 {
138                                         throw new ArgumentNullException("hostName");
139                                 }
140
141                         return hostName;
142                 } // GetHostName
143
144                 public static String GetMachineName()
145                 {
146                         
147                         String machineName = GetHostName();
148                         if (machineName != null)
149                                 machineName= Dns.GetHostByName(machineName).HostName;
150                                 
151                         if (machineName== null)
152                         {
153                                 throw new ArgumentNullException("machine");
154                         }
155                                     
156                         return machineName;      
157                 } 
158         }
159 }