Jump to content
  • Advertisement
Sign in to follow this  
Fibonacci One

[web] CGI and C

This topic is 5407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If this is the wrong forum, you can shoot me and move the thread to where it needs to be. Anyway, a friend an I have just begun designing something we plan on writing over the next semester and a half. For a certain part of the project, we're not exactly sure what language we want to use. We'd prefer it already have something to communicate with LUA. Thus, I've began to think cgi may be good for what I want to do because of the mod_cgi wrappers. Problem is, our schools library doesn't have any books on cgi, and everything I've found so far on the internet is rather vague as to CGI's capabilities. Would any of you that have experience with CGI want to give me a little description of CGI? Also, if I do decide to learn CGI, do any of you know of a site with a good cgi tutorial that uses C/C++? Everything I've found on google is perl or tcl based. While I plan on learning those soon, I would prefer to wait until after I've done this project. [Edited by - Fibonacci One on September 24, 2004 4:22:29 PM]

Share this post


Link to post
Share on other sites
Advertisement
It's pretty simple.

The webserver executes the cgi executable.

Information about how the cgi page was requested is usually available via environment variables, iirc.

copy /usr/bin/printenv to your cgi root, and you can see the kind of information available jsut by visiting that page and looking at the result. (edit: or, um, if you're not on a *nix, write your own "print out the environment" program)

I might be wrong, but I think POST data might come in via STDIN, not completely sure about that, however.

Share this post


Link to post
Share on other sites
Hrm, so, it just lets me run programs on the webserver, and gives me access to a bunch of environment variables (I'm sure this is a huge over-simplification)?

Then I would just use stdout to build a webpage?
ex: printf("(img src=\"%s\")", sImage);
replacing (,) with <,>. (doh)

would display an image on the page (assuming I already did the line that says it's a html file, and all the html code surrounding that).

[Edited by - Fibonacci One on September 24, 2004 12:47:42 AM]

Share this post


Link to post
Share on other sites
Yes, that would work. Basically CGI is a fancy way of saying "request in via environment variables, request out via stdout". And I do believe POST requests are from stdin, yes. The details of setting up cgi vary from webserver to webserver. Essentially, it's just an interface. Whether it's close to what you need or not will depend somewhat on what exactly you're trying to achieve, which you didn't really specify.

PS. Assuming you are indeed doing something web-based, I'll move this over to the Web Development forum.

Share this post


Link to post
Share on other sites
Hrm, that sounds perfect for what I want to do. One last problem I see, is I remember someone mentioning that not too many servers allow users to use C/C++ based cgi. If I were to write this part of the project with C, would I be cutting the number of people able to use it by much?

Share this post


Link to post
Share on other sites
Not all web hosts will allow you to use binary CGI programs - but then not all will let you use CGI *at all*. Probably the same ones.

One problems is that hosts use different systems for their web servers - some of the cheaper hosts use Win32 but most use either Linux or FreeBSD. Not necessarily the latest versions, either, they may have outdated C++ libraries etc, which aren't compatible with your binaries.

So you'll need to know what version of stuff they have on, and recompile accordingly.

I can't see any shared hosts upgrading their C libraries for you - so if you can't build exes that work on their system, you should make static binaries (think BIG exes)

Of course CGI programming is considerably involved:

- You need some kind of library to parse POST / GET requests into a useful format. Specifically, HTTP uploads are rather complicated.
- You really want a base set of functions for things like date formatting, url encoding, for things like links and cookies, otherwise you will spend a lot of time "reinventing the wheel"
- If you're planning to use Unicode (or any other non 8bit encoding), you probably want to consider it upfront and ensure you have the relevant string handling routines available.

Mark

Share this post


Link to post
Share on other sites
I already have access to a server that will get me the ability to do pretty much whatever I want as long as it's in the name of education. The thing is, we plan on realeasing this as open source and would like for as many people as possible to use it. I'm wondering if the fact I write the cgi part in C will prevent some people from using it?

Thanks for the cgi info though, all information is appreciated...

Share this post


Link to post
Share on other sites
One last question I'm putting into this thread rather then into a new one so people can have a better context.

Is there a reason it seems like there are very few cgi based forums out there? It seems like most of them are based off of php, asp, or perl. Is this just because those languages are easier to use or is there some huge obstical that I'd have to get around with cgi.
Thanks.

Share this post


Link to post
Share on other sites
I should note that you'll find that many Perl-based web apps are actually CGI apps.

However the main point is what markr said - CGI requires execution of arbitrary code, arbitrary code leads to pain and suffering, pain leads to the dark side. Etc.

ASP and PHP are more carefully controlled environments that are safer for hosts to supply. On top of that, they provide many built-in functions that make development of complex web applications so much simpler. For example, you have a templating syntax, cookie/session functionality, query string handling, HTTP header provision, form handling, routines for escaping and unescaping HTML and form data, etc etc. They also require no compilation step which allows for quick testing with just a browser refresh.

CGI is also hard to optimise since at its heart it relies on creating a single process per request. Other systems don't require this which tends to make them more scalable to the larger applications that need high performance.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!