Tuesday, April 22, 2008

Checking the MIME type of an URL (with Apex)

This posting is to announce that a new MIME type checking page is available at my company's site:


The need for this tool arose after discovering that a site i had bookmarked for doing the same thing had been discontinued or moved to another domain, so i thought it could be a good exercise to create a page with the help of Oracle Application Express and the built-in UTL_HTTP packaged procedures.
At the same time, i wanted to understand better the Oracle Apex HTTP redirect behavior so i came up with this tool addressing both issues.
In the end it was quite entertaining to write the code and i finally got a clear picture of the redirect problem.

But why on earth should one be concerned about the MIME type after all?

Knowing the MIME type of a document retrieved from the web may be necessary when diagnosing problems like a browser that fails to display a document inline, but attempts to download it as a binary file. This kind of situations is often caused by an improper setting of the MIME type in the web server configuration.
A typical example is with certain sites returning SVG documents with an invalid MIME type like application/svg-xml whereas the correct MIME type should be application/svg+xml (note the plus sign instead of the minus sign).
Another frequent hit is a wrong WSDL (Web Service Definition Language) MIME type, so instead of getting application/wsdl+xml, you get text/plain.

Coming to the underlying implementation, if you need to check for the MIME type of an URL programmatically, the functionality will be a refinement of the following basic expression:

text/vnd.wap.wml; charset=UTF-8
due to the http redirects, if you specify an URL pointing to an Oracle Application Express application using the PL/SQL expression above, you may get:

ORA-29273: HTTP request failed
ORA-06512: at "SYS.UTL_HTTP", line 1231
ORA-12545: Connect failed because target host or object does not exist
ORA-06512: at "SYS.HTTPURITYPE", line 108
The quick workaround consists in supplying a complete apex URL including a valid session id (the big number after the second colon), that is something like:

text/html; charset=UTF-8
Note that apex sessions expire over time, so you may get an error if you execute the program above as it is, you must use a "fresh" session ID.

Finding out the actual session id and take you to the final page or document is the primary task of the tool i made available on-line.
Clearly there is something more going on behind the scenes, because of the Apex redirect problem mentioned earlier and incidentally i found out that the built-in method called UTL_HTTP.SET_FOLLOW_REDIRECT(n) doesn't work correctly for n greater than zero when talking to an Apex application, it seems to me that it doesn't cope with relative redirects.
That's why i had to come up with this custom procedure.

So, in case you need to check out the MIME type of a document available online or watch what goes on behind the scenes when opening a public page served by Apex, this tool can help.

Or so i guess :-)

The page is a beta version, there could be situations that i didn't think of, so i strongly encourage to report any issues you may encounter or submit enhancement requests.
The errors returned by the UTL_HTTP package are automatically converted into web links to the yocoya's oracle error translation online tool that i introduced a while ago, providing error message translations in 30 languages and quick links to the online Oracle documentation of different releases and, last but not least, oraclequirks.com itself, just in case, however i haven't had the time to publish anything on these specific errors yet, so may be i'll post something in the next weeks.

No comments:

yes you can!

Two great ways to help us out with a minimal effort. Click on the Google Plus +1 button above or...
We appreciate your support!

latest articles