Bug in respond (newlines)

Hi, I belive this could be a bug in respond.

This is not working:

respond /robots.txt “User-agent: *\n Disallow: /” 200

You have to write it like this:

respond /robots.txt 200 {
body “User-agent: *
Disallow: /”
close
}

If it is allowed in second case, I guess it could be also allowed in first case, or is there something else in play here?

Thanks for great software.

Works for me:

:80

respond /robots.txt "User-agent: *\n Disallow: /" 200

Adapts to:

{
	"apps": {
		"http": {
			"servers": {
				"srv0": {
					"listen": [
						":80"
					],
					"routes": [
						{
							"match": [
								{
									"path": [
										"/robots.txt"
									]
								}
							],
							"handle": [
								{
									"body": "User-agent: *\\n Disallow: /",
									"handler": "static_response",
									"status_code": 200
								}
							]
						}
					]
				}
			}
		}
	}
}

What exactly isn’t working for you?

yes it writes OK in json, but response gives you literally \n, not new line if written like this. If you write it the ugly way, newline gets correctly shown in response. try with curl both ways and you will see the problem.

you get litteraly this: “User-agent: *\n Disallow: /”

Currently it seems “intentional” that \n in Caddyfile tokens aren’t interpreted as newlines. See this note in the code:

I agree with you though, I think it would be nice to support them. I’ll need to see what @matt thinks about it.

The only character that isn’t literally interpreted in a Caddyfile string is the bounding " characters, so there’s no need to escape anything else. You can use literal newlines in your Caddyfile strings. This also eases complexity w.r.t Windows slashes (\).

You can use literal newlines in your Caddyfile strings.
Then config becomes more messy, since you litteraly have to go to new line, it should support al least \n (newlines).

Just my though. Thanks,

If \n was supported, then paths like C:\Users\nicolas\site would fail on Windows.

1 Like

FYI I worked on a solution for this yesterday, adding heredoc syntax to the Caddyfile:

No guarantees it’ll make it in, but it should solve this for you very nicely:

:80 {
	respond /robots.txt <<EOF
	User-agent: *
	Disallow: /
	EOF 200
}

Or if you prefer, this should work too:

:80 {
	respond /robots.txt <<EOF
		User-agent: *
		Disallow: /
		EOF 200
}

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