HTTP Hypertext Transport Protocol Hypertext Transfer Protocol HTTP

  • Slides: 52
Download presentation
HTTP Hypertext Transport Protocol

HTTP Hypertext Transport Protocol

Hypertext Transfer Protocol (HTTP) n Communications protocol n Used to convey information on the

Hypertext Transfer Protocol (HTTP) n Communications protocol n Used to convey information on the World Wide Web n Original purpose was to provide a way to publish and retrieve HTML hypertext pages n Development of HTTP coordinated by: n n W 3 C (World Wide Web Consortium) IETF (Internet Engineering Task Force) n Culminating in the publication of a series of RFCs n Most notably RFC 2616 (June 1999) n Defines HTTP/1. 1 § Version of HTTP in common use today n HTTP/2 has been defined with RFC 7540 n Published May 2015

Hypertext Transfer Protocol n HTTP is a request/response protocol between clients and servers n

Hypertext Transfer Protocol n HTTP is a request/response protocol between clients and servers n Client makes an HTTP request n Referred to as the user agent § Web browser, spider, or other end-user tool n Server responds n Called the origin server § Stores or creates resources such as HTML files, text files, images and programs n In between the user agent and origin server may be several intermediaries n Routers, proxies, gateways, tunnels, etc.

Hypertext Transfer Protocol n HTTP does not require the use of TCP/IP or its

Hypertext Transfer Protocol n HTTP does not require the use of TCP/IP or its supporting layers Can be implemented on top of any other protocol on the Internet, or on other networks n Only presumes a reliable transport n n Any protocol that provides such guarantees can be used n Note: the rest of the lecture will implicitly assume TCP/IP

Hypertext Transfer Protocol n HTTP client initiates a request by establishing a Transmission Control

Hypertext Transfer Protocol n HTTP client initiates a request by establishing a Transmission Control Protocol (TCP) connection to a port on a host n n Port 80 by default HTTP server listening on that port waits for the client to send a request message n Upon receiving the request, the server sends back: n A status line n E. g. "HTTP/1. 1 200 OK“ n A message of its own n Body of which could be: § The requested file § An error message § Some other information …

Hypertext Transfer Protocol n Resources to be accessed by HTTP are identified using: Uniform

Hypertext Transfer Protocol n Resources to be accessed by HTTP are identified using: Uniform Resource Identifiers (URIs) n Or, more specifically, URLs n n Using the http: or https: URI schemes

URI/URL/URN SIDEBAR

URI/URL/URN SIDEBAR

URI/URL/URN? n Universal Resource Identifier (URI) n String of characters used to identify a

URI/URL/URN? n Universal Resource Identifier (URI) n String of characters used to identify a name or a resource on the Internet n One can classify URIs as n n n locators (URLs) names (URNs) —both— (rare) n Uniform Resource Name (URN) n Functions like an entity’s name n n Does not identify a specific entity Book’s ISBN functions as a URN n ISBN: 978 -0 -07 -154588 -4 § Linux Administration: A Beginner’s Guide 5 th edition n Uniform Resource Locator (URL) n Resembles a person's street address n Web addresses are URLs n http: //webpages. uncc. edu/~tkombol/index. htm n Summary: n URN defines an item's identity or label n URL provides a method for finding a specific item

Examples n URN n ISBN (International Standard Book Number) n Uniquely identifies books §

Examples n URN n ISBN (International Standard Book Number) n Uniquely identifies books § Not a specific copy n urn: isbn: 0 -486 -27557 -4 § Unambiguously a specific edition of Shakespeare's play Romeo and Juliet n ISAN (International Standard Audiovisual Number) n Uniquely identifies films § Root (base work), episode (for serial works), version n urn: isan: 0000 -9 E 59 -0000 -O-0000 -2 § Identifies 2002 release of Spider-Man n URL n To gain access to this object and read the book, one needs its location: n n a URL address Typical URL for this book on a Unix-like operating system would be a file path: n file: ///home/username/Romeo. And. Juliet. pdf § Identifies the electronic book saved in a file on a local hard disk n FTP URL template n ftp: //<user>: <password>@<host>: <port>/<url-path>; type=<typecode> n URNs and URLs have complementary purposes

