Skip to content

Parse the HTTP Last-Modified header without strptime - #111619

Merged
thevar1able merged 12 commits into
masterfrom
fix-http-last-modified-gmt-zone
Aug 11, 2026
Merged

thevar1able merged 12 commits into
masterfrom
fix-http-last-modified-gmt-zone

Conversation

@thevar1able

@thevar1able thevar1able commented Jul 23, 2026 •

Copy link
Copy Markdown
Member

ReadWriteBufferFromHTTP::parseFileInfo parsed the HTTP Last-Modified header with strptime and matched the trailing literal GMT with %Z, which is not portable. musl's %Z only knows the names of the currently configured local time zone, so it consumes nothing, the full-consumption check fails, and the _time virtual column of the url/urlCluster table functions is always NULL. Before the time zone database is initialized, musl's %Z can also dereference a NULL tzname[0]. This is why 04104_url_time_virtual_column failed on every musl configuration.

Replace strptime with a dedicated parser, tryParseHTTPDate. It accepts all three forms RFC 9110, 5.6.7 requires a recipient to handle - IMF-fixdate, rfc850-date and asctime-date - resolves the rfc850-date two-digit year against the 50-year sliding window, rejects calendar dates that do not exist instead of folding them into the next month, and matches the weekday, month and GMT tokens case-insensitively as RFC 9111, 4.2 asks of cache recipients. An absent or unparsable header leaves _time as NULL.

Covered by the existing 04104_url_time_virtual_column, the new src/IO/tests/gtest_parse_http_date.cpp, and the new integration test test_storage_url_last_modified.

