<?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>Guy Rutenberg &#187; cgi</title>
	<atom:link href="http://www.guyrutenberg.com/tag/cgi/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.guyrutenberg.com</link>
	<description>Keeping track of what I do</description>
	<lastBuildDate>Wed, 16 Jun 2010 19:53:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Introduction to C++ CGI &#8211; Processing Forms</title>
		<link>http://www.guyrutenberg.com/2007/09/07/introduction-to-c-cgi-processing-forms/</link>
		<comments>http://www.guyrutenberg.com/2007/09/07/introduction-to-c-cgi-processing-forms/#comments</comments>
		<pubDate>Fri, 07 Sep 2007 19:11:59 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[cgi]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/09/07/introduction-to-c-cgi-processing-forms/</guid>
		<description><![CDATA[In this post I will show you how to process HTML forms easily using CGIs in C++. I assume you have already basic knowledge of writing CGIs in C++, if you don&#8217;t go a head and read Introduction to C++ CGI.
Processing forms is the basic function of any CGI script and the main purpose of [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will show you how to process HTML forms easily using CGIs in C++. I assume you have already basic knowledge of writing CGIs in C++, if you don&#8217;t go a head and read <a href="/2007/08/10/introduction-to-c-cgi/">Introduction to C++ CGI</a>.</p>
<p>Processing forms is the basic function of any CGI script and the main purpose of CGIs. As you probably know there are two common ways to send form data back to the web server: &#8220;post&#8221; and &#8220;get&#8221;. When form data is sent with the &#8220;get&#8221; method it is appended to the URL string of the form submission URL. The &#8220;post&#8221; method is much like the &#8220;get&#8221; except the data is transmitted via http headers and not via the URL itself. When a form uses &#8220;get&#8221; it allows the user to easily bookmark the query created by the form as the data is transmitted in URL itself, on the other hand the &#8220;post&#8221; method allows to send much more data and spares to user from seeing the data in the URL.</p>
<p>Getting the &#8220;post&#8221; and &#8220;get&#8221; data is relatively easy. To get the data sent by &#8220;get&#8221; you can just call <code>getenv("QUERY_STRING")</code> and you will receive a pointer to null-terminated string containing the &#8220;get&#8221; data. Reading the &#8220;post&#8221; data is a bit more complicated. The data needs to be read from the standard input, but the program won&#8217;t receive an EOF when it reaches the end of the data but instead it should stop reading after reading a specified amount of bytes, which is defined in the environment variable &#8220;<code>CONTENT_LENGTH</code>&#8220;. So you should read <code>getenv("CONTENT_LENGTH")</code> bytes from the standard input to receive the &#8220;post&#8221; data. </p>
<p><span id="more-13"></span></p>
<p>But here it&#8217;s get tricky. The data is received in an encoded way. The data for both &#8220;post&#8221; and &#8220;get&#8221; is a string in the form of &#8220;<code>var1=value1&#038;var2=value2&#038;var3=value3</code>&#8221; and so on, where the &#8220;<code>var*</code>&#8221; is the names of the submitted form elements and &#8220;<code>value*</code>&#8221; is their values respectively. And now it gets a bit more tricky, both the elements&#8217; names and values are <a href="http://en.wikipedia.org/wiki/Url_encoding">url-encoded</a>. That means that almost every non alphanumeric character will be encoded as %XX, where XX represents its ASCII value (see the linked Wikipedia article for more information and extra reading). So to sum up the process of processing forms in CGI you should do the following:</p>
<ol>
<li>Get the data (either by &#8220;post&#8221; or &#8220;get&#8221;).</li>
<li>Parse the data to get key-value pairs.</li>
<li>Decode the url-encoded key-value pairs.</li>
<li>Process the data.</li>
</ol>
<p>Getting the first and last step done is easy. The second and third step require some work, but fortunately when done properly they should only have to be done once, as you will create functions/classes to handle those, so it is really only a one time job. That leaves you only to do yourself the first and last steps. </p>
<p>As I said the second and third steps are troublesome only when not done before. <a href='/wp-content/uploads/2007/09/getpost.h' title='getpost.h'>Here is</a> an implementation for those steps that I&#8217;ve written as a part of (yet unreleased) CGI library for C++. It implements functions that take the care of the first three steps and return to the user an STL map containing the form elements&#8217; names in the key and their value in the map pair&#8217;s value. This functions allows to process forms in a similar way to processing forms in PHP using <code>$_GET</code> and <code>$_POST</code>. </p>
<p>Here is a simple example of processing a &#8220;get&#8221; form using the above <code><a href='/wp-content/uploads/2007/09/getpost.h' title='getpost.h'>getpost.h</a></code> header file.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
<span style="color: #339900;">#include &lt;map&gt;</span>
<span style="color: #339900;">#include &quot;getpost.h&quot;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	map<span style="color: #000080;">&lt;</span>string,string<span style="color: #000080;">&gt;</span> Get<span style="color: #008080;">;</span>
	initializeGet<span style="color: #008000;">&#40;</span>Get<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">//notice that the variable is passed by reference!</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Content-type: text/html&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;&lt;html&gt;&lt;body&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;&lt;h1&gt;Processing forms&lt;/h1&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;&lt;form method=<span style="color: #000099; font-weight: bold;">\&quot;</span>get<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; &lt;label for=<span style="color: #000099; font-weight: bold;">\&quot;</span>fname<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;First name: &lt;/label&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; &lt;input type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text<span style="color: #000099; font-weight: bold;">\&quot;</span> name=<span style="color: #000099; font-weight: bold;">\&quot;</span>fname<span style="color: #000099; font-weight: bold;">\&quot;</span> id=<span style="color: #000099; font-weight: bold;">\&quot;</span>fname<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&lt;br&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; &lt;label for=<span style="color: #000099; font-weight: bold;">\&quot;</span>lname<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Last name: &lt;/label&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; &lt;input type=<span style="color: #000099; font-weight: bold;">\&quot;</span>text<span style="color: #000099; font-weight: bold;">\&quot;</span> name=<span style="color: #000099; font-weight: bold;">\&quot;</span>lname<span style="color: #000099; font-weight: bold;">\&quot;</span> id=<span style="color: #000099; font-weight: bold;">\&quot;</span>lname<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&lt;br&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; &lt;input type=<span style="color: #000099; font-weight: bold;">\&quot;</span>submit<span style="color: #000099; font-weight: bold;">\&quot;</span> /&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;&lt;/form&gt;&lt;br /&gt;&lt;br /&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>Get.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;fname&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">!</span><span style="color: #000080;">=</span>Get.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;&amp;</span> Get.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;lname&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">!</span><span style="color: #000080;">=</span>Get.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Hello &quot;</span><span style="color: #000080;">&lt;&lt;</span>Get<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">&quot;fname&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; &quot;</span><span style="color: #000080;">&lt;&lt;</span>Get<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">&quot;lname&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;, isn<span style="color: #000099; font-weight: bold;">\'</span>t &quot;</span>
			<span style="color: #FF0000;">&quot;processing CGI forms with C++ quite easy?&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Fill up the above from and press submit&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;&lt;/body&gt;&lt;/html&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>While this is a simple example, it&#8217;s enough to demonstarte how stuff is done. The same way you can process the &#8220;post&#8221; variables. Feel free to take a look at the <code><a href='/wp-content/uploads/2007/09/getpost.h' title='getpost.h'>getpost.h</a></code> source-code to see how the URL decoding and the other implementation aspects were done.</p>
<p><strong>Update 2009-09-18:</strong> There was a bug in <code>getpost.h</code>, see my comment bellow.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/09/07/introduction-to-c-cgi-processing-forms/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Introduction to C++ CGI</title>
		<link>http://www.guyrutenberg.com/2007/08/10/introduction-to-c-cgi/</link>
		<comments>http://www.guyrutenberg.com/2007/08/10/introduction-to-c-cgi/#comments</comments>
		<pubDate>Thu, 09 Aug 2007 21:22:12 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[cgi]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/08/10/introduction-to-c-cgi/</guid>
		<description><![CDATA[In this post and its follow ups I intend  to cover the basics of CGI programming in C++. There are great performance gain in writing CGIs in C++ compared to interpreted languages such as PHP and it&#8217;s usually it&#8217;s even faster than PHP scripts which are interpreted via mod_php. On the other hand PHP [...]]]></description>
			<content:encoded><![CDATA[<p>In this post and its follow ups I intend  to cover the basics of CGI programming in C++. There are great performance gain in writing CGIs in C++ compared to interpreted languages such as PHP and it&#8217;s usually it&#8217;s even faster than PHP scripts which are interpreted via mod_php. On the other hand PHP and other traditional web development languages are well suited for the task, by means of libraries and development time. However developing small highly efficient CGI scripts in C++ is easier  than you think.<br />
<span id="more-9"></span><br />
We shall start with a writing a basic &#8220;Hello World&#8221; CGI.  Basically, all output to <code>stdout</code> is directed by the web server to the client&#8217;s web browser. However, the web server requires the CGI application to declare in the first line on output what is the MIME type of the content it is producing. This line should be followed by a blank line. The common MIME types are <code>text/plain</code> for unformatted text and <code>text/html</code> for HTML formated content. The MIME type declaring should look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Content-type: text/html&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span></pre></div></div>

<p>So getting back to our &#8220;Hello World&#8221; CGI, it will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Content-type: text/plain&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Hello World!&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Congratulations! You have just created your first C++ CGI. To see it in action, move it to a directory on the web server configured to run CGI scripts, this directory is usually called cgi-bin. Now just point your web browser to the the address of CGI app to see in in action.</p>
<p>Now that you have seen the basic example, let&#8217;s move on to a bit more complex example.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Content-type: text/html&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;&lt;html&gt;&lt;body&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;&lt;h1&gt;Hello World!&lt;/h1&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;This is HTML formatted Hello World C++ CGI&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;&lt;/body&gt;&lt;/html&gt;&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>As you see, even writing pages with HTML content isn&#8217;t that hard. Yes, C++ may not be the easiest programming language for developing web applications, but it may turn out easier than you thought.</p>
<p>In this post I covered creating basic CGI that don&#8217;t relay on user input. As proccessing user input in one of the goals of CGI, I will cover it next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/08/10/introduction-to-c-cgi/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.591 seconds -->