BACK TO HTTP

BACK TO HTTP

Request message n Request message consists of the following: n Request line n What

Request message n Request message consists of the following: n Request line n What is needed to be done § e. g. GET /images/logo. gif HTTP/1. 1 § Requests the file logo. gif from the /images directory § Use HTTP 1. 1 n Headers n Additional information to aid processing § e. g. Accept-Language: en n n Empty line n n As many as needed Signals the end of the headers Message body n Optional

Request message n Request line and headers must all end with CRLF n Carriage

Request message n Request line and headers must all end with CRLF n Carriage return followed by a line feed n ASCII Code 13 followed by ASCII Code 10 An empty line must consist of only CRLF and no other whitespace n HTTP/1. 1: all headers except Host are optional n

HTTP Methods n HTTP defines eight methods n Indicates the desired action to be

HTTP Methods n HTTP defines eight methods n Indicates the desired action to be performed on the identified resource n Sometimes referred to as "verbs"

Request Methods (the big 3) n GET n Requests the specified resource n Most

Request Methods (the big 3) n GET n Requests the specified resource n Most common method used on the Web today § Data, e. g. parameters, (if any) sent as part of the URL n n Data (if present) is part of the URL Should not be used for operations that cause side-effects § E. g. Changes the state of the server § Using it for actions in web applications is a common misuse n n POST n Usually submits data to be processed by the identified resource n n HEAD n See 'safe methods' later Typically from an HTML form Data is part of the body of the request On the server this may result in: § Creation of a new resource § Updating of existing resources § Or both Asks for the response identical to the one that would correspond to a GET request, but without the response body n Useful for retrieving meta-information written in response headers, without having to transport the entire content

Request methods (the rest) n PUT n Uploads a representation of the specified resource

Request methods (the rest) n PUT n Uploads a representation of the specified resource n DELETE n Deletes the specified resource n TRACE n Echoes back the received request n Allows a client to see what intermediate servers are adding or changing in the request n OPTIONS n Returns the HTTP methods that the server supports n Can be used to check the functions available on a web server n CONNECT n Converts the request connection to a transparent TCP/IP tunnel n Usually to facilitate SSL-encrypted communication (HTTPS) through an unencrypted HTTP proxy

Request methods n HTTP servers should implement the following methods: GET n HEAD n

Request methods n HTTP servers should implement the following methods: GET n HEAD n OPTIONS n Whenever possible n n Note POST is not required

Request methods n Safe methods n Some methods are defined as safe n n

Request methods n Safe methods n Some methods are defined as safe n n n e. g. HEAD or GET Intended only for information retrieval Should not change the state of the server § In other words, they should not have side effects n Unsafe methods should be displayed to the user in a special way n e. g. POST, PUT and DELETE Typically as buttons rather than links n Make the user aware of possible obligations n § Such as a button that causes a financial transaction

Request methods n Safe methods n Despite the required safety of GET requests they

Request methods n Safe methods n Despite the required safety of GET requests they can cause changes on the server n For example: § Web server may use the retrieval through a simple hyperlink to initiate deletion of a domain database record § Thus causing a change of the server's state as a side-effect of a GET request § This is discouraged, because it can cause problems for: § Web caching § Search engines § Other automated agents § Can make unintended changes on the server n Another case is that a GET request may cause the server to create a cache space

Request methods n Idempotent methods and Web Applications n Idempotent: n n n HTTP

Request methods n Idempotent methods and Web Applications n Idempotent: n n n HTTP RFC allows a user-agent, such as a browser, to assume that any idempotent request can be retried without informing the user n n Multiple identical requests should have the same effect as a single request GET, HEAD, PUT, DELETE § OPTIONS and TRACE, being safe, are inherently idempotent Done to improve the user experience when connecting to unresponsive or heavily-loaded web servers However, note: n n Idempotence is not assured by the protocol or web server It is perfectly possible to write a web application in which (e. g. ) a database insert or update is triggered by a GET request § This would be a very normal example of what the spec refers to as "a change in server state" n This misuse of GET can combine with the retry behavior above to produce erroneous transactions and used, as intended, for document retrieval only n For this reason GET should be avoided for anything transactional

