harmonia icon indicating copy to clipboard operation
harmonia copied to clipboard

Implement HEAD HTTP request on /serve/*/ files

Open iwanb opened this issue 5 months ago • 1 comments

Currently doing a HEAD request against a file under /serve returns 404. It would be nice if it could return 200 and some metadata like Content-Length instead.

Or maybe just returning a 405 would be better than 404. Something like this (I could not find an example of implementing HEAD against the filesystem with actix).

Thanks!

iwanb avatar Jul 18 '25 15:07 iwanb

I tried and just adding this route seems to work, so maybe actix is just smart enough to handle it? I don't really see any code in NamedFile::into_response that would explicitly handle that case though:

            .route("/serve/{hash}{path:.*}", web::head().to(serve::get))
In [10]: out = client01.wait_until_succeeds("curl --head --fail --show-headers -v http://harmonia:5000/serve/vpqi2jz5nvvz239b0iql4jxnd33g0p9y/dir/file").strip()
client01: waiting for success: curl --head --fail --show-headers -v http://harmonia:5000/serve/vpqi2jz5nvvz239b0iql4jxnd33g0p9y/dir/file
client01 #   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
client01 #                                  Dload  Upload   Total   Spent    Left  Speed
client01 #   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Host harmonia:5000 was resolved.
client01 # * IPv6: 2001:db8:1::2
client01 # * IPv4: 192.168.1.2
client01 # *   Trying [2001:db8:1::2]:5000...
client01 # * Connected to harmonia (2001:db8:1::2) port 5000
client01 # * using HTTP/1.x
client01 # > HEAD /serve/vpqi2jz5nvvz239b0iql4jxnd33g0p9y/dir/file HTTP/1.1
client01 # > Host: harmonia:5000
client01 # > User-Agent: curl/8.12.1
client01 # > Accept: */*
client01 # >
client01 # < HTTP/1.1 200 OK
client01 # < content-length: 5
client01 # < accept-ranges: bytes
client01 # < last-modified: Thu, 01 Jan 1970 00:00:01 GMT
client01 # < content-disposition: attachment; filename="file"
client01 # < etag: "3720078:5:1:0"
client01 # < content-type: application/octet-stream
client01 # < date: Fri, 18 Jul 2025 19:43:41 GMT
client01 # <
client01 #   0     5    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
client01 # * Connection #0 to host harmonia left intact
client01: (finished: waiting for success: curl --head --fail --show-headers -v http://harmonia:5000/serve/vpqi2jz5nvvz239b0iql4jxnd33g0p9y/dir/file, in 0.04 seconds)

In [11]: out
Out[11]: 'HTTP/1.1 200 OK\r\ncontent-length: 5\r\naccept-ranges: bytes\r\nlast-modified: Thu, 01 Jan 1970 00:00:01 GMT\r\ncontent-disposition: attachment; filename="file"\r\netag: "3720078:5:1:0"\r\ncontent-type: application/octet-stream\r\ndate: Fri, 18 Jul 2025 19:43:41 GMT'

iwanb avatar Jul 18 '25 19:07 iwanb