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