HTTP is a client-server protocol for transmitting hypermedia documents across the internet. It uses a request-response paradigm where clients make requests which are answered by HTTP servers. Requests use methods like GET and POST, and include headers. Responses contain status lines, headers, and content. HTTP allows caching, cookies, authentication, and redirects. It is the foundation of data communication for the World Wide Web via the hypertext transfer protocol.
Introduction to HTTP
AviranMordo
Head Of Back-End Engineering @ Wix
@aviranm
http://www.linkedin.com/in/aviran
http://www.aviransplace.com
2.
Intro
• The HyperText Transfer Protocol (HTTP) is a client-server
network protocol
• In use by the World-Wide Web since 1990.
• It is based on Request – Response Paradigm.
3.
HTTP Request Message
HTTP
Method
relativeURL of the resource
or a full URL
HTTP
Version
GET /website/template/photography/
HTTP/1.1
Accept:*/*
Accept-Language: en-gb
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible;
MSIE 6.0)
Host: www.httpwatch.com
Connection: Keep-Alive
Head
ers
Everything is anextendable convention
• You may create your own headers
• You may create your own methods
• You may not implement some methods
• You may not implement header logic
6.
HTTP Methods
• GETmethod means retrieve whatever information. Is identified
by the Request-URI
• POST - used to send data to the server for updates.
• PUT - method requests that the enclosed entity be stored under
the supplied Request-URI.
• DELETE - requests that the origin server delete the resource
identified by the Request-URI.
• HEAD - identical to GET except that the server MUST NOT
return a message-body in the response.
• TRACE - llows the client to see what is being received at the
other end of the request chain and use that data for testing or
7.
HTTP Headers
• Accept:text/plain; - specify certain media types which are acceptable for the response.
• Accept-Encoding: compress, gzip ;
• Accept-Language: da, en-gb;q=0.8, en;q=0.7 ;
• Cache-Control: max-age=0, must-revalidate
• Vary: User-Agent,Accept-Encoding
• ETag: "xyzzy"
• Set-Cookie: _wixAB2=15361#2985#2014-08-05T13-30-00.000-0500
• Access-Control-Allow-Origin: *
• X-Seen-By: sputnik3.aus_dsp
• X-Wix-Dispatcher-Cache-Hit: no
• X-Wix-Renderer-Server: apu2.aus.wixpress.com
• X-Wix-Not-Found-Reason: Meta Site was not found
8.
HTTP Status Codesand Errors
• 1xx – Informational - intermediate response and indicates that the server
has received the request but has not finished processing it.
• 2xx – Successful: 200 OK
• 3xx – Redirection: 301-permanent, 302-temporary
• 4xx - Client Error: 400-bad request, 403-forbidden, 404-not found, 418
I'm a teapot
• 5xx - Server Error: 500 Internal Server Error, 503-Service
Unavailable,504-Gateway Timeout
9.
Cookies
Servers supply cookiesby populating the set-cookie
response header with the following details: Set-Cookie: name=value
Name Name of the cookie
Value Textual value to be held by the cookie
Expire Date/time when the cookie should be discarded by the browser.
s
If this field is empty the cookie expires at the end of the current browser session. This field can
also be used to delete a cookie by setting a date/time in the past.
Path Path below which the cookie should be supplied by the browser.
Domai Web site domain to which this cookie applies.
n
This will default to the current domain and attempts to set cookies on other domains are subject
to the privacy controls built into the browser.
10.
Type of cookies- Terminology
• Session cookie - Web browsers normally delete session cookies when the user closes
the browser
• Persistent cookie - A persistent cookie will outlast user sessions – expires on a set
timestamp
• Secure cookie - A secure cookie has the secure attribute enabled and is only used via
HTTPS
• HttpOnly cookie - On a supported browser, only when transmitting HTTP (or HTTPS)
requests. Not accessible to Javascript.
• Third-party cookie - cookies that belong to domains different from the one shown in the
address bar.
Browsers send cookies via request header. Cookie: name=value; name2=value2
• Supercookie - cookie with an origin of a TLD(such as .com)
11.
HTTP Caching
• PreventingCaching - Cache-Control: no-cache (HTTP 1.1);
Pragma: no-cache (HTTP 1.0)
• Last-Modified: Wed, 15 Sep 2004 12:00:00 GMT – Browser can
check the server for changes
• GET /images/logo.gif HTTP/1.1 Jan 2038 19:14:07 GMT - browser can reuse
Expires: Sun, 17
Accept: */*
the content without having to check the server
Referer: http://www.google.com/
Accept-Encoding: gzip, deflate
If-Modified-Since: Thu, 23 Sep 2004 17:42:04 GMT
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;)
Host: www.google.com
HTTP/1.1 304 Not Modified
Content-Type: text/html
Server: GWS/2.1
Content-Length: 0
Date: Thu, 04 Oct 2004 12:00:00 GMT
12.
HTTP Methods
• GET- method is used to retrieve information from a specified
URI and is assumed to be a safe, have no side effects
repeatable operation by browsers, caches and other HTTP
aware components.
• GET can only supply data in the form of parameters encoded in the
URI (Query String) or as cookies. Therefore, GET cannot be used for
uploading files or other operations that require large amounts of data to
be sent to the server.
• POST method is used for operations that have side effects and
cannot be safely repeated.
• POST request message has a content body that is normally used to
send parameters and data. Unlike using the request URI or cookies,
there is no upper limit on the amount of data that can be sent
Redirection
• HTTP allowsservers to redirect a client request to a different
location. Although, this will usually result in another network
30 round trip
Permanent redirect – Content move permanently at the location specified by the Location header
1 and future requests should be directed to this location.
30 Temporary Redirect - future requests should still be sent to the original location
2
30 This status code was intended to be the only status code that caused a POST to be converted to a
3 GET. However, most browsers treat a 302 like a 303.
30 Not modified - Used in response to an If-Modified header to redirect a request to the browser's local
4HTTP/1.1 302 Found
cache.
Cache-Control: private,Public
Content-Length: 162
Content-Type: text/html; charset=utf-8
Location: /httpgallery/redirection/default.aspx#example
Set-Cookie: balance=990; path=/httpgallery/redirection/
15.
HTTPS
• The SecureSockets Layer (SSL) was designed to encrypt any
TCP/IP based network traffic and provide the following
capabilities
• Prevents eavesdropping
• Prevents tampering or replaying of messages
• Uses certificates to authenticate servers and optionally clients
• The HTTPS protocol is the same text based protocol as HTTP
but is run over an encrypted SSL session.
16.
AJAX
XML
- Asynchronous JavaScriptAnd
• AJAX is a technology used by interactive web applications to
make HTTP requests to a server without causing page
transitions.
• Requests have to go to the same domain as the page
<script type="text/javascript">
function GetShoppingList()
{
// Create an instance of the HTTP request object
var xmlHttp = new XMLHttpRequest();
// Specify HTTP GET by default and supply the relative url
xmlHttp.open("GET", "getlist.aspx", false);
// Start a synchronous AJAX request and wait for the response
xmlHttp.send(null);
var targetNode = document.getElementById("divShoppingList")
;
// Use the HTML returned from server to create list
targetNode.innerHTML = xmlHttp.responseText;
}
17.
Cross-origin resource sharing(CORS)
• Allows JavaScript on a web page to make XMLHttpRequests to
another domain
• "preflighted" requests first send an HTTP OPTIONS method
OPTIONS /resources/post-here/ HTTP/1.1 on the other domain, to determine if the
request to the resource
Host: bar.other
request is safe to send.
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130
Minefield/3.1b3pre
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Connection: keep-alive
Origin: http://foo.example
Access-Control-Request-Method: POST
Access-Control-Request-Headers: X-PINGOTHER
HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 01:15:39 GMT
Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: X-PINGOTHER
Access-Control-Max-Age: 1728000
POST /resources/post-here/ HTTP/1.1
Host: bar.other
…
18.
JSONP - "JSONwith padding"
• Provides a method to request data from a server in a different
domain
• JSONP has nothing to do with Ajax, since it does not use
XMLHttpRequest. Instead it dynamically inserts
Step 1 - You need to create a callback function. The function accepts
some data.
function w3r_callback(data){
console.log(data);
}
Step 2 - Include a script in your web page which contains the callback
function created a step 1 as a parameter
<script src="http://www.example.com?q=w3r_callback"><script>
Step 3 - It outputs a script which calls the function
and requested data is passed
w3r_callback({
"FirstName" : "xyz",
"LastName" : "abc",
"Grade" : "A"
}
);