Lecture 7 TELNET Protocol Hyper Text Transfer Protocol

  • Slides: 47
Download presentation
Lecture 7 TELNET Protocol & Hyper. Text Transfer Protocol CPE 401 / 601 Computer

Lecture 7 TELNET Protocol & Hyper. Text Transfer Protocol CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger

TELNET vs. telnet q TELNET is a protocol that provides “a general, bi-directional, eight-bit

TELNET vs. telnet q TELNET is a protocol that provides “a general, bi-directional, eight-bit byte oriented communications facility”. q telnet is a program that supports the TELNET protocol over TCP. q Many application protocols are built upon the TELNET protocol. TELNET 2

The TELNET Protocol q Reference: RFC 854 q TCP connection q data and control

The TELNET Protocol q Reference: RFC 854 q TCP connection q data and control over the same connection. q Network Virtual Terminal v intermediate representation of a generic terminal. v provides a standard language for communication of terminal control functions. TELNET 3

Network Virtual Terminal Server Process NVT TCP TELNET 4

Network Virtual Terminal Server Process NVT TCP TELNET 4

Negotiated Options q All NVTs support a minimal set of capabilities. v Some terminals

Negotiated Options q All NVTs support a minimal set of capabilities. v Some terminals have more capabilities than the minimal set. q The set of options is not part of the TELNET protocol, v so that new terminal features can be incorporated without changing the TELNET protocol. q Two endpoints negotiate a set of mutually acceptable options Line mode vs. character mode v echo modes v character set (EBCDIC vs. ASCII) v TELNET 5

Control Functions q TELNET includes support for a series of control functions commonly supported

Control Functions q TELNET includes support for a series of control functions commonly supported by servers. q This provides a uniform mechanism for communication of (the supported) control functions. TELNET 6

Control Functions q Interrupt Process (IP) v suspend/abort process. q Abort Output (AO) v

Control Functions q Interrupt Process (IP) v suspend/abort process. q Abort Output (AO) v send no more output to user’s terminal. q Are You There (AYT) v check to see if system is still running. q Erase Character (EC) v delete last character sent q Erase Line (EL) v delete all input in current line. TELNET 7

Command Structure q All TELNET commands and data flow through the same TCP connection.

Command Structure q All TELNET commands and data flow through the same TCP connection. q Commands start with a special character called the Interpret as Command escape character The IAC code is 255. v If a 255 is sent as data - it must be followed by another 255. v q If IAC is found and the next byte is IAC v a single byte is presented to application/terminal q If IAC is followed by any other code v the TELNET layer interprets this as a command. TELNET 8

Playing with TELNET q You can use the telnet program to play with the

Playing with TELNET q You can use the telnet program to play with the TELNET protocol. q telnet is a generic TCP client. Sends whatever you type to the TCP socket. v Prints whatever comes back through the TCP socket v Useful for testing TCP servers (ASCII based protocols). v q Many Unix systems have these servers running (by default): echo v daytime v port 7 port 13 discard chargen port 9 port 19 TELNET 9

telnet hostname port > telnet amele-2. cse. unr. edu 7 Trying 134. 197. 40.

telnet hostname port > telnet amele-2. cse. unr. edu 7 Trying 134. 197. 40. 246. . . Connected to amele-2. cse. unr. edu (134. 197. 40. 246). Escape character is '^]'. Hi mehmet stop it ^] telnet> quit Connection closed. TELNET 10

