http

47
1 HTTP HTTP Hypertext Transfer Hypertext Transfer Protocol Protocol Refs: Refs: RFC 1945 (HTTP 1.0) RFC 1945 (HTTP 1.0) RFC 2616 (HTTP 1.1) RFC 2616 (HTTP 1.1)

Upload: eri-alam

Post on 19-May-2015

1.019 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Http

1

HTTPHTTPHypertext Transfer ProtocolHypertext Transfer Protocol

Refs: Refs:

RFC 1945 (HTTP 1.0)RFC 1945 (HTTP 1.0)

RFC 2616 (HTTP 1.1)RFC 2616 (HTTP 1.1)

Page 2: Http

2Netprog - HTTP

HTTP UsageHTTP Usage

• HTTP is the protocol that supports HTTP is the protocol that supports communication between web browsers communication between web browsers and web servers.and web servers.

• A “Web Server” is a HTTP serverA “Web Server” is a HTTP server

• Most clients/servers today speak Most clients/servers today speak version 1.1, but 1.0 is also in use.version 1.1, but 1.0 is also in use.

Page 3: Http

3Netprog - HTTP

From the RFCFrom the RFC

““HTTP is an application-level protocol with HTTP is an application-level protocol with the lightness and speed necessary for the lightness and speed necessary for distributed, hypermedia information distributed, hypermedia information systems.”systems.”

Page 4: Http

4Netprog - HTTP

Transport IndependenceTransport Independence

• The RFC states that the HTTP protocol The RFC states that the HTTP protocol generally takes place over a TCP generally takes place over a TCP connection, but the protocol itself is not connection, but the protocol itself is not dependent on a specific transport layer.dependent on a specific transport layer.

Page 5: Http

5Netprog - HTTP

Request - ResponseRequest - Response

• HTTP has a simple structure:HTTP has a simple structure:– client sends a requestclient sends a request– server returns a reply.server returns a reply.

• HTTP can support multiple request-HTTP can support multiple request-reply exchanges over a single TCP reply exchanges over a single TCP connection.connection.

Page 6: Http

6Netprog - HTTP

Well Known AddressWell Known Address

• The “well known” TCP port for HTTP The “well known” TCP port for HTTP servers is port 80.servers is port 80.

• Other ports can be used as well...Other ports can be used as well...

Page 7: Http

7Netprog - HTTP

HTTP VersionsHTTP Versions

• The original version now goes by the The original version now goes by the name “HTTP Version 0.9”name “HTTP Version 0.9”– HTTP 0.9 was used for many years.HTTP 0.9 was used for many years.

• Starting with HTTP 1.0 the version Starting with HTTP 1.0 the version number is part of every request.number is part of every request.– tells the server what version the client can tells the server what version the client can

talk (what options are supported, etc).talk (what options are supported, etc).

Page 8: Http

8Netprog - HTTP

HTTP 1.0+ RequestHTTP 1.0+ Request

• Lines of text (ASCII).Lines of text (ASCII).

• Lines end with CRLF Lines end with CRLF “\r\n”“\r\n”

• First line is called “Request-Line”First line is called “Request-Line”

Request-LineHeaders...

Content...blank lineblank line

Page 9: Http

9Netprog - HTTP

Request LineRequest Line

MethodMethod URIURI HTTP-VersionHTTP-Version\r\n\r\n

• The request line contains 3 The request line contains 3 tokenstokens (words). (words).

• space characters “ “ separate the tokens.space characters “ “ separate the tokens.

• Newline (\n) seems to work by itself (but the Newline (\n) seems to work by itself (but the protocol requires CRLF)protocol requires CRLF)

Page 10: Http

10Netprog - HTTP

Request MethodRequest Method

• The Request Method can be:The Request Method can be:

GETGET HEADHEAD PUTPUT

POSTPOST DELETEDELETE TRACETRACE

OPTIONSOPTIONS

