Skip to content

Commit c67f8b8

Browse files
authored
Merge pull request #2388 from RapTho/patch-4
small reformulations
2 parents e730c5a + b9481da commit c67f8b8

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

5-network/05-fetch-crossorigin/article.md

+25-25
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Seriously. Let's make a very brief historical digression.
2828

2929
**For many years a script from one site could not access the content of another site.**
3030

31-
That simple, yet powerful rule was a foundation of the internet security. E.g. an evil script from website `hacker.com` could not access user's mailbox at website `gmail.com`. People felt safe.
31+
That simple, yet powerful rule was a foundation of the internet security. E.g. an evil script from website `hacker.com` could not access the user's mailbox at website `gmail.com`. People felt safe.
3232

3333
JavaScript also did not have any special methods to perform network requests at that time. It was a toy language to decorate a web page.
3434

@@ -125,15 +125,15 @@ Contrary to that, requests with non-standard headers or e.g. method `DELETE` can
125125
126126
When we try to make a unsafe request, the browser sends a special "preflight" request that asks the server -- does it agree to accept such cross-origin requests, or not?
127127
128-
And, unless the server explicitly confirms that with headers, a unsafe request is not sent.
128+
And, unless the server explicitly confirms that with headers, an unsafe request is not sent.
129129
130130
Now we'll go into details.
131131

132132
## CORS for safe requests
133133

134-
If a request is cross-origin, the browser always adds `Origin` header to it.
134+
If a request is cross-origin, the browser always adds the `Origin` header to it.
135135

136-
For instance, if we request `https://anywhere.com/request` from `https://javascript.info/page`, the headers will be like:
136+
For instance, if we request `https://anywhere.com/request` from `https://javascript.info/page`, the headers will look like:
137137

138138
```http
139139
GET /request
@@ -144,9 +144,9 @@ Origin: https://javascript.info
144144
...
145145
```
146146

147-
As you can see, `Origin` header contains exactly the origin (domain/protocol/port), without a path.
147+
As you can see, the `Origin` header contains exactly the origin (domain/protocol/port), without a path.
148148

149-
The server can inspect the `Origin` and, if it agrees to accept such a request, adds a special header `Access-Control-Allow-Origin` to the response. That header should contain the allowed origin (in our case `https://javascript.info`), or a star `*`. Then the response is successful, otherwise an error.
149+
The server can inspect the `Origin` and, if it agrees to accept such a request, add a special header `Access-Control-Allow-Origin` to the response. That header should contain the allowed origin (in our case `https://javascript.info`), or a star `*`. Then the response is successful, otherwise it's an error.
150150
151151
The browser plays the role of a trusted mediator here:
152152
1. It ensures that the correct `Origin` is sent with a cross-origin request.
@@ -182,7 +182,7 @@ There's no `Content-Length` header in the list!
182182
This header contains the full response length. So, if we're downloading something and would like to track the percentage of progress, then an additional permission is required to access that header (see below).
183183
```
184184

185-
To grant JavaScript access to any other response header, the server must send `Access-Control-Expose-Headers` header. It contains a comma-separated list of unsafe header names that should be made accessible.
185+
To grant JavaScript access to any other response header, the server must send the `Access-Control-Expose-Headers` header. It contains a comma-separated list of unsafe header names that should be made accessible.
186186

187187
For example:
188188

@@ -197,17 +197,17 @@ Access-Control-Expose-Headers: Content-Length,API-Key
197197
*/!*
198198
```
199199

200-
With such `Access-Control-Expose-Headers` header, the script is allowed to read `Content-Length` and `API-Key` headers of the response.
200+
With such an `Access-Control-Expose-Headers` header, the script is allowed to read the `Content-Length` and `API-Key` headers of the response.
201201

202202
## "Unsafe" requests
203203

204204
We can use any HTTP-method: not just `GET/POST`, but also `PATCH`, `DELETE` and others.
205205

206206
Some time ago no one could even imagine that a webpage could make such requests. So there may still exist webservices that treat a non-standard method as a signal: "That's not a browser". They can take it into account when checking access rights.
207207

208-
So, to avoid misunderstandings, any "unsafe" request -- that couldn't be done in the old times, the browser does not make such requests right away. Before it sends a preliminary, so-called "preflight" request, asking for permission.
208+
So, to avoid misunderstandings, any "unsafe" request -- that couldn't be done in the old times, the browser does not make such requests right away. First, it sends a preliminary, so-called "preflight" request, to ask for permission.
209209
210-
A preflight request uses method `OPTIONS`, no body and two headers:
210+
A preflight request uses the method `OPTIONS`, no body and two headers:
211211
212212
- `Access-Control-Request-Method` header has the method of the unsafe request.
213213
- `Access-Control-Request-Headers` header provides a comma-separated list of its unsafe HTTP-headers.
@@ -221,7 +221,7 @@ If the server agrees to serve the requests, then it should respond with empty bo
221221

222222
![](xhr-preflight.svg)
223223

