darkweak / go-esi

Pure implementation of the non-standard ESI (Edge-Side-Include) specification in Go
MIT License
22 stars 6 forks source link

Support for hostless URLs #4

Closed dkarlovi closed 1 year ago

dkarlovi commented 1 year ago

I notice you're using full URLs in all examples. Symfony ESI will by default generate hostless URLs when generating the ESI include block: https://symfony.com/doc/current/http_cache/esi.html

Are these supported?

Example:

<esi:include src="/_fragment?signature=..."/>
darkweak commented 1 year ago

Hello @dkarlovi. It doesn't support the relative URLs yet but it could be supported with a check here https://github.com/darkweak/go-esi/blob/master/esi/include.go#L55. The check could be like

// include.go
var rgStartsWithSlash = regexp.MustCompile("^/[^/].+")

//....

// L.55
if rgStartsWithSlash.MatchString(i.src) {
    // Get infos from req.URL
    i.src = req.URL.Scheme + req.URL.Host + i.src
}
dkarlovi commented 1 year ago

Wouldn't it be faster to just check the first char, without a regex?

dkarlovi commented 1 year ago

Either way, I'll try a PR with this, thanks for the pointers!

darkweak commented 1 year ago

No because it could be //domain.com/something that is not valid but possible URL without scheme and we shouldn't prefix with the initial request scheme + host

dkarlovi commented 1 year ago

If leading //, it doesn't need the scheme + host, else if leading / it does, maybe?

darkweak commented 1 year ago

You're right, it would be simpler to use only conditions and could write the fix with these conditions

dkarlovi commented 1 year ago

https://github.com/darkweak/go-esi/pull/6