You may say "But CGI is for servers". Yes, it is, but you can look
at those external protocol handlers as servers. You could handle
unknown protocols by connecting to an HTTP server and giving it
the full URL and have it gateway for you. That's done all the
time by proxy servers. What you've done is cut out the HTTP middleman
and directly run the gateway. With the HTTP server out of the way,
CGI logically follows as the interface. Well, it _does_ make sense,
I may just not be explaining it well.
I've got some code which almost does what you want (except that it's
tied to a particular URL scheme), look under
http://www.cs.ubc.ca/doc/world/exec/intro to find it.