* HttpChannel.cs, HttpClientChannel.cs, HttpHelper.cs, HttpServer.cs,
[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 using System;
17 using System.IO;
18 using System.Net;
19 using System.Text.RegularExpressions;
20
21 namespace System.Runtime.Remoting.Channels.Http
22 {
23         internal class HttpHelper
24         {
25                 public static int d;
26                 public static string Parse(string URL , out string ObjectURI)
27                 {
28
29                         int Pos;
30                         ObjectURI = null;
31                         
32
33                         if(StartsWithHttp(URL))
34                         {
35                                 Pos = URL.IndexOf("/",7);
36                         }
37                         else
38                         {
39                                 Pos = URL.IndexOf("/",0);
40                         }
41
42                         if(Pos>0 || Pos == 0)
43                         {
44                                 ObjectURI = URL.Substring(Pos);
45                                 return URL.Substring(0,Pos);
46                         }
47                         return URL;
48                         
49                 }
50
51                 public static bool StartsWithHttp(string URL)
52                 {
53                         if(URL.StartsWith("http://"))
54                                 return true;
55                         else
56                                 return false;
57                 }
58
59                 public static Stream CopyStream(Stream inStream)
60                 {
61                         
62                         Stream outStream = new MemoryStream();
63                         
64                         int  temp;
65                         
66                         try
67                         {
68                                 while(true)
69                                 {
70                                         temp = inStream.ReadByte();
71                                         if(temp==-1)
72                                                 break;
73                                         outStream.WriteByte((byte)temp);
74                                 }
75                                 outStream.Flush();
76                                 outStream.Seek(0,SeekOrigin.Begin);
77                         }
78                         catch(Exception e)
79                         {
80                                 Console.WriteLine(e);
81                         }
82                         
83                         return outStream;
84                 }
85                 public static bool CopyStream(Stream inStream,ref Stream outStream)
86                 {
87                         int  temp;
88                         d++;
89                         try
90                         {
91                                 FileStream f=null;
92                                 if(d==2)
93                                         f = new FileStream("f.txt",System.IO.FileMode.Create);
94
95                                 while(true)
96                                 {
97                                         temp = inStream.ReadByte();
98                     
99                                         
100                                         if(d==2)
101                                                 f.WriteByte((byte)temp);
102                                                 
103                                         
104                                         if(temp==-1)
105                                                 break;
106                                         outStream.WriteByte((byte)temp);
107                                 }
108                                 
109                                 if(d==2)
110                                 f.Close();
111                                 outStream.Flush();
112                                 
113                                 if(outStream.CanSeek)
114                                         outStream.Seek(0,SeekOrigin.Begin);
115                         }
116                         catch(Exception e)
117                         {
118                                 Console.WriteLine(e);
119                                 return false;
120                         }
121                         return true;
122                         
123                 }
124
125
126                 public static String GetHostName()
127                 {
128                                 string hostName = Dns.GetHostName();
129
130                                 if (hostName == null)
131                                 {
132                                         throw new ArgumentNullException("hostName");
133                                 }
134
135                         return hostName;
136                 } // GetHostName
137
138                 public static String GetMachineName()
139                 {
140                         
141                         String machineName = GetHostName();
142                         if (machineName != null)
143                                 machineName= Dns.GetHostByName(machineName).HostName;
144                                 
145                         if (machineName== null)
146                         {
147                                 throw new ArgumentNullException("machine");
148                         }
149                                     
150                         return machineName;      
151                 } 
152
153                 public static String GetMachineIp()
154                 {
155                         
156                         String hostName = GetMachineName();
157                         String MachineIp=null;
158
159                         IPHostEntry Entries = Dns.GetHostByName(hostName);
160                         if ((Entries.AddressList.Length > 0)&&(Entries != null))
161                         {
162                                 MachineIp = Entries.AddressList[0].ToString();
163                         }               
164
165                         if (MachineIp == null)
166                         {
167                                 throw new ArgumentNullException("ip");
168                         }               
169                         return MachineIp;      
170                 } 
171
172         }
173
174
175 }