0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-02 04:38:21 -05:00

fix: chat example's content-security-policy (#4091)

This commit is contained in:
Yusuke Sakurai 2020-02-24 22:10:00 +09:00 committed by GitHub
parent 4e1abb4f3a
commit 162d66d23f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 6 deletions

View file

@ -1,5 +1,6 @@
<html> <html lang="en">
<head> <head>
<meta charset="UTF-8" />
<title>ws chat example</title> <title>ws chat example</title>
</head> </head>
<body> <body>
@ -10,7 +11,7 @@
<button id="closeButton" disabled>close</button> <button id="closeButton" disabled>close</button>
</div> </div>
<div id="status"></div> <div id="status"></div>
<ul id="timeline"></div> <ul id="timeline"></ul>
<script> <script>
let ws; let ws;
function messageDom(msg) { function messageDom(msg) {
@ -35,7 +36,7 @@
} }
function connect() { function connect() {
if (ws) ws.close(); if (ws) ws.close();
ws = new WebSocket("ws://0.0.0.0:8080/ws"); ws = new WebSocket(`ws://${location.host}/ws`);
ws.addEventListener("open", () => { ws.addEventListener("open", () => {
console.log("open", ws); console.log("open", ws);
applyState({connected: true}); applyState({connected: true});

View file

@ -36,12 +36,17 @@ listenAndServe({ port: 8080 }, async req => {
if (u.protocol.startsWith("http")) { if (u.protocol.startsWith("http")) {
// server launched by deno run http(s)://.../server.ts, // server launched by deno run http(s)://.../server.ts,
fetch(u.href).then(resp => { fetch(u.href).then(resp => {
resp.headers.set("content-type", "text/html"); return req.respond({
return req.respond(resp); status: resp.status,
headers: new Headers({
"content-type": "text/html"
}),
body: resp.body
});
}); });
} else { } else {
// server launched by deno run ./server.ts // server launched by deno run ./server.ts
const file = await Deno.open("./index.html"); const file = await Deno.open(u.pathname);
req.respond({ req.respond({
status: 200, status: 200,
headers: new Headers({ headers: new Headers({
@ -51,6 +56,14 @@ listenAndServe({ port: 8080 }, async req => {
}); });
} }
} }
if (req.method === "GET" && req.url === "/favicon.ico") {
req.respond({
status: 302,
headers: new Headers({
location: "https://deno.land/favicon.ico"
})
});
}
if (req.method === "GET" && req.url === "/ws") { if (req.method === "GET" && req.url === "/ws") {
if (acceptable(req)) { if (acceptable(req)) {
acceptWebSocket({ acceptWebSocket({