comparison Net/HTTP.cs @ 54:ba4e2cb031e0

Added general purpose tar archive reader class
author Ivo Smits <Ivo@UCIS.nl>
date Wed, 02 Oct 2013 21:17:30 +0200
parents 8df7f4dc5615
children 50d4aed66c67
comparison
equal deleted inserted replaced
53:df5534af12e9 54:ba4e2cb031e0
187 break; 187 break;
188 } 188 }
189 } 189 }
190 return; 190 return;
191 191
192 SendError400AndClose: 192 SendError400AndClose:
193 State = HTTPConnectionState.ProcessingRequest; 193 State = HTTPConnectionState.ProcessingRequest;
194 SendErrorAndClose(400); 194 SendErrorAndClose(400);
195 return; 195 return;
196 SendError500AndClose: 196 SendError500AndClose:
197 State = HTTPConnectionState.ProcessingRequest; 197 State = HTTPConnectionState.ProcessingRequest;
198 SendErrorAndClose(500); 198 SendErrorAndClose(500);
199 return; 199 return;
200 } 200 }
201 201
389 if (!File.Exists(TarFileName)) { 389 if (!File.Exists(TarFileName)) {
390 context.SendErrorAndClose(404); 390 context.SendErrorAndClose(404);
391 return; 391 return;
392 } 392 }
393 String reqname1 = context.RequestPath; 393 String reqname1 = context.RequestPath;
394 if (reqname1.Length > 0 && reqname1[0] == '/') reqname1 = reqname1.Substring(1); 394 if (reqname1.StartsWith("/")) reqname1 = reqname1.Substring(1);
395 String reqname2 = reqname1; 395 String reqname2 = reqname1;
396 if (reqname2.Length > 0 && !reqname2.EndsWith("/")) reqname2 += "/"; 396 if (reqname2.Length > 0 && !reqname2.EndsWith("/")) reqname2 += "/";
397 reqname2 += "index.htm"; 397 reqname2 += "index.htm";
398 using (FileStream fs = File.OpenRead(TarFileName)) { 398 foreach (TarchiveEntry file in new TarchiveReader(TarFileName)) {
399 while (true) { 399 if (!file.IsFile) continue;
400 Byte[] header = new Byte[512]; 400 if (!reqname1.Equals(file.Name, StringComparison.OrdinalIgnoreCase) && !reqname2.Equals(file.Name, StringComparison.OrdinalIgnoreCase)) continue;
401 if (fs.Read(header, 0, 512) != 512) break; 401 context.SendStatus(200);
402 int flen = Array.IndexOf<Byte>(header, 0, 0, 100); 402 context.SendHeader("Content-Length", file.Size.ToString());
403 if (flen == 0) continue; 403 String ctype = null;
404 if (flen == -1) flen = 100; 404 switch (Path.GetExtension(file.Name).ToLowerInvariant()) {
405 String fname = Encoding.ASCII.GetString(header, 0, flen); 405 case ".txt": ctype = "text/plain"; break;
406 String fsize = Encoding.ASCII.GetString(header, 124, 11); 406 case ".htm":
407 int fsizei = Convert.ToInt32(fsize, 8); 407 case ".html": ctype = "text/html"; break;
408 if (fname.StartsWith("./")) fname = fname.Length == 2 ? "/" : fname.Substring(2); 408 case ".css": ctype = "text/css"; break;
409 if (reqname1.Equals(fname, StringComparison.OrdinalIgnoreCase) || reqname2.Equals(fname)) { 409 case ".js": ctype = "application/x-javascript"; break;
410 context.SendStatus(200); 410 case ".png": ctype = "image/png"; break;
411 context.SendHeader("Content-Length", fsizei.ToString()); 411 case ".jpg":
412 String ctype = null; 412 case ".jpeg": ctype = "image/jpeg"; break;
413 switch (Path.GetExtension(fname).ToLowerInvariant()) { 413 case ".gif": ctype = "image/gif"; break;
414 case ".txt": ctype = "text/plain"; break; 414 case ".ico": ctype = "image/x-icon"; break;
415 case ".htm": 415 }
416 case ".html": ctype = "text/html"; break; 416 if (ctype != null) context.SendHeader("Content-Type", ctype);
417 case ".css": ctype = "text/css"; break; 417 using (Stream response = context.GetResponseStream(), source = file.GetStream()) {
418 case ".js": ctype = "application/x-javascript"; break; 418 byte[] buffer = new byte[Math.Min(source.Length, 1024 * 10)];
419 case ".png": ctype = "image/png"; break; 419 while (source.CanRead) {
420 case ".jpg": 420 int len = source.Read(buffer, 0, buffer.Length);
421 case ".jpeg": ctype = "image/jpeg"; break; 421 if (len <= 0) break;
422 case ".gif": ctype = "image/gif"; break; 422 response.Write(buffer, 0, len);
423 case ".ico": ctype = "image/x-icon"; break;
424 }
425 if (ctype != null) context.SendHeader("Content-Type", ctype);
426 Stream response = context.GetResponseStream();
427 int left = fsizei;
428 byte[] buffer = new byte[Math.Min(left, 1024 * 10)];
429 while (left > 0) {
430 int len = fs.Read(buffer, 0, Math.Min(left, buffer.Length));
431 if (len <= 0) break;
432 left -= len;
433 response.Write(buffer, 0, len);
434 }
435 response.Close();
436 return;
437 } else {
438 fs.Seek(fsizei, SeekOrigin.Current);
439 } 423 }
440 int padding = fsizei % 512; 424 }
441 if (padding != 0) padding = 512 - padding; 425 return;
442 fs.Seek(padding, SeekOrigin.Current);
443 }
444 } 426 }
445 context.SendErrorAndClose(404); 427 context.SendErrorAndClose(404);
446 } 428 }
447 } 429 }
448 } 430 }