Internet

Beginning CGI Programming with Perl

Using the Internet Connection

All your request headers, the response headers, your status lines, and other data are sent over the Internet. That always seemed like a giant mystery to me, but it certainly is part of the common gateway interface (CGI). So just how does it work?

On the Internet, the connection is made using TCP/IP connecting to a public socket over a predefined port. Did I lose you? If I didn’t, you can skip this section. For everyone else-that’s almost everybody, folks-I’ll break that sentence down into parts so that you can make some sense of what’s going on.

TCP/IP, the Public Socket, and the Port

On the Internet, the connection is made using TCP/IP… TCP/IP stands for Transport Control Protocol/Internet Protocol. That means that the method for transporting your request for a Web page is controlled by some dry technical document that begins with RFCs and defines the specifics of transferring Internet messages. (RFCs are Requests for Comments. RFCs are the means the Internet community uses to publish new ideas and protocols. Comments are accepted for up to six months after an RFC is published.) In short, your request message is bundled up into a language that every machine connected to the Net understands.

connecting to a public socket… Think of the public socket as the Yellow Pages phone number of the server on which your Web page is located. A socket is a software network address that networked UNIX machines use to talk to each other.

over a predefined port. A file named (services) in the directory (/etc) on your server contains the ports assigned for all the common services on the Internet-services such as FTP, Gopher, and HTTP connection. The default port for the HTTP connection is 80. So if you see an :80 (or any other number) appended to the end of the URI you clicked on to get a Web page, you now know that’s the port being used to connect the client to the server.

One More Time, Using the Switchboard Analogy

The topic of Internet connections seems to confuse lots of people, and it’s important that you begin to grasp this concept. If you can begin to understand how the client and the server communicate, writing your CGI programs and the forms that support them will be much easier.

So I would like to present you with this analogy to help you understand this concept. Think of your server as an old-fashioned switchboard with an operator waiting for incoming calls. You probably have seen an old-fashioned switchboard in some old, black-and-white films or maybe on a Saturday Night Live skit.

You Make the Call

  1. You look up the phone number of someone in the phone book. This is the Web page with a URI on it.
  2. You dial the number. This is you clicking on the URI.

The Operator Receives the Call

The operator receives a call on the switchboard and then gets the name of the person you want to talk to.

  1. The operator makes the connection to the correct person.
  2. The last thing the operator does is remove the original connection.

This is what is happening over the Internet. The next time you click on a Web page, watch the transaction occur. You can see this on Netscape browsers on the bottom of the screen. The first thing that happens is a connect message: Looking up Host, like a search for a Yellow Pages phone number. Next, you should see Host contacted: Waiting for reply. This is the phone ringing at the other end, waiting for the operator to answer. Finally, you should see a reading file or a transferring data message. Just before that last message, the server-or operator-at the other end was looking up the specific file (or person, to remain with the operator analogy) you requested. When the file is found, it is transferred back to the requesting client.

That’s how it works by analogy and TCP/IP. After the connection is made, the server receives a bunch of information in the HTTP request headers telling it what type of response is re-quested. This is important to you as a CGI programmer; you will use the headers later in the book to send back information to your client and to decode what the client wants from you.