📄 ch06.htm
字号:
<PRE><FONT COLOR="#0066FF">audio/bsic au snd
audio/x-aiff aif aiff aifc
audio/x-wav wav
</FONT></PRE>
<P><B><TT>Video</TT></B> This is a video Content-Type value for transmitting video
or moving image data, possibly with audio as part of the composite video data format:</P>
<PRE><FONT COLOR="#0066FF">video/mpeg mpeg mpg mpe
video/quicktime qt mov
video/x-msvideo avi
video/x-sgi-movie movie
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading5"></A><FONT COLOR="#000077">The Content-Transfer-Encoding
Header Field</FONT></H4>
<P>The Content-Transfer-Encoding header field is used to specify an auxiliary encoding
that was applied to the data in order to allow it to pass through mail transport
mechanisms that may have data or character set limitations.
<H4 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">Additional Header
Fields</FONT></H4>
<P>Two additional header fields can be used to further describe the data in a message
body: the <BR>
Content-ID and Content-Description header fields.
<H3 ALIGN="CENTER"><A NAME="Heading7"></A><FONT COLOR="#000077">How MIME Data Is
Encoded</FONT></H3>
<P>MIME data can be encoded in two different ways. Each encoding method has its advantages
and disadvantages, which are described later. The first method, Q encoding, is recommended
for use when the characters to be encoded are in the ASCII character set; otherwise,
the B encoding should be used. Both encoding/decoding methods are possible using
MIME::Base64 (B) and MIME::QuotedPrint (Q).</P>
<P>Only a subset of the printable ASCII characters may be used in encoded-text. Space
and tab characters are not allowed, so that the beginning and end of an encoded-word
are obvious. The ? character is used within an encoded-word to separate the various
portions of the encoded-word from one another and thus cannot appear in the encoded-text
portion. Other characters are also illegal in certain contexts. For example, an encoded-word
in a "phrase" preceding an address in a From header field may not contain
any of the "specials" defined in RFC 822. Finally, certain other characters
are disallowed in some contexts to ensure reliability for messages that pass through
internetwork mail gateways.</P>
<P>The B encoding automatically meets these requirements. The Q encoding allows a
wide range of printable characters to be used in non-critical locations in the message
header (such as Subject), with fewer characters available for use in other locations.
<H4 ALIGN="CENTER"><A NAME="Heading8"></A><FONT COLOR="#000077">B Base64 Encoding</FONT></H4>
<P>The B encoding is identical to the Base64 encoding defined by RFC 1521.
<H4 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">Q Quoted-Printable
Encoding</FONT></H4>
<P>The Q encoding is similar to the Quoted-Printable content-transfer-encoding defined
in RFC 1521. It is designed to allow text containing mostly ASCII characters to be
decipherable on an ASCII terminal without decoding.</P>
<P>For more information on RFC 1521 and specific information about the rules used
to encode data, go to:</P>
<PRE><FONT COLOR="#0066FF">http://www.freesoft.org/Connected/RFC/1521/index.html
</FONT></PRE>
<P>Data encoded in the "Q" or Quoted-Printable method follows these basic
rules:
<OL>
<LI>Any 8-bit value may be represented by a <TT>=</TT> followed by two hexadecimal
digits. For example, if the character set in use were ISO-8859-1, the <TT>=</TT>
character would thus be encoded as <TT>=3D</TT> and a <TT>SPACE</TT> by <TT>=20</TT>.
(Uppercase should be used for hexadecimal digits <BR>
A through F.)
<P>
<LI>The 8-bit hexadecimal value 20 (for example, ISO-8859-1 SPACE) may be represented
as <TT>_</TT> (underscore, ASCII 95). (This character may not pass through some internetwork
mail gateways, but its use will greatly enhance readability of Q encoded data with
mail readers that do not support this encoding.) Note that the <TT>_</TT> always
represents hexadecimal 20, even if the <TT>SPACE</TT> character occupies a different
code position in the character set in use.
<P>
<LI>8-bit values that correspond to printable ASCII characters other than <TT>=</TT>,
<TT>?</TT>, <TT>_</TT> (underscore), and <TT>SPACE</TT> may be represented as those
characters.
</OL>
<H3 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">Encoding and Decoding
MIME with libwww</FONT></H3>
<P>Mechanisms for encoding and decoding MIME messages are provided in the MIME::Base64
and Mime::QuotedPrint classes.
<H4 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">Using MIME::Base64</FONT></H4>
<P>Mime::Base64 includes two functions: <TT>encode_base64()</TT> and <TT>decode_base64()</TT>.
To use Mime::Base64 in your script, include the following line near the beginning
of your script:</P>
<PRE><FONT COLOR="#0066FF">use MIME::Base64;
</FONT></PRE>
<P>After called, encoding and decoding strings of MIME is quite simple. Encoding
is handled by sending a string of non-encoded text (which can be stored in a variable)
to the <TT>encode_base64</TT> routine. Here's how it's done:</P>
<PRE><FONT COLOR="#0066FF">$MyEncodedMime = encode_base64($MyPlainText);
</FONT></PRE>
<P>Where, <TT>$MyEncodedMime</TT> will contain the Base64 encoded version of <TT>$MyPlainText</TT>.</P>
<P>Decoding is handled in the same way, using <TT>decode_base64</TT>.</P>
<P>Here's an example:</P>
<PRE><FONT COLOR="#0066FF">$MyDecodedText = decode_base64($MyEncodedMIME);
</FONT></PRE>
<P>Many popular e-mail clients, such as Netscape Mail, have standardized on Base64
method encoding to attach binary files to ASCII text e-mail messages. Let's take
a look at an example of an <BR>
e-mail message generated by Netscape mail that has Base64 encoded data as an attachment
(see Listing 7.1).
<H3 ALIGN="CENTER"><A NAME="Heading12"></A><FONT COLOR="#000077">Listing 7.1. E-mail
with Base64 encoded</FONT></H3>
<H3 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">MIME attachment</FONT></H3>
<PRE><FONT COLOR="#0066FF">From - Mon Jan 13 01:50:26 1997
Return-Path: <ckemp@ro.com>
Received: from chris (ts8p1.ro.com [205.216.92.168])
by sh1.ro.com (8.8.4/8.8.4) with SMTP
id CAA12517 for <ckemp@ro.com>; Mon, 13 Jan 1997 02:50:08 -0600
Message-ID: <32DA04C3.75E@ro.com>
Date: Mon, 13 Jan 1997 01:47:47 -0800
From: Chris Kemp <ckemp@ro.com>
Reply-To: ckemp@ro.com
Organization: Silicon Graphics
X-Mailer: Mozilla 3.01 (Win95; I)
MIME-Version: 1.0
To: ckemp@ro.com
Subject: MIME Test
Content-Type: multipart/mixed; boundary="------------6B797646304"
X-UIDL: 48f2146a82b1f706edbd5e7c350b352a
X-Mozilla-Status: 0001
This is a multi-part message in MIME format.
--------------6B797646304
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Chris,
I have attached a picture of the Boeing F-22 to this message.
--------------6B797646304
Content-Type: image/jpeg; name="F22.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="F22.jpg"
/9j/4AAQSkZJRgABAgEASABIAAD/7QG4UGhvdG9zaG9wIDMuMAA4QklNA+kAAAAAAHgAAwAA
AEgASAAAAAAC2gIo/+H/4gL5AkYDRwUoA/wAAgAAAEgASAAAAAAC2AIoAAEAAABkAAAAAQAD
AwMAAAABJw8AAQABAAAAAAAAAAAAAAAAYAgAGQGQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA4QklNA+0AAAAAABAASAAAAAEAAQBIAAAAAQABOEJJTQPzAAAAAAAIAAAAAAAA
AAA4QklNJxAAAAAAAAoAAQAAAAAAAAACOEJJTQP1AAAAAABIAC9mZgABAGxmZgAGAAAAAAAB
AC9mZgABAKGZmgAGAAAAAAABADIAAAABAFoAAAAGAAAAAAABADUAAAABAC0AAAAGAAAAAAAB
OEJJTQP4AAAAAABwAAD/////////////////////////////A+gAAAAA////////////////
/////////////wPoAAAAAP////////////////////////////8D6AAAAAD/////////////
////////////////A+gAADhCSU0EBgAAAAAAAgAG/+4ADkFkb2JlAGQAAAAAAf/bAIQAAgIC
AgICAgICAgMCAgIDBAMCAgMEBQQEBAQEBQYFBQUFBQUGBgcHCAcHBgkJCgoJCQwMDAwMDAwM
DAwMDAwMDAEDAwMFBAUJBgYJDQoJCg0PDg4ODg8PDAwMDAwPDwwMDAwMDA8MDAwMDAwMDAwM
DAwMDAwMDAwMDAwMDAwMDAwM/8AAEQgCWAMYAwERAAIRAQMRAf/EAaIAAAAHAQEBAQEAAAAA
AAAAAAQFAwIGAQAHCAkKCwEAAgIDAQEBAQEAAAAAAAAAAQACAwQFBgcICQoLEAACAQMDAgQC
BgcDBAIGAnMBAgMRBAAFIRIxQVEGE2EicYEUMpGhBxWxQiPBUtHhMxZi8CRygvElQzRTkqKy
Y3PCNUQnk6OzNhdUZHTD0uIIJoMJChgZhJRFRqS0VtNVKBry4/PE1OT0ZXWFlaW1xdXl9WZ2
hpamtsbW5vY3R1dnd4eXp7fH1+f3OEhYaHiImKi4yNjo+Ck5SVlpeYmZqbnJ2en5KjpKWmp6
ipqqusra6voRAAICAQIDBQUEBQYECAMDbQEAAhEDBCESMUEFURNhIgZxgZEyobHwFMHR4SNC
FVJicvEzJDRDghaSUyWiY7LCB3PSNeJEgxdUkwgJChgZJjZFGidkdFU38qOzwygp0+PzhJSk
tMTU5PRldYWVpbXF1eX1RlZmdoaWprbG1ub2R1dnd4eXp7fH1+f3OEhYaHiImKi4yNjo+DlJ
WWl5iZmpucnZ6fkqOkpaanqKmqq6ytrq+v/dAAQAY//aAAwDAQACEQMRAD8A/P8A4q7FXYq7
<a few hundred K of encoded data removed>
sS+TZPzi8xpdeS/Jmoana6tf6VYteahqd8v1ixlmto4bi91KSRI45P8AX4tir9CMVdirsVdi
rsVdirsVdirsVdirsVdirsVdirsVdirsVdirsVdirsVdirsVdirsVdirsVdirsVdirsVdirs
VdirsVdirsVdirsVeRfn5y/5UX+dHH7X+BPMVP8AuGXGLCX0v//Z
--------------6B797646304--
</FONT></PRE>
<P>Notice the MIME content type declarations throughout the message. A practical
application of the Mime::Base64 decode_base64 function would be extracting the encoded
data to a file. Using this method, you could allow users to submit pictures via e-mail
of items that could be integrated into a Web page.
<H4 ALIGN="CENTER"><A NAME="Heading15"></A><FONT COLOR="#000077">Using MIME::QuotedPrint</FONT></H4>
<P>Mime::QuotedPrint includes the functions <TT>encode_qp</TT> and <TT>decode_qp</TT>
that function in the same way as their Base64 counterparts. The syntax is as follows:</P>
<PRE><FONT COLOR="#0066FF">$encoded = encode_yp($decoded);
$decoded = decode_yp($encoded);
</FONT></PRE>
<P>MIME is significant to the Web programmer in two main ways. MIME and its different
data types define standard types of data that can be transmitted as part of a document.
MIME headers are used in every HTTP transaction, thus a familiarity with them is
essential in advanced applications where your program must generate headers. MIME
encoding schemes allow binary data to be included in text messages that can be transported
over older e-mail (not HTTP) gateways.<BR>
<P ALIGN="CENTER"><A HREF="ch05.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/ch05.htm"><IMG SRC="blanprev.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blanprev.gif" WIDTH="37" HEIGHT="37"
ALIGN="BOTTOM" BORDER="2"></A><A HREF="index-1.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/index-1.htm"><IMG SRC="blantoc.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blantoc.gif" WIDTH="42"
HEIGHT="37" ALIGN="BOTTOM" BORDER="2"></A><A HREF="ch07.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/ch07.htm"><IMG SRC="blannext.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blannext.gif"
WIDTH="45" HEIGHT="37" ALIGN="BOTTOM" BORDER="2"></A>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -