Home | Software | Rules and About | Atom feed
Admin | Edit
published: Saturday 26 June 2021
modified: Sunday 27 June 2021
author: Hales
markup: textile

Why I recommend CGI instead of web frameworks

Originally written on lobste.rs back in 2018

A comment about CGI in general: it's absolutely beautiful.

When I wrote my own site backend a few years back I had no knowledge about the world of interfacing webservers with code. I discovered that there were many, many methods and protocols that each webserver only seemed to support a subset of. And many people telling me that CGI was old and bad and that I shouldn't use it.

I had a nightmare getting non-CGI things to work. I had no experience and background here, so not much of it made sense. I had presumed 'FCGI' was a "fixed" version of CGI, but didn't succeed at getting it to work after following a few guides and trying a few different webservers. I gave up.

I decided I should do the opposite of modern advice. I tried CGI. And I was immediately hooked by its simplicity.

For those not in the know, a fully working CGI script is as simple as this:

#!/bin/sh
printf 'content-type: text/html\n\n'
printf '<strong>Greetings Traveller</strong>'
printf "<p>The date and time are $(date)</p>"

The webserver itself handles all the difficult bits of the HTTP headers. You just need to provide a content-type and then the page itself. Done.

If you want to provide more (cookies, etc) you can; it's just one more 'printf' line and you're done. No libraries, no functions, no complexity. You don't even have to parse strange constructs. Just print.

If you want to look at URL strings (eg for GET) you just need to be able to access the environment variable QUERY_STRING. If you want to access body data (eg for POST) you just need to read input. Just as if someone was sitting there typing into stdin of your program.

It does get ugly for complex or multipart POSTs. That's where a library or program can help. But you only need to attack that once you get there.

Compare this to every other method of talking to a webserver out there:

A related story of teaching

A few months back I was helping some students with their website project. They were new to web development and had been recommended to use flask, a python library that acts as a webserver and webserver interface all in one. They were having extreme difficultly wrapping their heads around many concepts. Notably:

Many of their problems stemmed from them not knowing how HTTP worked in the first place, so I was teaching them this. What made this process horrible was then also trying to find out how and then explain how flask abstracts these concepts into its own processes and functions. I could understand how to beginners like them it seemed completely opaque.

They thought pages were unreadable objects generated by the templating code, and that the templates themselves were sent to the user's browser along with the page. They thought cookies were handled and stored by the webserver as well as the client. The way flask's functions worked and the examples they followed suggested this to them.

If I'm ever in the situation again of helping new people learn web technology then I'm going to get or convert them to use CGI right off the bat. It's easier to teach, it's easier to understand, easier to get working on most webservers and isn't locked in to any particular language or framework.

The only downside of CGI that I know about is the fact it starts a new process to handle each user request. Yes that's a problem in big sites handling hundreds or thousands of visitors per second. But by the time a new student gets to running a big site they will have already encountered many, many other scalability issues in their code and backend/storage. Let alone teaching them database and security concepts. There's a reason we have quotes like "premature optimisation is the root of all evil".

I don't think students new to webdev should be started on anything other than CGI. They can use any language they want. They can actually understand what they're doing. And they're not hitting any artificial barriers or limits set by frameworks or libraries.

Final notes

The whole idea that "CGI should be dead" makes little sense from my context and point of view. I run my own site, help maintain a few others and try to assist others in learning and coping with webdev.

I think the "CGI should be dead" makes sense only in the context of very high workload sites. Whilst these handle a large percentage of the web's total traffic, the percentage of people actually running these sites is small. Different units: traffic of visitors vs people running sites. I think we confuse them.

It's too easy to get caught up in "professional syndrome", where you look up to the big players and trust in their opinions. But you also need to understand that their opinions are based on their current experiences, which are often a world away from what the rest of us should be worrying about.

If a captain of a battleship says that cannons are his biggest problem then you shouldn't try to learn about and use cannons to build your first ship. You should then realise only a tiny fraction of ships need them, even the really big ones

