0
|
1 ???using System; |
|
2 using System.Collections.Generic; |
|
3 using System.IO; |
3
|
4 using System.Net; |
|
5 using System.Net.Sockets; |
0
|
6 using System.Text; |
3
|
7 using UCIS.Util; |
|
8 using HTTPHeader = System.Collections.Generic.KeyValuePair<string, string>; |
0
|
9 |
|
10 namespace UCIS.Net.HTTP { |
3
|
11 public class HTTPServer : TCPServer.IModule, IDisposable { |
|
12 public IHTTPContentProvider ContentProvider { get; set; } |
|
13 public Boolean ServeFlashPolicyFile { get; set; } |
|
14 private Socket Listener = null; |
0
|
15 |
3
|
16 public HTTPServer() { } |
0
|
17 |
3
|
18 public void Listen(int port) { |
|
19 Listen(new IPEndPoint(IPAddress.Any, port)); |
0
|
20 } |
|
21 |
3
|
22 public void Listen(EndPoint localep) { |
|
23 if (Listener != null) throw new InvalidOperationException("A listener exists"); |
|
24 Listener = new Socket(localep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); |
|
25 Listener.Bind(localep); |
|
26 Listener.Listen(5); |
|
27 Listener.BeginAccept(AcceptCallback, null); |
0
|
28 } |
|
29 |
3
|
30 private void AcceptCallback(IAsyncResult ar) { |
|
31 try { |
|
32 Socket socket = Listener.EndAccept(ar); |
|
33 new HTTPContext(this, socket); |
|
34 } catch (Exception) { } |
|
35 try { |
|
36 Listener.BeginAccept(AcceptCallback, null); |
|
37 } catch (Exception) { } |
0
|
38 } |
|
39 |
3
|
40 public void Dispose() { |
|
41 if (Listener != null) Listener.Close(); |
0
|
42 } |
|
43 |
3
|
44 bool TCPServer.IModule.Accept(TCPStream stream) { |
|
45 new HTTPContext(this, stream); |
|
46 return false; |
0
|
47 } |
|
48 } |
|
49 |
|
50 public class HTTPContext { |
3
|
51 public HTTPServer Server { get; private set; } |
|
52 public EndPoint LocalEndPoint { get; private set; } |
|
53 public EndPoint RemoteEndPoint { get; private set; } |
|
54 |
|
55 public String RequestMethod { get; private set; } |
|
56 public String RequestPath { get; private set; } |
|
57 public String RequestQuery { get; private set; } |
|
58 public int HTTPVersion { get; set; } |
|
59 |
|
60 public Socket Socket { get; private set; } |
|
61 public Boolean SuppressStandardHeaders { get; set; } |
|
62 |
|
63 private Stream Stream; |
|
64 private StreamWriter Writer; |
|
65 private List<HTTPHeader> RequestHeaders; |
|
66 private HTTPConnectionState State = HTTPConnectionState.Starting; |
|
67 |
|
68 private enum HTTPConnectionState { |
|
69 Starting = 0, |
|
70 ReceivingRequest = 1, |
|
71 ProcessingRequest = 2, |
|
72 SendingHeaders = 3, |
|
73 SendingContent = 4, |
|
74 Closed = 5, |
|
75 } |
|
76 |
|
77 public HTTPContext(HTTPServer server, TCPStream stream) { |
|
78 this.Server = server; |
|
79 this.Socket = stream.Socket; |
|
80 this.LocalEndPoint = Socket.LocalEndPoint; |
|
81 this.RemoteEndPoint = Socket.RemoteEndPoint; |
|
82 this.Stream = stream; |
|
83 Init(); |
|
84 } |
|
85 |
|
86 public HTTPContext(HTTPServer server, Socket socket) { |
|
87 this.Server = server; |
|
88 this.Socket = socket; |
|
89 this.LocalEndPoint = socket.LocalEndPoint; |
|
90 this.RemoteEndPoint = socket.RemoteEndPoint; |
|
91 this.Stream = new NetworkStream(socket, true); |
|
92 Init(); |
|
93 } |
|
94 |
|
95 private void Init() { |
|
96 Writer = new StreamWriter(Stream, Encoding.ASCII); |
|
97 Writer.NewLine = "\r\n"; |
|
98 Writer.AutoFlush = true; |
|
99 UCIS.ThreadPool.RunTask(ReceiveOperation, null); |
|
100 } |
|
101 |
|
102 private String ReadLine() { |
|
103 StringBuilder s = new StringBuilder(); |
|
104 while (true) { |
|
105 int b = Stream.ReadByte(); |
|
106 if (b == -1) { |
|
107 if (s.Length == null) return null; |
|
108 break; |
|
109 } else if (b == 13) { |
|
110 } else if (b == 10) { |
|
111 break; |
|
112 } else { |
|
113 s.Append((Char)b); |
|
114 } |
|
115 } |
|
116 return s.ToString(); |
|
117 } |
|
118 |
|
119 private void ReceiveOperation(Object state) { |
|
120 State = HTTPConnectionState.ReceivingRequest; |
|
121 try { |
|
122 String line = ReadLine(); |
|
123 if (line == null) { |
|
124 Close(); |
|
125 return; |
|
126 } |
|
127 if (Server.ServeFlashPolicyFile && line[0] == '<') { //<policy-file-request/> |
|
128 Writer.WriteLine("<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>"); |
|
129 Stream.WriteByte(0); |
|
130 Close(); |
|
131 return; |
|
132 } |
|
133 String[] request = line.Split(' '); |
|
134 if (request.Length != 3) goto SendError400AndClose; |
|
135 RequestMethod = request[0]; |
|
136 String RequestAddress = request[1]; |
|
137 switch (request[2]) { |
|
138 case "HTTP/1.0": HTTPVersion = 10; break; |
|
139 case "HTTP/1.1": HTTPVersion = 11; break; |
|
140 default: goto SendError400AndClose; |
|
141 } |
|
142 request = RequestAddress.Split(new Char[] { '?' }); |
|
143 RequestPath = Uri.UnescapeDataString(request[0]); |
|
144 RequestQuery = request.Length > 1 ? request[1] : null; |
|
145 RequestHeaders = new List<HTTPHeader>(); |
|
146 while (true) { |
|
147 line = ReadLine(); |
|
148 if (line == null) goto SendError400AndClose; |
|
149 if (line.Length == 0) break; |
|
150 request = line.Split(new String[] { ": " }, 2, StringSplitOptions.None); |
|
151 if (request.Length != 2) goto SendError400AndClose; |
|
152 RequestHeaders.Add(new HTTPHeader(request[0], request[1])); |
|
153 } |
|
154 IHTTPContentProvider content = Server.ContentProvider; |
|
155 if (content == null) goto SendError500AndClose; |
|
156 State = HTTPConnectionState.ProcessingRequest; |
|
157 content.ServeRequest(this); |
|
158 } catch (Exception ex) { |
|
159 Console.Error.WriteLine(ex); |
|
160 switch (State) { |
|
161 case HTTPConnectionState.ProcessingRequest: goto SendError500AndClose; |
|
162 default: |
|
163 Close(); |
|
164 break; |
|
165 } |
|
166 } |
|
167 return; |
|
168 |
|
169 SendError400AndClose: |
|
170 SendErrorAndClose(400); |
|
171 return; |
|
172 SendError500AndClose: |
|
173 SendErrorAndClose(500); |
|
174 return; |
|
175 } |
|
176 |
|
177 public String GetRequestHeader(String name) { |
|
178 if (State != HTTPConnectionState.ProcessingRequest && State != HTTPConnectionState.SendingHeaders && State != HTTPConnectionState.SendingContent) throw new InvalidOperationException(); |
|
179 foreach (HTTPHeader h in RequestHeaders) { |
|
180 if (name.Equals(h.Key, StringComparison.OrdinalIgnoreCase)) return h.Value; |
|
181 } |
|
182 return null; |
|
183 } |
|
184 public String[] GetRequestHeaders(String name) { |
|
185 if (State != HTTPConnectionState.ProcessingRequest && State != HTTPConnectionState.SendingHeaders && State != HTTPConnectionState.SendingContent) throw new InvalidOperationException(); |
|
186 String[] items = new String[0]; |
|
187 foreach (HTTPHeader h in RequestHeaders) { |
|
188 if (name.Equals(h.Key, StringComparison.OrdinalIgnoreCase)) ArrayUtil.Add(ref items, h.Value); |
|
189 } |
|
190 return items; |
|
191 } |
|
192 |
|
193 public void SendErrorAndClose(int state) { |
|
194 try { |
|
195 SendStatus(state); |
|
196 GetResponseStream(); |
|
197 } catch (Exception ex) { |
|
198 Console.Error.WriteLine(ex); |
|
199 } |
|
200 Close(); |
|
201 } |
|
202 |
|
203 public void SendStatus(int code) { |
|
204 String message; |
|
205 switch (code) { |
|
206 case 101: message = "Switching Protocols"; break; |
|
207 case 200: message = "OK"; break; |
|
208 case 400: message = "Bad Request"; break; |
|
209 case 404: message = "Not Found"; break; |
|
210 case 500: message = "Internal Server Error"; break; |
|
211 default: message = "Unknown Status"; break; |
|
212 } |
|
213 SendStatus(code, message); |
|
214 } |
|
215 public void SendStatus(int code, String message) { |
|
216 if (State != HTTPConnectionState.ProcessingRequest) throw new InvalidOperationException(); |
|
217 StringBuilder sb = new StringBuilder(); |
|
218 sb.Append("HTTP/"); |
|
219 switch (HTTPVersion) { |
|
220 case 10: sb.Append("1.0"); break; |
|
221 case 11: sb.Append("1.1"); break; |
|
222 default: throw new ArgumentException("The HTTP version is not supported", "HTTPVersion"); |
|
223 } |
|
224 sb.Append(" "); |
|
225 sb.Append(code); |
|
226 sb.Append(" "); |
|
227 sb.Append(message); |
|
228 Writer.WriteLine(sb.ToString()); |
|
229 State = HTTPConnectionState.SendingHeaders; |
|
230 if (!SuppressStandardHeaders) { |
|
231 SendHeader("Expires", "Expires: Sun, 1 Jan 2000 00:00:00 GMT"); |
|
232 SendHeader("Cache-Control", "no-store, no-cache, must-revalidate"); |
|
233 SendHeader("Cache-Control", "post-check=0, pre-check=0"); |
|
234 SendHeader("Pragma", "no-cache"); |
|
235 SendHeader("Server", "UCIS Webserver"); |
|
236 SendHeader("Connection", "Close"); |
|
237 } |
|
238 } |
|
239 public void SendHeader(String name, String value) { |
|
240 if (State == HTTPConnectionState.ProcessingRequest) SendStatus(200); |
|
241 if (State != HTTPConnectionState.SendingHeaders) throw new InvalidOperationException(); |
|
242 Writer.WriteLine(name + ": " + value); |
|
243 } |
|
244 public Stream GetResponseStream() { |
|
245 if (State == HTTPConnectionState.ProcessingRequest) SendStatus(200); |
|
246 if (State == HTTPConnectionState.SendingHeaders) { |
|
247 Writer.WriteLine(); |
|
248 State = HTTPConnectionState.SendingContent; |
|
249 } |
|
250 if (State != HTTPConnectionState.SendingContent) throw new InvalidOperationException(); |
|
251 return Stream; |
|
252 } |
|
253 |
|
254 public void Close() { |
|
255 if (State == HTTPConnectionState.Closed) return; |
|
256 Stream.Close(); |
|
257 State = HTTPConnectionState.Closed; |
|
258 } |
0
|
259 } |
|
260 |
3
|
261 public interface IHTTPContentProvider { |
|
262 void ServeRequest(HTTPContext context); |
|
263 } |
|
264 public class HTTPPathSelector : IHTTPContentProvider { |
|
265 private List<KeyValuePair<String, IHTTPContentProvider>> Prefixes; |
|
266 private StringComparison PrefixComparison; |
|
267 public HTTPPathSelector() : this(false) { } |
|
268 public HTTPPathSelector(Boolean caseSensitive) { |
|
269 Prefixes = new List<KeyValuePair<string, IHTTPContentProvider>>(); |
|
270 PrefixComparison = caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; |
|
271 } |
|
272 public void AddPrefix(String prefix, IHTTPContentProvider contentProvider) { |
|
273 Prefixes.Add(new KeyValuePair<string, IHTTPContentProvider>(prefix, contentProvider)); |
|
274 } |
|
275 public void DeletePrefix(String prefix) { |
|
276 Prefixes.RemoveAll(delegate(KeyValuePair<string, IHTTPContentProvider> item) { return prefix.Equals(item.Key, PrefixComparison); }); |
|
277 } |
|
278 public void ServeRequest(HTTPContext context) { |
|
279 KeyValuePair<string, IHTTPContentProvider> c = Prefixes.Find(delegate(KeyValuePair<string, IHTTPContentProvider> item) { return context.RequestPath.StartsWith(item.Key, PrefixComparison); }); |
|
280 if (c.Value != null) { |
|
281 c.Value.ServeRequest(context); |
|
282 } else { |
|
283 context.SendErrorAndClose(404); |
|
284 } |
|
285 } |
|
286 } |
|
287 public class HTTPFileProvider : IHTTPContentProvider { |
|
288 public String FileName { get; private set; } |
|
289 public String ContentType { get; private set; } |
|
290 public HTTPFileProvider(String fileName) : this(fileName, "application/octet-stream") { } |
|
291 public HTTPFileProvider(String fileName, String contentType) { |
|
292 this.FileName = fileName; |
|
293 this.ContentType = contentType; |
|
294 } |
|
295 public void ServeRequest(HTTPContext context) { |
|
296 if (File.Exists(FileName)) { |
|
297 using (FileStream fs = File.OpenRead(FileName)) { |
|
298 context.SendStatus(200); |
|
299 context.SendHeader("Content-Type", ContentType); |
|
300 context.SendHeader("Content-Length", fs.Length.ToString()); |
|
301 long left = fs.Length; |
|
302 Stream response = context.GetResponseStream(); |
|
303 byte[] buffer = new byte[1024 * 10]; |
|
304 while (fs.CanRead) { |
|
305 int len = fs.Read(buffer, 0, buffer.Length); |
|
306 if (len <= 0) break; |
|
307 left -= len; |
|
308 response.Write(buffer, 0, len); |
|
309 } |
|
310 response.Close(); |
|
311 } |
|
312 } else { |
|
313 context.SendErrorAndClose(404); |
|
314 } |
|
315 } |
0
|
316 } |
3
|
317 public class HTTPUnTarchiveProvider : IHTTPContentProvider { |
|
318 public String TarFileName { get; private set; } |
|
319 public HTTPUnTarchiveProvider(String tarFile) { |
|
320 this.TarFileName = tarFile; |
|
321 } |
|
322 public void ServeRequest(HTTPContext context) { |
|
323 if (!File.Exists(TarFileName)) { |
|
324 context.SendErrorAndClose(404); |
|
325 return; |
|
326 } |
|
327 String reqname1 = context.RequestPath; |
|
328 if (reqname1.Length > 0 && reqname1[0] == '/') reqname1 = reqname1.Substring(1); |
|
329 String reqname2 = reqname1; |
|
330 if (reqname2.Length > 0 && !reqname2.EndsWith("/")) reqname2 += "/"; |
|
331 reqname2 += "index.htm"; |
|
332 using (FileStream fs = File.OpenRead(TarFileName)) { |
|
333 while (true) { |
|
334 Byte[] header = new Byte[512]; |
|
335 if (fs.Read(header, 0, 512) != 512) break; |
|
336 int flen = Array.IndexOf<Byte>(header, 0, 0, 100); |
|
337 if (flen == 0) continue; |
|
338 if (flen == -1) flen = 100; |
|
339 String fname = Encoding.ASCII.GetString(header, 0, flen); |
|
340 String fsize = Encoding.ASCII.GetString(header, 124, 11); |
|
341 int fsizei = Convert.ToInt32(fsize, 8); |
|
342 if (reqname1.Equals(fname, StringComparison.OrdinalIgnoreCase) || reqname2.Equals(fname)) { |
|
343 context.SendStatus(200); |
|
344 context.SendHeader("Content-Length", fsizei.ToString()); |
|
345 String ctype = null; |
|
346 switch (Path.GetExtension(fname).ToUpperInvariant()) { |
|
347 case ".txt": ctype = "text/plain"; break; |
|
348 case ".htm": |
|
349 case ".html": ctype = "text/html"; break; |
|
350 case ".css": ctype = "text/css"; break; |
|
351 case ".js": ctype = "application/x-javascript"; break; |
|
352 case ".png": ctype = "image/png"; break; |
|
353 case ".jpg": |
|
354 case ".jpeg": ctype = "image/jpeg"; break; |
|
355 case ".gif": ctype = "image/gif"; break; |
|
356 case ".ico": ctype = "image/x-icon"; break; |
|
357 } |
|
358 if (ctype != null) context.SendHeader("Content-Type", ctype); |
|
359 Stream response = context.GetResponseStream(); |
|
360 int left = fsizei; |
|
361 while (left > 0) { |
|
362 byte[] buffer = new byte[1024 * 10]; |
|
363 int len = fs.Read(buffer, 0, buffer.Length); |
|
364 if (len <= 0) break; |
|
365 left -= len; |
|
366 response.Write(buffer, 0, len); |
|
367 } |
|
368 response.Close(); |
|
369 return; |
|
370 } else { |
|
371 fs.Seek(fsizei, SeekOrigin.Current); |
|
372 } |
|
373 int padding = fsizei % 512; |
|
374 if (padding != 0) padding = 512 - padding; |
|
375 fs.Seek(padding, SeekOrigin.Current); |
|
376 } |
|
377 } |
|
378 context.SendErrorAndClose(404); |
|
379 } |
0
|
380 } |
|
381 } |