<?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>Tim Van Wassenhove &#187; Perl</title>
	<atom:link href="http://www.timvw.be/category/information-technology/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.timvw.be</link>
	<description>The journey of a thousand miles begins with one step.</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:07:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Simple HTTP proxy with Perl</title>
		<link>http://www.timvw.be/simple-http-proxy-with-perl/</link>
		<comments>http://www.timvw.be/simple-http-proxy-with-perl/#comments</comments>
		<pubDate>Tue, 20 Dec 2005 19:23:45 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://www.timvw.be/simple-http-proxy-with-perl/</guid>
		<description><![CDATA[Today i had to demonstrate my version of a HTTP proxy and hand in the code. There are already various programs that do this, but here is my version: httpproxy.txt]]></description>
			<content:encoded><![CDATA[<p>Today i had to demonstrate my version of a HTTP proxy and hand in the code. There are already various programs that do this, but here is my version: <a href="http://www.timvw.be/wp-content/code/perl/httpproxy.txt">httpproxy.txt</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/simple-http-proxy-with-perl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Parsing http headers</title>
		<link>http://www.timvw.be/parsing-http-headers/</link>
		<comments>http://www.timvw.be/parsing-http-headers/#comments</comments>
		<pubDate>Mon, 14 Nov 2005 19:42:37 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://www.timvw.be/parsing-http-headers/</guid>
		<description><![CDATA[Today i updated my HTTP proxy a little. RFC 2616 describes Message Headers as following: message-header = field-name ":" [ field-value ] field-name = token field-value = *( field-content &#124; LWS ) field-content = &#60;the OCTETs making up the field-value and consisting of either *TEXT or combinations of token, separators, and quoted-string&#62; Here is the [...]]]></description>
			<content:encoded><![CDATA[<p>Today i updated my HTTP proxy a little. <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.html">RFC 2616</a> describes <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2">Message Headers</a> as following:</p>

<blockquote>
<div>
<pre>
message-header = field-name ":" [ field-value ]
field-name     = token
field-value    = *( field-content | LWS )
field-content  = &lt;the OCTETs making up the field-value and consisting of 
                 either *TEXT or combinations of token, separators, 
                 and quoted-string&gt;
</pre>
</div>
</blockquote>

<p>Here is the code i used to get the field-name and field-value. Do you see the bug?</p>
<pre class="brush: perl;">
my ($name, $value) = split /:/, $in;
</pre>

<p>Location headers look like &#8220;header: http://www.example.com&#8221;. Now, the problem is that <a href="http://perldoc.perl.org/functions/split.html">split</a> returns a list with &#8220;location&#8221;, &#8220;http&#8221; and &#8220;www.example.com&#8221;. Here is the solution:</p> 

<pre class="brush: perl;">
my $in = &quot;location: http://www.example.com&quot;;
my ($name, $value) = split /:/, $in, 2;
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/parsing-http-headers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading http chunked body</title>
		<link>http://www.timvw.be/reading-http-chunked-body/</link>
		<comments>http://www.timvw.be/reading-http-chunked-body/#comments</comments>
		<pubDate>Mon, 14 Nov 2005 19:40:53 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://www.timvw.be/reading-http-chunked-body/</guid>
		<description><![CDATA[Here is my implementation of Chunked Transfer Coding. I find it quite elegant compared to other snippets i&#8217;ve seen. ################################################### # {{{ read body chunked ################################################### sub getbodychunked { my $sh = shift; # the socket handle should be passed my ($in, $body); for ($in = &#60; $sh&#62;; defined $in; $in = &#60; $sh&#62;) { [...]]]></description>
			<content:encoded><![CDATA[<p>Here is my implementation of <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1">Chunked Transfer Coding</a>. I find it quite elegant compared to other snippets i&#8217;ve seen.</p>

<pre class="brush: perl;">
###################################################
# {{{ read body chunked
###################################################
sub getbodychunked
{
	my $sh = shift; # the socket handle should be passed
	my ($in, $body);

	for ($in = &lt; $sh&gt;; defined $in; $in = &lt; $sh&gt;)
	{
		$in = trim $in;
		my $chunk = hex $in;
		while (defined $in &amp;&amp; $chunk &gt; 0)
		{
			$chunk -= read $sh, $in, $chunk;
			$body .= $in;
		}
	}
	return $body;
}
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/reading-http-chunked-body/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handling options</title>
		<link>http://www.timvw.be/handling-options/</link>
		<comments>http://www.timvw.be/handling-options/#comments</comments>
		<pubDate>Thu, 10 Nov 2005 19:45:54 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://www.timvw.be/handling-options/</guid>
		<description><![CDATA[Last couple of days i&#8217;ve been writing a little script in Perl. One of the requirements is the following: it accepts (eventual) parameters to indicate the port it should use, where it should write log messages, how verbose the logging is. To do implement this i use the Getopt::Std module. You can see in the [...]]]></description>
			<content:encoded><![CDATA[<p>Last couple of days i&#8217;ve been writing a little script in Perl. One of the requirements is the following: it accepts (eventual) parameters to indicate the port it should use, where it should write log messages, how verbose the logging is. To do implement this i use the <a href="http://perldoc.perl.org/Getopt/Std.html">Getopt::Std</a> module. You can see in the following snippet how easy it makes things: (Notice how it takes care of invalid option switches etc&#8230;)</p>

<pre class="brush: perl;">
#!/usr/bin/env perl
###############################################
# Author: Tim Van Wassenhove
# Update: $Id:$
###############################################
# {{{ initialize
###############################################
use strict;
use diagnostics;
use Getopt::Std;

use constant {
        DEFAULT_PORT =&gt; 8888,
        DEFAULT_LOGFILE =&gt; &quot;&amp;STDOUT&quot;,
        DEFAULT_LOGLEVEL =&gt; 5,
        PROGNAME =&gt; &quot;stupid proxy/0.1&quot;,
};
# }}}
###############################################
# {{{ display usage options
###############################################
sub usage
{
        print STDERR &lt; &lt; &quot;EOF&quot;;
usage: $0 [-p port] [-f logfile] [-l level]

-h              : this (help) message
-p port         : the portnumber to run this proxy server on
-f logfile      : the file where the logging message go to
-l loglevel     : the level of verboseness of the logging, 0 is silent, 5 is loud

Report bugs and suggestions at http://timvw.madoka.be
EOF
        exit;
}
# }}}
###############################################
# {{{ main entry point
###############################################
my %options;
getopts 'p:f:l:h', \%options or usage;
usage if exists $options{h};

# use default port unless user gave different port
my $port = DEFAULT_PORT;
if (exists $options{p} &amp;&amp; $options{p} =~ /^\d+$/)
{
        $port = $options{p};
}

# use default logfile unless user gave different file
my $logfile = DEFAULT_LOGFILE;
if (exists $options{f})
{
        $logfile = $options{f};
}

# use default loglevel, unless user gave different level
my $loglevel = DEFAULT_LOGLEVEL;
if (exists $options{l} &amp;&amp; $options{l} &gt;= 0 &amp;&amp; $options{l} &lt; = 5)
{
        $loglevel = $options{l};
}
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/handling-options/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Passing a filehandle as parameter</title>
		<link>http://www.timvw.be/passing-a-filehandle-as-parameter/</link>
		<comments>http://www.timvw.be/passing-a-filehandle-as-parameter/#comments</comments>
		<pubDate>Thu, 20 Oct 2005 00:23:25 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://www.timvw.be/passing-a-filehandle-as-parameter/</guid>
		<description><![CDATA[To keep things maintainable we split our program in modules, classes, functions&#8230; In perlsub from the execellent perl documentation you can lookup the syntax of how to use functions. Offcourse, you have to digg pretty deep to find out how you can pass a filehandle: # clientproc(*STDOUT); # pass the socket clientproc(*CH); sub clientproc { [...]]]></description>
			<content:encoded><![CDATA[<p>To keep things maintainable we split our program in modules, classes, functions&#8230; In <a href="http://perldoc.perl.org/perlsub.html">perlsub</a>  from the execellent perl documentation you can lookup the syntax of how to use functions. Offcourse, you have to digg pretty deep to find out how you can pass a filehandle:</p>
<pre class="brush: perl;">
# clientproc(*STDOUT);
# pass the socket
clientproc(*CH);

sub clientproc
{
  $fh = shift;
  print $fh &quot;hello world&quot;;
}
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/passing-a-filehandle-as-parameter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
