CGI and Perl


Programming CGI applications for the WWW requires a fundamental understanding of the underlying mechanics of HTTP transactions. The communication which occurs between the Web browser and the server on which your CGI program is being run is fully accessible to your CGI program. Understanding this communication, or transaction, allows you to write dynamic, powerful, and efficient CGI programs. Understanding tools such as and the libwww modules make it easier than ever to harness this power in your programs.

MIME Documents

  • MIME, WWW, and CGI
  • How MIME Data Is Encoded
  • Encoding and Decoding MIME with libwww

MIME is an acronym for Multipurpose Internet Mail Extensions. In the previous chapter, we talked about how message headers in MIME format were used in an HTTP request. Despite its misleading name, MIME is used to define the structure and content of many different types of Internet messages other than just mail. MIME is a protocol that allows transmission of non-text files such as graphics, audio, video, and program files. Elements other than text, such as graphics and audio, are encoded in the message as ASCII text. The MIME message can then be easily transmitted via e-mail or other text-based protocols. The non-text elements in the message are then unencoded at the other end after the MIME message is downloaded.


In this chapter, we will look at how MIME messages and encoding schemes relate to the WWW and CGI. The MIME standard is introduced in RFC1341 and later completely defined in RFC 1521. RFC 1521 replaces the older RFC 1341 completely. A full discussion of the MIME standard is not necessary, but may be found at the following URL:

MIME provides three mechanisms which allow non-textual data to be specified and encoded into text documents that can be transferred over text e-mail gateways:

  • The MIME-Version Header Field
  • The Content-Type Header Field
  • The Content-Transfer-Encoding Header Field

In addition to these three main mechanisms, two optional header fields may be defined to add a unique ID and a description of the body of the message:

  • The Content-ID Header Field
  • The Content-Description Header Fields.

In this chapter, we will look at the preceding header fields and their significance to programming to the Web. In the "How MIME Data Is Encoded" section, we will look at how the Perl5 MIME:: module can be used to encode data, which can be used in MIME messages. We will then look at a multipart MIME message generated by Netscape Mail, and see how these mechanisms are used in a real-world situation.