echocat/caddy-filter

Sometimes an invalid content length error is produced

blaubaer opened this issue ยท 14 comments

If...

  1. caddy returns a content-length header
  2. The modified content-length is different from the original one
  3. gzip plugin is not used

...browsers will reject the usage of the responded content because of the mismatching header value.

Caddyfile:

:2015 {
  tls off
  filter rule {
    content_type .*
    search_pattern a
    replacement bb
  }
  proxy / :2016
}

:2016 {
  tls off
}

text.txt:

a

Results:

$ curl -i http://localhost:2015/test.txt
HTTP/1.1 200 OK
Content-Length: 2
Content-Type: text/plain; charset=utf-8
Date: Fri, 06 Jan 2017 09:20:04 GMT
Last-Modified: Fri, 06 Jan 2017 09:18:23 GMT
Server: Caddy

curl: (18) transfer closed with 2 bytes remaining to read

Adding { header_downstream Content-Length 3 } to the proxy directive allows the curl to succeed and output the correct results (i.e. bb).

You are right. I can reproduce this with the current version of caddy again. I will try to find a fix for this.

A workaround is to activate gzip for the same host configuration like this that uses filterdirective because this also fixes the content-length size.

Mhh... This only does not work with the packaged version of caddy if I download it from the page. If I build this fully on my disk there is no problem and all works. I created a thread in caddy forum to acquire more information about this.

gzip isn't an option if you're sending content downstream to another reverse proxy. I'm currently working around this by unsetting the Content-Length header and letting Varnish re-count the bytes downstream.

And I was able to reproduce it under Caddy 0.9.5 built from source with Go 1.8rc2.

Here is the way how to reproduce my way, could you please recheck it?

  1. Start and enter a new and clean docker container:
docker run --rm -ti -p 2020:2020 golang
  1. Prepare and start Caddy with caddy-filter:
# Download caddy sources with all dependencies
go get -u github.com/mholt/caddy/caddy
# Download caddy-filter with all dependencies
go get -u github.com/echocat/caddy-filter

# Configure caddy to use caddy-filter
sed -i 's/\t\/\/ This is where other plugins get plugged in (imported)/\t_ "github.com\/echocat\/caddy-filter"/' $GOPATH/src/github.com/mholt/caddy/caddy/caddymain/run.go

# Add Caddyfile
cat << EOF > /tmp/Caddyfile
localhost:2020
root /tmp
markdown
filter rule {
  content_type text/.*
  search_pattern "Replace me!"
  replacement "OK!"
}
EOF

# Add example index file
cat << EOF > /tmp/index.md
<html>
<head><title>Replace me!</title></head>
<body><p>Replace me!</p></body>
</html>
EOF

# Build caddy
go build -o $GOPATH/bin/caddy github.com/mholt/caddy/caddy

# Run caddy
caddy -conf /tmp/Caddyfile
  1. Check result:
    Open http://localhost:2020 and you should see the text OK! in your browser. If you see Replace me! or an error the latest fixes was not applied to this build.

You're missing a critical piece: you need a proxy directive. The issue only occurs when you are trying to modify an upstream response. This modified Caddyfile reproduces the error:

localhost:2020 {
  filter rule {
    content_type text/.*
    search_pattern "Replace me!"
    replacement "OK!"
  }

  proxy / localhost:2021
}

localhost:2021 {
  root /tmp
  markdown
}
$ curl -i localhost:2020
HTTP/1.1 200 OK
Content-Length: 212
Content-Type: text/html; charset=utf-8
Date: Wed, 25 Jan 2017 16:32:43 GMT
Last-Modified: Wed, 25 Jan 2017 16:32:39 GMT
Server: Caddy

<!DOCTYPE html>
<html>
	<head>
		<title>index</title>
		<meta charset="utf-8">
		
	</head>
	<body>
		<p><html>
<head><title>OK!</title></head>
<body><p>OK!</p></body>
</html></p>

	</body>
curl: (18) transfer closed with 16 bytes remaining to read
</html>

Ok understand but we are speaking here about to different problems. Also markdown does not work before and if I download the current version from the site it still does not work with the caddyfile mentioned above. The problem with the proxy is a new problem which I have to look at it.

The handling pipeline of Caddy is quite special ๐Ÿ˜‰

When I reported the issue here, you told me to move it here. ๐Ÿ˜„ Would you like me to open a new issue?

Sorry for that. But I'am a littlebit confused that my test scenario also does not work with the latest caddy version (but it should be) - but again: Yes I will check this proxy problem ๐Ÿ˜„

No worries! Thank you for all your help. This plugin is solving a huge problem for my Caddy use case so let me know if I can help troubleshoot the proxy issue any further.

So, now I can reproduce the problem reported by you and: It is really strange - I need some time to debug the whole procedure... ๐Ÿ˜•

So, now I fixed it also for proxy and added integration tests for static, markdown and proxy. I will close this issue if I can guarantee that the download page of caddy serves this version.

Can confirm that this fixes the issue in my environment. Thank you!