Foreword |
|
xv | |
Preface |
|
xvii | |
Acknowledgments |
|
xix | |
About This Book |
|
xxi | |
Author Online |
|
xxv | |
About The Author |
|
xxvi | |
About The Cover Illustration |
|
xxvii | |
Part 1 Introducing CORS |
|
1 | (36) |
|
|
3 | (9) |
|
|
4 | (1) |
|
|
5 | (4) |
|
|
7 | (1) |
|
|
7 | (1) |
|
|
7 | (2) |
|
|
9 | (1) |
|
|
9 | (1) |
|
|
9 | (1) |
|
|
10 | (1) |
|
|
10 | (1) |
|
Reduced maintenance overhead |
|
|
10 | (1) |
|
|
10 | (2) |
|
|
12 | (25) |
|
2.1 What is a cross-origin request? |
|
|
13 | (1) |
|
2.2 Browser support for CORS |
|
|
14 | (1) |
|
2.3 Using the XMLHttpRequest object |
|
|
15 | (13) |
|
|
17 | (3) |
|
Handling the HTTP response |
|
|
20 | (7) |
|
Including cookies on cross-origin requests |
|
|
27 | (1) |
|
2.4 XDomainRequest object in Internet Explorer 8 and 9 |
|
|
28 | (4) |
|
Differences between XDomainRequest and XMLHttpRequest |
|
|
30 | (2) |
|
2.5 Canvas and cross-origin images |
|
|
32 | (2) |
|
2.6 CORS requests from jQuery |
|
|
34 | (2) |
|
|
36 | (1) |
Part 2 CORS On The Server |
|
37 | (112) |
|
|
39 | (24) |
|
3.1 Setting up the sample code |
|
|
40 | (5) |
|
Setting up the sample API |
|
|
40 | (2) |
|
Setting up the sample client |
|
|
42 | (2) |
|
|
44 | (1) |
|
3.2 Making a CORS request |
|
|
45 | (2) |
|
3.3 Anatomy of a CORS request |
|
|
47 | (4) |
|
The players in a CORS request |
|
|
48 | (1) |
|
Lifecycle of a CORS request |
|
|
49 | (2) |
|
3.4 Making a request with the Origin header |
|
|
51 | (4) |
|
Viewing the Origin header |
|
|
51 | (1) |
|
|
52 | (3) |
|
Setting the Origin header |
|
|
55 | (1) |
|
3.5 Responding to a CORS request |
|
|
55 | (7) |
|
The Access-Control-Allow-Origin header |
|
|
55 | (1) |
|
Access-Control-Allow-Origin with a wildcard (*) value |
|
|
56 | (3) |
|
Access-Control-Allow-Origin with an origin value |
|
|
59 | (1) |
|
|
59 | (3) |
|
|
62 | (1) |
|
4 Handling preflight requests |
|
|
63 | (31) |
|
4.1 What is a preflight request? |
|
|
64 | (3) |
|
Lifecycle of a preflight request |
|
|
64 | (1) |
|
Why does the preflight request exist? |
|
|
64 | (3) |
|
4.2 Triggering a preflight request |
|
|
67 | (5) |
|
When is a preflight request sent? |
|
|
71 | (1) |
|
4.3 Identifying a preflight request |
|
|
72 | (3) |
|
|
72 | (1) |
|
|
73 | (1) |
|
Access-Control-Request-Method header |
|
|
74 | (1) |
|
|
75 | (1) |
|
4.4 Responding to a preflight request |
|
|
75 | (10) |
|
Supporting HTTP methods with Access-Control-Allow-Methods |
|
|
76 | (2) |
|
Supporting request headers with Access-Control-Allow-Headers |
|
|
78 | (5) |
|
Sending the actual request |
|
|
83 | (1) |
|
Rejecting a preflight request |
|
|
83 | (2) |
|
|
85 | (5) |
|
4.6 Preflight result cache |
|
|
90 | (3) |
|
|
93 | (1) |
|
5 Cookies and response headers |
|
|
94 | (18) |
|
5.1 Supporting cookies in CORS requests |
|
|
95 | (12) |
|
Setting cookies with a login page |
|
|
95 | (2) |
|
Reading the cookie on the server |
|
|
97 | (2) |
|
Including cookies in CORS requests |
|
|
99 | (3) |
|
How withCredentials and Access-Control-Allow-Credentials interact |
|
|
102 | (2) |
|
Caveats to cookie support |
|
|
104 | (3) |
|
5.2 Exposing response headers to the client |
|
|
107 | (4) |
|
Reading a response header |
|
|
108 | (1) |
|
Adding response header support |
|
|
108 | (3) |
|
|
111 | (1) |
|
|
112 | (37) |
|
6.1 Refactoring the sample code |
|
|
113 | (1) |
|
|
114 | (1) |
|
6.3 Setting the Access-Control-Allow-Origin header |
|
|
114 | (12) |
|
Allowing cross-origin access for everyone |
|
|
115 | (1) |
|
Limiting CORS requests to a set of origins |
|
|
116 | (5) |
|
|
121 | (2) |
|
|
123 | (1) |
|
Origin header on same-origin requests |
|
|
124 | (2) |
|
|
126 | (9) |
|
Including cookies on requests |
|
|
128 | (4) |
|
Authorizing requests using OAuth2 |
|
|
132 | (3) |
|
6.5 Handling preflight requests |
|
|
135 | (4) |
|
Whitelisting request methods and headers |
|
|
135 | (4) |
|
6.6 Reducing preflight requests |
|
|
139 | (3) |
|
Maximizing the preflight cache |
|
|
139 | (2) |
|
Changing your site to reduce preflight requests |
|
|
141 | (1) |
|
6.7 Exposing response headers |
|
|
142 | (2) |
|
|
144 | (3) |
|
|
147 | (2) |
Part 3 Debugging CORS Requests |
|
149 | (29) |
|
7 Debugging COBS requests |
|
|
151 | (27) |
|
|
152 | (1) |
|
7.2 Using the browser's developer tools |
|
|
153 | (5) |
|
|
155 | (1) |
|
|
155 | (3) |
|
7.3 Monitoring network traffic |
|
|
158 | (7) |
|
|
159 | (2) |
|
|
161 | (4) |
|
7.4 Using curl to simulate CORS requests |
|
|
165 | (4) |
|
Making CORS requests using curl |
|
|
165 | (2) |
|
Making preflight requests using curl |
|
|
167 | (1) |
|
|
168 | (1) |
|
7.5 Sending requests using test-cors.org |
|
|
169 | (5) |
|
Sending requests to a remote server |
|
|
169 | (2) |
|
Sending requests to the local server |
|
|
171 | (2) |
|
Understanding how the client works |
|
|
173 | (1) |
|
7.6 Tips for mobile debugging |
|
|
174 | (2) |
|
Log requests on the server |
|
|
175 | (1) |
|
|
175 | (1) |
|
Use remote debugging tools |
|
|
175 | (1) |
|
|
175 | (1) |
|
|
176 | (1) |
|
|
177 | (1) |
Appendixes |
|
178 | (28) |
|
|
178 | (4) |
|
|
178 | (2) |
|
|
178 | (1) |
|
|
178 | (2) |
|
A.2 Other terms used in CORS |
|
|
180 | (2) |
|
|
180 | (1) |
|
|
180 | (1) |
|
|
181 | (1) |
|
B Configuring your environment |
|
|
182 | (9) |
|
B.1 Setting up for the sample application |
|
|
182 | (5) |
|
|
182 | (4) |
|
|
186 | (1) |
|
|
187 | (3) |
|
|
187 | (2) |
|
|
189 | (1) |
|
|
189 | (1) |
|
|
190 | (1) |
|
|
191 | (8) |
|
|
191 | (4) |
|
C.2 Implementing CSRF protection for same-origin requests |
|
|
195 | (4) |
|
D Other cross-origin techniques |
|
|
199 | (7) |
|
|
199 | (2) |
|
|
201 | (2) |
|
D.3 postMessage and easyXDM |
|
|
203 | (1) |
|
|
204 | (2) |
Index |
|
206 | |