Skip to content

Conversation

@elboulangero
Copy link
Contributor

Normalize URL for fallback mirrors, to ensure it ends with a trailing slash. One less papercut for mirrorbits users.

The function `NormalizeURL` is used to ensure that a mirror URL ends
with a trailing slash. It's applied when the mirror configuration is
written to the database (cf. `setMirror` in `rpc/rpc.go`).

Mirrorbits then relies on this fact when it constructs the redirect
URLs, in `pagerenderer.go`:

```
path := strings.TrimPrefix(results.FileInfo.Path, "/")
[...]
http.Redirect([...], results.MirrorList[0].AbsoluteURL+path, http.StatusFound)
```

However, it turns out that `NormalizeURL` is not applied to the fallback
URLs. Meaning that if your fallback is `http://mirror.org` and you
request the file `foobar`, and if ever the fallback mechanism kicks in,
mirrorbits will happily redirect you to `http://mirror.orgfoobar`...

Easy to fix! One less papercut!
…kage

The previous commit introduced a import cycle:

```
package github.com/etix/mirrorbits
	imports github.com/etix/mirrorbits/cli from main.go
	imports github.com/etix/mirrorbits/filesystem from commands.go
	imports github.com/etix/mirrorbits/config from hash.go
	imports github.com/etix/mirrorbits/utils from config.go
	imports github.com/etix/mirrorbits/network from utils.go
	imports github.com/etix/mirrorbits/config from geoip.go: import cycle not allowed
```

This is because we now import `utils` from within `config`.

Let's move the two network-related utils to `network/utils.go`, so that
we don't need to import `network` in `utils.go` anymore. That's enough
to break the import cycle.
Now that we import `utils` in `config` (cf. previous commits), we can
use `IsInSlice` from `utils.go`, no need to duplicate it anymore.
@jbkempf jbkempf merged commit e1e1895 into etix:master Jun 5, 2025
3 checks passed
@jbkempf jbkempf added this to the v0.6.1 milestone Jun 5, 2025
@elboulangero elboulangero deleted the ensure-trailing-slash-fallback-url branch June 5, 2025 08:44
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.

2 participants