Welcome to DU! The truly grassroots left-of-center political community where regular people, not algorithms, drive the discussions and set the standards. Join the community: Create a free account Support DU (and get rid of ads!): Become a Star Member Latest Breaking News General Discussion The DU Lounge All Forums Issue Forums Culture Forums Alliance Forums Region Forums Support Forums Help & Search

Erich Bloodaxe BSN

(14,733 posts)
Thu Nov 5, 2015, 08:27 PM Nov 2015

Help with setting up RESTful API type of setups using C under linux?

So my boss wants to update how we do some things, and he wants to setup part of the system RESTfully, but reusing most of the code we've already got, which are .cgi scripts written up in C. I've started going through and converting all of my error setup to use the HTTP errors, while dumping my output back as JSON arrays, rather than webpages.

But my cgi files are still cgi files, all sitting in the cgi directory. I can't figure out how to word my google searches to bring up what I'm trying to find out, which is how to map incoming requests, so that, for instance, a GET to www.example.com/users/12?session=34534545 goes to my user_manager.cgi script in view mode for user number 12, while a PUT to www.example.com/users/12?session=34534545 goes to it in update mode, or a POST to www.example.com/users/12/orders goes to write_order.cgi and writes up a new order for user 12.

Do I have to write up some sort of parsing script that pulls apart everything after every incoming request, figures out which script it needs to hit and then calls that script with all of the variables being sent along? Or are there (surely!) plugin modules for apache2 servers that do what I need doing, that I can just add some routing info into?

5 replies = new reply since forum marked as read
Highlight: NoneDon't highlight anything 5 newestHighlight 5 most recent replies
Help with setting up RESTful API type of setups using C under linux? (Original Post) Erich Bloodaxe BSN Nov 2015 OP
Never did it in C... jeff47 Nov 2015 #1
So perhaps 'REST framework' is among the phrases I should be googling? Erich Bloodaxe BSN Nov 2015 #2
Basically. jeff47 Nov 2015 #3
Thanks muchly, I'll start delving into the list and see what seems best for what he wants to do. nt Erich Bloodaxe BSN Nov 2015 #4
Consider it's design in planning modifications amee Nov 2015 #5

jeff47

(26,549 posts)
1. Never did it in C...
Thu Nov 5, 2015, 08:53 PM
Nov 2015

Faced with this, I'd probably use a REST framework in Java or python, parse out the parameters, and then use the language's native interface to call the C code.

But a big part of that approach is I have no idea about any C/C++ REST frameworks. I have to assume they exist, but I have no idea which ones do not suck.

Erich Bloodaxe BSN

(14,733 posts)
2. So perhaps 'REST framework' is among the phrases I should be googling?
Thu Nov 5, 2015, 08:57 PM
Nov 2015

I've used system calls before in C to call one piece of code from another, but not outside of the language. I almost exclusively work in C, with a tiny bit of php or javascript if I can't avoid it, but haven't ever worked in java or python.

jeff47

(26,549 posts)
3. Basically.
Thu Nov 5, 2015, 09:03 PM
Nov 2015
Here's a wikipedia list, but again I have no idea what ones are good.

Basically, the way Python/C or Java's JNI work is your C code would reside in a .so or an executable. You'd load them in Java or Python, and then call a function in you write in C that matches a particular signature. In that function you deal with memory management from Java/Python, and then directly call your C functions like normal.

But going to another language adds a lot of complexity. So unless you're wanting to learn a new language, or you're trying to convince the boss to rewrite everything, it'll be a lot easier to use one of those C frameworks.

amee

(2 posts)
5. Consider it's design in planning modifications
Fri Nov 6, 2015, 02:41 AM
Nov 2015

Yes. You write logic that runs on the web server to parse each incoming request and branch to the appropriate CGI; called a gateway... a single file to handle all of your CGI `redirects`. This is how things would normally work. A gateway CGI, or some other server side logic to parse the request and branch.

Other workable solutions might include; with Apache, mod rewrite. With IIS, server-side filters.

Latest Discussions»Retired Forums»Website, DB, & Software Developers»Help with setting up REST...