224-
Let's see how it works step-by-step on example, for a cross-origin `PATCH` request (this method is often used to update data):
224+
Let's see how it works step-by-step on the example of a cross-origin `PATCH` request (this method is often used to update data):
225225
226226
```js
227227
let response = await fetch('https://site.com/service.json', {
@@ -240,7 +240,7 @@ There are three reasons why the request is unsafe (one is enough):
240240
241241
### Step 1 (preflight request)
242242
243-
Prior to sending such request, the browser, on its own, sends a preflight request that looks like this:
243+
Prior to sending such a request, the browser, on its own, sends a preflight request that looks like this:
244244
245245
```http
246246
OPTIONS /service.json
@@ -259,14 +259,14 @@ Access-Control-Request-Headers: Content-Type,API-Key
259259
260260
### Step 2 (preflight response)
261261
262-
The server should respond with status 200 and headers:
262+
The server should respond with status 200 and the headers:
263263
- `Access-Control-Allow-Origin: https://javascript.info`
264264
- `Access-Control-Allow-Methods: PATCH`
265265
- `Access-Control-Allow-Headers: Content-Type,API-Key`.
266266

267267
That allows future communication, otherwise an error is triggered.
268268

269-
If the server expects other methods and headers in the future, it makes sense to allow them in advance by adding to the list.
269+
If the server expects other methods and headers in the future, it makes sense to allow them in advance by adding them to the list.
270270

271271
For example, this response also allows `PUT`, `DELETE` and additional headers:
272272

@@ -280,13 +280,13 @@ Access-Control-Max-Age: 86400
280280

281281
Now the browser can see that `PATCH` is in `Access-Control-Allow-Methods` and `Content-Type,API-Key` are in the list `Access-Control-Allow-Headers`, so it sends out the main request.
282282

283-
If there's header `Access-Control-Max-Age` with a number of seconds, then the preflight permissions are cached for the given time. The response above will be cached for 86400 seconds (one day). Within this timeframe, subsequent requests will not cause a preflight. Assuming that they fit the cached allowances, they will be sent directly.
283+
If there's the header `Access-Control-Max-Age` with a number of seconds, then the preflight permissions are cached for the given time. The response above will be cached for 86400 seconds (one day). Within this timeframe, subsequent requests will not cause a preflight. Assuming that they fit the cached allowances, they will be sent directly.
284284
285285
### Step 3 (actual request)
286286
287-
When the preflight is successful, the browser now makes the main request. The algorithm here is the same as for safe requests.
287+
When the preflight is successful, the browser now makes the main request. The process here is the same as for safe requests.
288288
289-
The main request has `Origin` header (because it's cross-origin):
289+
The main request has the `Origin` header (because it's cross-origin):
290290

291291
```http
292292
PATCH /service.json
@@ -316,7 +316,7 @@ JavaScript only gets the response to the main request or an error if there's no
316316

317317
A cross-origin request initiated by JavaScript code by default does not bring any credentials (cookies or HTTP authentication).
318318

319-
That's uncommon for HTTP-requests. Usually, a request to `http://site.com` is accompanied by all cookies from that domain. But cross-origin requests made by JavaScript methods are an exception.
319+
That's uncommon for HTTP-requests. Usually, a request to `http://site.com` is accompanied by all cookies from that domain. Cross-origin requests made by JavaScript methods on the other hand are an exception.
320320
321321
For example, `fetch('http://another.com')` does not send any cookies, even those (!) that belong to `another.com` domain.
322322

@@ -362,12 +362,12 @@ From the browser point of view, there are two kinds of cross-origin requests: "s
362362

363363
The essential difference is that safe requests were doable since ancient times using `<form>` or `<script>` tags, while unsafe were impossible for browsers for a long time.
364364

365-
So, the practical difference is that safe requests are sent right away, with `Origin` header, while for the other ones the browser makes a preliminary "preflight" request, asking for permission.
365+
So, the practical difference is that safe requests are sent right away, with the `Origin` header, while for the other ones the browser makes a preliminary "preflight" request, asking for permission.
366366

367367
**For safe requests:**
368368

369-
- → The browser sends `Origin` header with the origin.
370-
- ← For requests without credentials (not sent default), the server should set:
369+
- → The browser sends the `Origin` header with the origin.
370+
- ← For requests without credentials (not sent by default), the server should set:
371371
- `Access-Control-Allow-Origin` to `*` or same value as `Origin`
372372
- ← For requests with credentials, the server should set:
373373
- `Access-Control-Allow-Origin` to same value as `Origin`
@@ -377,11 +377,11 @@ Additionally, to grant JavaScript access to any response headers except `Cache-C
377377

378378
**For unsafe requests, a preliminary "preflight" request is issued before the requested one:**
379379

380-
- → The browser sends `OPTIONS` request to the same URL, with headers:
380+
- → The browser sends an `OPTIONS` request to the same URL, with the headers:
381381
- `Access-Control-Request-Method` has requested method.
382382
- `Access-Control-Request-Headers` lists unsafe requested headers.
383-
- ← The server should respond with status 200 and headers:
383+
- ← The server should respond with status 200 and the headers:
384384
- `Access-Control-Allow-Methods` with a list of allowed methods,
385385
- `Access-Control-Allow-Headers` with a list of allowed headers,
386-
- `Access-Control-Max-Age` with a number of seconds to cache permissions.
387-
- Then the actual request is sent, the previous "safe" scheme is applied.
386+
- `Access-Control-Max-Age` with a number of seconds to cache the permissions.
387+
- Then the actual request is sent, and the previous "safe" scheme is applied.

0 commit comments

Comments
 (0)