2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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 void CopyStream (Stream inStream, Stream outStream)
77                 {
78                         byte[] buffer = new byte [1024];
79
80                         int nr;
81                         while ((nr = inStream.Read (buffer, 0, buffer.Length)) > 0)
82                                 outStream.Write (buffer, 0, nr);
83
84                         outStream.Flush ();
85
86                         if (outStream.CanSeek)
87                                 outStream.Seek (0,SeekOrigin.Begin);
88                 }
89
90                 public static String GetHostName()
91                 {
92                                 string hostName = Dns.GetHostName();
93
94                                 if (hostName == null)
95                                 {
96                                         throw new ArgumentNullException("hostName");
97                                 }
98
99                         return hostName;
100                 } // GetHostName
101
102                 public static String GetMachineName()
103                 {
104                         
105                         String machineName = GetHostName();
106                         if (machineName != null)
107                                 machineName= Dns.GetHostByName(machineName).HostName;
108                                 
109                         if (machineName== null)
110                         {
111                                 throw new ArgumentNullException("machine");
112                         }
113                                     
114                         return machineName;      
115                 } 
116         }
117 }