Bugfix validation (unit tests) is ERROR (inconclusive), not a refutation, and it cannot be made to pass here. That job overlays every changed src/**/tests/*.cpp file onto the merge-base and builds unit_tests_dbms without the fix; gtest_parse_http_date.cpp tests tryParseHTTPDate, which does not exist at the merge-base, so the before-binary cannot compile. A unit test of a newly added function can never build against the merge-base, so the runtime reproduction is delegated to the integration test test_storage_url_last_modified, which drives the real url table function against a server emitting each HTTP-date form.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed the _time virtual column of the url/urlCluster table functions always being NULL on musl-based builds, caused by non-portable parsing of the HTTP Last-Modified header.

Version info

  • Merged into: 26.8.1.1193 (included in 26.8 and later)

RFC 7231 IMF-fixdate always ends in the literal "GMT", but the parser
matched it with strptime's %Z. That is not portable: musl's %Z only
knows the names of the currently configured local time zone, so a
zero-length tzname match consumes nothing, "GMT" is left unparsed, the
full-consumption check fails, and the _time virtual column of the url
table function is always NULL (04104_url_time_virtual_column failed on
every musl configuration). If the time zone database has not been
initialized yet, musl's %Z even dereferences a NULL tzname[0].

Match the literal instead; verified to parse identically on musl (with
and without tzset) and glibc.
@clickhouse-gh

clickhouse-gh Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [40ad37e]

Summary: ❌

job_name test_name status info comment
Bugfix validation (unit tests) ERROR
Compile before-binary (ninja unit_tests_dbms, without the fix) ERROR IGNORED

AI Review

Summary

This PR replaces strptime with a dedicated HTTP-date parser for Last-Modified, wires it into ReadWriteBufferFromHTTP, and adds focused unit and integration coverage for the three accepted formats, the RFC 850 year window, invalid calendar dates, and case-insensitive cache-recipient parsing. I did not find any remaining correctness, compatibility, or evidence gaps in the merged code.

Final Verdict

✅ No blocking or major issues found.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.00% 79.00% +0.00%

Changed lines: Changed C/C++ lines covered: 189/191 (98.95%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 23, 2026
@scanhex12 scanhex12 self-assigned this Jul 23, 2026
@thevar1able
thevar1able marked this pull request as ready for review July 23, 2026 14:32
Comment thread src/IO/ReadWriteBufferFromHTTP.cpp Resolved
Comment thread src/IO/ReadWriteBufferFromHTTP.cpp Resolved
@thevar1able
thevar1able marked this pull request as draft July 28, 2026 22:06
Replace `strptime` in `parseFileInfo` with `tryParseHTTPDate`. `strptime` is a
C function whose `%a`, `%b` and `%Z` specifiers depend on the locale and on the
time zone database, which is what made the `Last-Modified` header unparseable
on musl.

Every form of an `HTTP-date` has a fixed layout and a fixed set of
case-sensitive day and month names, so the new parser checks the layout
directly and builds the timestamp with `DateLUTImpl::tryToMakeDateTime` on the
UTC time zone. Neither `strptime` nor `timegm` is needed.

RFC 9110, 5.6.7 requires a recipient to accept all three forms, so the obsolete
RFC 850 and `asctime` forms are now parsed as well, not only `IMF-fixdate`:

    Sun, 06 Nov 1994 08:49:37 GMT     ; IMF-fixdate
    Sunday, 06-Nov-94 08:49:37 GMT    ; obsolete RFC 850 format
    Sun Nov  6 08:49:37 1994          ; ANSI C's `asctime` format

The two-digit year of the RFC 850 form is resolved against the current time as
the same section requires: a timestamp that would appear to be more than 50
years in the future denotes the most recent past year with the same last two
digits. `tryParseHTTPDate` therefore has an overload taking an explicit
reference time, so that tests do not depend on the wall clock.

The new integration test `test_storage_url_last_modified` serves a one-row CSV
under a path per form and checks that `_time` is populated from `Last-Modified`
in each of them, and that an absent or malformed header leaves `_time` as
`NULL`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Comment thread src/IO/parseHTTPDate.cpp Resolved
RFC 9110, 5.6.7 defines the rule on the whole timestamp: a value that appears to
be more than 50 years in the future denotes the most recent past year with the
same last two digits. `resolveTwoDigitYear` compared years instead, so a date
inside the reference year but past the cutoff day stayed a century ahead. With a
reference time of 2026-01-01, `Friday, 02-Jan-76 00:00:00 GMT` resolved to 2076
rather than 1976, which made `_time` wrong by a century for such a
`Last-Modified` header.

Build the candidate timestamp first and compare it against the reference time
plus 50 years, subtracting a century only when it is actually past the cutoff.

`ParseHTTPDate.RFC850TwoDigitYear` asserted the year-granularity result for
`06-Nov-76`, so it is corrected, and `ParseHTTPDate.RFC850TwoDigitYearCutoff`
now pins both sides of the cutoff at 2076-01-01.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Comment thread src/IO/parseHTTPDate.cpp Resolved
`makeTimestamp` bounded the day of month by 31 and left the rest to
`DateLUTImpl::tryToMakeDateTime`, which only checks the same bound before
indexing the month-start table. A day past the end of its month was therefore
folded into the next month instead of being rejected: `Mon, 29 Feb 2021
00:00:00 GMT` parsed as 2021-03-01 and `Sun, 31 Apr 2026 00:00:00 GMT` as
2026-05-01, in all three forms of an `HTTP-date`.

That contradicts what this parser promises for a malformed `Last-Modified`: the
`_time` virtual column and the URL schema and count caches would take an
impossible date for a real modification time. `strptime` with `timegm` behaved
the same way before, so this tightens the long-standing behavior.

Bound the day by `DateLUTImpl::daysInMonth` for the parsed month and year
instead. The century a two-digit RFC 850 year resolves to therefore decides
whether 29 February exists, which `ParseHTTPDate.InvalidCalendarDate` pins along
with a non-leap 29 February, 31 April, and the last day of every month.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Comment thread src/IO/parseHTTPDate.cpp Resolved
The candidate year was anchored to the century of the reference time and
could only ever be moved 100 years into the past, so near the end of a
century a small two-digit year resolved a century too early: seen from
1999-12-31, `01-Jan-00` denoted 1900 instead of 2000. Pick the candidate
whose full timestamp falls into the (reference - 50 years, reference + 50
years] window instead; the candidates are 100 years apart, so at most one
matches. This also chooses the year before validating the day of month,
so a `29-Feb-00` seen from 1950 resolves to 2000-02-29 even though 1900
had no 29 February.

Co-Authored-By: Claude Fable 5 <[email protected]>
Comment thread src/IO/parseHTTPDate.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.60% 78.70% +0.10%

Changed lines: Changed C/C++ lines covered: 185/186 (99.46%) · Uncovered code

Full report · Diff report

RFC 9110, 5.6.7 defines `HTTP-date` as case sensitive, but RFC 9111, 4.2
relaxes that for cache recipients: "Although all date formats are
specified to be case-sensitive, a cache recipient SHOULD match the field
value case-insensitively."

`ReadWriteBufferFromHTTP` is such a recipient - `Last-Modified` decides
whether a cached schema or row count for a `URL` table is still valid -
and the previous `strptime` call folded case for `%a` and `%b`, so a
header like `sun, 06 nov 1994 08:49:37 gmt` used to parse and now did
not.

Match the weekday, the month and the `GMT` literal with
`equalsCaseInsensitive`.
@thevar1able thevar1able changed the title Parse the HTTP Last-Modified zone as the literal GMT, not %Z Parse the HTTP Last-Modified header without strptime Aug 3, 2026
@thevar1able thevar1able changed the title Parse the HTTP Last-Modified header without strptime Parse the HTTP Last-Modified header without strptime Aug 3, 2026
@thevar1able
thevar1able marked this pull request as ready for review August 3, 2026 12:16
Each of the three forms has a fixed width, checked against a literal
sample date. Give those samples names instead of repeating them inline.
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 CI status of the latest run (commit 40ad37e): the only failures are unrelated to this PR.

No unresolved review threads; the AI review verdict is ✅. The branch is merge-clean against master.

@thevar1able
thevar1able added this pull request to the merge queue Aug 11, 2026
Merged via the queue into master with commit 5d5ea5f Aug 11, 2026
349 of 355 checks passed
@thevar1able
thevar1able deleted the fix-http-last-modified-gmt-zone branch August 11, 2026 23:46
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants