Simply defined as a access control security restriction that is used to limit cross-domain communication based on the origin of a web site or app.
The key word here is Access Control - which is about authorization of http requests
#Thread \1
1. A Scheme /Protocol( http / https )
2. A Host/Sub+Top Level Domain ( google.com )
Ok ? Cool!
Now you ask
How does this CORS thing actually work ?
#Thread \2
e.g. accounts.google.com ---> makes request to ----> mail.google.com. How is that request handled ?
Well the first question to ask: is this a cross domain request or not ?
Yes... It's a cross domain request
#Thread \3
1. accounts.google.com
2. mail.google.com
"accounts" and "mail" are not the same
But just because it's cross domain doesn't mean it will be CORS worthy
#Thread \4
Why ? Because being a cross domain is not enough to trigger restrictions
There are as i said earlier 3 rules which you must keep in mind.
#Thread \6
RULE 1:
The request is not a GET, HEAD or POST
Simple and straight-forward huh ? Yeah!!
#Thread \7
When CORS is triggered, an OPTIONS (pre-flight) request is fired first before the main request.
#Thread \8
Access-Control-Request-* request headers
Access-Control-Allow-* response headers
We'll talk more about these exchanges in detail later on.
#Thread \9
If the request method is not GET or HEAD and includes a
"Content-Type" request header. The Content-Type header value is not any of these 3 values
1. text/plain
2. application/x-www-form-urlencoded
3. multipart/form-data
1. application/json
2. application/graphql
As opposed to
1. application/x-www-form-urlencoded
2. multipart/form-data
3. text/plain
#Thread \12
RULE 3:
Special/Custom request header(s) like "Authorization" or "X-Requested-WIth" is set
#Thread \13
It means that all 3 rules MUST be obeyed!
#Thread \14
#Thread \15
Then, setup the right Access-Control HTTP request headers
#Thread \16
#Thread \17
For example:
http-request-from-browser:
Access-Control-Request-Headers: Authorization
http-response-from-server:
Access-Control-Allow-Headers: Authorization
#Thread \18