Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy: set zero content length for default response #2459

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions proxy/context.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proxy

import (
"bytes"
stdlibcontext "context"
"errors"
"io"
Expand Down Expand Up @@ -62,15 +61,15 @@ type noopFlushedResponseWriter struct {
ignoredHeader http.Header
}

func defaultBody() io.ReadCloser {
return io.NopCloser(&bytes.Buffer{})
func noBody() io.ReadCloser {
return http.NoBody
}

func defaultResponse(r *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusNotFound,
Header: make(http.Header),
Body: defaultBody(),
Body: noBody(),
Request: r,
}
}
Expand All @@ -89,7 +88,7 @@ func cloneRequestMetadata(r *http.Request) *http.Request {
ProtoMinor: r.ProtoMinor,
Header: cloneHeader(r.Header),
Trailer: cloneHeader(r.Trailer),
Body: defaultBody(),
Body: noBody(),
ContentLength: r.ContentLength,
TransferEncoding: r.TransferEncoding,
Close: r.Close,
Expand All @@ -109,7 +108,7 @@ func cloneResponseMetadata(r *http.Response) *http.Response {
ProtoMinor: r.ProtoMinor,
Header: cloneHeader(r.Header),
Trailer: cloneHeader(r.Trailer),
Body: defaultBody(),
Body: noBody(),
ContentLength: r.ContentLength,
TransferEncoding: r.TransferEncoding,
Close: r.Close,
Expand Down Expand Up @@ -180,7 +179,7 @@ func (c *context) ensureDefaultResponse() {
}

if c.response.Body == nil {
c.response.Body = defaultBody()
c.response.Body = noBody()
}
}

Expand Down Expand Up @@ -235,7 +234,7 @@ func (c *context) Serve(r *http.Response) {
}

if r.Body == nil {
r.Body = defaultBody()
r.Body = noBody()
}

c.servedWithResponse = true
Expand Down
26 changes: 26 additions & 0 deletions proxy/defaultresponse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package proxy_test

import (
"net/http"
"testing"

"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/proxy/proxytest"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDefaultResponse(t *testing.T) {
p := proxytest.Config{
Routes: eskip.MustParse(`* -> <shunt>`),
}.Create()
defer p.Close()

rsp, body, err := p.Client().GetBody(p.URL)
require.NoError(t, err)

assert.Equal(t, http.StatusNotFound, rsp.StatusCode)
assert.Len(t, body, 0)
assert.Equal(t, "0", rsp.Header.Get("Content-Length"))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an indirect test. It is hard to test that response is not chunked because http client handles chunked encoding transparently. To test it we would need to create net connection and send/receive raw HTTP request and response.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind, but sounds simple enough to do:

GET / HTTP/1.1\r\n
Host: test\r\n
\r\n

}
6 changes: 4 additions & 2 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,7 @@ func (p *Proxy) rejectBackend(ctx *context, req *http.Request) (*http.Response,
if !p.limiters.Get(limit.Settings).Allow(req.Context(), s) {
return &http.Response{
StatusCode: limit.StatusCode,
Header: http.Header{"Content-Length": []string{"0"}},
Body: io.NopCloser(&bytes.Buffer{}),
Body: noBody(),
}, true
}
}
Expand Down Expand Up @@ -1211,6 +1210,9 @@ func (p *Proxy) serveResponse(ctx *context) {
start := time.Now()
p.tracing.logStreamEvent(ctx.proxySpan, StreamHeadersEvent, StartEvent)
copyHeader(ctx.responseWriter.Header(), ctx.response.Header)
if ctx.response.Body == noBody() {
ctx.responseWriter.Header()["Content-Length"] = []string{"0"}
}

if err := ctx.Request().Context().Err(); err != nil {
// deadline exceeded or canceled in stdlib, client closed request
Expand Down
Loading