HTTP versions n HTTP has evolved into multiple, mostly backwards-compatible protocol versions RFC 2145

HTTP versions n HTTP has evolved into multiple, mostly backwards-compatible protocol versions RFC 2145 describes the use of HTTP version numbers n Client’s initial request reports its version n n Server responds in the same or earlier version

HTTP versions n 0. 9 n n n HTTP/1. 0 (May 1996) n n

HTTP versions n 0. 9 n n n HTTP/1. 0 (May 1996) n n n Deprecated Supports only one command, GET — which does not specify the HTTP version Does not support headers Since this version does not support POST, the client can't pass much information to the server This is the first protocol revision to specify its version in communications Still in wide use, especially by proxy servers HTTP/1. 1 (June 1999) n n Current version; persistent connections enabled by default and works well with proxies Supports request pipelining n n n Allows multiple requests to be sent at the same time Allows the server to prepare for the workload and potentially transfer the requested resources more quickly to the client HTTP/1. 2 or HTTP/2 n The initial 1995 working drafts were prepared by the W 3 C and submitted to the IETF n n n an Extension Mechanism for HTTP proposed the Protocol Extension Protocol, abbreviated PEP was originally intended to become a distinguishing feature of HTTP/1. 2 In later PEP working drafts, however, the reference to HTTP/1. 2 was removed HTTP/2 is now documented in RFC 7540 (2015)

Status lines n HTTP/1. 0 and later n Status Line: n n First line

Status lines n HTTP/1. 0 and later n Status Line: n n First line of the HTTP response Includes: § § n The way the user agent handles the response primarily depends on: 1. 2. n Numeric status code (such as "404") Textual reason phrase (such as "Not Found") the code the response headers Custom status codes can be used n If the user agent encounters a code it does not recognize § Can use the first digit of the code to determine the general class of the response

Status lines n Standard reason phrases are only recommendations n Can be replaced with

Status lines n Standard reason phrases are only recommendations n Can be replaced with "local equivalents" at the web developer's discretion n If the status code indicated a problem n User agent might display the reason phrase to the user to provide further information about the nature of the problem n Standard also allows the user agent to attempt to interpret the reason phrase n This might be unwise since the standard explicitly specifies that: n Status codes are machine-readable n Reason phrases are human-readable

Status Code Groups n 1 xx Informational n 2 xx Success n 3 xx

Status Code Groups n 1 xx Informational n 2 xx Success n 3 xx Redirection n 4 xx Client Error n 5 xx Server Error

1 xx Informational n Request received, continuing process n This class of status code

1 xx Informational n Request received, continuing process n This class of status code indicates a provisional response n n n Consists only of the Status-Line and optional headers Terminated by an empty line HTTP/1. 0 did not define any 1 xx status codes n Servers MUST NOT send a 1 xx response to an HTTP/1. 0 client except under experimental conditions

1 xx Informational n 100 Continue n The server has received the request headers

1 xx Informational n 100 Continue n The server has received the request headers n The client should proceed to send the request body § in the case of a request for which a body needs to be sent § for example, a POST request n n If the request body is large, sending it to a server when a request has already been rejected based upon inappropriate headers is inefficient To have a server check if the request could be accepted based on the request's headers alone, a client must send § Expect: 100 -continue as a header in its initial request § see RFC 2616 § 14. 20: Expect header) § Check if a 100 Continue status code is received in response before § Continuing or § receive 417 Expectation Failed and not continue n 101 Switching Protocols n 102 Processing (Web. DAV)

2 xx Success n The action was successfully: n Received n Understood n Accepted

2 xx Success n The action was successfully: n Received n Understood n Accepted n This class of status code indicates that the client's request was successfully received, understood, and accepted

2 xx Success n 200 OK n Standard response for successful HTTP requests. n