Related: Time safety is more important than memory safety


metalinchains - website - Monday 26 July 2021

Why i recommend Linux? Cos Windows sucks!!! damn! i know this has nothing to do with the article but man.. working as a technician fixing workstations and laptops that runs Windows while my own computers run Linux is painful. I jump to Linux a couple of years ago and i cant go back. Working with Windows for my clients is pure pain. People with older hardware are suffreing corporate tyranny when their operating systems starts to auto-update to the latest version. Mega ultra bloated, spyware by design.... 2 Gigs of ram with the system idle!!! trying to connect to download updates or to collect private data. With my Manjaro XFCE only 800 megs and for me is more georgeous than Windows desktop.
Im start to think about spreading Linux love to my customers plus i can give them classes if they need. What do you think about that?
I think is a good time to do that, cos Windows 11 is coming and here in South America no everyone can aquiere new hardware just for run a new inoperating system. In my local area no one install Linux or even give people lessons so perhaps i can become the Richard Stallman of the underdevelopment xD
By the way, how are you doing my friend?
Its being a while since the last message :P

Hales - (site author) - Friday 30 July 2021

Hey MiC,

Ditto, but it's the way it is and it's not just Windows

Desktop Win10 machines at work breaking their printer drivers in new and creative ways every month or so. TWAIN and WIA fighting like madmen and the latter changing its settings ephemerally. Desktop Win7 machines forgetting their drivers for webcams (that you only installed last year) and deciding all of their audio should go out over optical SPDIF (which the motherboard doesn't even have a connector for). Microsoft deciding to take over the screen again with some blue wizard demanding you opt into their latest thing ("Finish setting up your computer"/"Try Edge"/"Sign in with your Microsoft Account"/"Printer requires sacrifices").

Alas a lot of this also happens in the *nix world. I've had systemd refuse to boot systems because they're missing something it wants. Pulseaudio years ago used to make my machine go silent, now it seems to break my microphone after video-conferencing with a few different browsers in a row. My parents have eternal troubles with Pulseaudio and HDMI to our TV. Web browsers & websites are an eternal pain regardless of platform.

The difference is my laptop is under _my_ control much more than most people control their computers. I set it up to avoid complex systems and things that take power away from me (and give it to other parties). It doesn't matter what OS I or you use, the many choices of program & config matter. You should have the crazy butchered Win setups I used to use! They were great. bb4win all the way, I'm very sad it doesn't work well anymore.

> Im start to think about spreading Linux love to my customers plus i can give them classes if they need. What do you think about that?

Random tips:
- For people that just browse the web and write documents: perfect. Just teach them how to "Save As->xlsx/docx" in Libreoffice Writer & Calc (and possibly that File->PageSetup is Format->PageStyle), that's about all the differences they need to know about. Oh and install ublock origin for them, please :)
- Many people can't use *nix due to "important apps", eg iTunes, Photoshop suite. Getting these working is a PITA and they break later on with updates, just give them Windows.
- On that note: try and re-use old Windows keys to enable Win10. Sometimes they work, sometimes they don't, once windows is activated it never bothers people about it again (Really MS doesn't care, they get most of their money from after Windows is installed).
- Ordinary folk don't care about "why Linux is better", they assume you are trying to sell them something. Don't even bother. If they want Windows just tell them "sure, but it will be slower and if the key on the case doesn't work then we'll have to pay for a copy". If they say yes then just do that, it's their device they are asking you to help them with (not yours).

> Older hardware

Not even that, there is new stuff out there that barely scratches by.

