Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lassie-fetcher): proxy lassie version header #465

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions container/shim/src/fetchers/lassie.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import fetch from "node-fetch";

import { LASSIE_ORIGIN, LASSIE_SP_ELIGIBLE_PORTION, hasNodeToken } from "../config.js";
import { submitLassieLogs } from "../modules/log_ingestor.js";
import { proxyResponseHeaders, toUtf8 } from "../utils/http.js";
import { proxyAllResponseHeaders, proxyCARResponseHeaders, toUtf8 } from "../utils/http.js";
import { debug as Debug } from "../utils/logging.js";

const debug = Debug.extend("lassie");
Expand Down Expand Up @@ -115,14 +115,16 @@ export async function respondFromLassie(req, res, { cidObj, format }) {
res.status(status);
res.set("Cache-Control", "public, max-age=29030400, immutable");

const headersObj = Object.fromEntries(lassieRes.headers.entries());
proxyAllResponseHeaders(headersObj, res);

// Stream errors will be propagated to the catch block.
pipeline(lassieRes.body, metricsStream, () => {});

if (isRawFormat) {
await getRequestedBlockFromCar(metricsStream, res, cidObj, blockFilename);
} else {
const headersObj = Object.fromEntries(lassieRes.headers.entries());
proxyResponseHeaders(headersObj, res);
proxyCARResponseHeaders(headersObj, res);
await pipelinePromise(metricsStream, res);
}
} catch (err) {
Expand Down
21 changes: 17 additions & 4 deletions container/shim/src/utils/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ const PROXY_REQUEST_HEADERS = [
// to the origin. The fix is to pass a custom header.
"x-if-none-match",
];
const PROXY_RESPONSE_HEADERS = [

const ALL_PROXY_RESPONSE_HEADERS = {
server: "x-lassie-version",
"server-timing": "server-timing",
};

const CAR_PROXY_RESPONSE_HEADERS = [
"content-disposition",
"content-type",
"content-length",
"cache-control",
"etag",
"last-modified",
"location",
"server-timing",
"x-ipfs-path",
"x-ipfs-roots",
"x-ipfs-datasize",
Expand Down Expand Up @@ -48,9 +53,17 @@ export function proxyRequestHeaders(reqHeaders) {
return headers;
}

export function proxyAllResponseHeaders(headersObj, nodeRes) {
for (const [key, newKey] of Object.entries(ALL_PROXY_RESPONSE_HEADERS)) {
if (key in headersObj) {
nodeRes.set(newKey, headersObj[key]);
}
}
}

// https://github.com/ipfs/specs/blob/main/http-gateways/PATH_GATEWAY.md#response-headers
export function proxyResponseHeaders(headersObj, nodeRes) {
for (const key of PROXY_RESPONSE_HEADERS) {
export function proxyCARResponseHeaders(headersObj, nodeRes) {
for (const key of CAR_PROXY_RESPONSE_HEADERS) {
if (key in headersObj) {
nodeRes.set(key, headersObj[key]);
}
Expand Down
Loading