mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-08-11 12:51:24 +00:00
RFC 9110 5.3 makes the order of header fields sharing a field name significant, but Headers was a std::unordered_multimap, which gives no ordering guarantee for equivalent keys. libstdc++ hands duplicates back in reverse insertion order while libc++ uses insertion order, so get_header_value() returned a different field depending on the platform, and code that picks a value out of an accidentally or maliciously duplicated field name had no way to say which one it wanted. Replace Headers with a small container that keeps the fields in the order they were received or set. Storage is a flat vector and lookup is a linear scan, which beats hashing for the at most CPPHTTPLIB_HEADER_MAX_COUNT fields a message carries. begin()/end() walk every field, while find() and equal_range() hand back the same iterator type restricted to one field name; equality compares only the position, so a restricted iterator still compares equal to end(). Erasing an equal_range() therefore removes only the fields with that name and leaves interleaved fields alone. std::multimap was the smaller change but sorts by field name, which would stop control data such as Host from leading the message. Instead Host is now prepended via emplace_front() so it keeps its place at the front of a request. Two side effects worth noting: incrementing past the last field of a name now saturates at end(), so an out-of-range id passed to get_header_value() returns the default instead of running off the container as it did before; and iterators follow std::vector rules, so they are invalidated by insertion. Fixes #2509