api/index.php: Fix search link rel header URLs in the result#1583
Merged
Conversation
Because $_SERVER['REQUEST_URI'] includes the ?query=foo part of the URL, the link rel URLs we were building included two copies of the query, and thus were syntactically invalid as well as doubling in length each time a client read and followed a rel="next" URL in the Link header. As an example, if a client did a query like ``` <$ROOT/api/v1/projects?per_page=1&page=1&field[]=pages_total&state[]=P3.proj_avail> ``` it would get a response with (edited for clarity) ``` <$ROOT/api/v1/projects?per_page=1&page=1&field[]=pages_total&state[]=P3.proj_avail?field[]=pages_total&state[]=P3.proj_avail&per_page=1&page=1>; rel="first", <$ROOT/api/v1/projects?per_page=1&page=1&field[]=pages_total&state[]=P3.proj_avail?field[]=pages_total&state[]=P3.proj_avail&per_page=1&page=2>; rel="next", <$ROOT/api/v1/projects?per_page=1&page=1&field[]=pages_total&state[]=P3.proj_avail?field[]=pages_total&state[]=P3.proj_avail&per_page=1&page=8>; rel="last" ``` Note the two `?`s in each response URL, and the differing order of the `per_page` and `page` parameters vs the client URL. Since we already have all the parameters in `$params`, just get the base URL path using `parse_url`, and re-build and append the query.
Collaborator
Author
|
Reproducer #!/bin/bash
set -euxo pipefail
API_KEY=bfoley
ROOT=https://www.pgdp.org/~bfoley/c.branch/fix-link-rel-next
curl -i -X GET "$ROOT/api/v1/projects?per_page=1&page=1&field[]=pages_total&state[]=P3.proj_avail" \
-H "Accept: application/json" \
-H "X-API-KEY: $API_KEY" |
cpeel
approved these changes
May 30, 2026
srjfoo
approved these changes
May 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Because $_SERVER['REQUEST_URI'] includes the ?query=foo part of the URL, the link rel URLs we were building included two copies of the query, and thus were syntactically invalid as well as doubling in length each time a client read and followed a rel="next" URL in the Link header.
As an example, if a client did a query like
it would get a response with (edited for clarity)
Note the two
?s in each response URL, and the differing order of theper_pageandpageparameters vs the client URL.Since we already have all the parameters in
$params, just get the base URL path usingparse_url, and re-build and append the query.