<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stuart Jones &#187; http post</title>
	<atom:link href="http://gingerbbm.com/tag/http-post/feed/" rel="self" type="application/rss+xml" />
	<link>http://gingerbbm.com</link>
	<description>User Experience Design &#38; Software Development</description>
	<lastBuildDate>Mon, 05 Dec 2011 23:13:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>HTML images using long GET strings</title>
		<link>http://gingerbbm.com/2009/06/html-images-using-long-get-strings/</link>
		<comments>http://gingerbbm.com/2009/06/html-images-using-long-get-strings/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 21:15:13 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[http get]]></category>
		<category><![CDATA[http post]]></category>
		<category><![CDATA[img]]></category>
		<category><![CDATA[long url]]></category>
		<category><![CDATA[wms]]></category>

		<guid isPermaLink="false">http://gingerbbm.com/?p=127</guid>
		<description><![CDATA[The Problem You&#8217;ve got a piece of code that creates an image in your web page by passing a bunch of parameters in a GET string to some server-side process: $&#40;'tile'&#41;.src = 'http://img.com/generate.php?a=1&#38;amp;b=2&#38;amp;c=3'; But what do you do when the &#8230; <a href="http://gingerbbm.com/2009/06/html-images-using-long-get-strings/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>The Problem</h2>
<p>You&#8217;ve got a piece of code that creates an image in your web page by passing a bunch of parameters in a GET string to some server-side process:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tile'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://img.com/generate.php?a=1&amp;amp;b=2&amp;amp;c=3'</span><span style="color: #339933;">;</span></pre></div></div>

<p>But what do you do when the URL goes <a href="http://www.boutell.com/newfaq/misc/urllength.html" target="_blank">beyond the limit</a>? Practically, for cross-browser solutions, the limit is around 2000 characters (thanks to IE), and I recently had to find a workaround when generating images via a <a href="http://en.wikipedia.org/wiki/Web_Map_Service" target="_blank">WMS</a>.<span id="more-127"></span></p>
<h2>The Solution</h2>
<p>When we&#8217;re dealing with HTML forms, if a GET isn&#8217;t big enough we have to use a POST. The difficulty in the scenario described above is that there&#8217;s no way to provide the POST variables when making the assignment to the image&#8217;s <em>src</em> attribute.</p>
<p>I thought about how to use an Ajax POST but even if image data could be streamed back to the browser in this way, there&#8217;s no mechanism for getting it into an HTML image.</p>
<p>And then I discovered the <a href="http://en.wikipedia.org/wiki/Data_URI_scheme" target="_blank">data URI scheme</a> which allows us to assign a <a href="http://en.wikipedia.org/wiki/Base64" target="_blank">base64</a> representation of an image to an HTML image like so (where the ellipsis indicates the position of the base64 data string):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tile'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'data:image/gif;base64,...'</span><span style="color: #339933;">;</span></pre></div></div>

<p>If I could issue a POST to the server to generate my image, I could convert that image into a base64 string to pass back to the browser, which I could then utilise in the data URI scheme syntax.</p>
<p>I discovered this method in <a href="http://www.phpied.com/data-urls-what-are-they-and-how-to-use/" target="_blank">this brilliant post</a> where the same trick is used to for CSS and to reduce HTTP requests. And by reading that post I learned that <a href="http://www.phpied.com/mhtml-when-you-need-data-uris-in-ie7-and-under/" target="_blank">we have to do it another way in IE</a>. In IE we have to use <a href="http://en.wikipedia.org/wiki/MHTML" target="_blank">MHTML</a> and it involves a little more work on the server but we can still end up with this (where <em>somestring</em> indicates the position in <em>mhtml.txt</em> of the base64 data string):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tile'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'mhtml:http://img.com/mhtml.txt!somestring'</span><span style="color: #339933;">;</span></pre></div></div>

<p>A proxy script on the server is required to pass the POST variables through to the image-generation program, and convert the resultant image into a base64 data string. Since the WMS I use is located on an external server I was already using <a href="http://www.troywolf.com/articles/php/class_http/proxy.phps" target="_blank">Troy Wolf&#8217;s PHP proxy script</a> (note that it requires <em><a href="http://www.troywolf.com/articles/php/class_http/class_http.phps" target="_blank">class.php</a></em>). Converting to base64 is a cinch in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$base64_encoded_image</span> <span style="color: #339933;">=</span> <span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I simply added the code I needed to the end of Troy&#8217;s proxy script.</p>
<p>So, the flow of events is:</p>
<ul>
<li>Client issues an Ajax POST request to the proxy script.</li>
<li>Proxy forwards the request to the image-generation program and converts the resultant image into a base64 data string.
<ul>
<li>For IE, the proxy saves the string to a file and returns the filename to the client.</li>
<li>For all other browsers, the proxy returns the string to the client.</li>
</ul>
</li>
<li>Client&#8217;s Ajax &#8220;on success&#8221; callback assigns the relevant string to the HTML image&#8217;s <em>src</em> attribute.</li>
</ul>
<p>Anyway, <a href="http://gingerbbm.com/demo/imagebypost/">here&#8217;s a demo page</a>.<br />
And <a href="http://gingerbbm.com/demo/imagebypost/imagebypost.zip">here&#8217;s a zip</a> containing all the necessary files.</p>
<h2>A Few Considerations</h2>
<p>The IE workaround involves creating a file on the server that contains the base64 representation of the generated image. The format of the file must be as follows in terms of line breaks, and in terms of the double quotes around the boundary parameter:</p>
<pre>Content-Type: multipart/related; boundary="_ANY_SEPARATOR"

--_ANY_SEPARATOR
Content-Location:somestring
Content-Transfer-Encoding:base64

R0lGODlh...snip...AACgAACoAAC</pre>
<p>The folder in which the MHTML files are generated obviously requires the appropriate privileges. If this method were used in a production environment an automated procedure would be required to delete periodically old MHTML files. <a href="http://www.microsoft.com/windows/internet-explorer/default.aspx" target="_blank">IE8</a> is a great improvement over previous versions and actually supports the data URI scheme, so one day in the distant future all the MHTML workaround shenanigans will be unnecessary. But until then we&#8217;re stuck with having to write a file to the server every time &#8211; and if we&#8217;re doing that it might be preferable simply to write the image to the server and return its URL to the browser. At least it would mean no JavaScript browser-sniffing, and that&#8217;s no bad thing.</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbbm.com/2009/06/html-images-using-long-get-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

