Repository navigation
urllib sets wrong Content-Length for pseudo files on Linux #93296
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on May 27, 2022 - changed the title
[-]`urllib.request.FileHandler.open_local_file` sets `Content-length: 0` for non-empty pseudo files[/-][+]urllib sets wrong `Content-Length` for pseudo files on Linux[/+]on May 27, 2022 - addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on May 27, 2022 I'm not sure this really is a bug. What would you expect?
I'm not sure this really is a bug. What would you expect?
I would expect a value of
Content-Lengthto be equal to the size of the HTTP message body regardless of the peculiarities of a file in this case.curlreturns the same result:$ curl --dump-header /dev/stdout file:///proc/cpuinfo Content-Length: 0 Accept-ranges: bytes Last-Modified: Fri, 16 Feb 2024 14:54:57 GMT processor : 0 ...Header
Content-Length: 0followed by non-empty body.AFAIK, a similar discrepancy can happen if the file changes while it's being prepared/sent.
urllibasks the system for size, mtime and contents, and gives them to you.
I don't think it's urllib's job to second-guess the system.The situation is the same as when a HTTP server sends a mismatched Content-Length, except here it's the kernel, not a server. It's up to you to handle it.
IMO, this is not an issue. If there are no objections, I'll close in a month.
curlreturns the same result:$ curl --dump-header /dev/stdout file:///proc/cpuinfo Content-Length: 0 Accept-ranges: bytes Last-Modified: Fri, 16 Feb 2024 14:54:57 GMT processor : 0 ...Header
Content-Length: 0followed by non-empty body.This is interesting, it used to return no content, but that was changed in 2016 curl/curl#681 (comment)
The situation is the same as when a HTTP server sends a mismatched Content-Length, except here it's the kernel, not a server. It's up to you to handle it.
IMO, this is not an issue. If there are no objections, I'll close in a month.
As a user I'd expect a server to handle known cases, but I don't insist on keeping the issue open.
Bug report
A value of the
Content-lengthheader returned byurllib.request.FileHandler.open_local_filemay mismatch the length of data on Linux.This happens when a file from a special file system (e.g., procfs or sysfs) is requested.
open_local_filerelies onst_size;st_sizeis equal to zero for pseudo files on Linux.cpython/Lib/urllib/request.py
Lines 1506 to 1511 in 8a0d9a6
Example
Your environment