Writing

Software, technology, sysadmin war stories, and more. Feed
Friday, March 29, 2013

"Virtual Rachel" is live, first lesson: query strings

Short version: It's live!

Long version:

Okay, it's been long enough, and it's time to start my series of small lessons for building web applications in C++ from (almost) scratch. The point here is not to make something which is going to be a real site, but rather to show what sorts of things happen along the way, and ultimately end with something which can be used to do real work.

You have to build up to writing nice code where everything is clean and laid out just so. With that in mind, I'm going to approach some problems from a relatively low level and work my way up.

The first task is to handle a HTTP query string when run as a CGI program. Whenever you see a URL which is like /foo/bar?a=1&b=2, that "a=1&b=2" part is the query string. This will show up in the environment as a variable called QUERY_STRING. You need to fetch it and parse it into something useful -- preferably something which will allow direct access to variables by name: "a", "b", and so on.

A web framework typically does this sort of thing for you, but we're operating at a lower level for the sake of exploration and learning here. That means we get to handle all of it directly.

In this lesson, I demonstrate reading it from the environment and then slicing it up with a simple state machine.

You should also tune in for future installments where I'll split things up for testing and general cleanliness, and then write those tests and run them in coverage mode. Then it'll be quite obvious how many paths have been tested and which ones still need some work.

Ready? Okay? Let's go!

It's cheaper than a trip to your favorite coffee shop.