<!doctype html><htmllang=en><head><metacharset=utf-8><metaname=viewportcontent="width=device-width,initial-scale=1"><linkrel="shortcut icon"href=/img/icon.pngtype=image/png><metaname=generatorcontent="Hugo 0.79.0"><metaproperty="og:title"content="Esoteric uses of CGI"><metaproperty="og:description"content="Or how to program the back-end of your website using Commodore BASIC."><metaproperty="og:type"content="article"><metaproperty="og:url"content="http://toasters.rocks/esoteric-uses-of-cgi/"><metaproperty="og:image"content="http://toasters.rocks/images/2019/12/photo-1461749280684-dccba630e2f6.jpg"><metaproperty="article:published_time"content="2019-12-21T04:41:19+00:00"><metaproperty="article:modified_time"content="2019-12-21T05:34:28+00:00"><metaname=twitter:cardcontent="summary_large_image"><metaname=twitter:imagecontent="http://toasters.rocks/images/2019/12/photo-1461749280684-dccba630e2f6.jpg"><metaname=twitter:titlecontent="Esoteric uses of CGI"><metaname=twitter:descriptioncontent="Or how to program the back-end of your website using Commodore BASIC."><metaname=theme-colorcontent="#660066"><title>Esoteric uses of CGI - toasters rocks</title><linkrel=stylesheethref=http://toasters.rocks/css/toastersrocks.min.css></head><body><header><imgsrc=/img/icon.png><h1>toasters rocks</h1></header><main><aside><nav><ahref=/><iclass="fas fa-home"></i>Home</a><br><ahref=http://juju2143.ca/><iclass="fas fa-user"></i>About</a><br><ahref=/fr/><iclass="fas fa-globe"></i>Français</a><br><ahref=https://yukiis.moe/><iclass="far fa-comment"></i>Comics</a><br><ahref=https://codewalr.us/><iclass="far fa-folder-open"></i>Forums</a><br></nav><br><nav><atitle=Twitterhref=https://twitter.com/juju2143><istyle=color:#4da7declass="fab fa-twitter"></i><spanstyle=color:#4da7de>Twitter</span></a><br><atitle=Discordhref=https://discord.gg/cuZcfcF><istyle=color:#7289daclass="fab fa-discord"></i><spanstyle=color:#7289da>Discord</span></a><br><atitle=GitHubhref=https://github.com/juju2143><istyle=color:#fafafaclass="fab fa-github"></i><spanstyle=color:#fafafa>GitHub</span></a><br><atitle=Patreonhref=https://patreon.com/juju2143><istyle=color:#f96854class="fab fa-patreon"></i><spanstyle=color:#f96854>Patreon</span></a><br><atitle=YouTubehref=https://youtube.com/user/julosoft><istyle=color:#e02a20class="fab fa-youtube"></i><spanstyle=color:#e02a20>YouTube</span></a><br><atitle="YouTube 2"href=https://youtube.com/c/juju2143><istyle=color:#e02a20class="fab fa-youtube"></i><spanstyle=color:#e02a20>YouTube 2</span></a><br><atitle=Twitchhref=https://twitch.tv/juju2143><istyle=color:#6441a5class="fab fa-twitch"></i><spanstyle=color:#6441a5>Twitch</span></a><br><atitle=Instagramhref=https://instagram.com/j.p.savard><istyle=color:#d6249fclass="fab fa-instagram"></i><spanstyle=color:#d6249f>Instagram</span></a><br><atitle=DeviantArthref=https://deviantart.com/juju2143><istyle=color:#c5d200class="fab fa-deviantart"></i><spanstyle=color:#c5d200>DeviantArt</span></a><br><atitle=SoundCloudhref=https://soundcloud.com/juju2143><istyle=color:#fe3801class="fab fa-soundcloud"></i><spanstyle=color:#fe3801>SoundCloud</span></a><br></nav></aside><articlestyle=background-image:url(/images/2019/12/photo-1461749280684-dccba630e2f6.jpg)><divclass=metadatastyle="height:calc((var(--height) - 2em) * 0.6675 - 3.5em)"><h2name=top>Esoteric uses of CGI</h2><p>Or how to program the back-end of your website using Commodore BASIC.</p><iclass="far fa-calendar-alt"></i><timedatetime=2019-12-21>December 21, 2019</time><br><iclass="fas fa-tags"></i>#<aclass="btn btn-sm btn-outline-dark tag-btn"href=http://toasters.rocks/tags/tech>Tech</a><br><iclass="fas fa-hourglass"></i>~5 minutes</div><p>Well, you probably all heard of <ahref=https://esolangs.org/>esoteric programming languages</a> before, but the question today is, programming languages used outside its intended use, would that be esoteric?</p><p>If I tell you back-end web languages, you’d immediately think PHP, Node.js, Ruby,
</code></pre></td></tr></table></div></div><p>Example nginx config block.</p><p>So, as long as you have an interpreter, you can turn it into a web back-end server language, right? Theorically, yes. We’ll take our good ol' Commodore 64 BASIC <ahref=https://github.com/mist64/cbmbasic>that has been ported to C</a> so it could work on your modern computer as a case study.</p><p>So basically, you need a console program that opens a file and interprets it:</p><divclass=highlight><divclass=chroma><tableclass=lntable><tr><tdclass=lntd><preclass=chroma><code><spanclass=lnt>1
</code></pre></td></tr></table></div></div><p>Example console session.</p><p>Mind the shebang (the <code>#!/usr/bin/cbmbasic</code> line), it basically turns <code>./program.bas</code> into the proper <code>cbmbasic program.bas</code>. You’ll need to run <code>chmod +x</code> on it for it to work. It’s going to be useful later as you have no way otherwise to tell your web server which interpreter you want to run that file with.</p><p>So now you can dump in your web directory a file like this:</p><divclass=highlight><divclass=chroma><tableclass=lntable><tr><tdclass=lntd><preclass=chroma><code><spanclass=lnt>1
</code></pre></td></tr></table></div></div><p>index.cgi</p><p>Navigate to it with your browser and sure enough, you have a nice “Hello, world!” with the current time. So it’s really easy to code something to show up in your browser. But of course, this ain’t PHP, you have to send the HTTP headers yourself (lines 10-30, anything followed by two newlines should be sufficient, but for best results you should send the HTTP code and the content type), but still, quite easy. But the question here is, how is it useful?</p><h2id=_post>$_POST</h2><p>Obviously, you’d need some kind of input, right? The HTTP protocol allows for GET and POST. POST allows you to send data in the body of the request, otherwise you can also get some data from the URL. In PHP that would be respectively the <code>$_POST</code> and <code>$_GET</code> arrays. But of course it won’t automatically parse these, so let’s do that.</p><p>For POST data, it’s just as easy as reading keyboard input, or STDIN, depending of how it works. If it’s a GET request, then you should get nothing (or a 0xFF byte in case of BASIC), otherwise you’d get the data your user sent in your POST form.</p><divclass=highlight><divclass=chroma><tableclass=lntable><tr><tdclass=lntd><preclass=chroma><code><spanclass=lnt> 1
</code></pre></td></tr></table></div></div><p>Add this code to get and print POST variables. Good thing there’s line numbers so I don’t have to tell you where to add them.
It’s safe to assume anything over 127 is the end of input, as anything above that will be percent-encoded. In particular, in BASIC, you get 199 if you’re past the end (EOF) and 255 if there’s nothing. Parsing the resulting string is left as an exercise to the reader. And now if you send some data in the form you’ll get:</p><pre><code> input=Something
</code></pre><p>You can even use brainfuck or something similar:</p><pre><code>100 PRINT"<form method=POST action='bf.cgi'>
</code></pre><p>Replace line 100 from above…</p><divclass=highlight><divclass=chroma><tableclass=lntable><tr><tdclass=lntd><preclass=chroma><code><spanclass=lnt>1
</span></code></pre></td></tr></table></div></div><p>…and create bf.cgi</p><h2id=_server-_get-_cookies-etc>$_SERVER, $_GET, $_COOKIES, etc.</h2><p>Now you’ll want some of the sweet variables the server sends you that tells where the request came from and similar stuff, which is <code>$_SERVER</code> in PHP. If your language doesn’t support environment variables from the OS, unfortunately (for brainfuck), you’ll need to open files here. On Linux, it’s <code>/proc/self/environ</code>, your mileage may vary on other OSes. It’s mostly the same code, except the lines are separated by null bytes.</p><divclass=highlight><divclass=chroma><tableclass=lntable><tr><tdclass=lntd><preclass=chroma><code><spanclass=lnt> 1
</code></pre></td></tr></table></div></div><p>You know what to do at this point.</p><p>Again, parsing the results is left as an exercise to the reader. Notice that Commodore BASIC treats null bytes as an empty string here, which completely breaks the <code>ASC()</code> function and so should be handled separately.</p><p>Interesting variables are <code>QUERY_STRING</code> (PHP’s <code>$_GET</code>, parses just like we did with POST), <code>HTTP_COOKIE</code> (cookies are here), <code>REQUEST_METHOD</code> (GET or POST), <code>REMOTE_ADDR</code> (your user’s IP), <code>REQUEST_SCHEME</code> (https?), you can find a lot of them <ahref=https://www.php.net/manual/reserved.variables.server.php>just here</a> (at least those who aren’t specific to PHP) or <ahref=https://www.rfc-editor.org/rfc/rfc3875.html#section-4.1>here</a>.</p><p>On that, that’s it for today, hope you have fun with this! I wonder what kind of weird stuff you’d make with this, please tell me if you code your website in an esoteric language :)</p></article><ulclass=pagination><liclass=page-item><aclass=previoushref=http://toasters.rocks/horse-life-98/>« Horse Life 98</a></li><liclass=page-item><aclass=nexthref=http://toasters.rocks/miyuki-2019/>Miyuki 2019 »</a></li></ul><article><divid=disqus_thread></div><scripttype=application/javascript>vardisqus_config=function(){};(function(){if(["localhost","127.0.0.1"].indexOf(window.location.hostname)!=-1){document.getElementById('disqus_thread').innerHTML='Disquscommentsnotavailablebydefaultwhenthewebsiteispreviewedlocally.';return;}