MOBILE

Programming the Mobile Web : Content Delivery (part 1) - Defining MIME Types

2/4/2011 4:44:31 PM
A common situation in the mobile web world is content delivery. Java applications, widgets, music, video, wallpapers, and any other content can be delivered to compatible devices, but this requires a bit of explanation and expertise.

1. Defining MIME Types

MIME types, are a key element for content delivery. Many mobile browsers don’t care about the file extension; they decide whether or not to accept the content based on the MIME type delivered by the server. Remember that the MIME type travels with the HTTP header response.

1.1. Static definition

The simplest way to define the right MIME types is to statically define them on your web server. If you are working with a shared hosting service, the control panels often allow you to define document MIME types. If you manage your own server, you can set them up with the following instructions.

1.1.1. Apache

In Apache, the simplest way is to open the mime.types file located in the conf folder of the Apache root. In your favorite text editor, you can add one row per MIME type to be configured.

You will find hundreds of MIME type declarations. The first thing to do is to look for the following line and change the MIME type to the correct one for mobile XHTML documents:

text/html                    html htm

As you can see, each line contains a MIME type followed by a series of spaces or tabs and a space-separated list of file extensions.


Warning:

This technique applies these changes to all the websites on the server. If you want to make changes to only one website or one folder of a website, you should create or edit the .htaccess file in the appropriate folder and use the AddType procedure:

AddType   text/x-vcard   vcf


You can find an Apache configuration file to download and use with all the important mobile web MIME types at http://www.mobilexweb.com/go/mime.

1.1.2. Internet Information Server

Configuring MIME types in Microsoft IIS can be done via the UI (as opposed to in Apache, where you need to edit a text file). To configure the MIME types in IIS 6.0 on Windows XP or Windows Server 2003:

  1. Go to IIS Manager.

  2. Right-click the whole server, a website, or a folder and select Properties.

  3. On the HTTP Headers tab, click MIME Types.

  4. Click New, type the file extension and MIME type, and press OK to finish.

In IIS 7.0 for Windows Vista/7 and Windows 2008 Server:

  1. Go to IIS Manager.

  2. Navigate to the level you want to manage.

  3. In Features View, double-click on MIME Types.

  4. In the Actions pane, click Add.

  5. Type the file extension and MIME type and press OK to finish.


Note:

In IIS, you can also manage MIME types from the command line. Check the documentation for more information.


In IIS 7.0, it is also possible to define static MIME type declarations in the web.config file in your ASP.NET folder. The syntax is:

<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".mp4" mimeType="video/mp4" />
<mimeMap fileExtension=".vcf" mimeType="text/x-vcard" />
</staticContent>
</system.webServer>
</configuration

1.2. Dynamic definition

The other possible way to declare MIME types is to use dynamic header declarations in your server script code.

In PHP, you should define the MIME type before any other output using the header function:

header('Content-Type: application/xhtml+xml');

If you are delivering downloadable content (not markup), like a video, you should also define a filename. If not, when the file is saved it will have a .php extension and it will not work.


Note:

Remember that it is better to serve XHTML MIME types to mobile websites. You can define them either statically or, if you are using a server-side language, dynamically. You can also query WURFL or another library to check what the preferred MIME type to deliver is and use it to define the header.


To define the name of the file we use the Content-disposition header, as shown in the following sample:

$path = '/videos/video.mp4';
header('Content-Type: video/mp4');
header('Content-disposition: attachment; filename=video.mp4');
// We serve the file from our local filesystem
header("Content-Length: " . filesize($path) );
readfile($path);

ASP.NET has a Response.ContentType property that we can define:

// This is C# code
Response.ContentType = "application/xhtml+xml";

The filename should also be defined if it is downloadable content, using Response.AddHeader.

In a Java servlet or JSP, you should define the headers using the setContentType method of the response object:

response.setContentType("application/xhtml+xml");


Note:

