Skip to content

Conversation

@jkhsjdhjs
Copy link

This removes trailing slashes (and redirects to paths with trailing slashes) from the API and makes it compatible with the PCF2 showcase and other webapps.

Previously, all routes were implemented with a trailing slash, e.g. /submodels/ instead of /submodels. While the API spec only specifies the routes without a trailing slash, this has the advantage of being compatible with requests to the path with a trailing slash and without trailing slash, as werkzeug redirects requests to the slash-terminated path, if available.

However, this poses a problem with browsers that make use of CORS preflight requests (e.g. Chromium-based browsers). Here, before doing an actual API request, the browser sends an OPTIONS request to the path it wants to request. This is done to check potential CORS headers (e.g. Access-Control-Allow-Origin) for the path, without retrieving the actual data. Our implementation doesn't support OPTIONS requests, which is fine.
After the browser has received the response to the preflight request (which may or may not have been successful), it attempts to retrieve the actual data by sending the request again with the correct request method (e.g. GET). With our server this request now results in a redirect, as we redirect to the path with a trailing slash appended. This is a problem, as the browser didn't send a CORS preflight request to the path it is now redirected to. It also doesn't attempt to send another CORS preflight request, as it already sent one, with the difference being the now slash-terminated path. Thus, following the redirect is prevented by CORS policy and the data fails to load.

By making the routes available via non-slash-terminated paths we avoid the need for redirects, which makes the server compatible with webapps viewed in browsers that use preflight requests. Requests to slash-terminated paths will no longer work (they won't redirect to the path without trailing slash). This shouldn't be a problem though, as the API is only specified without trailing slashes anyway.

@aorzelskiGH This fix might be of interest to you, feel free to retest once merged. This issue was noticed as part of testing done at and after the IDTA Plugfest.

This removes trailing slashes (and redirects to paths with trailing
slashes) from the API and makes it compatible with the PCF2 showcase and
other webapps.

Previously, all routes were implemented with a trailing slash, e.g.
`/submodels/` instead of `/submodels`. While the API spec only specifies
the routes without a trailing slash, this has the advantage of being
compatible with requests to the path with a trailing slash and without
trailing slash, as werkzeug redirects requests to the slash-terminated
path, if available.

However, this poses a problem with browsers that make use of [CORS
preflight requests][1] (e.g. Chromium-based browsers). Here, before
doing an actual API request, the browser sends an `OPTIONS` request to
the path it wants to request. This is done to check potential CORS
headers (e.g. `Access-Control-Allow-Origin`) for the path, without
retrieving the actual data. Our implementation doesn't support `OPTIONS`
requests, which is fine.
After the browser has received the response to the preflight request
(which may or may not have been successful), it attempts to retrieve the
actual data by sending the request again with the correct request method
(e.g. `GET`). With our server this request now results in a redirect, as
we redirect to the path with a trailing slash appended. This is a
problem, as the browser didn't send a CORS preflight request to the path
it is now redirected to. It also doesn't attempt to send another CORS
preflight request, as it already sent one, with the difference being the
now slash-terminated path. Thus, following the redirect is prevented by
CORS policy and the data fails to load.

By making the routes available via non-slash-terminated paths we avoid
the need for redirects, which makes the server compatible with webapps
viewed in browsers that use preflight requests. Requests to
slash-terminated paths will no longer work (they won't redirect to the
path without trailing slash). This shouldn't be a problem though, as the
API is only specified without trailing slashes anyway.
@jkhsjdhjs jkhsjdhjs requested a review from Frosty2500 July 19, 2024 23:04
Copy link

@Frosty2500 Frosty2500 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Frosty2500 Frosty2500 merged commit 4b67798 into feature/http_api Jul 22, 2024
@Frosty2500 Frosty2500 deleted the http_api/no_trailing_slash branch July 22, 2024 20:18
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