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