Internet Services II April 27, 2000 Topics dynamic content Tiny Web server tour 15-213 class28.ppt.

Slides:



Advertisements
Similar presentations
Common Gateway Interface (CGI). CGI is a protocol: CGI is not a programming language CGI is a protocol for the exchange of information between between.
Advertisements

Instructors: Randy Bryant and Dave O’Hallaron
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
How does the server format the information it gives to the appln program? As environment variables and in standard input.
Web Services Nov 26, 2002 Topics HTTP Serving static content Serving dynamic content class27.ppt “The course that gives CMU its Zip!”
Lecture 9, : The Internet, Summer : The Internet Lecture 9: Web Services II David O’Hallaron School of Computer Science and Department.
Definitions, Definitions, Definitions Lead to Understanding.
Lecture 13 Dynamic Web Servers & Common Gateway Interface CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Internet Services Nov 30, 2000 Topics A tour of the Tiny Web server The DNS service class27.ppt “The course that gives CMU its Zip!”
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
Sockets and Web services April 23, 2002 Topics Client-server model Sockets Interface HTTP Serving static content Serving dynamic content Reading 12.7,
Web Client/Server Communication A290/A590, Fall /09/2014.
CGI Programming Languages Web Based Software Development July 21, 2005 Song, JaeHa.
Common Gateway Interface
1 ‘Dynamic’ Web Pages So far, we have developed ‘static’ web-pages, e.g., cv.html, repair.html and order.html. There is often a requirement to produce.
1 Homework / Exam Exam 3 –Solutions Posted –Questions? HW8 due next class Final Exam –See posted schedule Websites on UNIX systems Course Evaluations.
Overview A plain HTML document is static A CGI program is executed in real-time, so that it can output dynamic information. CGI (Common Gateway Interface)
CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
HTTP; The World Wide Web Protocol
Agenda  Terminal Handling in Unix File Descriptors Opening/Assigning & Closing Sockets Types of Sockets – Internal(Local) vs. Network(Internet) Programming.
1 HTML and CGI Scripting CSC8304 – Computing Environments for Bioinformatics - Lecture 10.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
Chapter 33 CGI Technology for Dynamic Web Documents There are two alternative forms of retrieving web documents. Instead of retrieving static HTML documents,
TCP/IP Protocol Suite 1 Chapter 22 Upon completion you will be able to: World Wide Web: HTTP Understand the components of a browser and a server Understand.
University of Amsterdam Computer Systems – Iterative server Arnoud Visser 1 Computer Systems Iterative server.
Web application architecture
HTTP HTTP stands for Hypertext Transfer Protocol. It is an TCP/IP based communication protocol which is used to deliver virtually all files and other.
Networking Programming --Web Server(I) 1. Outline Web History Web Servers –HTTP Protocol –Web Content –CGI Suggested Reading: –
Web Services Topics HTTP Serving static content
Web Server Design Week 14 Old Dominion University Department of Computer Science CS 495/595 Spring 2010 Martin Klein 4/14/10.
USING PERL FOR CGI PROGRAMMING
1 In the good old days... Years ago… the WWW was made up of (mostly) static documents. –Each URL corresponded to a single file stored on some hard disk.
CS4273: Distributed System Technologies and Programming I Lecture 7: Java Networking.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: HTTP and CGI Fundamentals of Web Programming.
Carnegie Mellon 1 Web Services / : Introduction to Computer Systems 22 nd Lecture, April 7, 2015 Instructors: Seth Copen Goldstein, Franz.
Operating Systems Recitation 9, May 19-20, Iterative server Handle one connection request at a time. Connection requests stored in queue associated.
CSCE Systems Programming Lecture 21 Web Server: CGI -Dynamic Pages CSCE March 25, 2013.
10/13/2015 ©2006 Scott Miller, University of Victoria 1 Content Serving Static vs. Dynamic Content Web Servers Server Flow Control Rev. 2.0.
Agenda  Redirection: Purpose Redirection Facts How to redirecting stdin, stdout, stderr in a program  Pipes: Using Pipes Named Pipes.
Perl CGI What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. Perl have a *very* nice interface to create CGI.
Carnegie Mellon 1 Web Services : Introduction to Computer Systems 21 st Lecture, Nov. 4, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
Form Data Encoding GET – URL encoded POST – URL encoded
WWW: an Internet application Bill Chu. © Bei-Tseng Chu Aug 2000 WWW Web and HTTP WWW web is an interconnected information servers each server maintains.
Netprog 2002 CGI Programming1 CGI Programming CLIENT HTTP SERVER CGI Program http request http response setenv(), dup(), fork(), exec(),...
Carnegie Mellon 1 Web Services / : Introduction to Computer Systems 22 nd Lecture, Nov. 13, 2012 Instructors: Dave O’Hallaron, Greg Ganger,
1 WWW. 2 World Wide Web Major application protocol used on the Internet Simple interface Two concepts –Point –Click.
WEB SERVER Mark Kimmet Shana Blair. The Project Web Server Application  Receives request for web pages or images from a client browser via the internet.
Web Services November 30, 2004 Topics HTTP Serving static content Serving dynamic content “The course that gives CMU its Zip!” class26.ppt.
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Internet Applications (Cont’d) Basic Internet Applications – World Wide Web (WWW) Browser Architecture Static Documents Dynamic Documents Active Documents.
Carnegie Mellon 1 Web Services / : Introduction to Computer Systems 22 nd Lecture, Nov. 15, 2011 Instructors: Dave O’Hallaron, Greg Ganger,
Carnegie Mellon 1 Web Services / : Introduction to Computer Systems 22 nd Lecture, Nov. 12, 2013 Instructors: Randy Bryant, Dave O’Hallaron,
Networking Programming A Tiny Web Server 1. Outline Review of Web Server The Tiny Web Server Some Practical Issues Suggested Reading: –11.5~
Introduction to CGI PROG. CGI stands for Common Gateway Interface. CGI is a standard programming interface to Web servers that gives us a way to make.
Pass data1 Passing data from an HTML page to a program Dr Jim Briggs.
Web Server Design Week 13 Old Dominion University Department of Computer Science CS 495/595 Spring 2012 Michael L. Nelson 04/03/12.
Web Server Design Week 15 Old Dominion University Department of Computer Science CS 495/595 Spring 2009 Michael L. Nelson 4/20/09.
Lesson 11. CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface)
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
Web Protocols and Practice
HTTP – An overview.
Web Services Topics Proxies Sockets interface
Recitation 9 Greg Reshko
Network Programming Chapter 12
The HTTP Protocol COSC 2206 Internet Tools The HTTP Protocol
An Example of a TCP/IP Application: the World Wide Web
Web Server Design Week 16 Old Dominion University
Lecture 25: Networking (cont'd)
Presentation transcript:

Internet Services II April 27, 2000 Topics dynamic content Tiny Web server tour class28.ppt

CS 213 S’00– 2 – class28.ppt Serving dynamic content clientserver Client sends request to server. If request URI contains the string “ /cgi-bin ”, then the server assumes that the request is for dynamic content. GET /cgi-bin/env.pl HTTP/1.1

CS 213 S’00– 3 – class28.ppt Serving dynamic content clientserver The server creates a child process and runs the program identified by the URI in that process env.pl fork/exec

CS 213 S’00– 4 – class28.ppt Serving dynamic content clientserver The child runs and generates the dynamic content. The server captures the content of the child and forwards it without modification to the client env.pl content

CS 213 S’00– 5 – class28.ppt Serving dynamic content clientserver The child terminates. Server waits for the next client request.

CS 213 S’00– 6 – class28.ppt Issues in serving dynamic content How does the client pass program arguments to the server? How does the server pass these arguments to the child? How does the server pass other info relevant to the request to the child? How does the server capture the content produced by the child? These issues are addressed by the Common Gateway Interface (CGI) specification. clientserver content request create env.pl

CS 213 S’00– 7 – class28.ppt CGI Because the children are written according to the CGI spec, they are often called CGI programs. Because many CGI programs are written in Perl, they are often called CGI scripts. However, CGI really defines a simple standard for transferring information between the client (browser), the server, and the child process.

CS 213 S’00– 8 – class28.ppt add.com: THE Internet addition portal! Ever need to add two numbers together and you just can’t find your calculator? Try Dr. Dave’s addition service at add.com: THE Internet addition portal! Takes as input the two numbers you want to add together. Returns their sum in a tasteful personalized message. After the IPO we’ll expand to multiplication!

CS 213 S’00– 9 – class28.ppt The add.com experience input URL Output page hostportCGI programargs

CS 213 S’00– 10 – class28.ppt Serving dynamic content with GET Question: How does the client pass arguments to the server? Answer: The arguments are appended to the URI Can be encoded directly in a URL typed to a browser or a URL in an HTML link adder is the CGI program on the server that will do the addition. argument list starts with “?” arguments separated by “&” spaces represented by “+” or “%20” Can also be generated by an HTML form

CS 213 S’00– 11 – class28.ppt Serving dynamic content with GET URL: Result displayed on browser: Welcome to add.com: THE Internet addition portal. The answer is: = 3 Thanks for visiting! Tell your friends.

CS 213 S’00– 12 – class28.ppt Serving dynamic content with GET Question: How does the server pass these arguments to the child? Answer: In environment variable QUERY_STRING a single string containing everything after the “?” for add.com: QUERY_STRING = “ 1&2 ” /* child code that accesses the argument list */ if ((buf = getenv("QUERY_STRING")) == NULL) { exit(1); } /* extract arg1 and arg2 from buf and convert */... n1 = atoi(arg1); n2 = atoi(arg2);

CS 213 S’00– 13 – class28.ppt Serving dynamic content with GET Question: How does the server pass other info relevant to the request to the child? Answer: in a collection of environment variables defined by the CGI spec.

CS 213 S’00– 14 – class28.ppt Some CGI environment variables General SERVER_SOFTWARE SERVER_NAME GATEWAY_INTERFACE (CGI version) Request-specific SERVER_PORT REQUEST_METHOD ( GET, POST, etc) QUERY_STRING (contains GET args) REMOTE_HOST (domain name of client) REMOTE_ADDR (IP address of client) CONTENT_TYPE (for POST, type of data in message body, e.g., text/html ) CONTENT_LENGTH (length in bytes)

CS 213 S’00– 15 – class28.ppt Some CGI environment variables In addition, the value of each header of type type received from the client is placed in environment variable HTTP_ type Examples: –HTTP_ACCEPT –HTTP_HOST –HTTP_USER_AGENT (any “-” is changed to “_”)

CS 213 S’00– 16 – class28.ppt Serving dynamic content with GET Question: How does the server capture the content produced by the child? Answer: The child writes its headers and content to stdout. Server maps socket descriptor to stdout (more on this later). Notice that only the child knows the type and size of the content. Thus the child (not the server) must generate the corresponding headers. /* child generates the result string */ sprintf(content, "Welcome to add.com: THE Internet addition portal\ The answer is: %d + %d = %d\ Thanks for visiting!\n", n1, n2, n1+n2); /* child generates the headers and dynamic content */ printf("Content-length: %d\n", strlen(content)); printf("Content-type: text/html\n"); printf("\r\n"); printf("%s", content);

CS 213 S’00– 17 – class28.ppt Serving dynamic content with GET bass> tiny 8000 GET /cgi-bin/adder?1&2 HTTP/1.1 Host: bass.cmcl.cs.cmu.edu:8000 kittyhawk> telnet bass 8000 Trying Connected to BASS.CMCL.CS.CMU.EDU. Escape character is '^]'. GET /cgi-bin/adder?1&2 HTTP/1.1 Host: bass.cmcl.cs.cmu.edu:8000 HTTP/ OK Server: Tiny Web Server Content-length: 102 Content-type: text/html Welcome to add.com: THE Internet addition portal. The answer is: = 3 Thanks for visiting! Connection closed by foreign host. kittyhawk> HTTP request received by server HTTP request sent by client HTTP response generated by the server HTTP response generated by the CGI program

CS 213 S’00– 18 – class28.ppt The Tiny Web server Tiny is a minimal Web server written in 250 lines of C. Serves static and dynamic content with the GET method. text files, HTML files, GIFs, and JPGs. support CGI programs Neither robust, secure, nor complete. It doesn’t set all of the CGI environment variables. Only implements GET method. Weak on error checking. Interesting to study as a template for a real Web server. Ties together many of the subjects we have studied this semester: VM (mmap) process management (fork, wait, exec) network programming (sockets interface to TCP)

CS 213 S’00– 19 – class28.ppt Tiny: cerror cerror() returns HTML error messages to the client. stream is the childfd socket opened as a Unix stream so that we can use handy routines such as fprintf and fgets instead of read and write. /* * cerror - returns an error message to the client */ void cerror(FILE *stream, char *cause, char *errno, char *shortmsg, char *longmsg) { fprintf(stream, "HTTP/1.1 %s %s\n", errno, shortmsg); fprintf(stream, "Content-type: text/html\n"); fprintf(stream, "\n"); fprintf(stream, " Tiny Error "); fprintf(stream, " \n"); fprintf(stream, "%s: %s\n", errno, shortmsg); fprintf(stream, " %s: %s\n", longmsg, cause); fprintf(stream, " The Tiny Web server \n"); }

CS 213 S’00– 20 – class28.ppt Tiny: main loop Tiny loops continuously, serving client requests for static and dynamic content. /* open FTP listening socket */... while(1) { /* wait for connection request */ /* read and parse HTTP header */ /* if request is for static content, retrieve file */ /* if request is for dynamic content, run CGI program */ }

CS 213 S’00– 21 – class28.ppt Tiny: read HTTP request /* open the child socket descriptor as a stream */ if ((stream = fdopen(childfd, "r+")) == NULL) error("ERROR on fdopen"); /* get the HTTP request line */ fgets(buf, BUFSIZE, stream); sscanf(buf, "%s %s %s\n", method, uri, version); /* tiny only supports the GET method */ if (strcasecmp(method, "GET")) { cerror(stream, method, "501", "Not Implemented", "Tiny does not implement this method"); fclose(stream); close(childfd); continue; } /* read (and ignore) the HTTP headers */ fgets(buf, BUFSIZE, stream); while(strcmp(buf, "\r\n")) { fgets(buf, BUFSIZE, stream); }

CS 213 S’00– 22 – class28.ppt Tiny: Parse the URI in the HTTP request /* parse the uri */ if (!strstr(uri, "cgi-bin")) { /* static content */ is_static = 1; strcpy(cgiargs, ""); strcpy(filename, "."); strcat(filename, uri); if (uri[strlen(uri)-1] == '/') strcat(filename, "index.html"); } else { /* dynamic content: get filename and its args */ is_static = 0; p = index(uri, '?'); /* ? separates file from args */ if (p) { strcpy(cgiargs, p+1); *p = '\0'; } else { strcpy(cgiargs, ""); } strcpy(filename, "."); strcat(filename, uri); }

CS 213 S’00– 23 – class28.ppt Tiny: access check A real server would do extensive checking of access permissions here. /* make sure the file exists */ if (stat(filename, &sbuf) < 0) { cerror(stream, filename, "404", "Not found", "Tiny couldn't find this file"); fclose(stream); close(childfd); continue; }

CS 213 S’00– 24 – class28.ppt Tiny: serve static content A real server would serve many more file types. /* serve static content */ if (is_static) { if (strstr(filename, ".html")) strcpy(filetype, "text/html"); else if (strstr(filename, ".gif")) strcpy(filetype, "image/gif"); else if (strstr(filename, ".jpg")) strcpy(filetype, "image/jpg"); else strcpy(filetype, "text/plain"); /* print response header */ fprintf(stream, "HTTP/ OK\n"); fprintf(stream, "Server: Tiny Web Server\n"); fprintf(stream, "Content-length: %d\n", (int)sbuf.st_size); fprintf(stream, "Content-type: %s\n", filetype); fprintf(stream, "\r\n"); fflush(stream);...

CS 213 S’00– 25 – class28.ppt Tiny: serve static content (cont) Notice the use of mmap() to copy the file that the client requested back to the client, via the stream associated with the child socket descriptor.... /* print arbitrary sized response body */ fd = open(filename, O_RDONLY); p = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); fwrite(p, 1, sbuf.st_size, stream); munmap(p, sbuf.st_size); }

