Presentation is loading. Please wait.

Presentation is loading. Please wait.

Networking Programming A Tiny Web Server 1. Outline Review of Web Server The Tiny Web Server Some Practical Issues Suggested Reading: –11.5~11.6 2.

Similar presentations


Presentation on theme: "Networking Programming A Tiny Web Server 1. Outline Review of Web Server The Tiny Web Server Some Practical Issues Suggested Reading: –11.5~11.6 2."— Presentation transcript:

1 Networking Programming A Tiny Web Server 1

2 Outline Review of Web Server The Tiny Web Server Some Practical Issues Suggested Reading: –11.5~11.6 2

3 Web servers HTTP response (content) Web server HTTP request Web client (browser) IP TCP HTTP Datagrams Streams Web content http://www.w3.org/Protocols/rfc2616/rfc2616.html 3 Clients and servers communicate using the HyperText Transfer Protocol (HTTP) –client and server establish TCP connection –Client requests content –Server responds with requested content –client and server close connection (eventually)

4 The content returned in HTTP responses can be either static or dynamic –Static content: content stored in files and retrieved in response to an HTTP request Examples: HTML files, images, audio clips. Request identifies content file –Dynamic content: content produced on-the-fly in response to an HTTP request Example: content produced by a program executed by the server on behalf of the client. Request identifies file containing executable code Static and dynamic content Bottom line: All Web content is associated with a file that is managed by the server. 4

5 Each file managed by a server has a unique name called a URL (Universal Resource Locator) –http://www.cs.cmu.edu/index.htmlhttp://www.cs.cmu.edu/index.html –http://www.cs.cmu.edu:8000/cgi- bin/adder?15000&213http://www.cs.cmu.edu:8000/cgi- bin/adder?15000&213 Determine if request is for static or dynamic content. Find file on file system. How servers use URLs 5

6 HTTP Requests & Response Format HTTP request is a request line, followed by zero or more request headers –Request line: HTTP response is a response line followed by zero or more response headers. –Response line: Header: : 6

7 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 7

8 Outline Review of Web Server The Tiny Web Server –Features –Review of socket –Implementation Some Practical Issues 8

9 Tiny Web Server Tiny Web server described in text –Tiny is a sequential Web server. –Serves static and dynamic content to real browsers. text files, HTML files, GIF and JPEG images. –226 lines of commented C code. –Not as complete or robust as a real web server 9

10 Tiny Operation Read request from client Split into method / uri / version –If not GET, then return error If URI contains “ cgi-bin ” then serve dynamic content –(Would do wrong thing if had file “ abcgi- bingo.htm l”) –Fork process to execute program Otherwise serve static content –Copy file to output 10

11 /* Domain Name Service (DNS) host entry */ struct hostent { /* official name of host */ char *h_name; /* alias list */ char **h_aliases; /* host address type */ int h_addrtype; /* length of address */ int h_length; /* list of addresses */ char **h_addr_list; } 11 Review of Data Structures

12 /* Internet address */ struct in_addr { /* 32-bit IP address */ unsigned int s_addr; }; /* Generic socket address structure (for connect, bind, and accept) */ struct sockaddr { /* protocol family */ unsigned short sa_family ; /* address data */ char sa_data[14]; } 12 Review of Data Structures

13 /* Internet style socket address */ struct sockaddr_in { /* Address family (AF_INET) */ unsigned short sin_family; /* Port number */ unsigned short sin_port; /* IP address */ struct in_addr sin_addr; /* Pad to sizeof “struct sockaddr” */ unsigned char sin_zero[8]; }; 13 Review of Data Structures

14 Client / Server Session Review of the Sockets Interface ClientServer socket bind listen rio_readlineb rio_writenrio_readlineb rio_writen Connection request rio_readlineb close EOF Await connection request from next client open_listenfd open_clientfd acceptconnect 14

15 struct hostent *genhostbyname( const char *name); struct hostent *genhostbyaddr( const char *addr, int len,0); int socket( int domain, int type, int protocol); clientfd = socekt(AF_INET,SOCKET_STREAM, 0) ; int connect(int sockfd, struct sockaddr *serv_addr, int addr_len); 15

16 int bind(int sockfd, struct sockaddr *my_addr, int addrlen); int listen(int sockfd, int backlog) ; int accept(int listenfd, struct sockaddr *addr, int addrlen) ; 16

17 1 /* 2 * tiny.c - A simple HTTP/1.0 Web server that uses the GET 3 * method to serve static and dynamic content. 4 */ 5 #include "csapp.h" 6 7 void doit(int fd); 8 void read_requesthdrs(int fd); The Frame of a Tiny Web Server 17