telnet vs. TCP q Not all TCP servers talk TELNET (most don't) q You

telnet vs. TCP q Not all TCP servers talk TELNET (most don't) q You can use the telnet program to play with these servers, but the fancy commands won't do anything. v type ^], then "help" for a list of fancy TELNET stuff you can do in telnet. TELNET 11

Hyper. Text Transfer Protocol (HTTP) q HTTP is the protocol that supports communication between

Hyper. Text Transfer Protocol (HTTP) q HTTP is the protocol that supports communication between web browsers and web servers. q A “Web Server” is a HTTP server q Most clients/servers today speak version 1. 1, but 1. 0 is also in use. RFC 1945 (HTTP 1. 0) v RFC 2616 (HTTP 1. 1) v HTTP 13

From the RFC q “HTTP is an application-level protocol with the lightness and speed

From the RFC q “HTTP is an application-level protocol with the lightness and speed necessary for distributed, hypermedia information systems. ” q Transport Independence v The HTTP protocol generally takes place over a TCP connection, v but the protocol itself is not dependent on a specific transport layer. HTTP 14

Request - Response q HTTP has a simple structure: v client sends a request

Request - Response q HTTP has a simple structure: v client sends a request v server returns a reply. q HTTP can support multiple request-reply exchanges over a single TCP connection. q The “well known” TCP port for HTTP servers is port 80. v Other ports can be used as well. . . HTTP 15

HTTP 1. 0+ Request q Lines of text (ASCII). q Lines end with CRLF

HTTP 1. 0+ Request q Lines of text (ASCII). q Lines end with CRLF “rn” q First line is called “Request-Line” Request-Line Headers. . . blank line Content. . . HTTP 16

Request Line Method URI HTTP-Versionrn q The request line contains 3 tokens (words). q

Request Line Method URI HTTP-Versionrn q The request line contains 3 tokens (words). q space characters “ “ separate the tokens. q Newline (n) seems to work by itself v but the protocol requires CRLF HTTP 17

Request Method q The Request Method can be: GET PUT HEAD POST DELETE TRACE

Request Method q The Request Method can be: GET PUT HEAD POST DELETE TRACE OPTIONS future expansion is supported q GET, HEAD and POST are supported everywhere (including Lab 2!). q HTTP 1. 1 servers often support PUT, DELETE, OPTIONS & TRACE. HTTP 18

Methods q GET: v Typically used to retrieve an HTML document q HEAD: v

Methods q GET: v Typically used to retrieve an HTML document q HEAD: v retrieve meta-information about the URI. used to find out if a document has changed q POST: v retrieve information identified by the URI. send information to a URI and retrieve result. used to submit a form HTTP 19

More Methods q PUT: Store information in location named by URI. q DELETE: remove

More Methods q PUT: Store information in location named by URI. q DELETE: remove entity identified by URI. q TRACE: used to trace HTTP forwarding through proxies, tunnels, etc. q OPTIONS: used to determine the capabilities of the server, or characteristics of a named resource. HTTP 20

URI: Universal Resource Identifier q URIs defined in RFC 2396. q Absolute URI: v

URI: Universal Resource Identifier q URIs defined in RFC 2396. q Absolute URI: v scheme: //hostname[: port]/path v http: //www. cse. unr. edu: 80/~mgunes/cpe 401 q Relative URI: v /path v /blah/foo No server mentioned HTTP 21

URI Usage q When dealing with a HTTP 1. 1 server, only a path

URI Usage q When dealing with a HTTP 1. 1 server, only a path is used (no scheme or hostname). v HTTP 1. 1 servers are required to be capable of handling an absolute URI, but there are still some out there that won’t… q When dealing with a proxy HTTP server, an absolute URI is used. client has to tell the proxy where to get the document! v more on proxy servers in a bit…. v HTTP 22

HTTP Version Number “HTTP/1. 0” or “HTTP/1. 1” q Starting with HTTP 1. 0

HTTP Version Number “HTTP/1. 0” or “HTTP/1. 1” q Starting with HTTP 1. 0 the version number is part of every request. v Client tells the server what version it can talk (what options are supported, etc). q HTTP 0. 9 did not include a version number in a request line. If a server gets a request line with no HTTP version number, it assumes 0. 9 v HTTP 0. 9 was used for many years. v HTTP 23

The Header Lines q Request Headers provide information to the server about the client

The Header Lines q Request Headers provide information to the server about the client what kind of client v what kind of content will be accepted v who is making the request v q Each header line contains v an attribute name followed by a “: ” followed by a space and the attribute value. q There can be 0 headers (HTTP 1. 0) q HTTP 1. 1 requires a Host: header HTTP 24

Example HTTP Headers Accept: text/html Host: www. cse. unr. edu From: mgunes@cse. unr. edu

Example HTTP Headers Accept: text/html Host: www. cse. unr. edu From: mgunes@cse. unr. edu User-Agent: Mozilla/4. 0 Referer: http: //www. unr. edu/ HTTP 25

End of the Headers q Each header ends with a CRLF ( rn )

End of the Headers q Each header ends with a CRLF ( rn ) q The end of the header section is marked with a blank line. v just CRLF q For GET and HEAD requests, the end of the headers is the end of the request! HTTP 26

POST q A POST request includes some content (some data) after the headers (after

POST q A POST request includes some content (some data) after the headers (after the blank line). q There is no format for the data (just raw bytes). q A POST request must include a Content- Length line in the headers: Content-length: 267 HTTP 27

Example POST Request POST /~mgunes/cpe 401/grades. cgi HTTP/1. 1 Accept: */* Host: www. cse.

Example POST Request POST /~mgunes/cpe 401/grades. cgi HTTP/1. 1 Accept: */* Host: www. cse. unr. edu User-Agent: Secret. Agent V 2. 3 Content-Length: 35 Referer: http: //www. unr. edu/ stuid=6660182722&item=test 1&grade=99 HTTP 28

Example GET Request GET /~mgunes/cpe 401/lab 1. htm HTTP/1. 1 Accept: */* Host: www.

Example GET Request GET /~mgunes/cpe 401/lab 1. htm HTTP/1. 1 Accept: */* Host: www. cse. unr. edu User-Agent: Internet Explorer From: mgunes@cse. unr. edu Referer: http: //www. unr. edu/ There is a blank line here! HTTP 29

HTTP Response q ASCII Status Line q Headers Section Status-Line Headers. . . blank

HTTP Response q ASCII Status Line q Headers Section Status-Line Headers. . . blank line Content. . . q Content can be anything (not just text) v typically an HTML document or some kind of image. HTTP 30

Response Status Line HTTP-Version Status-Code Message q Status Code is 3 digit number (for

Response Status Line HTTP-Version Status-Code Message q Status Code is 3 digit number (for computers) v 1 xx Informational v 2 xx Success v 3 xx Redirection v 4 xx Client Error v 5 xx Server Error q Message is text (for humans) HTTP 31

Example Status Lines HTTP/1. 0 200 OK HTTP/1. 0 301 Moved Permanently HTTP/1. 0

Example Status Lines HTTP/1. 0 200 OK HTTP/1. 0 301 Moved Permanently HTTP/1. 0 400 Bad Request HTTP/1. 0 500 Internal Server Error HTTP 32

Response Headers q Provide the client with information about the returned entity (document). what

Response Headers q Provide the client with information about the returned entity (document). what kind of document v how big the document is v how the document is encoded v when the document was last modified v q Response headers end with blank line HTTP 33

Response Header Examples Date: Wed, 30 Jan 2002 12: 48: 17 EST Server: Apache/1.

Response Header Examples Date: Wed, 30 Jan 2002 12: 48: 17 EST Server: Apache/1. 17 Content-Type: text/html Content-Length: 1756 Content-Encoding: gzip HTTP 34

Content q Content can be anything (sequence of raw bytes). q Content-Length header is

Content q Content can be anything (sequence of raw bytes). q Content-Length header is required for any response that includes content. q Content-Type header also required. HTTP 35

Single Request/Reply q The client sends a complete request. q The server sends back

Single Request/Reply q The client sends a complete request. q The server sends back the entire reply. q The server closes it’s socket. q If the client needs another document it must open a new connection. This was the default for HTTP 1. 0 HTTP 36

Persistent Connections q HTTP 1. 1 supports persistent connections (this is the default). q

Persistent Connections q HTTP 1. 1 supports persistent connections (this is the default). q Multiple requests can be handled over a single TCP connection. q The Connection: header is used to exchange information about persistence (HTTP/1. 1) q 1. 0 Clients used a Keep-alive: header HTTP 37

Try it with telnet > telnet www. cse. unr. edu 80 GET / HTTP/1.

Try it with telnet > telnet www. cse. unr. edu 80 GET / HTTP/1. 0 200 OK Server: Apache. . . Requ est-lin e Blank Line (end o f head ers) Res e s n po HTTP 38

Try it with telnet (persistent) > telnet www. cse. unr. edu 80 GET /

Try it with telnet (persistent) > telnet www. cse. unr. edu 80 GET / HTTP/1. 1 Host: www. cse. unr. edu HTTP/1. 0 200 OK Server: Apache. . . Req uire HTTP d! 39

HTTP Proxy Server Browser Proxy HTTP Server HTTP 40

HTTP Proxy Server Browser Proxy HTTP Server HTTP 40

Network Lab #2 HTTP Proxy q You need to write a proxy server. q

Network Lab #2 HTTP Proxy q You need to write a proxy server. q Must be able to handle GET, HEAD and POST requests. q Filtering: Your proxy will be given a list of domain names on the command line, you should refuse to forward requests to any server whose name is within a specified domain. v send back status line: 403 Forbidden. Lab #2 41

The code you need q Proxy is both a client and a server q

The code you need q Proxy is both a client and a server q Parsing the HTTP request is needed. v You need to understand HTTP q You will need to parse headers. v need to look at Content-length, Connection, etc. Lab #2 42

Testing q Tell your browser to use a proxy v Edit preferences/options. q Interrupt

Testing q Tell your browser to use a proxy v Edit preferences/options. q Interrupt a long transfer (press stop). q Fill out a form (probably uses POST). q Test it with a browser. q Test it with telnet q Write an abusive client and a rude server! Lab #2 43

What is expected q We should be able to surf through your proxy! q

What is expected q We should be able to surf through your proxy! q Proxy should print some info about each request (print the request line). q No memory leaks! q Check every system call for errors! v We should not be able to kill your proxy by • sending a bad request. • using a server that sends bad replies. v No crashes, no matter what kind of nonsense we send your proxy. Lab #2 44

HTTP V 1. 1 Details q The RFC is 114 pages! q we don’t

HTTP V 1. 1 Details q The RFC is 114 pages! q we don’t expect you to read it all or to support every nitty-gritty detail. q work on creating a working proxy (one you can use through a browser). v performance is not a big deal (but it shouldn’t be horribly worse than without your proxy). q Don’t worry about persistence, pipelining, chunking, etc. v you need to turn off persistence if you don't want to handle it. Lab #2 45

HTTP Headers q You will need to look at the Content-Length header in a

HTTP Headers q You will need to look at the Content-Length header in a POST. v you need to know how many bytes to read after the end of the headers. q You will need to either look at Connection (Proxy-Connection) headers or (at a minimum) to force Connection: close as a request header. Lab #2 46

Stuff you might need to know (that we have not covered) q Converting hostnames

Stuff you might need to know (that we have not covered) q Converting hostnames to IP addresses. q Handling signals (SIGPIPE) v Check out section 5. 13 in the text q Providing Concurrency (not required, but not hard either). just fork the server after calling accept. v MAKE SURE YOU TAKE CARE OF ZOMBIES! v Lab #2 47