Fix custom base URL with subpath causing 404 errors#3161
Fix custom base URL with subpath causing 404 errors#3161chrisuthe wants to merge 3 commits intomusic-assistant:devfrom
Conversation
c13b4be to
63c011e
Compare
|
Updated the PR to handle both ingress and reverse proxy. I have tested with both but that does not cover all possible reverse proxy configurations. |
63c011e to
2e57612
Compare
| """Redirect root path to the base path when a subpath is configured.""" | ||
| raise web.HTTPFound(self._base_path + "/") | ||
|
|
||
| async def _handle_prefixed_catch_all( |
There was a problem hiding this comment.
This function introduces quite a bit of duplicate code. Could you refactor this into a function that can be used by both _handle_prefixed_catch_all and _handle_catch_all?
There was a problem hiding this comment.
created _resolve_dynamic_route taking request and lookup path then made _handle_prefixed and _handle_catch_all both thin wrappers.
There was a problem hiding this comment.
Since, as you mentioned, this as quite some test implications, I would love to see some unit tests for this that handle:
- Current use cases (correct, should stay correct)
- Current failing use cases (fail without your fix, pass with your fix applied)
Marking the PR as draft so we can keep track of things that need our attention. Please mark it as ready for review again when you want us to have another look 🙏
634e558 to
de906b1
Compare
I added a suite of tests but be warned they are generated by AI. They all make sense to me who didn't know exactly where to start on unit tests for these changes. LMK If they missed the mark. |
When base_url contains a subpath (e.g. https://link.com/music), register all routes at BOTH root level and the prefixed path. Root-level routes serve HA ingress (which strips the slug prefix) and direct access, while prefixed routes serve requests from the reverse proxy. Dynamic routes are stored without the prefix; a dedicated prefixed catch-all handler strips the base_path before looking up dynamic route handlers. Also marks base_url config as requires_reload since changing the subpath requires re-registering all routes. Fixes: music-assistant/support#4940
Extract _resolve_dynamic_route() to consolidate the identical route-matching logic from _handle_prefixed_catch_all and _handle_catch_all into a single method.
Cover dynamic route matching, prefix stripping, wildcard routes, registration/unregistration, redirect behavior, and edge cases for both standard and reverse-proxy-with-subpath configurations.
413eaf7 to
e34d970
Compare
When base_url contains a subpath (e.g., https://link.com/music), extract the path component and prefix all route registrations so the server works correctly behind a reverse proxy with path-based routing. Also redirect bare root to the subpath and mark base_url config as requires_reload since routes are registered at startup.
Fixes music-assistant/support#4940
Tested on HAOS With DEV Plugin

This is a small amount of code with GIANT testing implications