Jump to content

How does CGI work, actually?

- - - - -

  • Please log in to reply
7 replies to this topic

#1
totonex

totonex

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
Hey everyone, after a VERY short time searching on google and these forums, i decided to put this question. But the question is too vague, so i have to expand on it:

When i first learnt PHP, the one who told me about web programming taught me that the client requests something from the server (Apache...), it processes the thing (PHP file) and sends it to the client.
This explanation is pretty right - and it lets you write PHP scripts.

But i know nothing of how servers internally work. I have no idea how to 'set up a server' (Like, i needed something for Comet, Reverse Ajax and somebody told me about 'setting up the server' but i know NOTHING about that).

Given that i know nothing about the gears within a server, i also don't know what exactly is CGI. What i do know is that it's similar to PHP - a file gets processed and the result is sent to the client. I've even seen a C file compiled into a CGI script.
(BTW and WAIT A SECOND[WAS? new acronym?]: HOW exactly does "printf" [if i recall correctly] map to printing stuff on a page? Who does that? How is that done? How does compiled machine code map to a webpage response??). But that is C, let's go onto Perl and Python.
They are interpreted, so no compilation is needed. But still.

How does CGI work?

An a-little-more-in-depth response would be really appreciated,
Thank you very much.

#2
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
How CGI works? Well it's like the web server execute the CGI script/executable through console, read the output, then pass the output back to its clients. Simply put, CGI scripts/executables are simply CLI scripts/executables. The web server executes them through shell console (using StdOut interface) and read the output through the same means (StdIn).

Edited by LuthfiHakim, 30 January 2011 - 06:15 AM.
Fix typo


#3
totonex

totonex

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
It sounds well, but i think i would need even a more in-depth explanation which should encompass more server-related concepts & stuff. Thanks !

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
You basically get information from the user in either a URL-encoded format string or in raw data from a POST request. You can read that, and then you write an HTTP response (usually in HTML) and send that back to the user by writing to STDOUT.
sudo rm -rf /

#5
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I don't know if you know HTTP, but I though I'd post this URL anyway: HTTP Made Really Easy

The way I understand it is that the server works something like this:
1. Wait for an incoming connection.
2. Receive the request (I don't exactly know how the server knows when to stop receiving, though).
3. If the file is executable:
a. Write the request to a file.
b. Call the executable file with hStdIn set to the read handle for the file with the HTTP request.
c. Send the contents of the file from hStdOut back to the client.
4. If the file is not executable then process the request using default settings (I think it's send the HTTP headers + "\r\n\r\n" + the contents of the file requested back to the client.
5. Finish servicing this request.

I am not totally sure, but that's what I understand so far.

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Yeah, more or less.
sudo rm -rf /

#7
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts

LuthfiHakim said:

...The web server executes them through shell console (using StdOut interface) and read the output through the same means (StdIn).

Lol, I was telling in wrong order.. sorry folks haven't use them in quite long time :c-whistle:

#8
zarkyoc

zarkyoc

    Newbie

  • Members
  • Pip
  • 2 posts
CGI - Common Gateway Interface

W3C CGI

Edited by Orjan, 25 March 2011 - 02:06 AM.
Double posting





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users