Ocean studio-website construction expert: favorites to prevent forgetting: MIME type detection in Internet Explorer [mainly to prevent downloading garbled characters]

Source: Internet
Author: User
Tags dll file extension

Http://msdn.microsoft.com/en-us/library/ms775147.aspx

 

MIME type detection in Internet Explorer

In Microsoft Internet Explorer 4.0 and later, MIME type determination occurs in URL monikers through the findmimefromdata method. determining the MIME type allows URL monikers and other components to find and launch the correct Object Server or application to handle the associated content. this section provides a brief summary of the logic used in determining the MIME type from these sources, and also discusses some of the issues involved.

This topic contains the following sections:

    • Mime Type Detection Algorithm
    • Uploaded MIME types
    • Known MIME types
    • Registry locations
    • Mime Sniffing feature control
    • Mime Handling changes
Mime Type Detection Algorithm

The purpose of MIME type detection, or data sniffing, is to determine the MIME type (also known as content type or media type) of downloaded content using information from the following four sources:

 

    • The server-supplied MIME type, if available
    • An examination of the actual contents associated with a downloaded URL
    • The file name associated with the downloaded content (assumed to be derived from the associated URL)
    • Registry Settings (File Extension/MIME type associations or registered applications) in effect during the download

findmimefromdata contains hard-coded tests for (currently 26) Separate MIME types (see known MIME types ). this means that if a given buffer contains data in the format of one of these MIME types, A test exists in findmimefromdata that is designed (by scanning through the buffer contents) to recognize the corresponding MIME type. a mime type is known if it is one of these n mime types. a mime type is ambiguous if it is "text/plain," "application/octet-stream," an empty string, or null (that is, the server failed to provide it ). a mime type that is neither known nor ambiguous is termed unknown. the MIME types "text/plain" and "application/octet-stream" are termed ambiguous because they generally do not provide clear indications of which application or CLSID shocould be associated as the content handler. a mime type inferred from any one of the four possible sources can be categorized into one of these three classifications.

FindmimefromdataTypically specified es three parameters when invoked-the cache file name (assumed to be derived from the associated URL), a pointer to a buffer containing up to the first 256 bytes of the content, and a "suggested" MIME type that typically corresponds to the server-provided MIME type (through the Content-Type header ). determining the MIME type proceeds as follows:

 

  1. If the "suggested" (server-provided) MIME type is unknown (not known and not ambiguous ), findmimefromdata immediately returns this mime type as the final determination. the reason for this is that new MIME types are continually emerging, and these MIME types might have formats that are difficult to distinguish from the set of hard-coded MIME types for which tests exist. A good example of this is SGML, which can easily be classified incorrectly as HTML because it contains should of the same tags. rather than weakening the hard-coded tests or risking the incorrect classification of new and as-yet-unknown MIME types for hard-coded known ones, priority is given to the server-supplied MIME type if it is unknown, because these MIME types are both specific and likely uncommon, and there are no hard-coded tests that can positively identify them.
  2. If the server-provided MIME type is either known or ambiguous, the buffer is scanned in an attempt to verify or obtain a MIME type from the actual content. if a positive match is found (one of the hard-coded tests succeeded), this mime type is immediately returned as the final determination, overriding the server-provided MIME type (this type of behavior is necessary to identify. GIF file being sent as text/html ). during scanning, it is determined if the buffer is predominantly text or binary. note in Internet Explorer 6 for Windows XP Service Pack 2 (SP2), the MIME type "text/plain" is not ambiguous, and is never rendered as HTML in the restricted zone, even if the content suggests that this is the correct format.
  3. If no positive match is obtained during the data scan, and if the server-provided MIME type is known, an attempt is made to determine if the format (text or binary) of the known MIME type conflicts with the format (text or binary) That was determined from scanning the buffer. if no conflict exists (the data scan indicates primarily text and the server-provided MIME type has a text format, or the data scan indicates binary and the server-provided MIME type is a binary format), the server-provided MIME type is returned. the reasoning behind this is that new formats of MIME types might be added over time (image/TIF is one example) and the hard-coded tests might not recognize these new formats (a different pattern match might be required ). with the assumption that the basic format of MIME types will not change over time from primarily text to binary or vice versa, it will suffice that the formats of the server-provided MIME type and the format found from scanning the data do not disagree. if this is the case, the server-provided MIME type is returned. the format types for known MIME types are stored in a media information structure in URL monikers.
  4. If no positive match is obtained during the data scan, and the server-provided MIME type is ambiguous or the server-provided MIME type is known, and the data format agreement test in the previous step failed, an attempt is made to parse a file extension from the file name passed in. if this is successful, an attempt is made to find the MIME type associated with the file extension in the registry. this will be returned as the final determination if the MIME type associated with the file extension is unknown. the reason for this added requirement is as follows: if the file extension yields an ambiguous MIME type, this adds no information to what was already obtained through scanning the data. if the file extension yields a known MIME type, this mime type shocould have been found during scanning. because it was not found, it is suspect, and is rejected. an example of this is an arbitrary plain-text file being returned through an isapi dll, with the server returning 'text/plain 'as the MIME type. because the server-provided MIME type is ambiguous, a scan of the data is conducting CTED that only confirms that the data is plain text. subsequently, the file name is parsed for an extension. in this case, because the contents were downloaded using an isapi dll, the URL and hence the cache file name will have. DLL file extension that has the MIME type "application/X-msdownload" associated in the registry. this MIME type was already scanned for (Application/X-msdownload is a known MIME type), was not found, and is therefore the wrong determination (this results in a file download as opposed to the desired behavior, which is to display the text in-pane ).
  5. If all the preceding steps have failed to yield an unambiguous MIME type, a last check is made to see if any application is associated in the Registry with the file extension parsed from the file name, if one exists. if an associated application is found, the final determination is automatically set to "application/octet-stream. "This default value ensures that the registered application will be launched by the shell with the downloaded data, rather than displaying the data in-pane. as an example, this is necessary when downloading, among others ,. BAT and. CMD files, which are plain text files, are frequently identified by the server as "text/plain," and have no associated MIME type in the registry. without the final check for an associated application, these wocould be displayed in-pane, whereas the desired behavior is to launch the command interpreter. this is ensured by checking for an associated application, and defaulting to the final determined MIME type of "application/octet-stream. "Other types of files, such. reg files, behave similarly.
  6. Finally, if no file extension is found, or one is found with no associated MIME type or registered application, the MIME type "text/plain" is returned if the data scan indicated predominantly text, or "application/octet-stream" if the data scan indicated binary, because this is the furthest correct determination that cocould be made.