18 9 int parse_uri (char *uri, char *filename, char *cgiargs); 10 void serve_static (rio_t *rio, char *filename, int filesize); 11 void get_filetype (char *filename, char *filetype); 12 void serve_dynamic (rio_t *rio,char *filename,char*cgiargs); 13 void clienterror (rio_t *rio, char *cause, char *errnum, char *shortmsg, char *longmsg); The Frame of a Tiny Web Server 18

19 16 int main(int argc, char **argv) 17 { 18 int listenfd, connfd, port, clientlen; 19 struct sockaddr_in clientaddr; 20 21 /* check command line args */ 22 if (argc != 2) { 23 fprintf(stderr,"usage: %s \n", argv [0]); 24 exit(1); 25 } 26 port = atoi(argv[1]); 27 Main Function of the Tiny Web Server 19

20 28 listenfd = open_listenfd(port); 29 while (1) { 30 clientlen = sizeof(clientaddr); 31 connfd = Accept(listenfd, (SA *) &clientaddr, &clientlen); 32 doit(connfd); 33 Close(connfd); 34 } 35 } Main Function of the Tiny Web Server 20

21 1 void doit(int fd) 2 { 3 int is_static; 4 struct stat sbuf; 5 char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE]; 6 char filename[MAXLINE], cgiargs[MAXLINE]; 7 rio_t rio; 8 Prepare Buffer 21

22 9 /* read request line and headers */ 10 Rio_readinitb(&rio, fd); 11 Rio_readlineb(&rio, buf, MAXLINE); 12 sscanf(buf, "%s %s %s\n", method, uri, version); 13 if (strcasecmp(method, "GET")) { 14 clienterror(fd, method, "501", "Not Implemented", 15 "Tiny does not implement this method"); 16 return; 17 } 18 read_requesthdrs(&rio); 19 Process Request 22

23 20 /* parse URI from GET request */ 21 is_static = parse_uri(uri, filename, cgiargs); 22 if (stat(filename, &sbuf) < 0) { 23 clienterror(fd, filename, "404", "Not found", 24 "Tiny couldn’t find this file"); 25 return; 26 } 27 Parse URL and Locate the File 23

24 28 if (is_static) { /* serve static content */ 29 if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode)) { 30 clienterror(fd, filename, "403", 31 "Forbidden","Tiny couldn’t read the file"); 32 return; 33 } 34 serve_static(&rio, filename, sbuf.st_size); 35 } Process Static Request 24

25 34 else { /* serve dynamic content */ 35 if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) { 36 clienterror(fd, filename, "403", "Forbidden", 37 "Tiny couldn’t run the CGI program"); 38 return; 39 } 40 serve_dynamic(&rio, filename, cgiargs); 41 } 42 } Process Dynamic Request 25

26 1 void clienterror(rio_t *fd, char *cause, char *errnum, 2 char *shortmsg, char *longmsg) 3 { 4 char buf[MAXLINE], body[MAXBUF]; 5 6 /* build the HTTP response body */ 7 sprintf(body, " Tiny Error "); 8 sprintf(body, "%s \r\n", body); 9 sprintf(body, "%s%s: %s\r\n", body, errnum, shortmsg); 10 sprintf(body, "%s %s: %s\r\n", body, longmsg, cause); 11 sprintf(body, "%s The Tiny Web server \r\n", body); 12 13 /* print the HTTP response */ 14 sprintf(buf, "HTTP/1.0 %s %s\r\n", errnum, shortmsg); 15 Rio_writen(fd, buf, strlen(buf)); 16 sprintf(buf, "Content-type: text/html\r\n"); 17 Rio_writen(fd, buf, strlen(buf)); 18 sprintf(buf, "Content-length: %d\r\n\r\n", strlen(body)); 19 Rio_writen(fd, buf, strlen(buf)); 20 Rio_writen(fd, body, strlen(body)); 21 } 26

27 1 void read_requesthdrs(rio_t *fd) 2 { 3 char buf[MAXLINE]; 4 5 Readline(fd, buf, MAXLINE); 6 while(strcmp(buf, "\r\n")) 7 Rio_readlineb(fd, buf, MAXLINE); 8 return; 9 } The Tiny Web Server 27

