flush forgotten changelog
[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.Globalization;
39 using System.IO;
40 using System.Net;
41 using System.Text.RegularExpressions;
42
43 namespace System.Runtime.Remoting.Channels.Http
44 {
45         internal class HttpHelper
46         {
47                 public static string Parse(string URL , out string ObjectURI)
48                 {
49                         int Pos;
50                         ObjectURI = null;
51                         string ChannelURI = null;
52                         
53                         Pos = IndexOfChannelUri (URL);
54                         if(Pos > 0)
55                         {
56                                 Pos = URL.IndexOf("/", Pos);
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                 static int IndexOfChannelUri(string URL)
69                 {
70                         CompareInfo ci = CultureInfo.InvariantCulture.CompareInfo;
71
72                         if (ci.IsPrefix (URL, "http://", CompareOptions.IgnoreCase))
73                                 return "http://".Length;
74
75                         if (ci.IsPrefix (URL, "https://", CompareOptions.IgnoreCase))
76                                 return "https://".Length;
77
78                         return -1;
79                 }
80
81                 public static bool StartsWithHttp (string url)
82                 {
83                         return IndexOfChannelUri (url) > 0;
84                 }
85
86                 public static void CopyStream (Stream inStream, Stream outStream)
87                 {
88                         byte[] buffer = new byte [1024];
89
90                         int nr;
91                         while ((nr = inStream.Read (buffer, 0, buffer.Length)) > 0)
92                                 outStream.Write (buffer, 0, nr);
93
94                         outStream.Flush ();
95
96                         if (outStream.CanSeek)
97                                 outStream.Seek (0,SeekOrigin.Begin);
98                 }
99
100                 public static String GetHostName()
101                 {
102                                 string hostName = Dns.GetHostName();
103
104                                 if (hostName == null)
105                                 {
106                                         throw new ArgumentNullException("hostName");
107                                 }
108
109                         return hostName;
110                 } // GetHostName
111
112                 public static String GetMachineName()
113                 {
114                         
115                         String machineName = GetHostName();
116                         if (machineName != null)
117                                 machineName= Dns.GetHostByName(machineName).HostName;
118                                 
119                         if (machineName== null)
120                         {
121                                 throw new ArgumentNullException("machine");
122                         }
123                                     
124                         return machineName;      
125                 } 
126         }
127 }