Uploaded MIME types

When uploading files from the client to the Web server, Windows Internet Explorer uses findmimefromdata to determine a MIME type. Where possible,FindmimefromdataReturns hard-coded values from the list of known MIME types rather than relying on the MIME type value associated with the file extension in the system registry. in some cases, the detected MIME type can differ from the generally accepted value for backwards compatibility, as shown in the following table:

Standard MIME type Findmimefromdata returns
Image/JPEG Image/pjpeg
Image/PNG Image/X-PNG
Known MIME types

FindmimefromdataTests for the following MIME types:

Mime Type Description
Text/plain Plain text. Default if data is primarily text and no other type detected.
Text/html Html. Default if common tags detected and server does not supply image/* type.
Text/XML XML data. Default if data specifies<? XMLWith an unrecognized DTD.
Text/richtext Rich Text Format (RTF ).
Text/scriptlet Microsoft Windows Script component.
Audio/X-AIFF Audio Interchange File, Macintosh.
Audio/basic Audio file, UNIX.
Audio/Mid Internet Explorer 7 and later. MIDI sequence.
Audio/WAV Pulse Code Modulation (PCM) wave audio, windows.
Image/GIF Graphics Interchange Format (GIF ).
Image/JPEG JPEG image.
Image/pjpeg Default type for JPEG images.
Image/PNG Internet Explorer 7 and later. Portable Network Graphics (PNG ).
Image/X-PNG Internet Explorer 7 and later. Default type for PNG images.
Image/tiff Tagged Image File Format (TIFF) image.
Image/BMP Bitmap (BMP) image.
Image/X-xbitmap Removed from Internet Explorer 8.
Image/X-JG AOL Johnson-Grace compressed file.
Image/X-emf Enhanced Metafile (EMF ).
Image/X-WMF Windows Metafile format (WMF ).
Video/Avi Audio-video interleaved (AVI) file.
Video/MPEG MPEG stream file.
Application/octet-stream Binary file. Default if data is primarily binary.
Application/postscript Postscript (. AI,. EPS, or. ps) file.
Application/base64 Base64-encoded bytes.
Application/macbinhex40 Binhex for Macintosh.
Application/PDF Portable Document Format (PDF ).
Application/XML XML data. Must be server-supplied. See also "text/XML" type.
Application/atom + XML Internet Explorer 7 and later. Atom syndication format feed.
Application/RSS + XML Internet Explorer 7 and later. Really Simple Syndication (RSS) feed.
Application/X-compressed UNIX tar file, gzipped.
Application/X-zip-compressed Compressed archive file.
Application/X-gzip-compressed Gzip compressed archive file.
Application/Java Java applet.
Application/X-msdownload Executable (.exe or. dll) file.
Registry locations

Location usedFindmimefromdataTo find MIME type and progid from file extension:

Copy
 
Hkey_classes_root \.***

Location usedFindmimefromdataTo find application from progid:

Copy
 
Hkey_classes_root \ <progid> \ shell \ open \ command

Location used by URL monikers to find clsids from MIME types:

Copy
 
Hkey_classes_root \ mime \ database \ content type
Mime Sniffing feature control

In Windows XP SP2, a feature control was added to enable and disable MIME sniffing. For more information on feature controls, see Introduction to feature controls.

Mime Handling changes

Internet Explorer in Windows XP SP2 handles MIME types differently than previous versions. For more information, see handling MIME types in Internet Explorer.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.