kennethreitz/requests — pull request #1980 by alekstorm (2014-03-28 19:49:10+00:00)
Title: Create Request.links_multi and utils function for properly parsing Link headers

The current `Request.links` property has a few problems: it assumes there will only be a single link for a given relation type, and that each link has only a single relation type (see [RFC 5988](http://tools.ietf.org/html/rfc5988)); and the `utils.parse_header_links()` function it relies on does not properly parse inputs containing commas or semicolons within quoted strings. However, since I'm not sure of the proper way to fix either function without breaking backwards compatibility, I've instead created `Requests.links_multi` and `utils.parse_header_links_full`, if only to solicit feedback on the logic itself; the factoring can be changed in any way the maintainers prefer.

Specifically, `parse_header_links_full` returns a list of the new `Link` `namedtuple`s with `uri` and `attrs` attributes. Internally, it delegates to a new `tokenize` function, which splits a string into a list of tokens (identifiers, "specials", and quoted strings) according to the rules in [RFC 2616](http://tools.ietf.org/html/rfc2616#section-2.2), which makes the parsing itself quite straightforward with some application of `itertools`. In addition, `parse_header_links` is reimplemented in terms of this, to at least properly handle quoted delimiters.

`Request.links_multi`, in turn, returns a `CaseInsensitiveDict` keyed on the relation type (`Request.links` has been updated to do this as well), whose values are a list of all the links with that relation type. If a link has multiple relation types (e.g. `rel="foo bar"`), it will be added to the lists for each relation type.