OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Detecting mime types is a tricky business because we need to balance | 5 // Detecting mime types is a tricky business because we need to balance |
6 // compatibility concerns with security issues. Here is a survey of how other | 6 // compatibility concerns with security issues. Here is a survey of how other |
7 // browsers behave and then a description of how we intend to behave. | 7 // browsers behave and then a description of how we intend to behave. |
8 // | 8 // |
9 // HTML payload, no Content-Type header: | 9 // HTML payload, no Content-Type header: |
10 // * IE 7: Render as HTML | 10 // * IE 7: Render as HTML |
(...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 // sniffing gives us less room for error. If the version number ever changes, | 527 // sniffing gives us less room for error. If the version number ever changes, |
528 // we can just add an entry to this list. | 528 // we can just add an entry to this list. |
529 // | 529 // |
530 // TODO(aa): If we ever have another magic number, we'll want to pass a | 530 // TODO(aa): If we ever have another magic number, we'll want to pass a |
531 // histogram into CheckForMagicNumbers(), below, to see which one matched. | 531 // histogram into CheckForMagicNumbers(), below, to see which one matched. |
532 static const struct MagicNumber kCRXMagicNumbers[] = { | 532 static const struct MagicNumber kCRXMagicNumbers[] = { |
533 MAGIC_NUMBER("application/x-chrome-extension", "Cr24\x02\x00\x00\x00") | 533 MAGIC_NUMBER("application/x-chrome-extension", "Cr24\x02\x00\x00\x00") |
534 }; | 534 }; |
535 | 535 |
536 // Only consider files that have the extension ".crx". | 536 // Only consider files that have the extension ".crx". |
537 static const char kCRXExtension[] = ".crx"; | 537 if (!EndsWith(url.path(), ".crx", true)) |
538 // Ignore null by subtracting 1. | |
539 static const int kExtensionLength = arraysize(kCRXExtension) - 1; | |
540 if (url.path().rfind(kCRXExtension, std::string::npos, kExtensionLength) == | |
541 url.path().size() - kExtensionLength) { | |
542 counter->Add(1); | |
543 } else { | |
544 return false; | 538 return false; |
545 } | 539 |
| 540 counter->Add(1); |
546 | 541 |
547 *have_enough_content &= TruncateSize(kBytesRequiredForMagic, &size); | 542 *have_enough_content &= TruncateSize(kBytesRequiredForMagic, &size); |
548 if (CheckForMagicNumbers(content, size, | 543 if (CheckForMagicNumbers(content, size, |
549 kCRXMagicNumbers, arraysize(kCRXMagicNumbers), | 544 kCRXMagicNumbers, arraysize(kCRXMagicNumbers), |
550 NULL, result)) { | 545 NULL, result)) { |
551 counter->Add(2); | 546 counter->Add(2); |
552 } else { | 547 } else { |
553 return false; | 548 return false; |
554 } | 549 } |
555 | 550 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
656 return true; | 651 return true; |
657 return have_enough_content; | 652 return have_enough_content; |
658 } | 653 } |
659 | 654 |
660 // CRX files (chrome extensions) have a special sniffing algorithm. It is | 655 // CRX files (chrome extensions) have a special sniffing algorithm. It is |
661 // tighter than the others because we don't have to match legacy behavior. | 656 // tighter than the others because we don't have to match legacy behavior. |
662 if (SniffCRX(content, content_size, url, type_hint, | 657 if (SniffCRX(content, content_size, url, type_hint, |
663 &have_enough_content, result)) | 658 &have_enough_content, result)) |
664 return true; | 659 return true; |
665 | 660 |
| 661 // MHTML cannot be easily sniffed as its header might be relatively long |
| 662 // before the "Content-Type: multipart/related". |
| 663 std::string path = url.path(); |
| 664 if (EndsWith(path, ".mht", false) || EndsWith(path, ".mhtml", false)) { |
| 665 result->assign("multipart/related"); |
| 666 return true; |
| 667 } |
| 668 |
666 // We're not interested in sniffing for magic numbers when the type_hint | 669 // We're not interested in sniffing for magic numbers when the type_hint |
667 // is application/octet-stream. Time to bail out. | 670 // is application/octet-stream. Time to bail out. |
668 if (type_hint == "application/octet-stream") | 671 if (type_hint == "application/octet-stream") |
669 return have_enough_content; | 672 return have_enough_content; |
670 | 673 |
671 // Now we look in our large table of magic numbers to see if we can find | 674 // Now we look in our large table of magic numbers to see if we can find |
672 // anything that matches the content. | 675 // anything that matches the content. |
673 if (SniffForMagicNumbers(content, content_size, | 676 if (SniffForMagicNumbers(content, content_size, |
674 &have_enough_content, result)) | 677 &have_enough_content, result)) |
675 return true; // We've matched a magic number. No more content needed. | 678 return true; // We've matched a magic number. No more content needed. |
676 | 679 |
677 return have_enough_content; | 680 return have_enough_content; |
678 } | 681 } |
679 | 682 |
680 } // namespace net | 683 } // namespace net |
OLD | NEW |