If you’re writing RESTful web services, you need to be able to parse HTTP
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
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.