GOAL!

chromatic on 2002-12-26T00:04:57

I'm on holiday this week, visiting my parents. Part of being on holiday means not doing my day job. (Those of you who've seen me work or who have seen the output of my work know that this is pretty rare.) I do have most of my work resources with me -- my laptop, my phone, a notepad, and an Internet connection, but this week is devoted to something besides writing and editing.

One of my goals this week was to do more work on Everything. Though the company's long since out of business, it's still viable software. There are still plenty of improvements to make, and I'd like to use it for a couple of projects in the future.

To aid in my goals, I decided to write yet another standalone web server. Being a smart (read lazy) programmer, I used HTTP::Daemon. I could install Apache and mod_perl on my laptop and work through the configuration there, but this allows other people to debug Everything on their laptops and will help with a future goal.

HTTP::Daemon and CGI.pm have never played well together. With the GET request method, it's not difficult to populate %ENV appropriately. It's harder with POSTed parameters. One solution is to tie *STDIN. I've done it before. Another idea is to write your own CGI parser. Good luck. (I've done that too.)

This time, I took a two-pronged approach. Since Gisle had already explained the wonders of headers_only last July (thanks, Gisle!), I didn't have to work around HTTP::Daemon having already read the entire request. First, I modified Everything slightly so as to pass an initializer filehandle to the CGI constructor. Then, I overloaded the iterator (readline, basically) in HTTP::Daemon::ClientConn. With a few lines of code to read a line at a time (respecting buffers and reading from sockets), CGI now gets something it expects.

GET works, POST works, images work (and can be generalized to regular files easily), and cookies work. I'm pretty sure uploads work, but I haven't tested them. All of this in a hundred and thirty lines of readable code: EVServer.

That was a big boost to my mood. As it turns out, though, I was coming down with a nasty, nasty head cold. That explains why I've been wanting to sleep twelve hours a day for the last week.


Getting params from HTTP::Daemon

jjohn on 2002-12-27T20:27:34

I'm also working with HTTP::Daemon. It's a pretty neat class (even with the annoying SIGPIPE errors). Collecting CGI form params is a little annoying, but I use this method:

sub get_cgi {
  my ($self, $r) = @_;

  my $q = CGI->new($r->content); # POST args
  my %get_args = $r->url->query_form;

  while (my ($k,$v) = each %get_args) {
    $q->param($k, $v);
  }

  return $q;
}

This lets me pretend that the CGI module is working correctly in this environment. :-)