Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / mis.cs
1
2 using System.Net;
3 using System.Net.Sockets;
4 using System.IO;
5 using System;
6 using System.Text;
7 using System.Collections;
8
9 namespace T {
10         public class T {
11
12                 private static String docroot="/home/dick/mono/install/html";
13                 //private static String docroot="./";
14
15                 private static Hashtable mime_types = new Hashtable();
16
17                 private static Socket NetSetup() {
18                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
19                         s.Bind(new IPEndPoint(IPAddress.Any, 8000));
20
21                         Console.WriteLine("Listening on " + s.LocalEndPoint.ToString());
22
23                         s.Listen(5);
24
25                         return(s);
26                 }
27
28                 private static String NetRead(Socket sock) {
29                         byte[] buf=new byte[256];
30
31                         int count=sock.Receive(buf);
32
33                         // Supply the length because otherwise I get a
34                         // string of 260-odd chars instead of 30 for some reason
35                         String req=new String(Encoding.UTF8.GetChars(buf), 0, count);
36                         return(req);
37                 }
38
39                 private static void NetWrite(Socket sock, String data) {
40                         byte[] buf=new UTF8Encoding().GetBytes(data);
41
42                         sock.Send(buf);
43                 }
44
45                 private static void ReplyHeaders(Socket sock, int code,
46                                                  String detail,
47                                                  String content_type,
48                                                  String content_opt,
49                                                  long content_length) {
50                         NetWrite(sock, "HTTP/1.0 " + code + " " + detail + "\r\n");
51                         NetWrite(sock, "Date: Sat, 12 Jan 2002 01:52:56 GMT\r\n");
52                         NetWrite(sock, "Server: MIS\r\n");
53                         NetWrite(sock, "Last-Modified: Sat, 12 Jan 2002 01:52:56 GMT\r\n");
54                         NetWrite(sock, "Connection: close\r\n");
55                         if(content_length>0) {
56                                 NetWrite(sock, "Content-Length: " + content_length + "\r\n");
57                         }
58                         NetWrite(sock, "Content-type: " + content_type);
59                         if(content_opt!=null) {
60                                 NetWrite(sock, "; " + content_opt);
61                         }
62                         NetWrite(sock, "\r\n");
63                         NetWrite(sock, "\r\n");
64                 }
65
66                 private static void NotFound(Socket sock) {
67                         ReplyHeaders(sock, 404, "Not Found", "text/html",
68                                      "charset=iso-8859-1", 0);
69                         NetWrite(sock, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n");
70                         NetWrite(sock, "<HTML><HEAD>\r\n");
71                         NetWrite(sock, "<TITLE>404 Not Found</TITLE>\r\n");
72                         NetWrite(sock, "</HEAD><BODY>\r\n");
73                         NetWrite(sock, "<H1>Not Found</H1>\r\n");
74                         NetWrite(sock, "</BODY></HTML>\r\n");
75                 }
76
77                 static void GetHeaders(out String req, out String[] headers,
78                                        String data, Socket sock) {
79                         // First, find the \r\n denoting the end of the
80                         // request line
81                         int pos=data.IndexOf("\r\n");
82                         while(pos==-1) {
83                                 Console.WriteLine("Couldn't isolate request");
84                                 data=data+NetRead(sock);
85                                 pos=data.IndexOf("\r\n");
86                         }
87
88                         req=data.Remove(pos, data.Length-pos);
89
90                         // We've isolated the request line, now get the headers
91
92                         // Make sure we have all the headers
93                         pos=data.IndexOf("\r\n\r\n");
94                         while(pos==-1) {
95                                 //Console.WriteLine("Didn't read all the headers");
96                                 data=data+NetRead(sock);
97                                 pos=data.IndexOf("\r\n\r\n");
98                         }
99
100                         String hdr=data.Remove(0, req.Length+2);
101                         headers=hdr.Split(new char[]{'\r', '\n'});
102                 }
103
104                 private static void Get(Socket sock, String data) {
105                         String req;
106                         String[] headers;
107
108                         GetHeaders(out req, out headers, data, sock);
109                         for(int i=0; i<headers.Length; i++) {
110                                 if(headers[i].StartsWith("User-Agent: ")) {
111                                         Console.WriteLine(headers[i]);
112                                 }
113                         }
114
115                         // Remove the method, and prepend the docroot
116                         req=String.Concat(docroot, req.Remove(0, 4));
117
118                         // Trim the trailing protocol info
119                         int pos=req.IndexOfAny(new char[]{' '});
120                         if(pos>=0) {
121                                 req=req.Remove(pos, req.Length-pos);
122                         }
123
124                         pos=req.LastIndexOf('.');
125                         String filetype;
126                         if (pos != -1)
127                                 filetype = req.Substring(pos);
128                         else
129                                 filetype = "";
130                         
131
132
133                         string mime_type = (string) mime_types [filetype];
134                         if (mime_type == null)
135                                 mime_type = "text/plain";
136                         
137                         Console.WriteLine("File is " + req);
138                         Console.WriteLine("Mime type is " + mime_type);
139                         
140                         try {
141                                 FileStream f=new FileStream(req, FileMode.Open, FileAccess.Read);
142                                 byte[] fbuf=new byte[256];
143
144                                 ReplyHeaders(sock, 200, "OK",
145                                              mime_type,
146                                              null, f.Length);
147
148                                 int count;
149                                 while((count=f.Read(fbuf, 0, 256))>0) {
150                                         // Specify amount, so the last
151                                         // block doesnt send extra crud at
152                                         // the end
153                                         sock.Send(fbuf, count, SocketFlags.None);
154                                 }
155
156                                 f.Close();
157                         } catch(FileNotFoundException) {
158                                 Console.WriteLine("File not found");
159                                 NotFound(sock);
160                         } catch(IOException) {
161                                 Console.WriteLine("IO error");
162                                 NotFound(sock);
163                         }
164                 }
165
166                 private static void Head(Socket sock, String data) {
167                         String req;
168                         String[] headers;
169
170                         GetHeaders(out req, out headers, data, sock);
171                         for(int i=0; i<headers.Length; i++) {
172                                 if(headers[i].StartsWith("User-Agent: ")) {
173                                         Console.WriteLine(headers[i]);
174                                 }
175                         }
176
177                         // Remove the method, and prepend the docroot
178                         req=String.Concat(docroot, req.Remove(0, 5));
179
180                         // Trim the trailing protocol info
181                         int pos=req.IndexOfAny(new char[]{' '});
182                         if(pos>=0) {
183                                 req=req.Remove(pos, req.Length-pos);
184                         }
185
186                         pos=req.LastIndexOf('.');
187                         string filetype;
188                         if (pos != -1)
189                                 filetype=req.Substring(pos);
190                         else
191                                 filetype = "";
192
193                         string mime_type = (string) mime_types [filetype];
194                         if (mime_type == null)
195                                 mime_type = "text/plain";
196                         Console.WriteLine("File is " + req);
197                         Console.WriteLine("Mime type is " + mime_type);
198
199                         try {
200                                 FileStream f=new FileStream(req, FileMode.Open, FileAccess.Read);
201                                 byte[] fbuf=new byte[256];
202
203                                 ReplyHeaders(sock, 200, "OK",
204                                              mime_type,
205                                              null, f.Length);
206
207                                 f.Close();
208                         } catch(FileNotFoundException) {
209                                 Console.WriteLine("File not found");
210                                 NotFound(sock);
211                         } catch(IOException) {
212                                 Console.WriteLine("IO error");
213                                 NotFound(sock);
214                         }
215                 }
216
217                 public static int Main (string [] args) {
218                         // Set up mime types
219                         mime_types.Add(".html", "text/html");
220                         mime_types.Add(".jpeg", "image/jpeg");
221                         mime_types.Add(".png", "image/png");
222                         mime_types.Add(".cs", "text/plain");
223
224                         if (args.Length == 2 && args [0] == "--root"){
225                                 docroot = args [1];
226                         }
227                         
228                         Socket s=NetSetup();
229
230                         while(true) {
231                                 Socket newsock=s.Accept();
232                                 String req=NetRead(newsock);
233
234                                 if(String.Compare(req, 0, "GET ", 0, 4)==0) {
235                                         Get(newsock, req);
236                                 } else if(String.Compare(req, 0, "HEAD ", 0, 5)==0) {
237                                         Head(newsock, req);
238                                 } else {
239                                         Console.WriteLine("Unknown method!");
240                                         Console.WriteLine("[" + req + "]");
241                                 }
242
243                                 newsock.Close();
244                         }
245                 }
246         }
247 }