Skip to content

Commit

Permalink
core/io: doh cache headers
Browse files Browse the repository at this point in the history
  • Loading branch information
ignoramous committed Sep 27, 2024
1 parent cc6e798 commit 45c0799
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/commons/dnsutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ export function isAnswerQuad0(packet) {
return isAnswerBlocked(packet.answers);
}

export function ttl(packet) {
if (!hasAnswers(packet)) return 0;
return packet.answers[0].ttl || 0;
}

/**
* @param {any} dnsPacket
* @returns {string[]}
Expand Down
11 changes: 11 additions & 0 deletions src/core/io-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export default class IOState {
return util.concatHeaders(
util.dnsHeaders(),
util.contentLengthHeader(b),
this.cacheHeaders(),
xNileRegion,
xNileFlags,
xNileFlagsOk
Expand All @@ -215,6 +216,16 @@ export default class IOState {
}
}

// set cache from ttl in decoded-dns-packet
cacheHeaders() {
const ttl = dnsutil.ttl(this.decodedDnsPacket);
if (ttl <= 0) return null;

return {
"cache-control": "public, max-age=" + ttl,
};
}

assignBlockResponse() {
let done = this.initFlagsAndAnswers();
done = done && this.addData();
Expand Down

0 comments on commit 45c0799

Please sign in to comment.