2 xx Success n 200 OK n Standard response for successful HTTP requests. n 201 Created n Request has been fulfilled and resulted in a new resource being created n 202 Accepted n Request has been accepted for processing n The processing has not been completed n Request might or might not eventually be acted upon n It might be disallowed when processing actually takes place n 203 Non-Authoritative Information (since HTTP/1. 1) n 204 No Content n 205 Reset Content n 206 Partial Content n Notice that a file has been partially downloaded. n Used by tools like wget to enable resuming of interrupted downloads, or split a download into multiple simultaneous streams. n 207 Multi-Status (Web. DAV) n The message body that follows is an XML message and can contain a number of separate response codes, depending on how many sub-requests were made.

3 xx Redirection n The client must take additional action to complete the request

3 xx Redirection n The client must take additional action to complete the request n n n This class of status code indicates that further action needs to be taken by the user agent in order to fulfill the request The action required MAY be carried out by the user agent without interaction with the user if and only if the method used in the second request is GET or HEAD A user agent SHOULD NOT automatically redirect a request more than 5 times, since such redirections usually indicate an infinite loop

3 xx Redirection (1 of 2) n 300 Multiple Choices n Indicates multiple options

3 xx Redirection (1 of 2) n 300 Multiple Choices n Indicates multiple options for the URI that the client may follow. n Can be used to present different format options for video, list files with different extensions, or word sense disambiguation. n 301 Moved Permanently n This and all future requests should be directed to the given URI. n 302 Found n Most popular redirect code, but also an example of industrial practice contradicting the standard. n HTTP/1. 0 specification (RFC 1945) required the client to perform a temporary redirect (the original describing phrase was "Moved Temporarily"), but popular browsers implemented it as a 303 See Other. n Therefore, HTTP/1. 1 added status codes 303 and 307 to disambiguate between the two behaviors. n However, the majority of Web applications and frameworks still use the 302 status code as if it were the 303. n 303 See Other (since HTTP/1. 1) n The response to the request can be found under another URI using a GET method.

3 xx Redirection (2 of 2) n 304 Not Modified n Indicates the request

3 xx Redirection (2 of 2) n 304 Not Modified n Indicates the request URL has not been modified since last requested. n Typically, the HTTP client provides a header like the If-Modified-Since header to provide a time with which to compare n Utilizing this saves bandwidth and reprocessing on both the server and client. n 305 Use Proxy (since HTTP/1. 1) n Many HTTP clients (such as Mozilla [1] and Internet Explorer) don't correctly handle responses with this status code, primarily for security reasons n 306 Switch Proxy n No longer used. n 307 Temporary Redirect (since HTTP/1. 1) n In this occasion, the request should be repeated with another URI, but future requests can still be directed to the original URI. n In contrast to 303, the request method should not be changed when reissuing the original request. n For instance, a POST request must be repeated using another POST request

4 xx Client Error n The request contains bad syntax or cannot be fulfilled

4 xx Client Error n The request contains bad syntax or cannot be fulfilled n The 4 xx class of status code is intended for cases in which the client seems to have erred n Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition n These status codes are applicable to any request method n User agents SHOULD display any included entity to the user

4 xx Client Error n 400 Bad Request n n 401 Unauthorized n n

4 xx Client Error n 400 Bad Request n n 401 Unauthorized n n n Original intention to be used as part of a digital cash or micropayment scheme Has not happened, and this code has never been used 403 Forbidden n n Similar to 403, when authentication is possible but has failed or not been provided 402 Payment Required n n The request contains bad syntax or cannot be fulfilled Request was a legal request, but the server is refusing to respond to it Unlike a 401 Unauthorized response, authenticating will make no difference 404 Not Found 405 Method Not Allowed n Request made to a URL using a request method not supported by that URL n n n Using GET on a form which requires data to be presented via POST Using PUT on a read-only resource 406 Not Acceptable 407 Proxy Authentication Required 408 Request Timeout 409 Conflict

4 xx Client Error n 410 Gone n Indicates that the resource requested is

4 xx Client Error n 410 Gone n Indicates that the resource requested is no longer available and will not be available again n Should be used when a resource has been intentionally removed n In practice, a 404 Not Found is often issued instead n 411 Length Required n 412 Precondition Failed n 413 Request Entity Too Large n 414 Request-URI Too Long n 415 Unsupported Media Type n 416 Requested Range Not Satisfiable n 417 Expectation Failed

