Skip to content

api/index.php: Fix search link rel header URLs in the result#1583

Merged
cpeel merged 1 commit into
DistributedProofreaders:masterfrom
bpfoley:fix-link-rel-next
May 31, 2026
Merged

api/index.php: Fix search link rel header URLs in the result#1583
cpeel merged 1 commit into
DistributedProofreaders:masterfrom
bpfoley:fix-link-rel-next

Conversation

@bpfoley
Copy link
Copy Markdown
Collaborator

@bpfoley bpfoley commented May 30, 2026

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.

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.
@bpfoley
Copy link
Copy Markdown
Collaborator Author

bpfoley commented May 30, 2026

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 cpeel requested review from cpeel and srjfoo May 30, 2026 13:46
@cpeel cpeel merged commit d412501 into DistributedProofreaders:master May 31, 2026
9 checks passed
@bpfoley bpfoley deleted the fix-link-rel-next branch May 31, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants