{"id":118,"date":"2008-09-23T20:23:24","date_gmt":"2008-09-24T00:23:24","guid":{"rendered":"http:\/\/steve.vinoski.net\/blog\/?p=118"},"modified":"2008-09-23T20:23:24","modified_gmt":"2008-09-24T00:23:24","slug":"mimeparse-in-erlang","status":"publish","type":"post","link":"https:\/\/steve.vinoski.net\/blog\/2008\/09\/23\/mimeparse-in-erlang\/","title":{"rendered":"Mimeparse in Erlang"},"content":{"rendered":"<p>If you&#8217;re writing RESTful web services, you need to be able to parse HTTP <code>Accept<\/code> and <code>Content-type<\/code> headers. The <code>Accept<\/code> header tells you what media types the client can handle, and <code>Content-type<\/code> tells you what the client is sending when it invokes <code>PUT<\/code> or <code>POST<\/code>.<\/p>\n<p>Several years ago <a href=\"http:\/\/bitworking.org\/\">Joe Gregorio<\/a> wrote a <a href=\"http:\/\/www.xml.com\/pub\/a\/2005\/06\/08\/restful.html\">really nice article<\/a> explaining how to properly handle such parsing, and he implemented his ideas in the form of the <a href=\"http:\/\/code.google.com\/p\/mimeparse\/\"><code>mimeparse<\/code> Python module<\/a>. Later, others added Ruby and PHP versions.<\/p>\n<p>In my Erlang web services work I&#8217;ve been doing just enough to parse my <code>Accept<\/code> headers, knowing full well my approach had holes in it and would eventually need to be done right. When I recently stumbled on Joe&#8217;s article and code, I decided to port it to Erlang and use it. Joe&#8217;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 <code>best_match<\/code> 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.<\/p>\n<p>I also found that Java&#8217;s <code>URLConnection<\/code> class, or maybe it&#8217;s <code>HttpURLConnection<\/code>, sends a plain &#8220;*&#8221; as a wildcard MIME type, which I don&#8217;t believe is legal (please correct me if I&#8217;m wrong), but since I figure there&#8217;s probably more than a few clients out there based on that code, I modified the four <code>mimeparse<\/code> implementations to handle that as well, treating it as &#8220;*\/*&#8221;.<\/p>\n<p>All four implementations are available from the <a href=\"http:\/\/code.google.com\/p\/mimeparse\/\"><code>mimeparse<\/code> project home page<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58,4,43,11,10,62,7],"tags":[169,162,109,110,146],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry","category-code","category-erlang","category-http","category-python","category-rest","category-ruby","category-services","tag-code","tag-http","tag-mime-types","tag-parsing","tag-rest"],"_links":{"self":[{"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/comments?post=118"}],"version-history":[{"count":5,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"predecessor-version":[{"id":123,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/posts\/118\/revisions\/123"}],"wp:attachment":[{"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}