mirror of
https://github.com/Noratrieb/h2.js.git
synced 2026-01-14 09:55:03 +01:00
glory
This commit is contained in:
parent
c752478c73
commit
d59dae0707
1 changed files with 107 additions and 55 deletions
162
h2.mjs
162
h2.mjs
|
|
@ -971,6 +971,11 @@ const handleConnection =
|
||||||
const payload = Buffer.from(body);
|
const payload = Buffer.from(body);
|
||||||
const h2stream = streams.get(frame.streamIdentifier);
|
const h2stream = streams.get(frame.streamIdentifier);
|
||||||
|
|
||||||
|
if (!h2stream) {
|
||||||
|
setImmediate(cb);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const doWrite = () => {
|
const doWrite = () => {
|
||||||
h2stream.flowControlWindowSize -= payload.length;
|
h2stream.flowControlWindowSize -= payload.length;
|
||||||
|
|
||||||
|
|
@ -1117,6 +1122,36 @@ const handleConnection =
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case FRAME_TYPE.RST_STREAM: {
|
||||||
|
const errorCode = frame.payload.readUint32BE();
|
||||||
|
|
||||||
|
if (frame.streamIdentifier === 0) {
|
||||||
|
socket.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
streams.delete(frame.streamIdentifier);
|
||||||
|
|
||||||
|
if (debug) {
|
||||||
|
console.log("stream reset because of", ERROR_CODE_NAME[errorCode]);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case FRAME_TYPE.PING: {
|
||||||
|
if (frame.streamIdentifier !== 0) {
|
||||||
|
socket.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.write(
|
||||||
|
encodeFrame({
|
||||||
|
type: FRAME_TYPE.PING,
|
||||||
|
flags: 0x01, // ACK
|
||||||
|
streamIdentifier: 0,
|
||||||
|
payload: frame.payload,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case FRAME_TYPE.WINDOW_UPDATE: {
|
case FRAME_TYPE.WINDOW_UPDATE: {
|
||||||
// whatever
|
// whatever
|
||||||
const increment = frame.payload.readUint32BE();
|
const increment = frame.payload.readUint32BE();
|
||||||
|
|
@ -1202,6 +1237,8 @@ export const createH2Server = () => {
|
||||||
|
|
||||||
const { server, onConnection } = createH2Server();
|
const { server, onConnection } = createH2Server();
|
||||||
|
|
||||||
|
const rootPath = process.env.ROOT_PATH ?? ".";
|
||||||
|
|
||||||
server.on(
|
server.on(
|
||||||
"request",
|
"request",
|
||||||
/**
|
/**
|
||||||
|
|
@ -1215,10 +1252,40 @@ server.on(
|
||||||
res.writeHead({ status: 405 });
|
res.writeHead({ status: 405 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const filepath = path.join(".", path.normalize(req.path));
|
const filepath = path.join(rootPath, path.normalize(req.path));
|
||||||
|
|
||||||
console.log("opening", filepath);
|
console.log("opening", filepath);
|
||||||
|
|
||||||
|
const serveFile = (filepath, size) => {
|
||||||
|
const contentType =
|
||||||
|
{
|
||||||
|
".html": "text/html; charset=utf-8",
|
||||||
|
".css": "text/css; charset=utf-8",
|
||||||
|
".js": "text/javascript; charset=utf-8",
|
||||||
|
".png": "image/png",
|
||||||
|
".jpg": "image/jpeg",
|
||||||
|
".jpeg": "image/jpeg",
|
||||||
|
".webp": "image/webp",
|
||||||
|
".avif": "image/avif",
|
||||||
|
".svg": "image/svg+xml",
|
||||||
|
".woff": "font/woff",
|
||||||
|
".woff2": "font/woff2",
|
||||||
|
".txt": "text/plain",
|
||||||
|
}[path.extname(filepath)] ?? "application/octet-stream";
|
||||||
|
|
||||||
|
res.writeHead({
|
||||||
|
status: 200,
|
||||||
|
contentLength: size,
|
||||||
|
headers: [["content-type", contentType]],
|
||||||
|
});
|
||||||
|
|
||||||
|
const fileStream = fs.createReadStream(filepath, {
|
||||||
|
highWaterMark: 1_048_576,
|
||||||
|
});
|
||||||
|
|
||||||
|
fileStream.pipe(res.body);
|
||||||
|
};
|
||||||
|
|
||||||
fs.stat(filepath, (err, stat) => {
|
fs.stat(filepath, (err, stat) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err.code === "ENOENT") {
|
if (err.code === "ENOENT") {
|
||||||
|
|
@ -1231,69 +1298,54 @@ server.on(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stat.isDirectory()) {
|
if (stat.isDirectory()) {
|
||||||
fs.readdir(filepath, (err, files) => {
|
const indexhtml = path.join(filepath, "index.html");
|
||||||
|
fs.stat(indexhtml, (err, stat) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error("failed to read dir", err);
|
if (err.code === "ENOENT") {
|
||||||
res.writeHead({ status: 500 });
|
fs.readdir(filepath, (err, files) => {
|
||||||
|
if (err) {
|
||||||
|
console.error("failed to read dir", err);
|
||||||
|
res.writeHead({ status: 500 });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const html = `<ul>${files
|
||||||
|
.map(
|
||||||
|
(file) =>
|
||||||
|
`<li><a href="${encodeURIComponent(
|
||||||
|
file
|
||||||
|
)}">${file}</a></li>`
|
||||||
|
)
|
||||||
|
.join("\n")}</ul>`;
|
||||||
|
|
||||||
|
res.writeHead({
|
||||||
|
status: 200,
|
||||||
|
contentLength: html.length,
|
||||||
|
headers: [["content-type", "text/html; charset=utf-8"]],
|
||||||
|
});
|
||||||
|
res.body.write(html);
|
||||||
|
res.body.end();
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
console.error("failed to stat index.html", err);
|
||||||
|
res.writeHead({ status: 500 });
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const html = `<ul>${files
|
if (stat.isFile()) {
|
||||||
.map(
|
serveFile(indexhtml, stat.size);
|
||||||
(file) =>
|
} else {
|
||||||
`<li><a href="${encodeURIComponent(file)}">${file}</a></li>`
|
console.error(`${indexhtml} exists but is not a regular file`);
|
||||||
)
|
res.writeHead({ status: 500 });
|
||||||
.join("\n")}</ul>`;
|
}
|
||||||
|
|
||||||
res.writeHead({
|
|
||||||
status: 200,
|
|
||||||
contentLength: html.length,
|
|
||||||
headers: [["content-type", "text/html; charset=utf-8"]],
|
|
||||||
});
|
|
||||||
res.body.write(html);
|
|
||||||
res.body.end();
|
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stat.isFile()) {
|
if (stat.isFile()) {
|
||||||
fs.open(filepath, (err, fd) => {
|
serveFile(filepath, stat.size);
|
||||||
if (err) {
|
|
||||||
console.error("failed to read", err);
|
|
||||||
res.writeHead({ status: 500 });
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const size = stat.size;
|
|
||||||
|
|
||||||
const contentType =
|
|
||||||
{
|
|
||||||
".html": "text/html; charset=utf-8",
|
|
||||||
".css": "text/css; charset=utf-8",
|
|
||||||
".js": "text/javascript; charset=utf-8",
|
|
||||||
".png": "image/png",
|
|
||||||
".jpg": "image/jpeg",
|
|
||||||
".jpeg": "image/jpeg",
|
|
||||||
".webp": "image/webp",
|
|
||||||
".avif": "image/avif",
|
|
||||||
".woff": "font/woff",
|
|
||||||
".woff2": "font/woff2",
|
|
||||||
".txt": "text/plain",
|
|
||||||
}[path.extname(filepath)] ?? "application/octet-stream";
|
|
||||||
|
|
||||||
res.writeHead({
|
|
||||||
status: 200,
|
|
||||||
contentLength: size,
|
|
||||||
headers: [["content-type", contentType]],
|
|
||||||
});
|
|
||||||
|
|
||||||
const fileStream = fs.createReadStream(filepath, {
|
|
||||||
highWaterMark: 1_048_576,
|
|
||||||
});
|
|
||||||
|
|
||||||
fileStream.pipe(res.body);
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue