Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI in C Basics 2

Status
Not open for further replies.

AbidingDude

Programmer
Oct 7, 2012
74
0
0
US
So I'm trying to teach myself CGI programming. (I'm trying to do it in C because I know the language fairly well as opposed to other scripting languages which I do not.) I was trying to go through a tutorial:


I want to process simple form data (an idea I want to eventually implement at work). I've been trying to do all this from my hard drive. I have the executable in my compiler folder and the HTML file in another folder.

From what I gathered, I'll need to be able to process the "QUERY_STRING" variable, but it never seems to get set up properly. I try checking it with getenv("QUERY_STRING") but it always comes up NULL.

The example file I've been practicing with has the following segment for the FORM element:

<FORM METHOD="POST" ACTION="/cgi-bin/usr-inf">

Given that I was working on this in Windows, I changed it to:

<FORM METHOD="POST" ACTION="FILE://C:\<my_compiler_path>/hello.exe">

but Firefox keeps asking me where I want to save the file instead of running it. Understandable for security reason but still... (I originally had it as HTTP instead of FILE, but I kept getting "server not found" errors.)
The same things happens when I copied, pasted, and recompiled these files to my Ubuntu netbook.

The "hello.exe" is simply the "Hello" program from the tutorial page.

How can you simulate client/server interaction on a PC? I'm also surprised that CGI output is supposed to be written to standard-out, so of course I see the output in a command prompt window instead of a browser.
 
Hi

AbidingDude said:
<FORM METHOD="POST" ACTION="FILE://C:\<my_compiler_path>/hello.exe">
That looks bad. CGI script or program is an executable, so something needs to set up the environment, start the executable, pass it input and sends its output back to the one which made the request. That will not happen when using file:// protocol, only on And to handle that, you need a web server.

So first install a web server, configure it to run CGI, make sure hello.exe is found according to the configuration when needed and after that your program will get the [tt]QUERY_STRING[/tt]. ( Though will be empty string in case of your above example. )


Feherke.
feherke.ga
 
feherke said:
That looks bad. CGI script or program is an executable, so something needs to set up the environment, start the executable, pass it input and sends its output back to the one which made the request. That will not happen when using file:// protocol, only on And to handle that, you need a web server.

Yeah it does look bad [bigears] but it's only because I have no idea what I'm doing...

When you say, install a web server. Do you mean obtain (or build) another PC and turn it into a server? Sounds kinda fun actually, but I'm not sure about the finances or the physical space.
 
Hi

Absolutely not. Here server means service ( Windows terminology ) or daemon ( Unix terminology ). In other words, install a program capable to handle incoming HTTP requests :
[ul]
[li]The simplest for Windows in my opinion is Abyss Web Server. The free for personal use edition will be fine for your experiments.[/li]
[li]Not sure about Windows software distribution details, but as far as I know, Microsoft's own web server, IIS, is included in some editions, so maybe you already have it installed. IIS being more popular, we have forum41 for it.[/li]
[li]Or you can pick one from the Comparison of web server software Wikipedia article, as long it can run on Windows and supports CGI.[/li]
[li]While I worked on Windows, I used Lighttpd run in Cygwin. ( Not what I would recommend for a beginner, just to emphasize that it may be a wast variety of software. )[/li]
[/ul]

When the web server is running, it accepts incoming requests on certain ports ( by default 80 ). So if your machine has direct connection to the internet, make sure the firewall rejects any incoming request from outside. Of course, is enough to ask the web server to only listen on the loopback address ( IP address : 127.0.0.1, hostname : localhost ), as that is unique to every machine and can not be addressed from elsewhere. So the URL to your program will be something like [ignore][/ignore] .

By the way, multiple web servers may run on the same machine as long as they are configured to listen for incoming requests on different ports. So while experiencing, you may even install more web servers that you find attractive, try them all, then decide later which one ( or more ) to keep.

Regardless the language you use to write you program, your main problem in this moment seems to be the lack of CGI knowledge, for which forum452 is more appropriate.


Feherke.
feherke.ga
 
Thank you for breaking that down.

I just installed Abyss. It looks like I'll have to take some time to learn it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top