Close search

Hack book of Hoa\Mime

The type of resources is standardized based on a format called MIME Media Types. The Hoa\Mime library allows to manipulate this format and to deduce informations about a stream.

Table of contents

  1. Introduction
    1. History
  2. General informations
  3. Informations about a stream
  4. Types of Hoa
  5. Conclusion


All resources in Computer Science do not have the same type. A type allows to identify a resource in order to associate to it a specific interpretation. For example, a resource of type text/html represents an HTML documentation and will be interpreted by a Web browser. Another example, a resource of type video/webm represents a video encoded in the WebM format and can be interpreted in a video player.

We see that the type is expressed with the media/type syntax. A media represents a category of resources. There is 9 ones for now: application, audio, example, image, message, model, multipart, text and video. The standard types do not have any particular prefix, a contrario of experimental types that are prefixed by x-, like image/x-icon. There is also reserved types for the vendors, prefixed by vnd., like application/ For each type, one or many extensions can be associated, like html and htm for text/html. It means that a file with the name Foobar.html is an HTML document.

The authority that is in charge of specifying all types is the IANA (Internet Assigned Numbers Authority). We will find a list of standardized types in the MIME Media Types document (we will also find all the related RFC, especially the ones describing the procedure to standardize a new type).

The Hoa\Mime library allows to manipulate the Mime.types file which, historically, contains all the types (its format is derived from the format used by mailcap, see the RFC1524, and see the explanations on Wikipedia). It also allows to find the type of a stream (a resource) and other associated informations.


At the beginning, this formalism was used for mails (see the RFC2046), which were able to carry documents of any kinds. This is why we speak about MIME types, for Multipurpose Internet Mail Extensions. Thereafter, this format has been extended to other protocols, such as HTTP (with the Content-Type header). Now, this is de facto a standard for all new protocol.

General informations

There is two ways to use the Hoa\Mime\Mime class. We will start by the static approach.

The two main methods are Hoa\Mime\Mime::getExtensionsFromMime to get the associated extensions of a type, and Hoa\Mime\Mime::getMimeFromExtension for the opposite operation, namely to get the type based on an extension. Thus:


 * Will output:
 *     Array
 *     (
 *         [0] => html
 *         [1] => htm
 *     )


 * Will output:
 *     string(10) "video/webm"

We can also know if an extension exists in our Mime.types file thanks to the Hoa\Mime\Mime::extensionExists method (still static).

By default, Hoa\Mime\Mime will use the hoa://Library/Mime/Mime.types file as a database. We can specify another file by using the Hoa\Mime\Mime::compute method:


Every informations provided by the Hoa\Mime\Mime class are computed from this file. Consequently, it will be better to execute this method before any operation on the Hoa\Mime\Mime class.

Informations about a stream

Another way to use the Hoa\Mime\Mime library is dynamically, based on a stream. We instanciate our class by giving it a stream, and then we will be able to know its type, its extensions etc. Thus:

$type = new Hoa\Mime\Mime(new Hoa\File\Read('index.html'));

We now have the following methods:

Finally, we have two methods: isExperimental to know whether the type is experimental or not, and isVendor to know whether the type is a vendor or not. For example:


 * Will output:
 *     string(4) "html"
 *     array(1) {
 *       [0]=>
 *       string(3) "htm"
 *     }
 *     string(9) "text/html"
 *     bool(false)

That's all!

Types of Hoa

Hoa defines several languages, with their own types. It is for example the case of the PP language in Hoa\Compiler. Each library which provides a new language has a hoa://Library/library-name/.Mime file, with the same format as Mime.types. For the PP language, its extension is pp and its type is text/vnd.hoa.compiler.

To find all types defined by Hoa, you can execute the following command line:

$ find /usr/local/lib/Hoa -name '.Mime' | xargs cat


The Hoa\Mime library allows to manipulate quickly and simply the Mime.types file.

An error or a suggestion about the documentation? Contributions are welcome!