Mimeparse in Erlang

September 23rd, 2008  |  Published in code, erlang, HTTP, python, REST, Ruby, services  |  4 Comments  |  Bookmark on Pinboard.in

If you’re writing RESTful web services, you need to be able to parse HTTP Accept and Content-type headers. The Accept header tells you what media types the client can handle, and Content-type tells you what the client is sending when it invokes PUT or POST.

Several years ago Joe Gregorio wrote a really nice article explaining how to properly handle such parsing, and he implemented his ideas in the form of the mimeparse Python module. Later, others added Ruby and PHP versions.

In my Erlang web services work I’ve been doing just enough to parse my Accept headers, knowing full well my approach had holes in it and would eventually need to be done right. When I recently stumbled on Joe’s article and code, I decided to port it to Erlang and use it. Joe’s code is very clean and has accompanying unit tests, so the port was pretty easy. However, in the process of plugging it into my system I found a reasonably common case where the best_match function would return a wildcard match in preference to an exact match, so I added unit tests for that case and repaired the problem for all four languages.

I also found that Java’s URLConnection class, or maybe it’s HttpURLConnection, sends a plain “*” as a wildcard MIME type, which I don’t believe is legal (please correct me if I’m wrong), but since I figure there’s probably more than a few clients out there based on that code, I modified the four mimeparse implementations to handle that as well, treating it as “*/*”.

All four implementations are available from the mimeparse project home page.

Responses

  1. Subbu Allamaraju says:

    September 24th, 2008 at 5:51 pm (#)

    You are right about the HttpURLConnection sending “*” as the wildcard.

    By the way, in Java, the best way to parse MIME types is by using the javax.mail.internet.ContentType class, which conveniently deals with parameters (such as q, charset etc.) in a generic fashion.

  2. WSOAC#33 - Amazon’s Content Delivery Network - Service Endpoint says:

    September 25th, 2008 at 10:45 pm (#)

    [...] Vinoski – Mimeparse in Erlang Stu [...]

  3. Jacob Gabrielson says:

    October 3rd, 2008 at 2:43 pm (#)

    That’s a very interesting module. What http server implementation are you using with it? Mochiweb? Or something else?

  4. steve says:

    October 3rd, 2008 at 2:49 pm (#)

    @Jacob: I use it with Yaws, but it would work with any web server.