Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How does CGI work, actually?

ajax

  • Please log in to reply
7 replies to this topic

#1 totonex

totonex

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 82 posts

Posted 20 January 2011 - 02:18 PM

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.
  • 0

#2 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 20 January 2011 - 08:50 PM

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

  • 0

#3 totonex

totonex

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 82 posts

Posted 22 January 2011 - 01:26 AM

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

#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 28 January 2011 - 02:01 PM

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.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#5 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 28 January 2011 - 05:56 PM

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.
  • 0

#6 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 29 January 2011 - 12:26 AM

Yeah, more or less.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#7 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 30 January 2011 - 06:18 AM

...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:
  • 0

#8 zarkyoc

zarkyoc

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 21 March 2011 - 02:03 AM

CGI - Common Gateway Interface

W3C CGI

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

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download