CS 213 S’00– 26 – class28.ppt Tiny: serve dynamic content A real server would do more complete access checking and would initialize all of the CGI environment variables. /* serve dynamic content */ else { /* make sure file is a regular executable file */ if (!(S_IFREG & sbuf.st_mode) || !(S_IXUSR & sbuf.st_mode)) { cerror(stream, filename, "403", "Forbidden", "You are not allow to access this item"); fclose(stream); close(childfd); continue; } /* initialize the CGI environment variables */ setenv("QUERY_STRING", cgiargs, 1);...

CS 213 S’00– 27 – class28.ppt Tiny: serve dynamic content (cont) Next, the server sends as much of the HTTP response header to the client as it can. Only the CGI program knows the content type and size. Notice that we don’t mix stream (fprintf) and basic (write) I/O. Mixed outputs don’t generally go out in program order. /* print first part of response header */ sprintf(buf, "HTTP/ OK\n"); write(childfd, buf, strlen(buf)); sprintf(buf, "Server: Tiny Web Server\n"); write(childfd, buf, strlen(buf));...

CS 213 S’00– 28 – class28.ppt Tiny: serve dynamic content (cont) dup2(fd1, fd2) makes descriptor fd2 to be a copy of fd1, closing fd2 if necessary. /* create and run the child CGI process */ pid = fork(); if (pid < 0) { perror("ERROR in fork"); exit(1); } else if (pid > 0) { /* parent */ wait(&wait_status); } else { /* child */ close(0); /* close stdin */ dup2(childfd, 1); /* map socket to stdout */ dup2(childfd, 2); /* map socket to stderr */ if (execve(filename, NULL, environ) < 0) { perror("ERROR in execve"); } } /* end while(1) loop */ The dup2 calls are the reason that the bytes that the child sends to stdout end up back at the client. Notice the use of libc’s global environ variable in the execve call.

CS 213 S’00– 29 – class28.ppt Tiny sources The complete sources for the Tiny server are available from the course Web page. follow the “Lectures” link.