<?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>Kevin Grad &#187; html</title>
	<atom:link href="http://couchware.ca/www/kev/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://couchware.ca/www/kev</link>
	<description>Designer, Programmer and Co-Founder</description>
	<lastBuildDate>Sat, 05 Feb 2011 20:51:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Coding a web server with C# &#8211; part 2</title>
		<link>http://couchware.ca/www/kev/2011/02/05/coding-a-web-server-with-c-part-2/</link>
		<comments>http://couchware.ca/www/kev/2011/02/05/coding-a-web-server-with-c-part-2/#comments</comments>
		<pubDate>Sat, 05 Feb 2011 20:48:11 +0000</pubDate>
		<dc:creator>kev</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[web-server]]></category>

		<guid isPermaLink="false">http://couchware.ca/www/kev/?p=269</guid>
		<description><![CDATA[Hey all,
We left off at the end of my last article being able to grab and print a GET request from a web page. There were a few issues with the way we did things however. Firstly, we were reading in a buffer the size of the maximum possible buffer size, so when we were [...]]]></description>
			<content:encoded><![CDATA[<p>Hey all,</p>
<p>We left off at the end of my last article being able to grab and print a GET request from a web page. There were a few issues with the way we did things however. Firstly, we were reading in a buffer the size of the maximum possible buffer size, so when we were printing out our request it was followed by a ton of whitespace. In order to fix this I tried to trim the string for whitespace, this did not work.<br />
<span id="more-269"></span></p>
<p>It turns out that if you take a look at the ASCII characters of the string you get in the byte buffer you will notice that there are a ton of 0&#8217;s at the end of the string. This corresponds to the null character. When Receive() grabs data from the socket, it will fill up the remainder of the array with null characters, so to trim the string we simply use</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;">    convertedData <span style="color: #008000;">=</span> convertedData.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\0</span>'</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Our next issue is how to parse the request we get. Basically all we want to do is find out what page/resource they are looking for and then check to see if that page exists, if it does respond with it, otherwise respond with nothing. However, there is a special case we have to look for. When a person browses to a site like www.blah.com the first get request is always going to look like &#8220;GET / http/1.1&#8243;, followed by the rest of the GET request. What this means is that the browser is looking for the &#8220;/&#8221; file or resource. By convention this &#8220;/&#8221; is always index.html (unless otherwise defined) so we have to check for this in our server. </p>
<p>So I created a method to parse our get that just looks at the first line of the request and returns the proper resource.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> ParseGet<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span> getRequest<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Set the directory to server from to desktop\sites.</span>
    var path <span style="color: #008000;">=</span> Environment.<span style="color: #0000FF;">GetFolderPath</span><span style="color: #000000;">&#40;</span>Environment.<span style="color: #0000FF;">SpecialFolder</span>.<span style="color: #0000FF;">DesktopDirectory</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span>sites&quot;</span><span style="color: #008000;">;</span>
    var words <span style="color: #008000;">=</span> getRequest.<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>words<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;GET&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>words<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;/&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">!</span>File.<span style="color: #0000FF;">Exists</span><span style="color: #000000;">&#40;</span>path <span style="color: #008000;">+</span> words<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">:</span> File.<span style="color: #0000FF;">ReadAllBytes</span><span style="color: #000000;">&#40;</span>path <span style="color: #008000;">+</span> words<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">else</span>
    <span style="color: #000000;">&#123;</span>
        var bytes <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">ReadAllBytes</span><span style="color: #000000;">&#40;</span>path<span style="color: #008000;">+</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span>index.html&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> bytes<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Our final problem is that currently, we can only accept one request at a time. We listen on the port until a request is made, then we process that request until completion, then we listen again. We want to be able to handle multiple requests concurrently, so we must thread our request procession. To do this, we move our request processing out into its own method:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ProcessRequest<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">Object</span> o<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    var acceptedSocket <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Socket<span style="color: #000000;">&#41;</span> o<span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">// Read request.</span>
    var data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span>acceptedSocket.<span style="color: #0000FF;">ReceiveBufferSize</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
    acceptedSocket.<span style="color: #0000FF;">Receive</span><span style="color: #000000;">&#40;</span>data<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    var convertedData <span style="color: #008000;">=</span> Encoding.<span style="color: #0000FF;">ASCII</span>.<span style="color: #0000FF;">GetString</span><span style="color: #000000;">&#40;</span>data<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Receive() fills the buffer with nulls after the data has been read.</span>
    convertedData <span style="color: #008000;">=</span> convertedData.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\0</span>'</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">char</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> delimiters <span style="color: #008000;">=</span> <span style="color: #000000;">&#123;</span> <span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\r</span>'</span>, <span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\n</span>'</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    var lines <span style="color: #008000;">=</span> convertedData.<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span>delimiters, StringSplitOptions.<span style="color: #0000FF;">RemoveEmptyEntries</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0}&quot;</span>, convertedData<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    var outData <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Write response.</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Make sure there is data to parse.</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span> <span style="color: #008000;">!=</span> lines.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        outData <span style="color: #008000;">=</span> ParseGet<span style="color: #000000;">&#40;</span>lines<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    acceptedSocket.<span style="color: #0000FF;">Send</span><span style="color: #000000;">&#40;</span>outData<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    acceptedSocket.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You will notice from the above code that ProcessRequest takes an Object instead of a socket. This is too bad, it would be nice if you could do that<br />
ParameterizedThreadStart requires a method that takes an object and I could find no way around this.</p>
<p>Our final run method now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Set up socket for listening.</span>
    var hostIp <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Dns.<span style="color: #0000FF;">GetHostAddresses</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;127.0.0.1&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Listening on {0}:{1}&quot;</span>, hostIp, Port<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    var endPoint <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IPEndPoint<span style="color: #000000;">&#40;</span>hostIp, Port<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    TcpSocket.<span style="color: #0000FF;">Bind</span><span style="color: #000000;">&#40;</span>endPoint<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        TcpSocket.<span style="color: #0000FF;">Listen</span><span style="color: #000000;">&#40;</span>BackLog<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        var processThread <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ParameterizedThreadStart<span style="color: #000000;">&#40;</span>ProcessRequest<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        processThread.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span>TcpSocket.<span style="color: #0000FF;">Accept</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>If you create a directory on your desktop, place an index.html in there and then navigate to http://127.0.0.1 it will now<br />
serve your very own index.html. Here is mine:<br />
<img src="http://couchware.ca/users/kevin/index.png" alt="Index!" /><br />
If you would like to check out the code, it is available here: <a href="http://code.google.com/p/kevserver/">KevServer</a></p>
]]></content:encoded>
			<wfw:commentRss>http://couchware.ca/www/kev/2011/02/05/coding-a-web-server-with-c-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using custom attributes for validation</title>
		<link>http://couchware.ca/www/kev/2009/02/02/using-custom-attributes-for-validation/</link>
		<comments>http://couchware.ca/www/kev/2009/02/02/using-custom-attributes-for-validation/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 18:20:39 +0000</pubDate>
		<dc:creator>kev</dc:creator>
				<category><![CDATA[jquery]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[selectors]]></category>
		<category><![CDATA[servlet]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/kev/?p=66</guid>
		<description><![CDATA[I was hired a couple of months ago to build a validation system for a legacy web-app built on Java Servlet technology. The app was a gigantic system with many existing web forms.

The job requirements included extendability, maintenance, minimal overhead, and it had to work with the current code, i.e. I couldn&#8217;t go and redesign [...]]]></description>
			<content:encoded><![CDATA[<p>I was hired a couple of months ago to build a validation system for a legacy web-app built on <a href="http://java.sun.com/products/servlet/">Java Servlet technology</a>. The app was a gigantic system with many existing web forms.</p>
<p><span id="more-66"></span></p>
<p>The job requirements included extendability, maintenance, minimal overhead, and it had to work with the current code, i.e. I couldn&#8217;t go and redesign the entire system. There were already hundreds of forms in the system with thousands of fields requiring validation. My first task was to explore the different possibilities. I came up with a few various schemes for both client side and server side validations. I opted to go with a client side solution for various reasons, including the fact that I could update the page with any error messages without ever needing to contact the server.</p>
<p>The major problem that I had to address was how to insert my validation functions within the already created web forms in an easy, extensible way. At the time I had been playing around with a JavaScript library known as <a href="http://jquery.com">jQuery</a>. I had very little experience with it, but it seemed very interesting. Cam had also recently begun using jQuery and was big on the library.</p>
<p>One of jQueries big features is easy manipulation of the DOM tree. jQuery ingeniously makes use of <a href="http://www.w3.org/TR/CSS2/selector.html">CSS Selectors</a> to wrap a group of elements in a jQuery wrapper. If you have never used CSS selectors, I HIGHLY recommend taking a look at them.</p>
<p>Using jQuery, the following simple code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.foo'</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>returns an array of all DOM elements with the attribute class=&#8221;foo&#8221;. You can then iterate over this wrapped set in a number of ways. This is immensely powerful. Additionally, jQuery will recognize custom attributes.</p>
<p>The code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[required=true]'</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>will return all input elements with a custom attribute required=&#8221;true&#8221;. What this means is, if you have in your HTML a text field like this:</p>
<p>&lt;input type=&#8221;text&#8221; required=&#8221;true&#8221;&gt;</p>
<p>The jQuery code above will find that element and return it in a wrapped jQuery array. I used this as the basis for my validation system.</p>
<p>What I did, was define a set of custom attributes, and a set of JavaScript functions based on requirements given to me by the hiring company. Then I created a validate() function which sequentially called all of my JavaScript functions. This validate function was meant to be called in an onSubmit() but could be called anytime.</p>
<p>If you ever wanted a field to be validated, you simply added my custom attributes to its HTML markup. So for example, if you wanted a field to be marked required, you simply went to that field and added required=&#8221;true&#8221;. If you wanted a field to be checked for a valid integer, you went to the field and added type=&#8221;integer&#8221;.</p>
<p>The beauty of this approach is that it allows a field to be marked with an arbitrary number of validations. And removing validation is as easy as removing the custom attribute. Additionally, if you add a new function to the set of JavaScipt functions, all you need to do to add it to a field is add the new custom attribute.</p>
<p>The way this works is, when the person submits the form, the validate function is called. The validate function uses jQuery to parse the DOM tree and iterate over the results. So within the validate function, the first function looked something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[required=yes]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>    
&nbsp;
  <span style="color: #006600; font-style: italic;">// validate the required strings.</span>
  <span style="color: #003366; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> validateRequired<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'field_name'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// If we had an error, concatenate it into the error string and add the errorCase</span>
  errorStr <span style="color: #339933;">+=</span> errorCheck<span style="color: #009900;">&#40;</span>str<span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What this did was, it searched the page for any input elements with the custom attribute required=&#8221;yes&#8221;. It then iterated over that set and passed the value into the JavaScript function validateRequired() which checked whether the field was non-empty. It then returned an error string from a function that properly formatted error messages.</p>
<p>Each method concatenated an error string when there were any errors, and at the end of the validate() function if there were any error messages it printed them to the screen in a custom div. Otherwise, it allowed the submit to go through.</p>
<p>This was done for every type of validation, even if there was no elements with that custom tag within the current page. This makes it easy to simply add new validations on the fly. Also, all the JavaScript functions are in a central place and are easy to modify/update and have it filter through the system automatically.</p>
<p>This solution worked perfectly as it was easy to implement and fit the requirements perfectly. The client was pleased and everything worked out.</p>
<p>This may not be the way I would design the validation system today, but it was an excellent choice at the time.</p>
]]></content:encoded>
			<wfw:commentRss>http://couchware.ca/www/kev/2009/02/02/using-custom-attributes-for-validation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

