Code is Love

Hacking CakePHP’s request_handler.php to accept Compressed XML

by someguy on Apr.03, 2009, under Uncategorized

Disclaimer: There are undoubtedly cleaner ways of accomplishing this task, which is why I didn’t consider submitting this code back to CakePHP. Feel free to take my problem and come up with your own better solution — this worked for me, and certainly there are others who could benefit from it.

I was having a rough time trying to figure out how I was going to get CakePHP to handle compressed input. In my case, it was from the OCS Inventory Agent, which outputs zlib compressed XML server inventory reports to the host of your choosing. I was essentially doing some REST work between the OCS client and CakePHP — and CakePHP was handing off the request to request_handler properly thanks to the very simple suggested Routes configuration. The problem was that request handler didn’t know what to do with the mime-type “application/x-compress” (i.e. zlib compression), never mind detect the xml inside it and then magically do what it does with plan text XML.

Patch is here. request_handler looks for the “application/x-compress” Content-Type, decompresses the input (POST), then uses simplexml_load_string to determine if the contents are XML since the other check is simply checking for an xml Content-Type. Then the content is turned into an instance of CakePHP’s Xml object, which is then assigned to the $data property of the controller automagically.

Thanks to this post for assistance with handling CRC’s since not all zlib compressed POSTs are created equally. Also thanks to the people who wrote wireshark, which saved me as I debugged HTTP headers in this endeavor.

:, ,

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...