Mercurial > hg > ucis.core
comparison Net/HTTP.cs @ 3:0cc7be03775f
Improved HTTP Server components
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Tue, 08 Jan 2013 02:13:27 +0100 |
parents | 3ab940a0c7a0 |
children | 9e2e6433f57a |
comparison
equal
deleted
inserted
replaced
2:d0117dc37c34 | 3:0cc7be03775f |
---|---|
1 using System; | 1 using System; |
2 using System.Collections.Generic; | 2 using System.Collections.Generic; |
3 using System.IO; | 3 using System.IO; |
4 using System.Net; | |
5 using System.Net.Sockets; | |
4 using System.Text; | 6 using System.Text; |
5 using System.Threading; | 7 using UCIS.Util; |
8 using HTTPHeader = System.Collections.Generic.KeyValuePair<string, string>; | |
6 | 9 |
7 namespace UCIS.Net.HTTP { | 10 namespace UCIS.Net.HTTP { |
8 public class HTTPServer : TCPServer.IModule { | 11 public class HTTPServer : TCPServer.IModule, IDisposable { |
9 public Dictionary<string, HTTPContent> Content = new Dictionary<string, HTTPContent>(StringComparer.InvariantCultureIgnoreCase); | 12 public IHTTPContentProvider ContentProvider { get; set; } |
10 public HTTPContent DefaultContent = null; | 13 public Boolean ServeFlashPolicyFile { get; set; } |
11 | 14 private Socket Listener = null; |
12 public bool Accept(TCPStream Stream) { | 15 |
13 StreamReader StreamReader = new StreamReader(Stream, Encoding.ASCII); | 16 public HTTPServer() { } |
14 String Line = StreamReader.ReadLine(); | 17 |
15 String[] Request = Line.Split(' '); | 18 public void Listen(int port) { |
16 | 19 Listen(new IPEndPoint(IPAddress.Any, port)); |
17 //Console.WriteLine("HTTP.Server.Accept Request: " + Line); | 20 } |
18 | 21 |
19 if (Request == null || Request.Length < 2 || Request[0] != "GET" || Request[1][0] != '/') { | 22 public void Listen(EndPoint localep) { |
20 //Console.WriteLine("HTTP.Server.Start Bad request"); | 23 if (Listener != null) throw new InvalidOperationException("A listener exists"); |
21 SendError(Stream, 400); | 24 Listener = new Socket(localep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); |
22 return true; | 25 Listener.Bind(localep); |
23 } | 26 Listener.Listen(5); |
24 | 27 Listener.BeginAccept(AcceptCallback, null); |
25 Request = Request[1].Split(new Char[] { '?' }, 2); | 28 } |
26 HTTPContent content; | 29 |
27 if (Content.TryGetValue(Request[0], out content)) { | 30 private void AcceptCallback(IAsyncResult ar) { |
28 } else if (DefaultContent != null) { | 31 try { |
29 content = DefaultContent; | 32 Socket socket = Listener.EndAccept(ar); |
33 new HTTPContext(this, socket); | |
34 } catch (Exception) { } | |
35 try { | |
36 Listener.BeginAccept(AcceptCallback, null); | |
37 } catch (Exception) { } | |
38 } | |
39 | |
40 public void Dispose() { | |
41 if (Listener != null) Listener.Close(); | |
42 } | |
43 | |
44 bool TCPServer.IModule.Accept(TCPStream stream) { | |
45 new HTTPContext(this, stream); | |
46 return false; | |
47 } | |
48 } | |
49 | |
50 public class HTTPContext { | |
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 } | |
259 } | |
260 | |
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); | |
30 } else { | 282 } else { |
31 SendError(Stream, 404); | 283 context.SendErrorAndClose(404); |
32 return true; | 284 } |
33 } | 285 } |
34 HTTPContext Context = new HTTPContext(); | 286 } |
35 Context.Stream = Stream; | 287 public class HTTPFileProvider : IHTTPContentProvider { |
36 Context.Request = new HTTPRequest(); | 288 public String FileName { get; private set; } |
37 Context.Request.Method = Method.GET; | 289 public String ContentType { get; private set; } |
38 Context.Request.Path = Request[0]; | 290 public HTTPFileProvider(String fileName) : this(fileName, "application/octet-stream") { } |
39 if (Request.Length == 2) { | 291 public HTTPFileProvider(String fileName, String contentType) { |
40 Context.Request.Query = Request[1]; | 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 } | |
41 } else { | 312 } else { |
42 Context.Request.Query = null; | 313 context.SendErrorAndClose(404); |
43 } | 314 } |
44 HTTPContent.Result ServeResult = content.Serve(Context); | 315 } |
45 | 316 } |
46 if (ServeResult == HTTPContent.Result.OK_KEEPALIVE) { | 317 public class HTTPUnTarchiveProvider : IHTTPContentProvider { |
47 return false; | 318 public String TarFileName { get; private set; } |
48 } else if (!(ServeResult == HTTPContent.Result.OK_CLOSE)) { | 319 public HTTPUnTarchiveProvider(String tarFile) { |
49 SendError(Stream, (int)ServeResult); | 320 this.TarFileName = tarFile; |
50 } | 321 } |
51 return true; | 322 public void ServeRequest(HTTPContext context) { |
52 } | 323 if (!File.Exists(TarFileName)) { |
53 | 324 context.SendErrorAndClose(404); |
54 public static void SendError(Stream Stream, int ErrorCode) { | 325 return; |
55 string ErrorText = null; | 326 } |
56 switch (ErrorCode) { | 327 String reqname1 = context.RequestPath; |
57 case 400: | 328 if (reqname1.Length > 0 && reqname1[0] == '/') reqname1 = reqname1.Substring(1); |
58 ErrorText = "The request could not be understood by the server due to malformed syntax."; | 329 String reqname2 = reqname1; |
59 break; | 330 if (reqname2.Length > 0 && !reqname2.EndsWith("/")) reqname2 += "/"; |
60 case 404: | 331 reqname2 += "index.htm"; |
61 ErrorText = "The requested file can not be found."; | 332 using (FileStream fs = File.OpenRead(TarFileName)) { |
62 break; | 333 while (true) { |
63 default: | 334 Byte[] header = new Byte[512]; |
64 ErrorText = "Unknown error code: " + ErrorCode.ToString() + "."; | 335 if (fs.Read(header, 0, 512) != 512) break; |
65 break; | 336 int flen = Array.IndexOf<Byte>(header, 0, 0, 100); |
66 } | 337 if (flen == 0) continue; |
67 SendHeader(Stream, ErrorCode, "text/plain", ErrorText.Length); | 338 if (flen == -1) flen = 100; |
68 WriteLine(Stream, ErrorText); | 339 String fname = Encoding.ASCII.GetString(header, 0, flen); |
69 Thread.Sleep(100); | 340 String fsize = Encoding.ASCII.GetString(header, 124, 11); |
70 return; | 341 int fsizei = Convert.ToInt32(fsize, 8); |
71 } | 342 if (reqname1.Equals(fname, StringComparison.OrdinalIgnoreCase) || reqname2.Equals(fname)) { |
72 | 343 context.SendStatus(200); |
73 public static void SendHeader(Stream Stream, int ResultCode, string ContentType) { | 344 context.SendHeader("Content-Length", fsizei.ToString()); |
74 SendHeader(Stream, ResultCode, ContentType, -1); | 345 String ctype = null; |
75 } | 346 switch (Path.GetExtension(fname).ToUpperInvariant()) { |
76 public static void SendHeader(Stream Stream, int ResultCode, string ContentType, int ContentLength) { | 347 case ".txt": ctype = "text/plain"; break; |
77 //ResultCode = 200, ContentType = null, ContentLength = -1 | 348 case ".htm": |
78 string ResultString; | 349 case ".html": ctype = "text/html"; break; |
79 switch (ResultCode) { | 350 case ".css": ctype = "text/css"; break; |
80 case 200: ResultString = "OK"; break; | 351 case ".js": ctype = "application/x-javascript"; break; |
81 case 400: ResultString = "Bad Request"; break; | 352 case ".png": ctype = "image/png"; break; |
82 case 404: ResultString = "Not found"; break; | 353 case ".jpg": |
83 default: ResultString = "Unknown"; break; | 354 case ".jpeg": ctype = "image/jpeg"; break; |
84 } | 355 case ".gif": ctype = "image/gif"; break; |
85 WriteLine(Stream, "HTTP/1.1 " + ResultCode.ToString() + " " + ResultString); | 356 case ".ico": ctype = "image/x-icon"; break; |
86 WriteLine(Stream, "Expires: Mon, 26 Jul 1990 05:00:00 GMT"); | 357 } |
87 WriteLine(Stream, "Cache-Control: no-store, no-cache, must-revalidate"); | 358 if (ctype != null) context.SendHeader("Content-Type", ctype); |
88 WriteLine(Stream, "Cache-Control: post-check=0, pre-check=0"); | 359 Stream response = context.GetResponseStream(); |
89 WriteLine(Stream, "Pragma: no-cache"); | 360 int left = fsizei; |
90 WriteLine(Stream, "Server: UCIS Simple Webserver"); | 361 while (left > 0) { |
91 WriteLine(Stream, "Connection: Close"); | 362 byte[] buffer = new byte[1024 * 10]; |
92 if ((ContentType != null)) WriteLine(Stream, "Content-Type: " + ContentType); | 363 int len = fs.Read(buffer, 0, buffer.Length); |
93 if (ContentLength != -1) WriteLine(Stream, "Content-Length: " + ContentLength.ToString()); | 364 if (len <= 0) break; |
94 WriteLine(Stream, ""); | 365 left -= len; |
95 } | 366 response.Write(buffer, 0, len); |
96 | 367 } |
97 public static void WriteLine(Stream Stream, string Line) { | 368 response.Close(); |
98 byte[] Buffer = null; | 369 return; |
99 Buffer = Encoding.ASCII.GetBytes(Line); | 370 } else { |
100 Stream.Write(Buffer, 0, Buffer.Length); | 371 fs.Seek(fsizei, SeekOrigin.Current); |
101 Stream.WriteByte(13); | 372 } |
102 Stream.WriteByte(10); | 373 int padding = fsizei % 512; |
103 } | 374 if (padding != 0) padding = 512 - padding; |
104 } | 375 fs.Seek(padding, SeekOrigin.Current); |
105 | 376 } |
106 public abstract class HTTPContent { | 377 } |
107 public abstract Result Serve(HTTPContext Context); | 378 context.SendErrorAndClose(404); |
108 public enum Result : int { | 379 } |
109 OK_CLOSE = -2, | |
110 OK_KEEPALIVE = -1, | |
111 ERR_NOTFOUND = 404 | |
112 } | |
113 } | |
114 | |
115 public class HTTPFileContent : HTTPContent { | |
116 public string Filename { get; private set; } | |
117 public string ContentType { get; private set; } | |
118 | |
119 public HTTPFileContent(string Filename) : this(Filename, "application/octet-stream") { } | |
120 public HTTPFileContent(string Filename, string ContentType) { | |
121 this.Filename = Filename; | |
122 this.ContentType = ContentType; | |
123 } | |
124 | |
125 public override Result Serve(HTTPContext Context) { | |
126 if (!File.Exists(Filename)) return Result.ERR_NOTFOUND; | |
127 | |
128 using (FileStream FileStream = File.OpenRead(Filename)) { | |
129 HTTPServer.SendHeader(Context.Stream, 200, ContentType, (int)FileStream.Length); | |
130 byte[] Buffer = new byte[1025]; | |
131 while (FileStream.CanRead) { | |
132 int Length = FileStream.Read(Buffer, 0, Buffer.Length); | |
133 if (Length <= 0) break; | |
134 Context.Stream.Write(Buffer, 0, Length); | |
135 } | |
136 } | |
137 return Result.OK_CLOSE; | |
138 } | |
139 } | |
140 | |
141 public class HTTPContext { | |
142 public HTTPRequest Request { get; internal set; } | |
143 public TCPStream Stream { get; internal set; } | |
144 } | |
145 | |
146 public enum Method { | |
147 GET = 0, | |
148 HEAD = 1, | |
149 POST = 2, | |
150 PUT = 3 | |
151 } | |
152 | |
153 public class HTTPRequest { | |
154 public HTTP.Method Method { get; internal set; } | |
155 public string Path { get; internal set; } | |
156 public string Query { get; internal set; } | |
157 } | 380 } |
158 } | 381 } |