4 xx Client Error n 422 Unprocessable Entity (Web. DAV) Request was well-formed but

4 xx Client Error n 422 Unprocessable Entity (Web. DAV) Request was well-formed but was unable to be followed due to semantic errors 423 Locked (Web. DAV) n The resource that is being accessed is locked 424 Failed Dependency (Web. DAV) n The request failed due to failure of a previous request (e. g. a PROPPATCH). 425 Unordered Collection n Defined in drafts of Web. Dav Advanced Collections n Not present in "Web Distributed Authoring and Versioning (Web. DAV) Ordered Collections Protocol" 426 Upgrade Required n The client should switch to TLS/1. 0 (use https) 449 Retry With n A Microsoft extension: The request should be retried after doing the appropriate action. n n n

4 xx examples n Directory listing allowed (no file referenced) n http: //ajklinux 2.

4 xx examples n Directory listing allowed (no file referenced) n http: //ajklinux 2. uncc. edu/grades/ n Not found or Forbidden? n http: //webpages. uncc. edu/~tkombol n n http: //webpages. uncc. edu/~tkombol/ITIS 2110/fileabc. htm n n n Works – has index. html (Apache default) 404 - fileabc. htm does not exist Should be 403 – ITIS 2110 is a dir, listing not allowed http: //ajklinux 2. uncc. edu/dir 1/ n n http: //ajklinux 2. uncc. edu/dir 3 Real 403 n File found n http: //ajklinux 2. uncc. edu/grades. htm n File not found n http: //ajklinux 2. uncc. edu/grades/gradex. htm

5 xx Server Error n The server failed to fulfill an normally valid request

5 xx Server Error n The server failed to fulfill an normally valid request n Response status codes beginning with the digit "5" indicate cases in which the server is aware that it has erred or is incapable of performing the request n Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition n User agents SHOULD display any included entity to the user n These response codes are applicable to any request method

5 xx Server Error n 500 Internal Server Error n 501 Not Implemented n

5 xx Server Error n 500 Internal Server Error n 501 Not Implemented n 502 Bad Gateway n 503 Service Temporarily Unavailable n 504 Gateway Timeout n 505 HTTP Version Not Supported n 506 Variant Also Negotiates n 507 Insufficient Storage (Web. DAV) n 509 Bandwidth Limit Exceeded n Not an official HTTP status code n Still used by many servers n 510 Not Extended (RFC 2774)

Persistent connections n HTTP/0. 9 and 1. 0: n the connection is closed after

Persistent connections n HTTP/0. 9 and 1. 0: n the connection is closed after a single request/response pair n HTTP/1. 1: a keep-alive-mechanism was introduced n n A connection could be reused for more than one request Such persistent connections reduce lag perceptibly n The client does not need to re-negotiate the TCP connection after the first request has been sent n Version 1. 1 of the protocol also introduced: n Chunked transfer encoding n n HTTP pipelining n l. Main Allows content on persistent connections to be streamed, rather than buffered Allows clients to send some types of requests before the previous response has been received, further reducing lag article: HTTP persistent connections

HTTP session state n HTTP is stateless n Can pose problems for Web developers

HTTP session state n HTTP is stateless n Can pose problems for Web developers and applications n n The advantage of a stateless protocol is that hosts do not need to retain information about users between requests Forces use of alternative methods for maintaining users' state n n n e. g. a host would like to customize content for a user who has visited before One common method for solving this problem involves the use of sending and requesting cookies Other methods include n n Server side sessions Hidden variables § When current page is a form n URL encoded parameters § Such as /index. php? userid=3

Resume 2/22

Resume 2/22

HTTPS SECURE HTTP

HTTPS SECURE HTTP

Secure HTTP n Two methods of establishing a secure HTTP connection: HTTPS URI scheme

Secure HTTP n Two methods of establishing a secure HTTP connection: HTTPS URI scheme n HTTP 1. 1 Upgrade header n n Introduced by RFC 2817 n Browser support for the Upgrade header is nearly non-existent n HTTPS URI scheme is the dominant method of establishing a secure HTTP connection

