15213 recitation section d

23
15213 Recitation Section D • Extending echo server • HTTP • Broken pipe error • Feedback and evaluation Anubhav Gupta Nov. 25, 2002 Outli ne

Upload: tehya

Post on 17-Jan-2016

33 views

Category:

Documents


0 download

DESCRIPTION

Anubhav Gupta. Nov. 25, 2002. 15213 Recitation Section D. Extending echo server HTTP Broken pipe error Feedback and evaluation. Outline. Important Dates. Lab 7 Proxy: due on Thursday, Dec 5 Final Exam: Tuesday, Dec 17. Extending Echo Server. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 15213 Recitation Section D

15213 Recitation Section D

• Extending echo server• HTTP• Broken pipe error• Feedback and evaluation

Anubhav GuptaNov. 25, 2002

Outline

Page 2: 15213 Recitation Section D

215213 Recitation D Anubhav Gupta

Important Dates

• Lab 7 Proxy: due on Thursday, Dec 5• Final Exam: Tuesday, Dec 17

Page 3: 15213 Recitation Section D

315213 Recitation D Anubhav Gupta

Extending Echo Server

• We will recap the echo client and server taught in the last lecture

• Then we will extend the echo server to build an echo proxy

Page 4: 15213 Recitation Section D

415213 Recitation D Anubhav Gupta

An Echo Client-Server Transaction

EchoClient

process

EchoServer

process

1. Client sends an input line

2. Server sends the same text as it receives3. Client displays the text

it receives

Page 5: 15213 Recitation Section D

515213 Recitation D Anubhav Gupta

rio_readlineb

close

closeEOF

rio_readlineb

rio_writenrio_readlineb

rio_writen

listen

bind

Review of the Sockets InterfaceClient Server

socket socket

acceptconnect

Connectionrequest

Await connectionrequest fromnext client

open_listenfd

open_clientfd

Page 6: 15213 Recitation Section D

615213 Recitation D Anubhav Gupta

Echo Client Main Routine#include "csapp.h"

/* usage: ./echoclient host port */int main(int argc, char **argv){ int clientfd, port; char *host, buf[MAXLINE]; rio_t rio; host = argv[1]; port = atoi(argv[2]); clientfd = Open_clientfd(host, port); Rio_readinitb(&rio, clientfd); while (Fgets(buf, MAXLINE, stdin) != NULL) { Rio_writen(clientfd, buf, strlen(buf)); Rio_readlineb(&rio, buf, MAXLINE); Fputs(buf, stdout); } Close(clientfd); exit(0); }

Page 7: 15213 Recitation Section D

715213 Recitation D Anubhav Gupta

Echo Client: open_clientfdint open_clientfd(char *hostname, int port) { int clientfd; struct hostent *hp; struct sockaddr_in serveraddr; if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; /* check errno for cause of error */ /* Fill in the server's IP address and port */ if ((hp = gethostbyname(hostname)) == NULL) return -2; /* check h_errno for cause of error */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; bcopy((char *)hp->h_addr, (char *)&serveraddr.sin_addr.s_addr, hp->h_length); serveraddr.sin_port = htons(port); /* Establish a connection with the server */ if (connect(clientfd, (SA *)&serveraddr, sizeof(serveraddr))<0) return -1; return clientfd; }

This function opens a connection from the client to the server at hostname:port

Page 8: 15213 Recitation Section D

815213 Recitation D Anubhav Gupta

Echo Server: Main Routineint main(int argc, char **argv) { int listenfd, connfd, port, clientlen; struct sockaddr_in clientaddr; struct hostent *hp; char *haddrp;

port = atoi(argv[1]); /* the server listens on a port passed on the command line */ listenfd = open_listenfd(port);

while (1) { clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET); haddrp = inet_ntoa(clientaddr.sin_addr); printf("server connected to %s (%s)\n", hp->h_name, haddrp); echo(connfd); Close(connfd); }}

Page 9: 15213 Recitation Section D

915213 Recitation D Anubhav Gupta

Echo Server: open_listenfdint open_listenfd(int port) { int listenfd, optval=1; struct sockaddr_in serveraddr; /* Create a socket descriptor */ if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; /* Eliminates "Address already in use" error from bind. */ if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int)) < 0) return -1; ... (more)

Page 10: 15213 Recitation Section D

1015213 Recitation D Anubhav Gupta

Echo Server: open_listenfd (cont)

...

/* Listenfd will be an endpoint for all requests to port on any IP address for this host */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons((unsigned short)port); if (bind(listenfd,(SA *)&serveraddr,sizeof(serveraddr))<0) return -1; /* Make it a listening socket ready to accept connection requests */ if (listen(listenfd, LISTENQ) < 0) return -1; return listenfd; }

Page 11: 15213 Recitation Section D

1115213 Recitation D Anubhav Gupta

Echo Server: echo

void echo(int connfd) { size_t n; char buf[MAXLINE]; rio_t rio; Rio_readinitb(&rio, connfd); while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) { printf("server received %d bytes\n", n); Rio_writen(connfd, buf, n); } }

Page 12: 15213 Recitation Section D

1215213 Recitation D Anubhav Gupta

Proxy

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

EchoClient

process

EchoServer

process

Echoproxy

process

Page 13: 15213 Recitation Section D