When you are serving non-markup content using a dynamic script, if an error occurs you will not see the error details and the content will be broken (imagine a JPEG with a PHP error as the contents). You should capture any error, send yourself an email or log the error details, and replace the output with generic content.
Other  
  •  iPhone Application Development : Using Switches, Segmented Controls, and Web Views (part 3)
  •  iPhone Application Development : Using Switches, Segmented Controls, and Web Views (part 2)
  •  iPhone Application Development : Using Switches, Segmented Controls, and Web Views (part 1)
  •  iPhone Application Development : Using Advanced Interface Objects and Views - User Input and Output
  •  Windows Phone 7 Development : Wiring Up Events to an Application Bar ( part 2)
  •  Windows Phone 7 Development : Wiring Up Events to an Application Bar ( part 1) - Reacting to Add Button Events
  •  Adding an Application Bar to a Windows Phone 7 Application (part 3) - Adding an Application Bar Using Managed Code
  •  Adding an Application Bar to a Windows Phone 7 Application (part 2) - Adding a Local Application Bar Using XAML & Adding Menu Items
  •  Adding an Application Bar to a Windows Phone 7 Application (part 1) - Adding Images for Use with Application Bar Buttons & Adding a Global Application Bar Using XAML
  •  iPhone Application Development : Creating and Managing Image Animations and Sliders (part 3) - Finishing the Interface
  •  iPhone Application Development : Creating and Managing Image Animations and Sliders (part 2) - Adding an Image View
  •  iPhone Application Development : Creating and Managing Image Animations and Sliders (part 1)
  •  iPhone Application Development : User Input and Output
  •  Windows Phone 7 : Using Accelerometer Data to Move a Ball
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 4) - Device Libraries
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 3) - Transcoders
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 2) - Detecting the Context
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 1) - HTTP
  •  Using Windows Phone 7 Technologies : Retrieving Accelerometer Data (part 2)
  •  Using Windows Phone 7 Technologies : Retrieving Accelerometer Data (part 1)
  •  
    Top 10
    Build Mobile Websites and Apps for Smart Devices : Design for Mobile - Application Icons
    Build Mobile Websites and Apps for Smart Devices : Design for Mobile - Reviewing Our Design
    Build Mobile Websites and Apps for Smart Devices : Design for Mobile - Establish a Style
    HP ProLiant Servers AIS : Processors and Multiprocessing - The Processor Performance Evolution
    HP ProLiant Servers AIS : Processors and Multiprocessing - How Processors Work
    Windows Server 2008 and Windows Vista : Working with GPOs - Group Policy Modeling
    Windows Server 2008 and Windows Vista : Working with GPOs - Group Policy Results
    Microsoft ASP.NET 4 : File System Information (part 2) - The DirectoryInfo and FileInfo Classes, The DriveInfo Class
    Microsoft ASP.NET 4 : File System Information (part 1) - The Path Class, The Directory and File Classes
    Silverlight : Binding Across Elements
    Most View
    Windows Tip Of The Month – October 2012 (Part 2)
    Sweet 660 Ti DrectCU II TCP
    Managing Exchange Server 2010 : Archiving and compliancy (part 2) - Messaging Records Management
    The best browser hacks (part 3) - MS Internet Explorer
    Securing Internet Explorer
    Programming Hashing Algorithms (part 2) - Instantiating the Algorithm
    SQL Azure : Tuning Techniques (part 1) - Dynamic Management Views
    Windows Phone 7 Development : Push Notifications - Implementing Cloud Service to Track Push Notifications
    CPU System Workshop (Part 5) - ZALMAN CNPS11X PERFORMA, Corsair Vengenance K60
    IIS 7.0 : Editing Configuration - Understanding Configuration Errors
    Using SQL Server 2005 Integration Services : Extensibility (part 3) - Script Components
    iPhone Application Development : Getting the User’s Attention - Exploring User Alert Methods
    Multifaceted Tests : Attempting Command Injection Interactively & Attempting Command Injection Systematically
    BenQ XL2420T : Holy Swivelling Monitor!
    Get More From Your Mobile Devices (Part 2)
    Zotac Zbox Id80 Plus
    PhotoDirector 3 - Gets snap happy
    Where Windows Malware Hides
    IIS 7.0 : Managing Virtual Directories
    Windows Phone 7 : Working with Controls and Themes - Adding Transition Effects