Skip to content

headers: JSON strings deleted when setting response headers #4418

Closed

Description

@hairyhenderson

With the following Caddyfile, I expect there to be a response header with the value Foo: {"foo": "bar"}:

localhost

header Foo "{\"foo\":\"bar\"}"
respond "hello world"

But instead, I get:

$ curl -I https://localhost
HTTP/2 200 
foo: 
server: Caddy
content-length: 11
date: Sun, 14 Nov 2021 15:18:34 GMT

It seems that any text between the {} is being lost, for example, header Foo "hello{}world" results in a response header of Foo: helloworld.

Activity

  1. added
    bug 🐞
    on Nov 14, 2021
  2. hairyhenderson commented on Nov 14, 2021

    @hairyhenderson
    CollaboratorAuthor

    It's also worth noting that this isn't a Caddyfile-specific bug - the equivalent JSON config also fails:

    {
      "apps": {
        "http": {
          "servers": {
            "srv0": {
              "listen": [
                ":443"
              ],
              "routes": [
                {
                  "match": [
                    {
                      "host": [
                        "localhost"
                      ]
                    }
                  ],
                  "handle": [
                    {
                      "handler": "subroute",
                      "routes": [
                        {
                          "handle": [
                            {
                              "handler": "headers",
                              "response": {
                                "set": {
                                  "Foo": [
                                    "{\"foo\":\"bar\"}"
                                  ]
                                }
                              }
                            },
                            {
                              "body": "hello world",
                              "handler": "static_response"
                            }
                          ]
                        }
                      ]
                    }
                  ],
                  "terminal": true
                }
              ]
            }
          }
        }
      }
    }
  3. hairyhenderson commented on Nov 14, 2021

    @hairyhenderson
    CollaboratorAuthor

    It's being interpreted as a placeholder - escaping the first { (i.e. \{) makes this work. Still - is that expected behaviour? I’d expect either the string to be interpreted literally if it can’t resolve the placeholder, or some kind of error logged to tell me that the placeholder is invalid

  4. added
    discussion 💬
    and removed
    bug 🐞
    on Nov 15, 2021
  5. mholt commented on Nov 15, 2021

    @mholt
    Member

    Yeah, so this is kind of intentional, because you can escape curly braces it makes it possible to have the flexibility of placeholders that don't always have values without rendering a raw literal placeholder, while also using curly braces by means of escaping. (See #4419 (review).)

    I'm open to changing this behavior so that escaping is not necessary, but I believe you'll lose the ability to use placeholders that aren't always set.

  6. francislavoie commented on Dec 3, 2021

    @francislavoie
    Member

    Since you closed the PR, I guess we can close the issue as well.

  7. hairyhenderson commented on Dec 3, 2021

    @hairyhenderson
    CollaboratorAuthor

    thanks @francislavoie - I meant to do this but totally missed it 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussion 💬

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions