{labelN} Placeholder - Endian Issue

The caddy {labelN} placeholder is described as follows:

The Nth label of the host (where N is an integer, at least 1); examples: {label2} of “sub.example.com” is “example”. {label1} of “*.example.com” is the subdomain portion only.

Unfortunately, the label numbers are assigned from left-to-right, rather than from right-to-left. For example, here are the label numbers for various domain/sub-domain combinations under the current (left-to-right) eval implementation:

caddy.com: label1=caddy, label2=com
www.caddy.com: label1=www, label2=caddy, label3=com
admin.www.caddy.com: label1=admin, label2=www, label3=caddy, label4=com

By contrast, right-to-left assignment would result in more consistent output (see label1 and label2):

caddy.com: label1=com, label2=caddy
www.caddy.com: label1=com, label2=caddy, label3=www
admin.www.caddy.com: label1=com, label2=caddy, label3=www, label4=admin

Right-to-left assignment enables many common configuration scripting use-cases. For example, the following rather useful auto-placeholder is currently impossible under current assignment rules:

caddy.com www.caddy.com admin.www.caddy.com {
    root /www/web site/caddy.com
    # auto-generate of "/www/weblogs/caddy.com/access.log" that works
    # for the above block is not possible under left-to-right eval...
    log / /www/weblogs/{labelX}.{labelY}/access.log
    # but this would always work, if {labelN} were right-to-left assigned;
    # the result would always be "caddy.com" regardless of conn host:
    log / /www/weblogs/{label2}.{label1}/access.log
}

By contrast, if the {labelN} placeholder were assigned right-to-left, the above would work regardless of the number of levels of sub-domains present in the domain block directive.

Is there a chance you would consider having the {labelN} placeholder eval in a direction (right-to-left) which results in more useful output?

1 Like

Why do you gotta keep making such good points, @LewPayne. :sweat_smile:

This would be a pretty major breaking change. (Which, I guess we should do before 1.0.)

Or maybe we have {rlabelN} instead, for reverse…

{rlabelN} is better than no label. Yes, this would work if the collateral damage of reworking {labelN} is too great.

Or maybe {label-2} to mean second from right.

{label-2} to mean 2nd from right.

This would be equally fine, despite my distaste for it… which I can’t justify, other than bike-shedding (nit-picking at every little detail, rather than getting off my ass and doing the work myself) at this point.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.