3 using System
.Net
.Sockets
;
7 using System
.Collections
;
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());
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
);
39 private static void NetWrite(Socket sock
, String data
) {
40 byte[] buf
=new UTF8Encoding().GetBytes(data
);
45 private static void ReplyHeaders(Socket sock
, int code
,
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
81 int pos
=data
.IndexOf("\r\n");
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");
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
) {
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[]{' '}
);
121 req
=req
.Remove(pos
, req
.Length
-pos
);
124 pos
=req
.LastIndexOf('.');
127 filetype
= req
.Substring(pos
);
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
);
141 FileStream f
=new FileStream(req
, FileMode
.Open
, FileAccess
.Read
);
142 byte[] fbuf
=new byte[256];
144 ReplyHeaders(sock
, 200, "OK",
149 while((count
=f
.Read(fbuf
, 0, 256))>0) {
150 // Specify amount, so the last
151 // block doesnt send extra crud at
153 sock
.Send(fbuf
, count
, SocketFlags
.None
);
157 } catch(FileNotFoundException
) {
158 Console
.WriteLine("File not found");
160 } catch(IOException
) {
161 Console
.WriteLine("IO error");
166 private static void Head(Socket sock
, String data
) {
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[]{' '}
);
183 req
=req
.Remove(pos
, req
.Length
-pos
);
186 pos
=req
.LastIndexOf('.');
189 filetype
=req
.Substring(pos
);
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
);
200 FileStream f
=new FileStream(req
, FileMode
.Open
, FileAccess
.Read
);
201 byte[] fbuf
=new byte[256];
203 ReplyHeaders(sock
, 200, "OK",
208 } catch(FileNotFoundException
) {
209 Console
.WriteLine("File not found");
211 } catch(IOException
) {
212 Console
.WriteLine("IO error");
217 public static int Main (string [] args
) {
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"){
231 Socket newsock
=s
.Accept();
232 String req
=NetRead(newsock
);
234 if(String
.Compare(req
, 0, "GET ", 0, 4)==0) {
236 } else if(String
.Compare(req
, 0, "HEAD ", 0, 5)==0) {
239 Console
.WriteLine("Unknown method!");
240 Console
.WriteLine("[" + req
+ "]");