future expansion is supportedfuture expansion is supported

Page 11: Http

11Netprog - HTTP

MethodsMethods• GET: retrieve information identified by GET: retrieve information identified by

the URI.the URI.

• HEAD: retrieve meta-information about HEAD: retrieve meta-information about the URI.the URI.

• POST: send information to a URI and POST: send information to a URI and retrieve result.retrieve result.

Page 12: Http

12Netprog - HTTP

Methods (cont.)Methods (cont.)

• PUT: Store information in location PUT: Store information in location named by URI.named by URI.

• DELETE: remove DELETE: remove entityentity identified by identified by URI.URI.

Page 13: Http

13Netprog - HTTP

More MethodsMore Methods

• TRACE: used to trace HTTP forwarding TRACE: used to trace HTTP forwarding through proxies, tunnels, etc.through proxies, tunnels, etc.

• OPTIONS: used to determine the OPTIONS: used to determine the capabilities of the server, or capabilities of the server, or characteristics of a named resource.characteristics of a named resource.

Page 14: Http

14Netprog - HTTP

Common UsageCommon Usage

• GET, HEAD and POST are supported GET, HEAD and POST are supported everywhere (including HW#2!).everywhere (including HW#2!).

• HTTP 1.1 servers often support PUT, HTTP 1.1 servers often support PUT, DELETE, OPTIONS & TRACE.DELETE, OPTIONS & TRACE.

Page 15: Http

15Netprog - HTTP

URI: Universal Resource IdentifierURI: Universal Resource Identifier

• URIs defined in RFC 2396.URIs defined in RFC 2396.

• Absolute URI: Absolute URI: scheme://hostname[:port]/pathscheme://hostname[:port]/pathhttp://www.cs.rpi.edu:80/blah/foohttp://www.cs.rpi.edu:80/blah/foo

• Relative URI: Relative URI: /path/path/blah/foo/blah/foo

No server mentioned

Page 16: Http

16Netprog - HTTP

URI UsageURI Usage• When dealing with a HTTP 1.1 server, only a When dealing with a HTTP 1.1 server, only a

pathpath is used (no scheme or hostname). is used (no scheme or hostname).– HTTP 1.1 servers are required to be capable of HTTP 1.1 servers are required to be capable of

handling an absolute URI, but there are still some handling an absolute URI, but there are still some out there that won’t…out there that won’t…

• When dealing with a When dealing with a proxyproxy HTTP server, an HTTP server, an absolute URI is used.absolute URI is used.– client has to tell the proxy where to get the client has to tell the proxy where to get the

document!document!– more on proxy servers in a bit….more on proxy servers in a bit….

Page 17: Http

17Netprog - HTTP

HTTP Version NumberHTTP Version Number

““HTTP/1.0HTTP/1.0” or “” or “HTTP/1.1HTTP/1.1””

HTTP 0.9 did not include a version HTTP 0.9 did not include a version number in a request line.number in a request line.

If a server gets a request line with no If a server gets a request line with no HTTP version number, it assumes 0.9HTTP version number, it assumes 0.9

Page 18: Http

18Netprog - HTTP

The Header LinesThe Header Lines

• After the After the Request-LineRequest-Line come a number come a number (possibly zero) of HTTP (possibly zero) of HTTP header linesheader lines..

• Each header line contains an attribute Each header line contains an attribute name followed by a “:” followed by a name followed by a “:” followed by a space and the attribute value.space and the attribute value.

The Name and Value are just text.

Page 19: Http

19Netprog - HTTP

HeadersHeaders

• Request Headers provide information to Request Headers provide information to the server about the clientthe server about the client– what kind of clientwhat kind of client– what kind of content will be acceptedwhat kind of content will be accepted– who is making the requestwho is making the request

• There can be 0 headers (HTTP 1.0)There can be 0 headers (HTTP 1.0)• HTTP 1.1 requires a HTTP 1.1 requires a Host:Host: header header

Page 20: Http

20Netprog - HTTP

Example HTTP HeadersExample HTTP Headers

Accept: text/htmlAccept: text/html

Host: www.rpi.eduHost: www.rpi.edu

From: [email protected]: [email protected]

User-Agent: Mozilla/4.0User-Agent: Mozilla/4.0

Referer: http://foo.com/blahReferer: http://foo.com/blah

Page 21: Http

21Netprog - HTTP

End of the HeadersEnd of the Headers

• Each header ends with a CRLF ( Each header ends with a CRLF ( \r\n \r\n ))

• The end of the header section is marked The end of the header section is marked with a blank line. with a blank line. – just CRLFjust CRLF

• For GET and HEAD requests, the end of For GET and HEAD requests, the end of the headers is the end of the request!the headers is the end of the request!

Page 22: Http

22Netprog - HTTP

POSTPOST• A POST request includes some A POST request includes some content content

(some data)(some data) after the headers (after the blank after the headers (after the blank line).line).

• There is no format for the data (just raw There is no format for the data (just raw bytes).bytes).

• A POST request must include a Content-A POST request must include a Content-Length line in the headers:Length line in the headers:

Content-length: 267Content-length: 267

Page 23: Http

23Netprog - HTTP

Example GET RequestExample GET Request

GET /~hollingd/testanswers.html HTTP/1.1GET /~hollingd/testanswers.html HTTP/1.1

Accept: */*Accept: */*

Host: www.cs.rpi.eduHost: www.cs.rpi.edu

User-Agent: Internet Explorer User-Agent: Internet Explorer

From: [email protected]: [email protected]

Referer: http://foo.com/Referer: http://foo.com/There is a blank line here!

Page 24: Http

24Netprog - HTTP

Example POST RequestExample POST Request

POST /~hollingd/changegrade.cgi HTTP/1.1POST /~hollingd/changegrade.cgi HTTP/1.1

Accept: */*Accept: */*

Host: www.cs.rpi.eduHost: www.cs.rpi.edu

User-Agent: SecretAgent V2.3User-Agent: SecretAgent V2.3

Content-Length: 35Content-Length: 35

Referer: Referer: http://monte.cs.rpi.edu/blah

stuid=6660182722&item=test1&grade=99stuid=6660182722&item=test1&grade=99

Page 25: Http

25Netprog - HTTP

Typical Method UsageTypical Method Usage

GET used to retrieve an HTML document.GET used to retrieve an HTML document.

HEAD used to find out if a document has HEAD used to find out if a document has changed.changed.

POST used to submit a form.POST used to submit a form.

Page 26: Http

26Netprog - HTTP

HTTP ResponseHTTP Response

• ASCII Status LineASCII Status Line

• Headers SectionHeaders Section

• Content can be anything (not just text)Content can be anything (not just text)– typically an HTML document or some kind typically an HTML document or some kind

of image.of image.

Status-LineHeaders...

Content...

blank lineblank line

Page 27: Http

27Netprog - HTTP

Response Status LineResponse Status Line

HTTP-Version Status-Code MessageHTTP-Version Status-Code Message

• Status CodeStatus Code is 3 digit number (for is 3 digit number (for computers)computers)

• Message is text (for humans)Message is text (for humans)

Page 28: Http

28Netprog - HTTP

Status CodesStatus Codes

1xx1xx InformationalInformational

2xx2xx SuccessSuccess

3xx3xx RedirectionRedirection

4xx4xx Client ErrorClient Error

5xx5xx Server ErrorServer Error

Page 29: Http

29Netprog - HTTP

Example Status LinesExample Status Lines

HTTP/1.0 200 OKHTTP/1.0 200 OK

HTTP/1.0 301 Moved PermanentlyHTTP/1.0 301 Moved Permanently

HTTP/1.0 400 Bad RequestHTTP/1.0 400 Bad Request

HTTP/1.0 500 Internal Server ErrorHTTP/1.0 500 Internal Server Error

Page 30: Http

30Netprog - HTTP

Response HeadersResponse Headers

• Provide the client with information about Provide the client with information about the returned the returned entityentity (document). (document).– what kind of documentwhat kind of document– how big the document ishow big the document is– how the document is encodedhow the document is encoded– when the document was last modifiedwhen the document was last modified

• Response headers end with blank lineResponse headers end with blank line

Page 31: Http

31Netprog - HTTP

Response Header ExamplesResponse Header Examples

Date: Wed, 30 Jan 2002 12:48:17 ESTDate: Wed, 30 Jan 2002 12:48:17 EST

Server: Apache/1.17Server: Apache/1.17

Content-Type: text/htmlContent-Type: text/html

Content-Length: 1756Content-Length: 1756

Content-Encoding: gzipContent-Encoding: gzip

Page 32: Http

32Netprog - HTTP

ContentContent

• Content can be anything (sequence of Content can be anything (sequence of raw bytes).raw bytes).

• Content-Length header is required for Content-Length header is required for any response that includes content.any response that includes content.

• Content-Type header also required.Content-Type header also required.

Page 33: Http

33Netprog - HTTP

Single Request/ReplySingle Request/Reply

• The client sends a complete request.The client sends a complete request.

• The server sends back the entire reply.The server sends back the entire reply.

• The server closes it’s socket.The server closes it’s socket.

• If the client needs another document it If the client needs another document it must open a new connection. must open a new connection.

This was the default for HTTP 1.0

Page 34: Http

34Netprog - HTTP

Persistent Connections Persistent Connections

• HTTP 1.1 supports persistent connections HTTP 1.1 supports persistent connections (this is the default).(this is the default).

• Multiple requests can be handled over a Multiple requests can be handled over a single TCP connection.single TCP connection.

• The The Connection:Connection: header is used to header is used to exchange information about persistence exchange information about persistence (HTTP/1.1)(HTTP/1.1)

• 1.0 Clients used a 1.0 Clients used a Keep-alive:Keep-alive: header header

Page 35: Http

35Netprog - HTTP

Try it with Try it with telnettelnet

> telnet www.cs.rpi.edu 80> telnet www.cs.rpi.edu 80

GET / HTTP/1.0GET / HTTP/1.0

HTTP/1.0 200 OKHTTP/1.0 200 OK

Server: ApacheServer: Apache

...... Response

Request-line

Blank Line(end of headers)

Page 36: Http

36Netprog - HTTP

Try it with Try it with telnet 1.1telnet 1.1

> telnet www.cs.rpi.edu 80> telnet www.cs.rpi.edu 80

GET / HTTP/1.1GET / HTTP/1.1

Host: www.cs.rpi.eduHost: www.cs.rpi.edu

HTTP/1.0 200 OKHTTP/1.0 200 OK

Server: ApacheServer: Apache

......

Required!

Page 37: Http

37Netprog - HTTP

HTTP Proxy ServerHTTP Proxy Server

HTTPServer

Browser Proxy

Page 38: Http

38Netprog - HTTP

Project #2 HTTP ProxyProject #2 HTTP Proxy

• You need to write a proxy server.You need to write a proxy server.

• Test it with a browser.Test it with a browser.• Test it with telnetTest it with telnet• Write an abusive client!Write an abusive client!• Write a rude server!Write a rude server!

• Must be able to handle GET, HEAD and Must be able to handle GET, HEAD and POST requests.POST requests.

Page 39: Http

39Netprog - HTTP

Filtering (blocking)Filtering (blocking)

• Your proxy will be given a list of domain Your proxy will be given a list of domain names on the comand line, you should names on the comand line, you should refuse to forward requests to any server refuse to forward requests to any server whose name is within a specified whose name is within a specified domain.domain.– send back status line: send back status line: 403 Forbidden403 Forbidden..

Page 40: Http

40Netprog - HTTP

What you need to knowWhat you need to know

• YouYou need to understand HTTP need to understand HTTP

• You You needneed to understand HTTP to understand HTTP

• You need to You need to understandunderstand HTTP HTTP

• You need to understand You need to understand HTTPHTTP

Page 41: Http

41Netprog - HTTP

The code you needThe code you need

• Proxy is both a client and a serverProxy is both a client and a server

• Parsing the HTTP request is needed.Parsing the HTTP request is needed.

• You will need to parse headers.You will need to parse headers.– need to look at Content-length, need to look at Content-length,

Connection, etc.Connection, etc.

Page 42: Http

42Netprog - HTTP

TestingTesting

• Tell your browser to use a proxyTell your browser to use a proxy– Edit preferences/options.Edit preferences/options.

• Interrupt a long transfer (press stop).Interrupt a long transfer (press stop).

• Fill out a form (probably uses POST).Fill out a form (probably uses POST).

Page 43: Http

43Netprog - HTTP

What is expectedWhat is expected

• We should be able to surf through your proxy!We should be able to surf through your proxy!• We should not be able to kill your proxy by We should not be able to kill your proxy by

sending a bad request.sending a bad request.• We should not be able to kill your proxy by We should not be able to kill your proxy by

using a server that sends bad replies.using a server that sends bad replies.• Proxy should print some info about each Proxy should print some info about each

request (print the request line).request (print the request line).

Page 44: Http

44Netprog - HTTP

More ExpectationsMore Expectations

• Iterative Server is fine (concurrency is not Iterative Server is fine (concurrency is not required).required).

• No memory leaks!No memory leaks!

• No crashes, no matter what kind of nonsense No crashes, no matter what kind of nonsense we send your proxy.we send your proxy.

• Check every system call for errors!Check every system call for errors!

Page 45: Http

45Netprog - HTTP

HTTP V1.1 DetailsHTTP V1.1 Details

• The RFC is 176 pages!The RFC is 176 pages!– we don’t expect you to read it all or to support we don’t expect you to read it all or to support

every nitty-gritty detail.every nitty-gritty detail.– work on creating a working proxy (one you can work on creating a working proxy (one you can

use through a browser).use through a browser).• performance is not a big deal (but it shouldn’t be horribly performance is not a big deal (but it shouldn’t be horribly

worse than without your proxy).worse than without your proxy).

– Don’t worry about persistence, pipelining, Don’t worry about persistence, pipelining, chunking, etc.chunking, etc.

• you need to turn off persistence if you don't want to you need to turn off persistence if you don't want to handle it.handle it.

Page 46: Http

46Netprog - HTTP

HTTP Headers and HW2HTTP Headers and HW2

• You will need to look at the Content-Length You will need to look at the Content-Length header in a POST.header in a POST.– you need to know how many bytes to read after you need to know how many bytes to read after

the end of the headers.the end of the headers.

• You will need to either look at You will need to either look at ConnectionConnection ((Proxy-ConnectionProxy-Connection) headers or (at a ) headers or (at a minimum) to force minimum) to force Connection: closeConnection: close as as a request header.a request header.

Page 47: Http

47Netprog - HTTP

Stuff you might need to knowStuff you might need to know(that we have not covered)(that we have not covered)

• Converting hostnames to IP addresses.Converting hostnames to IP addresses.

• Handling signals (SIGPIPE)Handling signals (SIGPIPE)– Check out section 5.13 in the textCheck out section 5.13 in the text

• Providing Concurrency (not required, but not Providing Concurrency (not required, but not hard either).hard either).– just fork the server after calling accept.just fork the server after calling accept.– MAKE SURE YOU TAKE CARE OF ZOMBIES!MAKE SURE YOU TAKE CARE OF ZOMBIES!