Skip to content

Commit a7ee1e2

Browse files
authored
Merge pull request vert-x3#256 from FlamingTuri/master
fixed nopreflight.html, improved Server.java and preflight.html implementation
2 parents 494b14f + e75f2a6 commit a7ee1e2

File tree

4 files changed

+114
-143
lines changed

4 files changed

+114
-143
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
trim_trailing_whitespace = true
8+
end_of_line = lf
9+
insert_final_newline = true

web-examples/src/main/java/io/vertx/example/web/cors/Server.java

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
import io.vertx.core.AbstractVerticle;
44
import io.vertx.core.MultiMap;
55
import io.vertx.core.http.HttpMethod;
6+
import io.vertx.core.http.HttpServerResponse;
67
import io.vertx.example.util.Runner;
78
import io.vertx.ext.web.Router;
89
import io.vertx.ext.web.handler.CorsHandler;
910
import io.vertx.ext.web.handler.StaticHandler;
1011

1112
import java.util.Map;
13+
import java.util.HashSet;
14+
import java.util.Set;
1215

1316
/*
1417
* @author <a href="mailto:[email protected]">Paulo Lopes</a>
18+
* reviewed by: Giacomo Venturini mail: [email protected]"
1519
*/
1620
public class Server extends AbstractVerticle {
1721

@@ -25,38 +29,50 @@ public void start() throws Exception {
2529

2630
Router router = Router.router(vertx);
2731

28-
router.route().handler(CorsHandler.create("*")
29-
.allowedMethod(HttpMethod.GET)
30-
.allowedMethod(HttpMethod.POST)
31-
.allowedMethod(HttpMethod.OPTIONS)
32-
.allowedHeader("X-PINGARUNER")
33-
.allowedHeader("Content-Type"));
32+
Set<String> allowedHeaders = new HashSet<>();
33+
allowedHeaders.add("x-requested-with");
34+
allowedHeaders.add("Access-Control-Allow-Origin");
35+
allowedHeaders.add("origin");
36+
allowedHeaders.add("Content-Type");
37+
allowedHeaders.add("accept");
38+
allowedHeaders.add("X-PINGARUNER");
39+
40+
Set<HttpMethod> allowedMethods = new HashSet<>();
41+
allowedMethods.add(HttpMethod.GET);
42+
allowedMethods.add(HttpMethod.POST);
43+
allowedMethods.add(HttpMethod.OPTIONS);
44+
/*
45+
* these methods aren't necessary for this sample,
46+
* but you may need them for your projects
47+
*/
48+
allowedMethods.add(HttpMethod.DELETE);
49+
allowedMethods.add(HttpMethod.PATCH);
50+
allowedMethods.add(HttpMethod.PUT);
51+
52+
router.route().handler(CorsHandler.create("*").allowedHeaders(allowedHeaders).allowedMethods(allowedMethods));
3453

3554
router.get("/access-control-with-get").handler(ctx -> {
36-
37-
ctx.response().setChunked(true);
38-
55+
HttpServerResponse httpServerResponse = ctx.response();
56+
httpServerResponse.setChunked(true);
3957
MultiMap headers = ctx.request().headers();
4058
for (String key : headers.names()) {
41-
ctx.response().write(key);
42-
ctx.response().write(headers.get(key));
43-
ctx.response().write("\n");
59+
httpServerResponse.write(key + ": ");
60+
httpServerResponse.write(headers.get(key));
61+
httpServerResponse.write("<br>");
4462
}
45-
46-
ctx.response().end();
63+
httpServerResponse.putHeader("Content-Type", "application/text").end("Success");
4764
});
4865

4966
router.post("/access-control-with-post-preflight").handler(ctx -> {
50-
ctx.response().setChunked(true);
51-
67+
HttpServerResponse httpServerResponse = ctx.response();
68+
httpServerResponse.setChunked(true);
5269
MultiMap headers = ctx.request().headers();
5370
for (String key : headers.names()) {
54-
ctx.response().write(key);
55-
ctx.response().write(headers.get(key));
56-
ctx.response().write("\n");
71+
httpServerResponse.write(key + ": ");
72+
httpServerResponse.write(headers.get(key));
73+
httpServerResponse.write("<br>");
5774
}
58-
59-
ctx.response().end();
75+
httpServerResponse.putHeader("Content-Type", "application/text").end("Success");
6076
});
6177

6278
// Serve the static resources

web-examples/src/main/java/io/vertx/example/web/cors/webroot/nopreflight.html

Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,41 @@
22
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
33
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
44
<head>
5-
<title>Simple use of Cross-Site XMLHttpRequest (Using Access Control)</title>
6-
<script type="text/javascript">
7-
//<![CDATA[
5+
<title>Simple use of Cross-Site XMLHttpRequest (Using Access Control)</title>
6+
<script type="text/javascript">
7+
var url = 'http://localhost:8080/access-control-with-get/';
88

9-
var invocation = new XMLHttpRequest();
10-
var url = 'http://localhost:8080/access-control-with-get/';
11-
var invocationHistoryText;
12-
13-
function callOtherDomain(){
14-
if(invocation)
15-
{
16-
invocation.open('GET', url, true);
17-
invocation.onreadystatechange = handler;
18-
invocation.send();
19-
}
20-
else
21-
{
22-
invocationHistoryText = "No Invocation TookPlace At All";
23-
var textNode = document.createTextNode(invocationHistoryText);
24-
var textDiv = document.getElementById("textDiv");
25-
textDiv.appendChild(textNode);
26-
}
27-
28-
}
29-
function handler(evtXHR)
30-
{
31-
if (invocation.readyState == 4)
32-
{
33-
if (invocation.status == 200)
34-
{
35-
var response = invocation.responseXML;
36-
var invocationHistory = response.getElementsByTagName('invocationHistory').item(0).firstChild.data;
37-
invocationHistoryText = document.createTextNode(invocationHistory);
38-
var textDiv = document.getElementById("textDiv");
39-
textDiv.appendChild(invocationHistoryText);
40-
41-
}
42-
else
43-
alert("Invocation Errors Occured");
44-
}
45-
else
46-
dump("currently the application is at" + invocation.readyState);
47-
}
48-
//]]>
49-
50-
51-
</script>
9+
function callOtherDomain() {
10+
var xhttp = new XMLHttpRequest();
11+
xhttp.onreadystatechange = function() {
12+
if (this.readyState == 4 && this.status == 200) {
13+
var e = document.createElement('p');
14+
e.innerHTML = xhttp.responseText;
15+
document.getElementById("textDiv").appendChild(e);
16+
} else {
17+
console.log("XMLHttpRequest readyState:" + this.readyState
18+
+ " status: " + this.status);
19+
}
20+
};
21+
xhttp.open("GET", url, true);
22+
xhttp.send();
23+
}
24+
</script>
5225
</head>
5326
<body>
54-
<form id="controlsToInvoke" action="">
55-
<p>
56-
<input type="button" value="Click to Invoke Another Site" onclick="callOtherDomain()" />
57-
</p>
58-
</form>
59-
<p id="intro">
60-
This page basically makes invocations to another domain using cross-site XMLHttpRequest mitigated by Access Control. This is the simple scenario that is <em>NOT</em> preflighted, and the invocation to a resource on another domain takes place using a simple HTTP GET.
61-
</p>
62-
<div id="textDiv">
63-
This XHTML document invokes another resource using cross-site XHR.
64-
</div>
27+
<form id="controlsToInvoke" action="">
28+
<p>
29+
<input type="button" value="Click to Invoke Another Site"
30+
onclick="callOtherDomain()" />
31+
</p>
32+
</form>
33+
<p id="intro">
34+
This page basically makes invocations to another domain using
35+
cross-site XMLHttpRequest mitigated by Access Control. This is the
36+
simple scenario that is <em>NOT</em> preflighted, and the invocation
37+
to a resource on another domain takes place using a simple HTTP GET.
38+
</p>
39+
<div id="textDiv">This XHTML document invokes another resource
40+
using cross-site XHR.</div>
6541
</body>
6642
</html>

web-examples/src/main/java/io/vertx/example/web/cors/webroot/preflight.html

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,43 @@
22
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
33
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
44
<head>
5-
<title>Simple use of Cross-Site XMLHttpRequest (Using Access Control)</title>
6-
<script type="text/javascript">
7-
//<![CDATA[
5+
<title>Simple use of Cross-Site XMLHttpRequest (Using Access Control)</title>
6+
<script type="text/javascript">
7+
var url = 'http://localhost:8080/access-control-with-post-preflight/';
88

9-
var invocation = new XMLHttpRequest();
10-
var url = 'http://localhost:8080/access-control-with-post-preflight/';
11-
var invocationHistoryText;
12-
var body = '<?xml version="1.0"?><person><name>Arun</name></person>';
13-
14-
function callOtherDomain(){
15-
if(invocation)
16-
{
17-
invocation.open('POST', url, true);
18-
invocation.setRequestHeader('X-PINGARUNER', 'pingpong');
19-
invocation.setRequestHeader('Content-Type',
20-
'application/xml');
21-
invocation.onreadystatechange = handler;
22-
invocation.send(body);
23-
}
24-
else
25-
{
26-
invocationHistoryText = "No Invocation TookPlace At All";
27-
var textNode = document.createTextNode(invocationHistoryText);
28-
var textDiv = document.getElementById("textDiv");
29-
textDiv.appendChild(textNode);
30-
}
31-
32-
}
33-
function handler(evtXHR)
34-
{
35-
if (invocation.readyState == 4)
36-
{
37-
if (invocation.status == 200)
38-
{
39-
var response = invocation.responseText;
40-
//var invocationHistory = response.getElementsByTagName('invocationHistory').item(0).firstChild.data;
41-
invocationHistoryText = document.createTextNode(response);
42-
var textDiv = document.getElementById("textDiv");
43-
textDiv.appendChild(invocationHistoryText);
44-
45-
}
46-
else
47-
{
48-
alert("Invocation Errors Occured " + invocation.readyState + " and the status is " + invocation.status);
49-
}
50-
}
51-
else
52-
{
53-
dump("currently the application is at" + invocation.readyState);
54-
}
55-
}
56-
//]]>
57-
58-
59-
</script>
9+
function callOtherDomain() {
10+
var xhttp = new XMLHttpRequest();
11+
xhttp.onreadystatechange = function() {
12+
if (this.readyState == 4 && this.status == 200) {
13+
var e = document.createElement('p');
14+
e.innerHTML = xhttp.responseText;
15+
document.getElementById("textDiv").appendChild(e);
16+
} else {
17+
console.log("XMLHttpRequest readyState:" + this.readyState
18+
+ " status: " + this.status);
19+
}
20+
};
21+
xhttp.open("POST", url, true);
22+
xhttp.setRequestHeader('X-PINGARUNER', 'pingpong');
23+
xhttp.setRequestHeader('Content-Type', 'application/text');
24+
xhttp.send();
25+
}
26+
</script>
6027
</head>
6128
<body>
62-
<form id="controlsToInvoke" action="">
63-
<p>
64-
<input type="button" value="Click to Invoke Another Site" onclick="callOtherDomain()" />
65-
</p>
66-
</form>
67-
<p id="intro">
68-
This page POSTs XML data to another domain using cross-site XMLHttpRequest mitigated by Access Control. This is the preflight scenario and the invocation to a resource on another domain takes place using first an OPTIONS request, then an actual POST request.
69-
</p>
70-
<div id="textDiv">
71-
This XHTML document POSTs to another resource using cross-site XHR. If you get a response back, the content of that response should reflect what you POSTed.
72-
</div>
29+
<form id="controlsToInvoke" action="">
30+
<p>
31+
<input type="button" value="Click to Invoke Another Site"
32+
onclick="callOtherDomain()" />
33+
</p>
34+
</form>
35+
<p id="intro">This page POSTs XML data to another domain using
36+
cross-site XMLHttpRequest mitigated by Access Control. This is the
37+
preflight scenario and the invocation to a resource on another domain
38+
takes place using first an OPTIONS request, then an actual POST
39+
request.</p>
40+
<div id="textDiv">This XHTML document POSTs to another resource
41+
using cross-site XHR. If you get a response back, the content of that
42+
response should reflect what you POSTed.</div>
7343
</body>
7444
</html>

0 commit comments

Comments
 (0)