Http HEAD method

Final edit :slight_smile:

I was able to patch and test and it works now!

To apply the patch against master:

git clone https://github.com/genieacs/genieacs.git
cd genieacs
git apply ../http-head-patch.diff

To apply the patch against v.1.2.9 tag

git clone https://github.com/genieacs/genieacs.git
cd genieacs
git checkout tags/v1.2.9
git apply ../http-head-patch-1.2.9.diff

PATCH against master branch

diff --git a/lib/fs.ts b/lib/fs.ts
index e1cc5a1..5a6cc1e 100644
--- a/lib/fs.ts
+++ b/lib/fs.ts
@@ -64,8 +64,8 @@ export async function listener(
   request: IncomingMessage,
   response: ServerResponse
 ): Promise<void> {
-  if (request.method !== "GET") {
-    response.writeHead(405, { Allow: "GET" });
+  if (request.method !== "GET" && request.method !== "HEAD") {
+    response.writeHead(405, { Allow: "GET, HEAD" });
     response.end("405 Method Not Allowed");
     return;
   }
@@ -74,7 +74,7 @@ export async function listener(
   const filename = decodeURIComponent(urlParts.pathname.substring(1));

   const log = {
-    message: "Fetch file",
+    message: "Fetch file (" + request.method + ")",
     filename: filename,
     remoteAddress: getRequestOrigin(request).remoteAddress,
   };
@@ -100,9 +100,13 @@ export async function listener(
     "Content-Length": file.length,
   });

-  pipeline(Readable.from(chunks), response, () => {
-    // Ignore errors resulting from client disconnecting
-  });
+  if (request.method === "GET") {
+    pipeline(Readable.from(chunks), response, () => {
+      // Ignore errors resulting from client disconnecting
+    });
+  } else {
+    response.end();
+  }

   logger.accessInfo(log);
 }
 }

PATCH against v.1.2.9 tag

diff --git a/lib/fs.ts b/lib/fs.ts
index f1bf1fb..f662784 100644
--- a/lib/fs.ts
+++ b/lib/fs.ts
@@ -56,8 +56,8 @@ export async function listener(
   request: IncomingMessage,
   response: ServerResponse
 ): Promise<void> {
-  if (request.method !== "GET") {
-    response.writeHead(405, { Allow: "GET" });
+  if (request.method !== "GET" && request.method !== "HEAD") {
+    response.writeHead(405, { Allow: "GET, HEAD" });
     response.end("405 Method Not Allowed");
     return;
   }
@@ -92,6 +92,11 @@ export async function listener(
     "Content-Length": file.length,
   });

-  response.end(buffer);
+  if (request.method === "HEAD") {
+    response.end();
+  }
+  else {
+    response.end(buffer);
+  }
   logger.accessInfo(log);
 }
root@acs:~/genieacs# HEAD http://127.0.0.1:7567/FRITZ.Box_5530-07.50.image
200 OK
Connection: close
Date: Wed, 14 Jun 2023 09:25:50 GMT
Content-Length: 34467840
Content-Type: application/octet-stream
Client-Date: Wed, 14 Jun 2023 09:25:50 GMT
Client-Peer: 127.0.0.1:7567
Client-Response-Num: 1