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