28 1 int parse_uri(char *uri, char *filename, char *cgiargs) 2 { 3 char *ptr; 4 5 if (!strstr(uri, "cgi-bin")) { /* static content */ 6 strcpy(cgiargs, ""); 7 strcpy(filename, "."); 8 strcat(filename, uri); 9 if (uri[strlen(uri)-1] == ’/’) 10 strcat(filename, "home.html"); 11 return 1; 12 } 13 else { /* dynamic content */ 14 ptr = index(uri, ’?’); 15 if (ptr) { 16 strcpy(cgiargs, ptr+1); 17 *ptr = ’\0’; 18 } else 20 strcpy(cgiargs, ""); 21 strcpy(filename, "."); 22 strcat(filename, uri); 23 return 0; 24 } 25 } 28

29 1 void serve_static(int fd, char *filename, int filesize) 2 { 3 int srcfd; 4 char *srcp, filetype[MAXLINE], buf[MAXBUF]; 5 6 /* send response headers to client */ 7 get_filetype(filename, filetype); 8 sprintf(buf, "HTTP/1.0 200 OK\r\n"); 9 sprintf(buf, "%sServer: Tiny Web Server\r\n", buf); 10 sprintf(buf, "%sContent-length: %d\n", buf, filesize); 11 sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype); 12 Rio_writen(fd, buf, strlen(buf)); 13 14 /* send response body to client */ 15 srcfd = Open(filename, O_RDONLY, 0); 16 srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); 17 Close(srcfd); 18 Rio_writen(fd, srcp, filesize); 19 Munmap(srcp, filesize); 20 } 21 29 –Serve file specified by filename –Use file metadata to compose header –“Read” file via mmap –Write to output

30 22 /* 23 * get_filetype - derive file type from file name 24 */ 25 void get_filetype(char *filename, char *filetype) 26 { 27 if (strstr(filename, ".html")) 28 strcpy(filetype, "text/html"); 29 else if (strstr(filename, ".gif")) 30 strcpy(filetype, "image/gif"); 31 else if (strstr(filename, ".jpg")) 32 strcpy(filetype, "image/jpg"); 33 else 34 strcpy(filetype, "text/plain"); 35 } The Tiny Web Server 30

31 The Tiny Web Server 1 void serve_dynamic(int fd, char *filename, char *cgiargs) 2 { 3 char buf[MAXLINE]; 4 5 /* return first part of HTTP response */ 6 sprintf(buf, "HTTP/1.0 200 OK\r\n"); 7 Rio_writen(fd, buf, strlen(buf)); 8 sprintf(buf, "Server: Tiny Web Server\r\n"); 9 Rio_writen(fd, buf, strlen(buf)); 10 11 if (Fork() == 0) { /* child */ 12 /* real server would set all CGI vars here */ 13 setenv("QUERY_STRING", cgiargs, 1); 14 Dup2(fd, STDOUT_FILENO); /* redirect output to client*/ 15 Execve(filename, NULL, environ); /* run CGI program */ 16 } 17 Wait(NULL); /* parent reaps child */ 18 } 31 –Fork child to execute CGI program –Change stdout to be connection to client –Execute CGI program with execve

32 For More Information Study the Tiny Web server described in your text –Tiny is a sequential Web server. –Serves static and dynamic content to real browsers. text files, HTML files, GIF and JPEG images. –220 lines of commented C code. –Also comes with an implementation of the CGI script for the add.com addition portal. See the HTTP/1.1 standard: –http://www.w3.org/Protocols/rfc2616/rfc2616.html 32

33 Outline Review of Web Server The Tiny Web Server Some Practical Issues 33

34 Why persistent connection? Organization of a page –A HTML file –Some MIME objects Pictures Audio Video Script files 34

35 Why persistent connection? Implementation of network stack –The way to establish a connection –The life of a socket On behalf of kernel 35 listenfd(3) Client clientfd Server listenfd(3) Client clientfd Server Connection request listenfd(3) Client clientfd Server connfd(4) Connection

36 Improve experience with a proxies A proxy is an intermediary between a client and an origin server. –To the client, the proxy acts like a server. –To the server, the proxy acts like a client. ClientProxy Origin Server 1. Client request2. Proxy request 3. Server response4. Proxy response 36

37 Improve experience with a proxies Can perform useful functions as requests and responses pass by –Examples: Caching, logging, anonymization, filtering, transcoding Client A Proxy cache Origin Server Request foo.html foo.html Client B Request foo.html foo.html Fast inexpensive local network Slower more expensive global network 37

38 How to use resources efficiently? Characteristics of resources –Powerful CPU –Slow storage Concurrent programming –Many live connections –I/O multiplexing 38

39 Next Concurrent Programming –Process –I/O Multiplexing Suggested Reading –12.1~12.2 39


Download ppt "Networking Programming A Tiny Web Server 1. Outline Review of Web Server The Tiny Web Server Some Practical Issues Suggested Reading: –11.5~11.6 2."

Similar presentations


Ads by Google