MIME type encyclopedia, you are worth collecting

Time:2021-3-3

[please indicate the source for reprint]:https://blog.csdn.net/huahao1989/article/details/107746738

Multipurpose Internet Mail Extension (MIME) typesIs a standardized way to represent the nature and format of a document. It’s inIETF RFC 6838It is defined and standardized in. Internet number distribution organization(IANA)Is the official organization responsible for tracking all official MIME typesmedia typePage to find the latest complete list.

Browsers usually use MIME types (rather than file extensions) to determine how documents are processed; therefore, it is important that the server is set up correctly to attach the correct MIME type to the header of the response object.

grammar

General structure

type/subtype

The structure of mime is very simple; it consists of two strings of type and subtype'/'They are divided into two parts. Spaces are not allowed.type Represents an independent category that can be divided into multiple subcategories.Subtype represents each type after subdivision.

MIME types are not case sensitive, but they are traditionally written in lowercase.

Independent type

text/plain
text/html
image/jpeg
image/png
audio/mpeg
audio/ogg
audio/*
video/mp4
application/*
application/json
application/javascript
application/ecmascript
application/octet-stream

independentType indicates the classification of files, which can be one of the following:

type describe Typical examples
text It shows that the document is plain text and theoretically human readable text/plain, text/html, text/css, text/javascript
image It’s some kind of image. Video is not included, but dynamic graphics (such as dynamic GIF) also use the image type image/gif, image/png, image/jpeg, image/bmp, image/webp, image/x-icon, image/vnd.microsoft.icon
audio Indicates some kind of audio file audio/midi, audio/mpeg, audio/webm, audio/ogg, audio/wav
video Indicates that it’s some kind of video file video/webm, video/ogg
application Indicates some kind of binary data application/octet-stream, application/pkcs12, application/vnd.mspowerpoint, application/xhtml+xml, application/xml, application/pdf

For text file types, if there is no specific subtype, usetext/plain. Similarly, binary files do not have a specific or known subtype, that is, they useapplication/octet-stream

Multipart type

multipart/form-data
multipart/byteranges

MultipartTypes represent the types of file types in the subdivision domain, and often correspond to different MIME types. This isreunite withA representation of files.multipart/form-dataCan be used to contactHTML FormsandPOSTMethods, in additionmultipart/byterangesUse status code206 Partial ContentTo send a subset of the entire file, while HTTP uses a special way for complex files that cannot be processed: to send the information directly to the browser (at this time, a “save as” window may be created, but you don’t know how to display the inline file.)

Important MIME types
  • application/octet-stream

This is the default value for application files. intendUnknown application file,Browsers generally do not execute automatically or ask for execution. The browser will set the HTTP header as if it were being treatedContent-DispositionThe value isattachmentTreat this kind of file as if it were a file.
staymultipart/form-dataIn the response message body of type,Content-DispositionHeaders can be used inmultipartIn the sub part of message body, it is used to give the relevant information of its corresponding field. Each sub part consists ofContent-TypeThe separator defined in. It has no practical significance when used in the message body itself.

  • text/plain

Text file default. Even if it isMeans unknown text fileBut the browser thinks it can be displayed directly.
text/plainIt doesn’t mean some kind of text data. If the browser wants an explicit type of text file, the browser does not consider whether they match. For example, if you download CSS files through a<link>Link to download atext/plainDocuments. If the information provided is text / plain, the browser will not recognize it as a valid CSS file. The CSS type requires text / CSS.

  • text/css

Any CSS file to be parsed into CSS in a web page must specify mime astext/css. Usually, the server does not recognize the MIME type of the file with. CSS suffix, but takes it as mimetext/plainorapplication/octet-streamTo send to the browser: in this case, most browsers do not recognize it as a CSS file and ignore it directly. In particular, be careful to provide the correct MIME type for CSS files.

  • text/html

This type should be used for all HTML content. Other MIME types of XHTML (such asapplication/xml+html)It’s almost no longer used (HTML5 unifies these formats).

Note: You still need to use application/xml or application/xhtml+xml if you intend to make use of XML’s strict parsing rules, use [<![CDATA[…]]>] or elements from non‑HTML, non‑SVG or non‑MathML XML namespaces, as text/html’s parsing semantics are subtly incompatible with those of application/xml.

  • JavaScript types

According to the mime sniffer standard, here are the valid JavaScript MIME types

  • application/javascript
  • application/ecmascript
  • application/x-ecmascript
  • application/x-javascript
  • text/ecmascript
  • text/javascript
  • text/javascript1.0
  • text/javascript1.1
  • text/javascript1.2
  • text/javascript1.3
  • text/javascript1.4
  • text/javascript1.5
  • text/jscript
  • text/livescript
  • text/x-ecmascript
  • text/x-javascript

be-alltextJavaScript type has been changedRFC 4329Abandoned.

Picture type

Only a small number of image types are widely supported, web secure, and can be used in web pages at any time

MIME type Picture type
image/gif Gif image (replaced by PNG in lossless compression)
image/jpeg JPEG image
image/png Png pictures
image/svg+xml Svg image (vector image)

There are some controversies about the type division here. Some people think that webp should be added here(image/webp)However, each new image type will increase the number of code, which will bring some new security problems, so browser vendors are very careful about adding types.

Other types of images can be found in Web documents. For example, many browsers supportIcon type icon asFavicons or similar icons, and the browser’simage/x-iconSupport ICO image

althoughimage/vnd.microsoft.icon Register with AnaIt’s still not widely supported,image/x-iconUsed as a substitute.

Audio and video types

Html is not explicitly defined for<audio>and<video>Element, so only a relatively small set of types is used on the web. articleMedia formats supported by the HTML audio and video elementsExplains the decoder or video file formats that can be used.

The most common formats of video files in Web environment are as follows:

MIME type Audio or video type
audio/wave <br/> audio/wav <br/> audio/x-wav <br/> audio/x-pn-wav Audio streaming file. Generally, it supports PCM audio coding (wave codec “1”), and other decoders have limited support (if any).
audio/webm WebM audio file format. Vorbis and opus are the most commonly used decoders.
video/webm Audio and video files using WebM video file format. VP8 and VP9 are the most commonly used video decoders. Vorbis and opus are the most commonly used audio decoders.
audio/ogg Audio file in Ogg multimedia file format. Vorbis is the most commonly used audio decoder for this multimedia file format.
video/ogg Audio and video files in Ogg multimedia file format. The commonly used video decoder is Theora; the audio decoder is Vorbis.
application/ogg Audio and video files in Ogg multimedia file format. The commonly used video decoder is Theora; the audio decoder is Vorbis.
application/json application/json (MIME_type) <br/>https://en.wikipedia.org/wiki/Media_type#Common_examples <br/> https://www.iana.org/assignments/media-types/application/json
multipart/form-data

multipart/form-dataCan be used forhtml form Send information from the browser to the server. As a Multipart Document Format, it consists of boundary lines (a'--'The beginning of the string) is divided into different parts. Each part has its own entity and its own HTTP request header,Content-DispositionandContent-TypeFor the field of file upload, the most commonly used(Content-LengthBecause the boundary line is ignored as a separator).

Content-Type: multipart/form-data; boundary=aBoundaryString
(other headers associated with the multipart document as a whole)

--aBoundaryString
Content-Disposition: form-data; name="myFile"; filename="img.jpg"
Content-Type: image/jpeg

(data)
--aBoundaryString
Content-Disposition: form-data; name="myField"

(data)
--aBoundaryString
(more subparts)
--aBoundaryString--

The form is as follows:

<form action="http://localhost:8000/" method="post" enctype="multipart/form-data">
  <input type="text" name="myTextField">
  <input type="checkbox" name="myCheckBox">Check</input>
  <input type="file" name="myFile">
  <button>Send the file</button>
</form>

A request is sent:

POST / HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Content-Type: multipart/form-data; boundary=---------------------------8721656041911415653955004498
Content-Length: 465

-----------------------------8721656041911415653955004498
Content-Disposition: form-data; name="myTextField"

Test
-----------------------------8721656041911415653955004498
Content-Disposition: form-data; name="myCheckBox"

on
-----------------------------8721656041911415653955004498
Content-Disposition: form-data; name="myFile"; filename="test.txt"
Content-Type: text/plain

Simple file.
-----------------------------8721656041911415653955004498--
multipart/byteranges

multipart/byterangesIt is used to send part of the response message back to the browser. When sending status code206 Partial ContentThis MIME type is used to indicate that the file consists of several parts, each with its request scope. Just like many other typesContent-TypeUse separators to draw boundaries. Every different part has its ownContent-TypeThis HTTP header describes the actual type of the file, andContent-RangeTo illustrate its scope.

HTTP/1.1 206 Partial Content
Accept-Ranges: bytes
Content-Type: multipart/byteranges; boundary=3d6b6a416f9b5
Content-Length: 385

--3d6b6a416f9b5
Content-Type: text/html
Content-Range: bytes 100-200/1270

eta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="vieport" content
--3d6b6a416f9b5
Content-Type: text/html
Content-Range: bytes 300-400/1270

-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: "Open Sans", "Helvetica
--3d6b6a416f9b5--

The importance of setting the correct MIME type

Many web servers use the defaultapplication/octet-streamTo send an unknown type. For some security reasons, the browser is not allowed to set some custom default operations for these resources, so users must store them locally for use. The common file types that cause server configuration errors are as follows:

  • Rar encoding file. In this case, it’s ideal to set the real encoded file type; but this is usually not possible (either a type unknown to the server or the file contains many other different file types). In this case, the server will sendapplication/x-rar-compressedAs a MIME type, users do not define it as a useful default operation.
  • Audio or video files. Only files with the MIME type set correctly can be used<video>Or ‘< audio > to recognize and play. Refer touse the correct type for audio and video
  • Proprietary document type. Special attention should be paid when it is a proprietary document. useapplication/octet-streamAs a special treatment, it is not allowed: for general MIME type browsers, it is not allowed to define the default behavior (such as “open in word”)

Mime sniffer

When the MIME type is missing or the client thinks that the file has the wrong MIME type, the browser may perform mime sniffing by viewing the resources. Each browser will perform different operations in different situations. Because this operation will have some security problems, some MIME types represent executable content and some are not. The browser can use the request headerContent-TypeTo set upX-Content-Type-OptionsTo prevent mime sniffing.

Other ways to transfer file types

MIME type is not the only way to convey document type information:

  • Sometimes name suffixes are used, especially on Microsoft Windows systems. Not all operating systems consider these suffixes meaningful (especially Linux and Mac OS), and like external MIME types, they are not guaranteed to be correct.
  • Magic numbers. The syntax of different types of files allows file type inference by looking at the structure. For example, each GIF file begins with a PNG file with a 47 49 46 38 hexadecimal value [gif89] or 89 50 4E 47 [. PNG]. Not all types of files have magic numbers, so this is not a 100% reliable way.

other

Welcome to “back end old bird” The official account will be followed by a series of thematic articles, including Java, Python, Linux, SpringBoot, SpringCloud, Dubbo, algorithm, management of technical team, and various brain maps and learning materials, NFC technology, search technology, crawler technology, recommendation technology, audio frequency interactive live broadcast, etc. as long as there is time, I will organize and share. Please look forward to ready-made notes. If you need a map and learning materials, you can get the official account message in advance. Because I am basically in the role of tackling key problems and exploring the way in all the teams, I have done many things, and I have encountered many holes and solved many problems. Welcome to join the official account and learn together.

[please indicate the source for reprint]:https://blog.csdn.net/huahao1989/article/details/107746738

MIME type encyclopedia, you are worth collecting