2007-12-06 Jb Evain <jbevain@novell.com>
[mono.git] / mono / tests / mis.cs
blobd6bb5b22a8c97f347b6048451b1e037325dbb247
2 using System.Net;
3 using System.Net.Sockets;
4 using System.IO;
5 using System;
6 using System.Text;
7 using System.Collections;
9 namespace T {
10 public class T {
12 private static String docroot="/home/dick/mono/install/html";
13 //private static String docroot="./";
15 private static Hashtable mime_types = new Hashtable();
17 private static Socket NetSetup() {
18 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
19 s.Bind(new IPEndPoint(IPAddress.Any, 8000));
21 Console.WriteLine("Listening on " + s.LocalEndPoint.ToString());
23 s.Listen(5);
25 return(s);
28 private static String NetRead(Socket sock) {
29 byte[] buf=new byte[256];
31 int count=sock.Receive(buf);
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);
39 private static void NetWrite(Socket sock, String data) {
40 byte[] buf=new UTF8Encoding().GetBytes(data);
42 sock.Send(buf);
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");
58 NetWrite(sock, "Content-type: " + content_type);
59 if(content_opt!=null) {
60 NetWrite(sock, "; " + content_opt);
62 NetWrite(sock, "\r\n");
63 NetWrite(sock, "\r\n");
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");
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");
88 req=data.Remove(pos, data.Length-pos);
90 // We've isolated the request line, now get the headers
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");
100 String hdr=data.Remove(0, req.Length+2);
101 headers=hdr.Split(new char[]{'\r', '\n'});
104 private static void Get(Socket sock, String data) {
105 String req;
106 String[] headers;
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]);
115 // Remove the method, and prepend the docroot
116 req=String.Concat(docroot, req.Remove(0, 4));
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);
124 pos=req.LastIndexOf('.');
125 String filetype;
126 if (pos != -1)
127 filetype = req.Substring(pos);
128 else
129 filetype = "";
133 string mime_type = (string) mime_types [filetype];
134 if (mime_type == null)
135 mime_type = "text/plain";
137 Console.WriteLine("File is " + req);
138 Console.WriteLine("Mime type is " + mime_type);
140 try {
141 FileStream f=new FileStream(req, FileMode.Open, FileAccess.Read);
142 byte[] fbuf=new byte[256];
144 ReplyHeaders(sock, 200, "OK",
145 mime_type,
146 null, f.Length);
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);
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);
166 private static void Head(Socket sock, String data) {
167 String req;
168 String[] headers;
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]);
177 // Remove the method, and prepend the docroot
178 req=String.Concat(docroot, req.Remove(0, 5));
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);
186 pos=req.LastIndexOf('.');
187 string filetype;
188 if (pos != -1)
189 filetype=req.Substring(pos);
190 else
191 filetype = "";
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);
199 try {
200 FileStream f=new FileStream(req, FileMode.Open, FileAccess.Read);
201 byte[] fbuf=new byte[256];
203 ReplyHeaders(sock, 200, "OK",
204 mime_type,
205 null, f.Length);
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);
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");
224 if (args.Length == 2 && args [0] == "--root"){
225 docroot = args [1];
228 Socket s=NetSetup();
230 while(true) {
231 Socket newsock=s.Accept();
232 String req=NetRead(newsock);
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 + "]");
243 newsock.Close();