|
|
|
Lab 12: Client requests
Yesterday we talked a lot about how a browser actually goes about the business of issuing a request for a user, and we got to see our response come back. We learned that:
- Our request needed to be stated within the bounds of a certain protocol which defines the procedure for communication.
- We call our request, with no specified destination, the message
- The port number of the receiving application protocol is set by the operating system at the transport layer. The port number is similar to the room number at the dorms. You can send mail to the right dorm building (host), but without the room number it will never get to the right person (the application).
- The transport layer takes the message and gives it a port number. Once it has its port number the message becomes a segment.
- Finally the destination address, expressed as a unique 32 bit digit called the internet protocol or IP address, is set at the network layer and sent out to the Internet, destined for its address.
- When the segment gets its IP address then it becomes known as a datagram
We also saw how to actually complete a request to a wikipedia web server. Let's do a warm-up:
Challenge 1, place your own GET request
- Open up a terminal
- Run telnet:
$ telnet
- Open an HTTP connection to wikipedia. At the prompt, type
telnet> open en.wikipedia.org 80
Where 'en' signifies that this is one of wikipedia's English servers.
- Request information on your favorite animal! Run a GET request of this format:
GET /wiki/<animal> HTTP/1.1
So, if your favorite animal is a dinosaur, you'd type:
GET /wiki/Dinosaur HTTP/1.1
And press enter. Make sure to use a capital first letter!
- The HTTP protocol now expects us to provide the host name of the server we are making the request from. Now let's enter
Host: en.wikipedia.org
- And now, according to HTTP we should press enter twice. Doing so will send back a very large amount of HTML and information about your favorite animal. If you were a browser we could take this HTML and display it. This is exactly the same process your browser uses to fulfill requests when you use it:
GET Request Served
Challenge 2, send data by issuing a POST request to a program on the web server
We also talked yesterday about an ability to send data to a server from the client. Let's look again at assignment 6, where we did exactly this, and delve a little deeper in to our AJAX process.
In that assignment we were asked to send data to a program that would compute the number of unique words in a string, and send us back the occurrences of each word. Specifically we would open up a request like this:
request.open("POST", "http://www-users.itlabs.umn.edu/~carl/cgi/wordcount.cgi", true /* asynchronous? */ )
request.send( "text=" + postEscape( document.form.text.value ) )
Our first hint is the first argument to the open command. As we might suspect, as we were sending data to another program on another host, that we would use a POST request. POST requests are different from GET requests in that a client provides the server with data that it would like processed. For instance a string of words, such as this current sentence, can have the number of its words computed. Let's try the same POST request as in the assignment manually.
- Open an HTTP connection to the ITlabs web server:
telnet> open www-users.itlabs.umn.edu 80
-
Next issue a post request of a string to the wordcount.cgi program. The format for this is as follows:
POST /~carl/cgi/wordcount.cgi HTTP/1.1
Host: www-users.itlabs.umn.edu
- Now, too, we must specify another piece of data in our request:
Content-Length: 10
This tells the server how much data to expect, in number of characters (or, equivalently, bytes).
- Finally we'll complete our post request. First, press enter once and then type
text=hello
This is the text that the application on the server is expecting. When it sees the string text= it knows it take the string and compute the numbers of each word in the string, returning the result to the requester (us!). We will see what how the application on the server computes this response soon!
And press enter. Look, it's the same HTML we got back in our assignment 6! Remember we sent the string that the user entered to a program called wordcount.cgi on the web server, and then we got this piece of HTML back. Now we see a little bit better how an POST request works. Your request, all together, should look like this:
$ telnet
telnet> open www-users.itlabs.umn.edu 80
Trying 128.101.38.208...
Connected to www-users.itlabs.umn.edu.
Escape character is '^]'.
POST /~carl/cgi/wordcount.cgi HTTP/1.1
Host: www-users.itlabs.umn.edu
Content-Length: 10
text=hello
HTTP/1.1 200 OK
Date: Tue, 21 Apr 2009 08:29:05 GMT
Server: Apache
Transfer-Encoding: chunked
Content-Type: text/plain; charset=ISO-8859-1
1e
hello 1
0
Challenge 3, something cool. Send an email from telnet.
Remember how I mentioned that HTTP or port 80 was not the only application layer protocol on the block? Email is another protocol that we all cherish and use everyday, let's try to make a request to a mail server instead of a web server!
Email, or the SMTP (Simple Mail Transfer Protocol) uses port 25 on servers.
Let's use Telnet to send ourselves a fake email!
- First establish an SMTP connection to the ITlab's mail server:
$ telnet
telnet> open mail.itlabs.umn.edu 25
- Unlike an HTTP request, however, an SMTP server will respond to our requests in an interactive way. The format of an SMTP connection is as follows:
HELO itlabs.umn.edu
To this request the server responds:
250 mail.itlabs.umn.edu
250 means that the request was a good one.
Moving on this this fashion we issue the next commands:
MAIL FROM: <specify any email address (really, any!)>
RCPT TO: <specify your email address>
DATA
Subject: Sending email from telnet!
Hello me, this is you from the past!
Your friend,
Me
.
QUIT
Entering this data will send yourself an email. It's true, too, when I say that you can enter any email address as the from address. This is how spammers can fool servers in to sending phony emails.
|
|