15-213 Class 26 Web Services 11/28/2005 Web server is based on a client server protocol called HyperText Transfer Protocol (HTTP). Sits on top of TCP/IP. Session: Client & server establish TCP/IP connection Client requests content Server responds with requested content (Possibly repeat) Client & server close connection Current version is HTTP/1.1 Content: Sequence of bytes with a specified MIME (Multipurpose Internet Mail Extension) format. E.g.: test/html test/plain image/gif Static vs. dynamic content Static: Content stored in files, retrieved in response to HTTP request Dynamic: Content generated based on HTTP request URL: Universal resource locator General form: http://www.cs.cmu.edu:80/index.html (Can drop port 80, and index.html) http://www.cs.cmu.edu:8000/cgi-bin/adder?15000&213 Example: telnet www.cs.cmu.edu 80 GET /~bryant/test.html HTTP/1.1 # GET page host: www.cs.cmu.edu\n # Must specify host \n # Signals end of GET request (Telnet is a general tool for sending & receiving text) General format: METHOD URI VERSION Methods GET: Retrieve static or dynamic content. Request encoded in URI POST: Dynamic content. Request encoded in body ... Responses: HTTP/1.1 200 OK # Version Status_Code Status Lots of header information e.g., Content-Length Content-Type Demonstration using Web Proxy Web Proxy sits between client & server. Demonstrate with old version from 15-213. Setup Mozilla to use proxy on local host ./proxy -p 15213 -v 2 See serving of both HTML & image data. Notice all the stuff in the header. Interesting part: Chunked Transfer encoding XXX\r\n .. data .. YYY\r\n .. data .. 0\r\n \r\n Specify length of each chunk with hex digits. Dynamic Content GET or POST request with /cgi-bin in URI. Server forks process to perform requested computation Example: (Set up tiny on home machine. Type in URL: :PORT/home.html Contains following: Tiny Home

Free Division Service

Numerator

Divisor

When enter data and hit "submit" button, composes CGI get. Can also do manually: HOME:PORT/cgi-bin/divide?num=N&div=D How does it work? 1. Web server uses fork & exec to create child process running specified file as program. Can be in any language (but don't try using C code under CYGWIN. Can't find DLL). 2. Arguments passed by environment variable QUERY_STRING 3. Program must split apart argument string, format complete HTTP message and print on stdout. 4. Server redirects child's stdout to client. Code for divide: /* * divide.c - a minimal CGI program that divides two numbers */ #include "csapp.h" int main(void) { char *buf; char msg[MAXLINE], content[MAXLINE]; int n1=1, n2=1; /* Extract the two arguments */ if ((buf = getenv("QUERY_STRING")) != NULL) { if (sscanf(buf, "num=%d&div=%d\n", &n1, &n2) != 2) { sprintf(msg, "Can't parse buffer '%s'", buf); } else if (n2 == 0) { sprintf(msg, "Attempt to divide %d by zero", n1); } else { sprintf(msg, "%d / %d -> %d, remainder %d\n", n1, n2, n1/n2, n1%n2); } } else { sprintf(msg, "Can't find query string"); } /* Make the response body */ sprintf(content, "Welcome to divide.com:

%s

Bye!\n", msg); /* Generate the HTTP response */ printf("Content-length: %d\r\n", strlen(content)); printf("Content-type: text/html\r\n\r\n"); printf("%s", content); fflush(stdout); exit(0); }