Secure HTTP n HTTPS URI scheme n URI scheme syntactically identical to the http:

Secure HTTP n HTTPS URI scheme n URI scheme syntactically identical to the http: scheme used for normal HTTP connections n Signals the browser to use an added encryption layer of SSL/TLS to protect the traffic § SSL – Secure Sockets Layer § TLS – Transport Layer Security § Note: since SSL is especially suited for HTTP since it can provide some protection even if only one side of the communication is authenticated n In the case of HTTP transactions over the Internet, typically, only the server side is authenticated n l. Main article: https

Secure HTTP n HTTP 1. 1 Upgrade header n n HTTP 1. 1 introduced

Secure HTTP n HTTP 1. 1 Upgrade header n n HTTP 1. 1 introduced support for the Upgrade header. In the exchange n Client begins by making a clear-text request § Later upgraded to TLS n n Either the client or the server may request (or demand) that the connection be upgraded The most common usage is a clear-text request by the client followed by a server demand to upgrade the connection, which looks like this: § Client: § GET /encrypted-area HTTP/1. 1 § Host: www. example. com § Server: § HTTP/1. 1 426 Upgrade Required § Upgrade: TLS/1. 0, HTTP/1. 1 § Connection: Upgrade n The server returns a 426 status-code because 400 level codes indicate a client failure n Correctly alerts legacy clients that the failure was client-related

Secure HTTP n Benefits of using this method for establishing a secure connection are:

Secure HTTP n Benefits of using this method for establishing a secure connection are: n n n Removes messy and problematic redirection and URL rewriting on the server side Allows virtual hosting (single IP, multiple domain-names) of secured websites Reduces user confusion by providing a single way to access a particular resource n A weakness with this method is: n Requirement for secure HTTP cannot be specified in the URI n In practice, the (untrusted) server will be responsible for enabling secure HTTP, not the (trusted) client

EXAMPLE

EXAMPLE

Sample n Following is a sample conversation between an HTTP client and an HTTP

Sample n Following is a sample conversation between an HTTP client and an HTTP server running on www. example. com, port 80

Sample Client Request n Client request (browser) n url entered: n http: //www. example.

Sample Client Request n Client request (browser) n url entered: n http: //www. example. com/index. html n Generated the following http request: GET /index. html HTTP/1. 1 Host: www. example. com <CRLF> n GET followed by a blank line n Request ends with a double newline § Carriage return followed by a line feed n The "Host" header n Distinguishes between various DNS names sharing a single IP address § Allows name-based virtual hosting § Optional in HTTP/1. 0, mandatory in HTTP/1. 1

Sample Server Response n HTTP/1. 1 200 OK Date: Mon, 23 May 2005 22:

Sample Server Response n HTTP/1. 1 200 OK Date: Mon, 23 May 2005 22: 38: 34 GMT Server: Apache/1. 3. 27 (Unix) (Red-Hat/Linux) Last-Modified: Wed, 08 Jan 2003 23: 11: 55 GMT Etag: "3 f 80 f-1 b 6 -3 e 1 cb 03 b" Accept-Ranges: bytes Content-Length: 438 Connection: close Content-Type: text/html; charset=UTF-8 <CRLF> data… ETag (entity tag) n n Accept-Ranges: bytes n n Specifies the Internet media type of the data conveyed by the http message Connection: close n n Size of the response Content-Type n n Useful if the connection was interrupted before the data was completely transferred to the client Content-Length indicates its length in bytes. n n Used to determine if the URL cached is identical to the requested URL on the server. Webserver will close the TCP connection immediately after the transfer of this package. Server response n Follows a blank line <CRLF>, this is the response n Hopefully the requested page

Which code response range indicates a client error? A. 1 xx B. 2 xx

Which code response range indicates a client error? A. 1 xx B. 2 xx C. 3 xx D. 4 xx E. 5 xx Has 30 second countdown

Which code response indicates success: A. 101 B. 200 C. 302 D. 403 E.

Which code response indicates success: A. 101 B. 200 C. 302 D. 403 E. 404 F. 503 Has 30 second countdown