Introduction to Computer Systems

Download Report

Transcript Introduction to Computer Systems

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
Web servers
• 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)
Web
client
(browser)
HTTP
request
Web
server
HTTP response
(content)
HTTP
TCP
IP
Web content
Streams
Datagrams
http://www.w3.org/Protocols/rfc2616/rfc2616.html
3
Static and dynamic content
• 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
Bottom line: All Web content is associated with a file that is
managed by the server.
4
How servers use URLs
• Each file managed by a server has a unique
name called a URL (Universal Resource
Locator)
– http://www.cs.cmu.edu/index.html
– http://www.cs.cmu.edu:8000/cgibin/adder?15000&213
• Determine if request is for static or dynamic
content.
• Find file on file system.
5
HTTP Requests & Response Format
• HTTP request is a request line, followed by
zero or more request headers
– Request line: <method> <uri> <version>
• HTTP response is a response line followed by
zero or more response headers.
– Response line: <version> <status code>
<status msg>
• Header: <header name>:<header data>
6
Issues in Serving Dynamic Content
• How does the client pass
program arguments to the
Client
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.
Request
Content
Server
Content
Create
env.pl
7
Outline
• Review of Web Server
• The Tiny Web Server
– Features
– Review of socket
– Implementation
• Some Practical Issues
8
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
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 “abcgibingo.html”)
– Fork process to execute program
• Otherwise serve static content
– Copy file to output
10
Review of Data Structures
/* 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
/* 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
/* 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 the Sockets Interface
Client
Server
socket
socket
bind
open_listenfd
open_clientfd
listen
connect
Client /
Server
Session
Connection
request
rio_writen
rio_readlineb
rio_readlineb
close
accept
rio_writen
EOF
Await connection
request from
next client
rio_readlineb
14
close
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
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
The Frame of a Tiny Web Server
1
2
3
4
/*
* tiny.c - A simple HTTP/1.0 Web server that uses the GET
* method to serve static and dynamic content.
*/
5 #include "csapp.h"
6
7 void doit(int fd);
8 void read_requesthdrs(int fd);
17
The Frame of a Tiny Web Server
9
10
11
12
13
int parse_uri
(char *uri, char *filename, char *cgiargs);
void serve_static
(rio_t *rio, char *filename, int filesize);
void get_filetype
(char *filename, char *filetype);
void serve_dynamic
(rio_t *rio,char *filename,char*cgiargs);
void clienterror
(rio_t *rio, char *cause, char *errnum,
char *shortmsg, char *longmsg);
18
Main Function of the Tiny Web Server
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 <port>\n", argv [0]);
24
exit(1);
25
}
26
port = atoi(argv[1]);
27
19
Main Function of the Tiny Web Server
28
29
30
31
32
33
34
35 }
listenfd = open_listenfd(port);
while (1) {
clientlen = sizeof(clientaddr);
connfd = Accept(listenfd, (SA *)
&clientaddr, &clientlen);
doit(connfd);
Close(connfd);
}
20
Prepare Buffer
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
21
Process Request
9
10
11
12
13
14
15
16
17
18
19
/* read request line and headers */
Rio_readinitb(&rio, fd);
Rio_readlineb(&rio, buf, MAXLINE);
sscanf(buf, "%s %s %s\n", method, uri, version);
if (strcasecmp(method, "GET")) {
clienterror(fd, method, "501", "Not Implemented",
"Tiny does not implement this method");
return;
}
read_requesthdrs(&rio);
22
Parse URL and Locate the File
20
21
22
23
24
25
26
27
/* parse URI from GET request */
is_static = parse_uri(uri, filename, cgiargs);
if (stat(filename, &sbuf) < 0) {
clienterror(fd, filename, "404", "Not found",
"Tiny couldn’t find this file");
return;
}
23
Process Static Request
28
29
30
31
32
33
34
35
if (is_static) { /* serve static content */
if (!(S_ISREG(sbuf.st_mode)) ||
!(S_IRUSR & sbuf.st_mode)) {
clienterror(fd, filename, "403",
"Forbidden","Tiny couldn’t read the file");
return;
}
serve_static(&rio, filename, sbuf.st_size);
}
24
Process Dynamic Request
34
35
36
37
38
39
40
41
42
else { /* serve dynamic content */
if (!(S_ISREG(sbuf.st_mode)) ||
!(S_IXUSR & sbuf.st_mode)) {
clienterror(fd, filename, "403", "Forbidden",
"Tiny couldn’t run the CGI program");
return;
}
serve_dynamic(&rio, filename, cgiargs);
}
}
25
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, "<html><title>Tiny Error</title>");
8
sprintf(body, "%s<body bgcolor=""ffffff"">\r\n", body);
9
sprintf(body, "%s%s: %s\r\n", body, errnum, shortmsg);
10
sprintf(body, "%s<p>%s: %s\r\n", body, longmsg, cause);
11
sprintf(body, "%s<hr><em>The Tiny Web server</em>\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));
26
21 }
The Tiny Web Server
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 }
27
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
1 void serve_static(int fd, char *filename, int filesize)
2{
3
int srcfd;
– Serve file
specified by
4
char *srcp, filetype[MAXLINE], buf[MAXBUF];
filename
5
6
/* send response headers to client */
– Use file metadata
to compose header
7
get_filetype(filename, filetype);
8
sprintf(buf, "HTTP/1.0 200 OK\r\n");
– “Read” file via
mmap
9
sprintf(buf, "%sServer: Tiny Web Server\r\n", buf);
10
sprintf(buf, "%sContent-length: %d\n", buf, filesize);
– Write to output
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 }
29
21
The Tiny Web Server
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
* get_filetype - derive file type from file name
*/
void get_filetype(char *filename, char *filetype)
{
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");
}
30
1 void serve_dynamic(int fd, char *filename, char *cgiargs)
2 {The Tiny Web Server
– Fork child to
3
char buf[MAXLINE];
execute CGI
program
4
5
/* return first part of HTTP response */
– Change stdout to
6
sprintf(buf, "HTTP/1.0 200 OK\r\n");
be connection to
7
Rio_writen(fd, buf, strlen(buf));
client
8
sprintf(buf, "Server: Tiny Web Server\r\n");
– Execute CGI
9
Rio_writen(fd, buf, strlen(buf));
program with
10
execve
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
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
Outline
• Review of Web Server
• The Tiny Web Server
• Some Practical Issues
33
Why persistent connection?
• Organization of a page
– A HTML file
– Some MIME objects
•
•
•
•
Pictures
Audio
Video
Script files
34
Why persistent connection?
• Implementation of network stack
– The way to establish a connection
clientfd
clientfd
Client
Client
Server
Connection
request
Server
listenfd(3)
listenfd(3)
listenfd(3)
clientfd
Client
Connection
Server
connfd(4)
– The life of a socket
• On behalf of kernel
35
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.
1. Client request
Client
2. Proxy request
Proxy
4. Proxy response
Origin
Server
3. Server response
36
Improve experience with a proxies
• Can perform useful functions as requests and
responses pass by
– Examples: Caching, logging, anonymization, filtering,
transcoding
Client
A
Request foo.html
foo.html
Request foo.html
Client
B
Proxy
cache
foo.html
Request foo.html
foo.html
Origin
Server
Slower more
expensive
global
network
37
Fast inexpensive local network
How to use resources efficiently?
• Characteristics of resources
– Powerful CPU
– Slow storage
• Concurrent programming
– Many live connections
– I/O multiplexing
38
Next
• Virtual memory management
– Physical and virtual addressing
– Address space
– Roles of VM
• Suggested Reading
– 9.1~9.5
39