Skip to content

Commit

Permalink
Change LinkHeaders to use ",\n" as delimiter in Link HTTP header
Browse files Browse the repository at this point in the history
Previously LinkHeaders would delimit multiple links with \n alone
like so:

    Link: <http://example.org/a>; rel="next"
          <http://example.org/b>; rel="prev"

However, comma + newline is used as the delimiter in the
specification's (https://tools.ietf.org/html/rfc5988#section-5.5)
examples and, additionally, it states:

> Note that extension relation types are REQUIRED to be absolute
> URIs in Link headers, and MUST be quoted if they contain a
> semicolon (";") or comma (",") (as these characters are used as
> delimiters in the header itself).

In comma + newline appears to be the standard in practice (e.g.
GitHub's API). This changes LinkHeaders to use comma + newline as
the delimieter between multiple links, e.g:

    Link: <http://example.org/a>; rel="next",
          <http://example.org/b>; rel="prev"
  • Loading branch information
petedmarsh committed Apr 15, 2016
1 parent 78d5a18 commit 1d28112
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions sinatra-contrib/lib/sinatra/link_header.rb
Expand Up @@ -90,7 +90,7 @@ def link(*urls)
link = (response["Link"] ||= "")

urls.map do |url|
link << "\n" unless link.empty?
link << ",\n" unless link.empty?
link << (http_pattern % url)
html_pattern % url
end.join "\n"
Expand All @@ -117,7 +117,7 @@ def link(*urls)
def link_headers
yield if block_given?
return "" unless response.include? "Link"
response["Link"].lines.map do |line|
response["Link"].split(",\n").map do |line|
url, *opts = line.split(';').map(&:strip)
"<link href=\"#{url[1..-2]}\" #{opts.join " "} />"
end.join "\n"
Expand Down
2 changes: 1 addition & 1 deletion sinatra-contrib/spec/link_header_spec.rb
Expand Up @@ -41,7 +41,7 @@
it "takes an options hash" do
get '/'
elements = ["<something>", "foo=\"bar\"", "rel=\"from-filter\""]
headers['Link'].lines.first.strip.split('; ').sort.should == elements
headers['Link'].split(",\n").first.strip.split('; ').sort.should == elements
end
end

Expand Down

0 comments on commit 1d28112

Please sign in to comment.