1315213 Recitation D Anubhav Gupta

Changing ”echo” procedurevoid echo_forward(int connfd, char *server, int server_port) { int forwardfd; rio_t rio_conn, rio_forward; ssize_t n; char buf[MAXLINE];

/* connect to the server */ forwardfd = Open_clientfd(server, server_port); Rio_readinitb(&rio_forward, forwardfd);

Rio_readinitb(&rio_conn, connfd); while (1) { if ((n = Rio_readlineb(&rio_conn, buf, MAXLINE)) == 0) break; Rio_writen(forwardfd, buf, n);

if ((n = Rio_readlineb(&rio_forward, buf, MAXLINE)) == 0) break; Rio_writen(connfd, buf, n); } Close(forwardfd);}

Page 14: 15213 Recitation Section D

1415213 Recitation D Anubhav Gupta

Changing mainint main(int argc, char **argv) { int listenfd, connfd, port, clientlen, server_port; struct sockaddr_in clientaddr; struct hostent *hp; char *haddrp, *server;

port = atoi(argv[1]); server = argv[2]; server_port = atoi(argv[3]); listenfd = Open_listenfd(port); while (1) {

clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);

. . .

echo_forward(connfd, server, server_port); Close(connfd);

} exit(0);}

Page 15: 15213 Recitation Section D

1515213 Recitation D Anubhav Gupta

L7 Proxy

• Different request and response (HTTP)

• Concurrency

EchoClient

process

EchoServer

process

Echoproxy

process

WebBrowserprocess

WebServer

process

Webproxy

process

Page 16: 15213 Recitation Section D

1615213 Recitation D Anubhav Gupta

HTTP Request and Response

• Let’s use telnet to examine a simple HTTP request and response

• More details in Tuesday’s lecture

Page 17: 15213 Recitation Section D

1715213 Recitation D Anubhav Gupta

HTTP Request and Response[chensm@bass ~]$ telnet www.cmu.edu 80Trying 128.2.11.43...Connected to WEB3.andrew.cmu.edu.Escape character is '^]'.GET / HTTP/1.1host: www.cmu.edu

HTTP/1.1 200 OKDate: Sun, 24 Nov 2002 21:52:53 GMTServer: Apache/1.3.26 (Unix) PHP/4.2.2 mod_pubcookie/a5/1.76-009

mod_ssl/2.8.10 OpenSSL/0.9.6dLast-Modified: Thu, 21 Nov 2002 15:28:47 GMTETag: "d3226-38b1-3ddcfbaf"Accept-Ranges: bytesContent-Length: 14513Content-Type: text/html

<HTML> <HEAD> <TITLE> Carnegie Mellon University </TITLE> </HEAD>. . . . . .

Page 18: 15213 Recitation Section D

1815213 Recitation D Anubhav Gupta

HTTP Request

GET / HTTP/1.1

host: www.cmu.edu

Request line: <method> <uri> <version>Request line: <method> <uri> <version>

Request headers: <header name>: <header data>Request headers: <header name>: <header data>

‘‘\r\n’ to mark the end of the request\r\n’ to mark the end of the request

Page 19: 15213 Recitation Section D

1915213 Recitation D Anubhav Gupta

HTTP Response

HTTP/1.1 200 OKDate: Sun, 24 Nov 2002 21:52:53 GMTServer: Apache/1.3.26 (Unix) PHP/4.2.2 mod_pubcookie/a5/1.76-009

mod_ssl/2.8.10 OpenSSL/0.9.6dLast-Modified: Thu, 21 Nov 2002 15:28:47 GMTETag: "d3226-38b1-3ddcfbaf"Accept-Ranges: bytesContent-Length: 14513Content-Type: text/html

<HTML> <HEAD> <TITLE> Carnegie Mellon University </TITLE> </HEAD>. . . . . .

Response line: <version> <status code> <status msg>Response line: <version> <status code> <status msg>

Response headers: <header name>: <header data>Response headers: <header name>: <header data>

‘‘\r\n’\r\n’ Response body: Web pageResponse body: Web page

Page 20: 15213 Recitation Section D

2015213 Recitation D Anubhav Gupta

Broken Pipe Error

• When reading or writing to a socket

• If the socket has already been closed at the other end– e.g. click “stop” on web browser

• SIGPIPE signal and EPIPE errno

• Don’t want to terminate the web server

Page 21: 15213 Recitation Section D

2115213 Recitation D Anubhav Gupta

Details

• Read from a broken pipe– Kernel returns -1– errno is set to EPIPE

• Write to a broken pipe– For the first write, return -1 and set EPIPE– For subsequent writes,

• Send SIGPIPE signal, which terminates process• If the signal is blocked or handled, return -1 and

set EPIPE.

• Example of broken pipe with echo server

Page 22: 15213 Recitation Section D

2215213 Recitation D Anubhav Gupta

How to deal with broken pipe?

• Block SIGPIPE signal• Ignore EPIPE error in Rio wrappers of csapp.c

• Example of how we handle broken pipe in echo server– In server main(), we block SIGPIPE signal

– In csapp.c, we ignore EPIPE error in Rio wrappers

– In echo(), deal with cases when return value is -1

Page 23: 15213 Recitation Section D

2315213 Recitation D Anubhav Gupta

Feedback and Evaluation