My parents use cheap low-end laptops for their daily stuff (and they're happy with them). They originally ran Win10, amusingly the first time setup outright apologised for 10 minutes that "Hmm, things are taking longer than usual".

Luckily the stagnation of desktop processors means any desktop from the past 8-10 years can happily run Win10. I buy refurbished machines for my work (circa ~300AUD each), we don't need anything more.

For general home use: 4GB of ram absolute minimum (otherwise web browsers will stomp the computers for certain users). It's important to try putting 30-40AUD into a small 120GB SSD; but it depends on the budgets of your locals, that's not always possible. Ditto for 8GB of RAM.

> By the way, how are you doing my friend?

Going OK. My city (Sydney) is under lockdown again but this time the case numbers don't seem to be under control, immunisation rates are very low. My work still requires me to leave the house and go onsite, doctor's computers and equipment do not stop breaking during COVID. I've reduced my onsite hours and I'm now doing as much as possible from home, that's the best I can do (other than abandon the doctors :P). I've also been setting them up with webcams and teaching them how to video-conference patients.

My family is a little scared but otherwise coping. I have not been able to see some relatives for a long time, including my grandmother, which makes me a little sad. In our first lockdown much of my family lost their jobs, but this time around it seems the business' are better at coping and everyone has kept them.

How is your corner of the world faring?

adi - website - Tuesday 24 August 2021

I'm very much considering using my sh preprocessor https://mkws.sh/pp.html over CGI at some point. Sort of like a mini PHP.

Hales - (site author) - Tuesday 24 August 2021

Ooh so it's an inside-out version of what I'm doing (plaintext inside code versus code inside plaintext). I like it :) Indeed very PHP-like.

Have you tried making it a runtime interpreter rather than a before-hand compiler? ie:

* add #!/bin/pp to the start of the files
* name the files index.pp, about.pp, etc
* chmod a+x *.pp
* setup the HTTP server to execute/interpret files ending in .pp (just as does for .php files)

(this might be exactly what you're saying, apologies if it is)

adi - website - Wednesday 25 August 2021

That's exactly what I'm doing! Actually ran some code like that for while, works really well.

metalinchains - website - Friday 3 September 2021

I'm doing fine (at least the best i can). We are very used to live in constant crisis haha. My mother got sick last week so she got hospitalized for 15 days (covid protocol). Now is at home doing fine. I had the regular flu that last for two weeks (a strong one, or i was a bit weak). Here is ultra difficult to find a job. I'm working freelance. Im searching for some place to do quick remote jobs via internet to get some "dollars" to compensate the country inflation (working locally is a waste of time and efforts). For example 1 dollar equals 200 ARS(our local currency).. so imagine.. 60 dollars = 12.000 ARS.. and 60 dollars is not that much money for the rest of the world (or at least not that much as here). Argentina is dirty cheap if you have the green stuff, the washingtong stuff, if not, you can struggle very bad with a regular job. Regular jobs here pay so bad that you are more poor than a poor person but with responsabilities and a job, here poor people get more money for doing nothing than what you can get for working hard in a 9 hours, 6 days a week regular job (and they get extra financial help for child). The lovely socialism. While the middle class is dying they are fabricating misery to keep the cash flowing. A regular job can pay you 200 dollars a month (obviusly in our currency with inflation it worth nothing)... ridiculous. That's why i decided to start working on the net. If you know someone thay needs anything (data entry, something remote, etc) let me know, I'm searching dor that kind of stuff. Euros... dollars.. i don't care.. anything is better than our money haha.
-----------------------------------------------
Changing subjects.. that version of Windows you mentioned.. do you think it can be used to play retro games? cos i find Linux a bit rusty to give me 100 % compatibility with all my GOG games.. and sometimes i want to play some old RPG like Baldurs Gate and that kind of things and just won't run or wine refuses to run smoothly (you know that feeling.. you want to do something without having to troobleshoot for two hours to make it run). I'm going to use that machine offline so i don't need updates or anything fancy.
Bye bye my friend!


Add your own comment:

Name:
Email (optional):
URL (optional):
Enter the word 'irrlicht' (antispam):
Leave this box blank (antispam):

Comment (plaintext only):

If you provide an email address: it will only be used for the site admin to contact you, it will not be made public.

If you provide a URL: your name will hyperlink to it.