Learning CGI: Trying the Simplest Thing Possible: Input a String and Reverse It


So the simplest thing is a “Hello, world!” application. What’s one step up from that? What about something as simple as taking in an input string, and serving some simple output? So I had the idea of something as basic as an html form that takes a string and returns the string reversed, which would mean “Hello, World!” becomes “!dlroW ,olleH”. Can I do that?

I want to learn how to use the BCHS stack. I’m interested because it’s based on principles of security and minimalism. I also like the idea of it being based on C.

Why C? why not C…

Modern life is built on foundations of C, so what can be so wrong with it? Also, I figure, the people working with C back in the day would have solved a lot of hard problems so there’s no point reinventing the wheel. Just look at how they solved things and continue from there. Can I do this challenge then?

Here on the tilde.institute servers, it’s been setup to automatically serve cgi binaries if they end in .cgi.

Initially I tried to build the very minimal “Hello, world!” example that’s given in the manpage for kcgi. I typed it out and compiled it how I thought it should be compiled, but it didn’t work. So after some initial frustration, I was pointed to the kcgi-starter on #institute by ~ahriman. It’s a bit silly because this was all mentioned on the tilde wiki to start with, so I should have known really.

The suggested starter kit is a modified simple version of kcgi and it also comes with a handy makefile that installs it with correct permissions in the public folder here. It compiles the file and the nifty thing is that it has an if-else switch in the code to handle two pages and in the makefile sym-links a second path back to the same .cgi script. That’s handy…

Ok, so now I’m feeling a bit better and getting somewhere, can I tweak it and make soemthing as mundane as a small form?

I messed around a bit with the kcgi-starter kit but wasn’t getting very far. So I ended up reading the man-pages and the website of kcgi. I’m very happy I did. There’s so much good documentation in OpenBSD a man search away. Anyways, there is a tutorial on buildnig a form that validates a string. I’ve typed it out and it looks like the following:


At the time of writing this blog, I’ve typed out the tutorial, compiled and installed the cgi script, and built the most basic of forms. So it doesn’t actually reverse the string… but it’s very close. That will come shortly!